/*
*       Die Datei katalog stellt die Steuerung
*       zur Erstellung des online- als auch
*       pdf- Kataloges dar.
*
*
*       @author Friedhelm Matten
*       @author Phoenix-Database Informationstechnologie GmbH
*       @date 05.12.2003 14:40
*
*/

import java.io.FileOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
import com.lowagie.text.pdf.PdfWriter;

import java.lang.*;
import java.io.*;
import java.sql.*;
import java.util.*;
import java.net.*;

import java.awt.Color;


import de.phoenix_database.database.KatalogManager;

import beans.programmForm;
import beans.terminForm;
import beans.terminortForm;
import beans.zeitForm;

import java.util.regex.*;



import beans.tagPoints;
import beans.listTags;


public class termine {





   public static void main(String args[]) {

       float currentY=558;
        
       // step 1: creation of a document-object
      // Document document = new Document();

             // step 1: creation of a document-object
       Document document = new Document(PageSize.A5);


       try {
                




             // step 2:
           // we create a writer that listens to the document
           // and directs a PDF-stream to a file
           PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("standartTermin.pdf"));
           PdfContentByte cb = writer.getDirectContent();

         //   float currentY = 0f;
            ColumnText ct1 = new ColumnText(cb);
            ColumnText ct2 = new ColumnText(cb);
            ColumnText ct3 = new ColumnText(cb);
            ColumnText ct4 = new ColumnText(cb);
           int counter=0;

           // step 3: we open the document
           document.open();



                
                
                //get the id of the course
              //  String V_ART = args[0];
                //  System.out.println("vart= "+vart);

                //create a bean container for the information  
                programmForm form = new programmForm();
                

                //open a connetction to the database
                Connection con = KatalogManager.getConnection();
            
                //create a statement
                Statement st0 = con.createStatement();


                String sqlall = "SELECT F_KAT.V_ART  FROM F_KAT WHERE F_KAT.Jahr=? "
                               +" AND F_KAT.Halbjahr=? AND F_KAT.JN_frei <> ?  AND F_KAT.Format ='Standard'"
                               +"  ";
               // System.out.println(sql1+"\n");


                PreparedStatement prepstmtall = con.prepareStatement(sqlall);



                prepstmtall.setString(1,"2003");
                prepstmtall.setString(2,"1");
                prepstmtall.setString(3,"0");
                //prepstmt1.setString(4,vart+"%");

                ResultSet rsall = prepstmtall.executeQuery();                 

                String[] theHead = new String[10];
                String[] theText = new String[10];
                String V_ART = null;

                String fkatbez    = "";

                String altzusatz  = "";

                String altort     = "";

                String alttermin  = "";







                while(rsall.next()){   

     
                V_ART = rsall.getString(1);

                System.out.println("V_ART: "+V_ART );


                 

                String sql_terminc =" select count(*) "
                                     +"   "
                                     +"   "
                                     +"   from F_V_KAT  "
                                     +" left outer join F_VERANS on F_VERANS.V_Nr=F_V_KAT.V_Nr  "
                                     +" where f_v_kat.jahr='2003' AND f_v_kat.halbjahr='1' AND F_V_KAT.JN_frei<> '0' AND F_V_KAT.V_ART LIKE '"+V_ART+"%' ";

               //  System.out.println("sql_terminc: "+sql_terminc );

                 PreparedStatement prepstmtTerminc = con.prepareStatement(sql_terminc);


                 ResultSet rsTerminc = prepstmtTerminc.executeQuery();

                 rsTerminc.next();


                 int count = rsTerminc.getInt(1);


               if(count > 0){
               

                  
                String sql_termin =" select F_V_KAT.V_NR, F_VERANS.Ort, "
                                      +" F_VERANS.Beginn, F_VERANS.Ende, F_VERANS.Beitrag, F_V_KAT.Termin, "
                                      +" F_VERANS.V_Bez_1, F_VERANS.V_Bez_2, F_VERANS.V_Bez_3,  "
                                      +" F_V_KAT.Zusatz, F_V_KAT.Ort, F_V_KAT.Termin  from F_V_KAT  "
                                      +" left outer join F_VERANS on F_VERANS.V_Nr=F_V_KAT.V_Nr  "
                                      +" where f_v_kat.jahr='2003' AND f_v_kat.halbjahr='1' AND F_V_KAT.JN_frei<> '0' AND F_V_KAT.V_ART LIKE '"+V_ART+"%' ";
                   
               //   System.out.println("sql_termin: "+sql_termin );
                  
                  PreparedStatement prepstmtTermin = con.prepareStatement(sql_termin);


                  ResultSet rsTermin = prepstmtTermin.executeQuery();

                  
                  String tverantnr   = "";
                  String tort        = "";
                  String tortold     = "";
                  String tbegin      = "";
                  String tend        = "";
                  String tbeitrag    = "";
                  String tbeitragold = "";

                  int j = 0;

                  Vector termine = new Vector();





                  while(rsTermin.next()){
                     terminForm termin = new terminForm();

                            tverantnr = rsTermin.getString(1);
                           // System.out.println(tverantnr+"\n");
                            termin.setVeran_tnr(tverantnr);
                            tort      = rsTermin.getString(2);
                             System.out.println(tort+"\n");

                            if(j==0){
                                tortold = tort;
                                termin.setVeran_tort(tort);
                            }else{
                                if(tort.equals(tortold)){
                                  termin.setVeran_tort("");
                                }else{
                                   tortold = tort;
                                   termin.setVeran_tort(tort);
                                }                              
                            }


                            tbegin    = rsTermin.getString(3);
                            tbegin  = tbegin;
                            System.out.println(tbegin+"\n");
                            if(tbegin==null) tbegin="";
                            if(tbegin.length()==0){
                               termin.setBterm(0);
                            }else{
                            termin.setVeran_tbegin(formatDate(tbegin));
                            }
                            

                            tend      = rsTermin.getString(4);
                            tend      = tend;

                            System.out.println(tend+"\n");
                            if(tend==null) tend="";
                            if(tend.length()==0){
                                termin.setEterm(0);
                            }else{
                            termin.setVeran_tend(formatDate(tend));
                            }


                            tbeitrag    = rsTermin.getString(5);
                            
                            if(j==0){
                                tbeitragold = tbeitrag;
                                termin.setVeran_tbeitrag(tbeitrag);
                            }else{
                                if(tbeitrag.equals(tbeitragold)){
                                }else{
                                   tbeitragold = tbeitrag;
                                   termin.setVeran_tbeitrag(tbeitrag);
                               }
                            }




                            j++;
                            termin.setTermintext(0);
                            termine.add(termin);

                           fkatbez = rsTermin.getString(7) +" "+ rsTermin.getString(8) +" "+rsTermin.getString(9);
                           System.out.println("fkatbez"+fkatbez);
                           if(fkatbez==null) fkatbez=""; 
                           altzusatz = rsTermin.getString(10);
                           if(altzusatz ==null) altzusatz ="";
                           altort = rsTermin.getString(11);
                           if(altort==null) altort="";
                           alttermin = rsTermin.getString(12);
                           if(alttermin==null) alttermin="";


                    }





//******************  BEGIN OF Construction and FONTS ********************************************

            com.lowagie.text.pdf.BaseFont buv47cl   = com.lowagie.text.pdf.BaseFont.createFont("UVCL____.ttf", com.lowagie.text.pdf.BaseFont.CP1252, com.lowagie.text.pdf.BaseFont.EMBEDDED);
            com.lowagie.text.Font uv47cl10    = new com.lowagie.text.Font(buv47cl, 10);


            com.lowagie.text.pdf.BaseFont buv67cb  = com.lowagie.text.pdf.BaseFont.createFont("UVCB____.ttf", com.lowagie.text.pdf.BaseFont.CP1252, com.lowagie.text.pdf.BaseFont.EMBEDDED);
            com.lowagie.text.Font uv67cb10  = new com.lowagie.text.Font(buv67cb, 10);
            com.lowagie.text.Font uv67cb11   = new com.lowagie.text.Font(buv67cb, 11);


      //******************  END OF Construction and FONTS ********************************************
             

            Phrase head0 = new Phrase("Veranstaltungsart,   -bezeichnung       -nummer -ort               -termin",uv67cb10);

           ColumnText ct0 = new ColumnText(cb);
           ct0.setSimpleColumn(56.6986f,currentY,399.9487f,currentY-8,14,Element.ALIGN_LEFT);
           ct0.addText(head0);
           ct0.go();

         currentY =   ct0.getYLine();

        System.out.println("currentY after Header"+currentY);          

    /*
        Phrase head10 = new Phrase("010 Zentrale Verwaltungsaufgaben",uv47cl10);
        
        ColumnText ct10 = new ColumnText(cb);
        ct10.setSimpleColumn(56.6986f,534.52852f,399.9487f,551.063f,16,Element.ALIGN_LEFT);
        ct10.addText(head10);
        ct10.go();

        currentY = ct10.getYLine()-9f;

     */                       



        
        
//********************The nested table2 BEGIN***************************************
   terminForm termin=null;

                 Phrase[] termin_city = new Phrase[100];
                 Phrase[] termin_vnr = new Phrase[100];
                 Phrase[] termin_date = new Phrase[100];




             Iterator i       = termine.iterator();
             String tnr       = "";
             String theTermin = "";
             String tbetrag = "";
             
             int m=0;
             

             while(i.hasNext()){

                 termin = (terminForm) i.next();


                 if(termin.getVeran_tbegin()==null) termin.setVeran_tbegin("");
                 if(termin.getVeran_tend()==null) termin.setVeran_tend("");


                 if(m==0){
                   tort = tort+termin.getVeran_tort();
                 }else{
                   tort = tort+"\n"+termin.getVeran_tort();
                 }

                 if(m==0){
                   tnr = tnr+termin.getVeran_tnr();
                 }else{
                   tnr = tnr+"\n"+termin.getVeran_tnr();
                 }



                 if(termin.getBterm()==1 && termin.getEterm()==0){

                      if(m==0){
                       if(termin.getVeran_tbegin().equals("01.01.2003") | termin.getVeran_tbegin().equals("02.01.2003")){
                       tbegin="1)";
                       }else{                
                       tbegin = tbegin+termin.getVeran_tbegin();
                       }
                       tbetrag = tbetrag+termin.getVeran_tbeitrag();
                      }else{
                       tbegin = tbegin+"\n"+termin.getVeran_tbegin();
                       tbetrag = tbetrag+"\n"+termin.getVeran_tbeitrag();
                      }
                 }else if(termin.getBterm()==1 && termin.getEterm()==1){        
                      if(m==0){
                       if(termin.getVeran_tbegin().equals("01.01.2003") | termin.getVeran_tbegin().equals("02.01.2003")){                
                       tbegin="1)";
                       }else{
                        tbegin = termin.getVeran_tbegin().substring(0,6);
                        tend = termin.getVeran_tend();
                        theTermin=theTermin+tbegin+"-"+tend;
                        

                       }
                        tbetrag = tbetrag+termin.getVeran_tbeitrag();
                     }else{
                       if(termin.getVeran_tbegin().equals("01.01.2003") | termin.getVeran_tbegin().equals("02.01.2003")){
                        tbegin="1)";
                       }else{
                       tbegin = termin.getVeran_tbegin().substring(0,6);
                       tend = termin.getVeran_tend();
                       theTermin=theTermin+"\n"+tbegin+"-"+tend;
                      

                       }
                       tbetrag = tbetrag+"\n"+termin.getVeran_tbeitrag();
                     }
                 }


                 m++;
             }





             if(m==0){
                  termin_city[1] = new Phrase("", uv47cl10);
             }else{
                  termin_city[1] = new Phrase(tort, uv47cl10);
             }


             termin_vnr[1] = new Phrase(tnr, uv47cl10);        
             System.out.println("termin_vnr[1]"+termin_vnr[1]);

          if(termin.getBterm()==1 && termin.getEterm()==0){
              termin_date[1] = new Phrase(tbegin, uv67cb10);
          }
          System.out.println("termin_date[1]"+termin_date[1]);


          if(termin.getBterm()==1 && termin.getEterm()==1){     
             termin_date[1] = new Phrase(theTermin, uv67cb10);
          }
          System.out.println("termin_date[1]"+termin_date[1]);



          if(currentY < 50f){
            document.newPage();
            currentY=558f;
          }


          Phrase head110 = new Phrase(tverantnr+" "+fkatbez,uv47cl10);

          ColumnText ct110 = new ColumnText(cb);
          
          



         // System.out.println("currentY vor ct12 : "+currentY+"\n");
          float   currentY1 = currentY;




          Chunk head12 = new Chunk(fkatbez,uv47cl10);
          HyphenationAuto autoDE = new HyphenationAuto("de","DR",2,2);
          head12.setHyphenation(autoDE);



          int status12 = 0;
          ColumnText ct12 = new ColumnText(cb);
          ct12.addText(head12);
          
          while((status12 & ct12.NO_MORE_TEXT) == 0) {          
          ct12.setSimpleColumn(56.6986f,currentY-8,220.7002f,currentY,8,Element.ALIGN_JUSTIFIED);          
          if(ct12.NO_MORE_COLUMN>1) currentY=ct12.getYLine()-10;
          status12=ct12.go();
          }
          currentY= ct12.getYLine()-9;

         // Phrase head13 = new Phrase("010.33",uv47cl10);
         // System.out.println("currentY vor ct13 : "+currentY+"\n");



          ColumnText ct13 = new ColumnText(cb);
          ct13.setSimpleColumn(234.7002f,currentY1-8,272.6674f,currentY1,8,Element.ALIGN_CENTER);
          ct13.addText(termin_vnr[1]);
          ct13.go();

         // Phrase head14 = new Phrase("Hannover",uv47cl10);
        //  System.out.println("currentY vor ct14 : "+currentY+"\n");


          ColumnText ct14 = new ColumnText(cb);
          ct14.setSimpleColumn(272.6674f,currentY1-8,320.15f,currentY1,8,Element.ALIGN_CENTER);
          ct14.addText(termin_city[1]);
          ct14.go();

      //     Chunk ck15 = new Chunk("21.03.-24.03.2003\n12.04.-15.04.2003\nfgsdfgs\ndfgsf dgsfdg",uv47cl10);
      //     ck15.setBackground(new Color(240,240,240));

          // Phrase head15 = new Phrase("21.03.-24.03.2003\n12.04.-15.04.2003\nfgsdfgs\ndfgsf dgsfdg",uv47cl10);

       //    System.out.println("currentY vor ct15 : "+currentY+"\n");


  
       
          Phrase head16 = new Phrase("12.04.-15.04.2003",uv47cl10);
          ColumnText ct15 = new ColumnText(cb);
          if(termin_date[1] == null) {
              ct15.addText(head16);

          }else{
             ct15.addText(termin_date[1]);
          }
          
          
                    System.out.println("nach add termin_date1");


          int status15 = 0;
          int NO_MORE_TEXT = 0;

       //   ct15.addText(head16);
          while((status15 & ct15.NO_MORE_TEXT) == 0) {
          ct15.setSimpleColumn(320.15f,currentY1-8,399.94f,currentY1,8,Element.ALIGN_RIGHT);
          status15=ct15.go();
          
          if(ct15.NO_MORE_COLUMN>1) currentY1=ct15.getYLine()-2;
          System.out.println("More cols: "+ct15.NO_MORE_COLUMN);
          System.out.println("currentY1: "+currentY1);
          }

       

          currentY1 = ct15.getYLine()-9f;

          if(currentY > currentY1){
               currentY = currentY1;
          }else{
              
          }
 
         System.out.println("currentY nach ct15 : "+currentY+"\n");
    
       }


      }//ende while loop for all pages


      
           
      document.close();     
           
           
       }catch(Exception ex)
        {
          System.out.println(ex.getMessage());
          ex.printStackTrace(System.out);
        }


    }//end main


   /***************************************************************
  * Method: formatDate
	+ Format Date for outprint in german sytle
  ***************************************************************/

    public static String formatDate (String _fdatum)
    {
			if(_fdatum == null) _fdatum = "0000-00-00";
                        if(_fdatum.length()<=9) _fdatum = "0000-00-00";
			String fyear	= _fdatum.substring(0,4);
			String fmonth = _fdatum.substring(5,7);
			String fday		= _fdatum.substring(8,10);
			String _fdate = fday+"."+fmonth+"."+fyear;
       
			return _fdate;
    }

  public static String cleanhtml(String string) {
    string = string.replaceAll("\n","");

    string = string.replaceAll("&quote","\"");
    string = string.replaceAll("&.*;","");

    string = string.replaceAll(" {4,}","\n");
    string = string.replaceAll(" {2,}"," ");

    string = string.replaceAll("\t","\n");


    string = string.replaceAll("<(b|B)(r|R)>","\n");
    string = string.replaceAll("<(u|U)(l|L)>","\n");
    string = string.replaceAll("<(l|L)(i|I)>","- ");


    string = string.replaceAll("<.*>","");
    string = string.replaceAll("-{2,}","- ");
    string = string.replaceAll("\n{2,}","\n");
    return string;
  }


}//end class

