com.nxhoaf.dbhelper.view.ExtractDataView.java Source code

Java tutorial

Introduction

Here is the source code for com.nxhoaf.dbhelper.view.ExtractDataView.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nxhoaf.dbhelper.view;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.nxhoaf.dbhelper.controller.ExtractorController;
import com.nxhoaf.dbhelper.domain.ConnectionInfo;
import com.nxhoaf.dbhelper.domain.ExtractorInfo;
import com.nxhoaf.dbhelper.domain.QueryInfo;

/**
 *
 * @author nxhoaf
 */
public class ExtractDataView extends javax.swing.JFrame implements ExtractorObserver {

    /**
    * 
    */
    private static final long serialVersionUID = 1L;
    @Autowired
    private ExtractorController extractDataController;

    /**
     * Creates new form MainFrame
     *
     * @param extractDataController
     */
    public ExtractDataView(ExtractorController extractDataController) {
        this.extractDataController = extractDataController;
        initComponents();
        initDefaultData();
        extractDataController.addObserver(this);
    }

    public void update(String message) {
        returnMessage.setText(message);
    }

    private void initDefaultData() {
        ExtractorInfo extractorInfo = extractDataController.getDefaultExtractInfo();

        ConnectionInfo connInfo = extractorInfo.getConnectionInfo();
        url.setText(connInfo.getConnectionUrl());
        driver.setText(connInfo.getDriverClass());
        username.setText(connInfo.getUsername());
        password.setText(connInfo.getPassword());

        QueryInfo queryInfo = extractorInfo.getQueryInfo();
        tableName.setText(queryInfo.getTableName());
        sqlQuery.setText(queryInfo.getQuery());

        fileLocation.setText(extractorInfo.getFileLocation());
    }

    private ExtractorInfo getExtractorInfo() {
        ExtractorInfo extractorInfo = new ExtractorInfo();

        ConnectionInfo databaseInfo = getConnectionInfo();
        extractorInfo.setConnectionInfo(databaseInfo);

        QueryInfo queryInfo = getQueryInfo();
        extractorInfo.setQueryInfo(queryInfo);

        extractorInfo.setFileLocation(fileLocation.getText());

        return extractorInfo;
    }

    private ConnectionInfo getConnectionInfo() {
        ConnectionInfo connectionInfo = new ConnectionInfo();
        connectionInfo.setDriverClass(driver.getText());
        connectionInfo.setConnectionUrl(url.getText());
        connectionInfo.setUsername(username.getText());
        connectionInfo.setPassword(password.getText());

        return connectionInfo;
    }

    private QueryInfo getQueryInfo() {
        QueryInfo queryInfo = new QueryInfo();
        queryInfo.setTableName(tableName.getText());
        queryInfo.setQuery(sqlQuery.getText());

        return queryInfo;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jDialog1 = new javax.swing.JDialog();
        jDialog2 = new javax.swing.JDialog();
        jLabel1 = new javax.swing.JLabel();
        dbToXmlBtn = new javax.swing.JButton();
        canvas1 = new java.awt.Canvas();
        jLabel2 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        url = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        username = new javax.swing.JTextField();
        password = new javax.swing.JTextField();
        xmlToDbBtn = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        fileLocation = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        sqlQuery = new javax.swing.JTextArea();
        tableName = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        driver = new javax.swing.JTextField();
        jLabel4 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        returnMessage = new javax.swing.JTextArea();
        jLabel13 = new javax.swing.JLabel();
        allDbToXml = new javax.swing.JButton();

        javax.swing.GroupLayout jDialog1Layout = new javax.swing.GroupLayout(jDialog1.getContentPane());
        jDialog1.getContentPane().setLayout(jDialog1Layout);
        jDialog1Layout.setHorizontalGroup(jDialog1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 400, Short.MAX_VALUE));
        jDialog1Layout.setVerticalGroup(jDialog1Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 300, Short.MAX_VALUE));

        javax.swing.GroupLayout jDialog2Layout = new javax.swing.GroupLayout(jDialog2.getContentPane());
        jDialog2.getContentPane().setLayout(jDialog2Layout);
        jDialog2Layout.setHorizontalGroup(jDialog2Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 400, Short.MAX_VALUE));
        jDialog2Layout.setVerticalGroup(jDialog2Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 300, Short.MAX_VALUE));

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 24)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 153, 0));
        jLabel1.setText("Database Extractor");

        dbToXmlBtn.setText("DB to XML");
        dbToXmlBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                dbToXmlBtnActionPerformed(evt);
            }
        });

        jLabel5.setText("Url:");

        jLabel6.setText("Username:");

        jLabel7.setText("Password:");

        xmlToDbBtn.setText("XML to DB");
        xmlToDbBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                xmlToDbBtnActionPerformed(evt);
            }
        });

        jLabel8.setText("File Location:");

        jLabel9.setText("Table name:");

        jLabel10.setText("SQL:");

        sqlQuery.setColumns(20);
        sqlQuery.setRows(5);
        jScrollPane1.setViewportView(sqlQuery);

        tableName.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                tableNameActionPerformed(evt);
            }
        });

        jLabel11.setText("Driver:");

        jLabel4.setFont(new java.awt.Font("Lucida Grande", 2, 10)); // NOI18N
        jLabel4.setText("Written by nxhoaf@gmail.com");

        jLabel3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/icon.png"))); // NOI18N

        returnMessage.setColumns(20);
        returnMessage.setRows(5);
        jScrollPane2.setViewportView(returnMessage);

        jLabel13.setText("Return Msg:");

        allDbToXml.setText("All DB to XML");
        allDbToXml.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                allDbToXmlActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createSequentialGroup()
                                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 262,
                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(170, 170, 170))
                                .addGroup(layout.createSequentialGroup().addGap(16, 16, 16).addGroup(layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup().addComponent(jLabel12)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jLabel2)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                .addComponent(jLabel3).addGap(234, 234, 234)
                                                .addComponent(canvas1, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                                .addComponent(jLabel4))
                                        .addGroup(layout
                                                .createSequentialGroup()
                                                .addGroup(
                                                        layout.createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                                .addComponent(jLabel8).addComponent(jLabel6))
                                                .addGap(0, 0, Short.MAX_VALUE))))
                                .addGroup(layout.createSequentialGroup().addGroup(layout
                                        .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addGroup(layout.createSequentialGroup().addGap(16, 16, 16).addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addComponent(jLabel10)
                                                .addGroup(layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(jLabel9,
                                                                javax.swing.GroupLayout.Alignment.TRAILING)
                                                        .addComponent(jLabel5)))
                                                .addGap(18, 18, 18))
                                        .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                                layout.createSequentialGroup().addContainerGap()
                                                        .addComponent(jLabel13).addGap(27, 27, 27)))
                                        .addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(layout.createSequentialGroup().addGroup(layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING, false)
                                                        .addComponent(url, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                325, Short.MAX_VALUE)
                                                        .addComponent(username)).addGap(18, 18, 18)
                                                        .addGroup(layout
                                                                .createParallelGroup(
                                                                        javax.swing.GroupLayout.Alignment.LEADING)
                                                                .addComponent(jLabel7).addComponent(jLabel11))
                                                        .addPreferredGap(
                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addGroup(layout
                                                                .createParallelGroup(
                                                                        javax.swing.GroupLayout.Alignment.TRAILING)
                                                                .addComponent(driver).addComponent(password)))
                                                .addGroup(
                                                        layout.createSequentialGroup()
                                                                .addComponent(tableName,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 135,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addGap(459, 459, Short.MAX_VALUE))
                                                .addComponent(fileLocation)
                                                .addGroup(layout
                                                        .createSequentialGroup()
                                                        .addGroup(layout
                                                                .createParallelGroup(
                                                                        javax.swing.GroupLayout.Alignment.TRAILING,
                                                                        false)
                                                                .addComponent(jScrollPane1,
                                                                        javax.swing.GroupLayout.Alignment.LEADING,
                                                                        javax.swing.GroupLayout.DEFAULT_SIZE, 599,
                                                                        Short.MAX_VALUE)
                                                                .addComponent(jScrollPane2,
                                                                        javax.swing.GroupLayout.Alignment.LEADING)
                                                                .addGroup(layout.createSequentialGroup()
                                                                        .addComponent(xmlToDbBtn).addGap(18, 18, 18)
                                                                        .addComponent(dbToXmlBtn).addGap(13, 13, 13)
                                                                        .addComponent(allDbToXml)))
                                                        .addGap(0, 0, Short.MAX_VALUE)))))
                        .addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                        .addComponent(jLabel2).addComponent(canvas1,
                                                javax.swing.GroupLayout.Alignment.TRAILING,
                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addComponent(jLabel12))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel4)
                                .addGroup(layout.createSequentialGroup().addGap(12, 12, 12).addComponent(jLabel1))
                                .addComponent(jLabel3))
                        .addGap(35, 35, 35)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel5)
                                .addComponent(url, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel11).addComponent(driver, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(20, 20, 20)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel6)
                                .addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel7).addComponent(password,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(24, 24, 24)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel8).addComponent(fileLocation,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(16, 16, 16)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel9).addComponent(tableName,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel10).addComponent(jScrollPane1,
                                        javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                        .addComponent(dbToXmlBtn).addComponent(allDbToXml))
                                .addComponent(xmlToDbBtn))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 108,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel13))
                        .addGap(14, 14, 14)));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void dbToXmlBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_dbToXmlBtnActionPerformed
        ExtractorInfo extractorInfo = getExtractorInfo();
        extractDataController.dbToXmlPartial(extractorInfo);
    }//GEN-LAST:event_dbToXmlBtnActionPerformed

    private void tableNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_tableNameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_tableNameActionPerformed

    private void xmlToDbBtnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_xmlToDbBtnActionPerformed
        // TODO add your handling code here:
        ExtractorInfo extractorInfo = getExtractorInfo();
        extractDataController.xmlToDB(extractorInfo);
    }//GEN-LAST:event_xmlToDbBtnActionPerformed

    private void allDbToXmlActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_allDbToXmlActionPerformed
        // TODO add your handling code here:
        ExtractorInfo extractorInfo = getExtractorInfo();
        extractDataController.dbToXmlFull(extractorInfo);
    }//GEN-LAST:event_allDbToXmlActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(ExtractDataView.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(ExtractDataView.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(ExtractDataView.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(ExtractDataView.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        }
        //</editor-fold>
        //</editor-fold>
        @SuppressWarnings("resource")
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:beans.xml");
        final ExtractorController extractDataController = (ExtractorController) context
                .getBean("extractorController");
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new ExtractDataView(extractDataController).setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton allDbToXml;
    private java.awt.Canvas canvas1;
    private javax.swing.JButton dbToXmlBtn;
    private javax.swing.JTextField driver;
    private javax.swing.JTextField fileLocation;
    private javax.swing.JDialog jDialog1;
    private javax.swing.JDialog jDialog2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField password;
    private javax.swing.JTextArea returnMessage;
    private javax.swing.JTextArea sqlQuery;
    private javax.swing.JTextField tableName;
    private javax.swing.JTextField url;
    private javax.swing.JTextField username;
    private javax.swing.JButton xmlToDbBtn;
    // End of variables declaration//GEN-END:variables
}