oliver_lundag -4 Newbie Poster
/*
*It's a Running program that generates DBF file
*Question is: How can I make an IDX file Out of this.
*I tried to changed the extension by .idx but I'm not sure if it is *correct.
*/



package src.maintenance.DBFextraction;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JInternalFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JProgressBar;
import javax.swing.event.InternalFrameEvent;
import javax.swing.event.InternalFrameListener;

import org.apache.poi.hssf.record.IndexRecord;

import src.maintenance.DBConn.DBConnect;
import src.maintenance.Functions.Message;
import src.maintenance.core.mainform;

import com.linuxense.javadbf.DBFException;
import com.linuxense.javadbf.DBFField;
import com.linuxense.javadbf.DBFWriter;
import com.sun.corba.se.impl.ior.WireObjectKeyTemplate;

public class dbf_File extends JInternalFrame{

    /**
     * 
     */
    private static final long serialVersionUID = -6709587281437774236L;

    DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
    Date vatEffDate = null;
    Date vatExDate = null;
    SimpleDateFormat format = new SimpleDateFormat( "dd.MM.yyyy" );

    //JPanel
    private JPanel contentPane = null;

    //JLabel
    private JLabel JLProcess = null;

    //JButton
    private JButton btnGenerate = null;

    //progress bar
    private JProgressBar jProgressBar = null;
    private int Value = 0;
    private int maxValue = 0;
    NumberFormat NoFormat = NumberFormat.getInstance();

    //Variables
    String Action = "Stop";
    private String querypath = "";
//  static String effdate;
//  static String exdate;   
    Date effdate;
    Date exdate;    
    private static String tempVateffdate = "";
    private static String tempVatexdate = "";


    public dbf_File(){
        super ("DBF File Creator",//title
                false, //rezisable
                true, //closable
                false, //maximizable
                false); //iconifiable
        initialize();
        System.setSecurityManager(null);
        Action = "Start";
    }

    private void initialize(){
        this.setSize(192, 150);
        this.setTitle("DBF File Creator");
        this.setResizable(false);
        this.setVisible(true);
        this.setContentPane(getJContentPane());
        this.addInternalFrameListener(new InternalFrameListener(){
            public void internalFrameActivated(InternalFrameEvent arg0) {
                mainform.tree.setEnabled(false);
            }
            public void internalFrameClosed(InternalFrameEvent arg0) {}
            public void internalFrameClosing(InternalFrameEvent arg0) {
                mainform.tree.setEnabled(true);
            }
            public void internalFrameDeactivated(InternalFrameEvent arg0) {}
            public void internalFrameDeiconified(InternalFrameEvent arg0) {}
            public void internalFrameIconified(InternalFrameEvent arg0) {}
            public void internalFrameOpened(InternalFrameEvent arg0) {}
        });
        setTitle("DBF File Creator");
        querypath();
    }

    private JPanel getJContentPane(){
        if (contentPane == null){

            JLProcess = new JLabel();
            JLProcess.setBounds(new Rectangle(58, 55, 90, 23));
            JLProcess.setFont(new Font("Dialog", Font.BOLD, 12));
            JLProcess.setText("Processing...");
            JLProcess.setVisible(false);
        }
        contentPane = new JPanel();
        contentPane.setLayout(null);

        contentPane.add(JLProcess,null);
        contentPane.add(getbtnGenerate(),null);
        contentPane.add(getJProgressBar(),null);

        return contentPane;

    }

    private JProgressBar getJProgressBar() {
        if (jProgressBar == null) {
            jProgressBar = new JProgressBar();
            jProgressBar.setLocation(new Point(10, 80));
            jProgressBar.setString("waiting for events...");
            jProgressBar.setStringPainted(true);
            jProgressBar.setSize(new Dimension(170, 25));
        }
        return jProgressBar;
    }


    private JButton getbtnGenerate(){
        if (btnGenerate == null) {
            btnGenerate = new JButton();
            btnGenerate.setText("Generate DBF");
            btnGenerate.setBounds(new Rectangle(20, 20, 150, 30));
            btnGenerate.setMnemonic('G');
            btnGenerate.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent e){
                    try {
//                      String title = "screenShot";
//                      captureScreen(title);
                    } catch (Exception e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                    }
                    try{
                        if(Action.equalsIgnoreCase("Start")){
                            JLProcess.setVisible(true);
                            getRecord();
                            try{
//                              DBFWriter writer = new DBFWriter( new File(querypath+"hcvat.dbf"));

                                DBFWriter writer = new DBFWriter();

                                DBFField fields[] = new DBFField[8];

                                fields[0] = new DBFField();
                                fields[0].setName("COMP_CODE");
                                fields[0].setDataType(DBFField.FIELD_TYPE_C);
                                fields[0].setFieldLength(10);

                                fields[1] = new DBFField();
                                fields[1].setName("COMP_NAME");
                                fields[1].setDataType(DBFField.FIELD_TYPE_C);
                                fields[1].setFieldLength(30);

                                fields[2] = new DBFField();
                                fields[2].setName("COMP_ADD");
                                fields[2].setDataType(DBFField.FIELD_TYPE_C);
                                fields[2].setFieldLength(30);

                                fields[3] = new DBFField();
                                fields[3].setName("COMP_TIN");
                                fields[3].setDataType(DBFField.FIELD_TYPE_C);
                                fields[3].setFieldLength(15);

                                fields[4] = new DBFField();
                                fields[4].setName("VAT_FLAG");
                                fields[4].setDataType(DBFField.FIELD_TYPE_C);
                                fields[4].setFieldLength(1);

                                fields[5] = new DBFField();
                                fields[5].setName("HC_FLAG");
                                fields[5].setDataType(DBFField.FIELD_TYPE_C);
                                fields[5].setFieldLength(1);

                                fields[6] = new DBFField();
                                fields[6].setName("VAT_START");
//                              fields[6].setDataType(DBFField.FIELD_TYPE_C);
                                fields[6].setDataType(DBFField.FIELD_TYPE_D);
//                              fields[6].setFieldLength(8);

                                fields[7] = new DBFField();
                                fields[7].setName("VAT_END");
//                              fields[7].setDataType(DBFField.FIELD_TYPE_C);
                                fields[7].setDataType(DBFField.FIELD_TYPE_D);
//                              SimpleDateFormat format = new SimpleDateFormat( "dd.MM.yyyy" );
//                              fields[7].setFieldLength(8);

                                writer.setFields(fields);

                                try{

                                    String Select = DBConnect.Select( "bCustMasterFile", 
                                                                                            "custAccntNum, bNum, custAccntName," +
                                                                                            " address1, tinNo, vatExFlag, hCFlag, effdate, exdate",
                                                                                            null, null);

                                    Statement stmt = DBConnect.getConnection().createStatement();
                                    ResultSet rs = stmt.executeQuery(Select);

                                    while(rs.next()){   

                                        try{
                                            try{
                                                try{
                                                    effdate = rs.getDate("effdate");
                                                    exdate = rs.getDate("exdate");
//                                                  System.out.println(effdate);
//                                                  System.out.println(exdate);
                                                }catch(SQLException excp){}
                                            }catch (Exception excp) {}
                                        }catch(NullPointerException excp){}

                                        Object rowData[] = new Object[8];
                                        rowData[0] = rs.getString("custAccntNum")+rs.getString("bNum");
                                        rowData[1] = rs.getString("custAccntName");
                                        rowData[2] = rs.getString("address1");
                                        rowData[3] = rs.getString("tinNo");
                                        rowData[4] = rs.getString("vatExFlag");
                                        rowData[5] = rs.getString("hCFlag");
//                                      rowData[6] = effdate.substring(8, 10)+""+effdate.substring(5, 7)+""+effdate.substring(0, 4);
//                                      rowData[7] = exdate.substring(8, 10)+""+exdate.substring(5, 7)+""+exdate.substring(0, 4);
                                        rowData[6]  = effdate;
                                        rowData[7] = exdate;

                                        writer.addRecord(rowData);

                                    }
                                    jProgressBar.setString("processing record "+NoFormat.format(maxValue)+" of "+NoFormat.format(maxValue) );

                                    File filePath = new File(querypath); 
                                    File fileExist = new File(filePath+"/"+"hcvat.dbf");
                                    if (fileExist.exists()){
                                        Message.messageWarning("The File was already generated.");
                                        try{
                                            int setDialog = Message.messageYesNo("Do you want to overwrite the existing file?");
                                            if(JOptionPane.YES_OPTION == setDialog){
                                                FileOutputStream fileOutput = new FileOutputStream(querypath+"hcvat.dbf");
                                                FileOutputStream idxFileOutput = new FileOutputStream(querypath+"hcvat.idx");


                                                writer.write(fileOutput);
                                                writer.write(idxFileOutput);

                                                fileOutput.close();
                                                idxFileOutput.close();

                                                JLProcess.setVisible(false);
                                                jProgressBar.setValue( maxValue );
                                                Rectangle progressRect = jProgressBar.getBounds();
                                                progressRect.x = 0;
                                                progressRect.y = 0;
                                                jProgressBar.paintImmediately( progressRect );
                                                jProgressBar.setString("Successful");
                                                rs.close();
                                                stmt.close();
                                                Message.messageInfo("DBF file was successfull generated.");
                                            }else{
                                                JLProcess.setVisible(false);
                                                jProgressBar.setString("waiting for events...");
                                            }
                                        }catch(Exception exc){}
                                    }else{

                                        FileOutputStream fileOutput = new FileOutputStream(querypath+"hcvat.dbf");
                                        FileOutputStream idxFileOutput = new FileOutputStream(querypath+"hcvat.idx");

                                        writer.write( fileOutput);
                                        writer.write(idxFileOutput);

                                        fileOutput.close();
                                        idxFileOutput.close();

                                        JLProcess.setVisible(false);
                                        jProgressBar.setValue( maxValue );
                                        Rectangle progressRect = jProgressBar.getBounds();
                                        progressRect.x = 0;
                                        progressRect.y = 0;
                                        jProgressBar.paintImmediately( progressRect );
                                        jProgressBar.setString("Successful");
                                        rs.close();
                                        stmt.close();
                                        Message.messageInfo("DBF file was successfull generated.");
                                        jProgressBar.setString("waiting for events...");
                                    }
                                }catch(SQLException excp){
                                    excp.printStackTrace();
                                }

                            }catch(DBFException excp){
                                excp.printStackTrace();
                            }
                        }
                    }catch(IOException excp){
                        excp.printStackTrace();
                    }
                }
            });
        }
        return btnGenerate;
    }//end of bntGenerate Method

    private void querypath(){
        String query = "select dirdbf from branchSysFile";
        try{
            Statement st = DBConnect.getConnection().createStatement();
            ResultSet rs = st.executeQuery(query);

            while(rs.next()){
                querypath = rs.getString(1);
            }

            st.close();
            rs.close();

        }catch(Exception ex){

        }
    }//end of class

    private void getRecord(){
        int max = 0;
        try {
            String Query = "select count(custAccntNum) as countx from bCustMasterFile";
            Statement st0 = DBConnect.getConnection().createStatement();
            ResultSet rsh   = st0.executeQuery(Query);
            while(rsh.next()){
                max = rsh.getInt(1);
            }
            rsh.close();
            st0.close();

            maxValue = max;
            jProgressBar.setMinimum( 0 );
            jProgressBar.setMaximum( max );
            jProgressBar.setValue( 0 );
        }
        catch(Exception e){
            System.out.println(e.getMessage());
        }
    }//end of method

    public void captureScreen(String fileName) throws Exception {  

           Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();  
           Rectangle screenRectangle = new Rectangle(screenSize);  
           Robot robot = new Robot();  
           BufferedImage image = robot.createScreenCapture(screenRectangle);  
           ImageIO.write(image, "png", new File(querypath+fileName+".jpg"));  

        }  

}// end of class