org.smart.migrate.ui.UpdateRelatePKDialog.java Source code

Java tutorial

Introduction

Here is the source code for org.smart.migrate.ui.UpdateRelatePKDialog.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 org.smart.migrate.ui;

import java.awt.event.ItemEvent;
import java.sql.Connection;
import java.util.List;
import java.util.prefs.Preferences;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import org.apache.commons.lang3.StringUtils;
import org.smart.migrate.dao.MetaDao;
import org.smart.migrate.dao.MetaDaoFactory;
import org.smart.migrate.model.Field;
import org.smart.migrate.service.ImportManager;
import org.smart.migrate.setting.MigratePlan;
import org.smart.migrate.util.ConnectionUtils;

/**
 *
 * @author sindtom
 */
public class UpdateRelatePKDialog extends javax.swing.JDialog {

    private ImportManager importManager;

    private MigratePlan migratePlan;

    private MetaDao metaDao;

    private Connection connection;

    private final Preferences preferences = Preferences.userRoot().node(this.getClass().getName());

    private void loadParams() {
        getParam("fk.table", cbxFKTable);
        getParam("fk.logicfk", cbxFKLogic);
        getParam("fk.fk", cbxFK);

        getParam("pk.table", cbxPKTable);
        getParam("pk.logicpk", cbxPKLogic);
        getParam("pk.pk", cbxPK);
    }

    private void saveParams() {
        setParam("fk.table", cbxFKTable);
        setParam("fk.logicfk", cbxFKLogic);
        setParam("fk.fk", cbxFK);

        setParam("pk.table", cbxPKTable);
        setParam("pk.logicpk", cbxPKLogic);
        setParam("pk.pk", cbxPK);

    }

    private void setParam(String key, JComboBox cbx) {
        if (StringUtils.isNotBlank((String) cbx.getSelectedItem())) {
            preferences.put(key, (String) cbx.getSelectedItem());
        }
    }

    private void getParam(String key, JComboBox cbx) {
        cbx.getModel().setSelectedItem(preferences.get(key, null));
    }

    private java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/smart/migrate/ui/Bundle"); // NOI18N;

    /**
     * Creates new form UpdateRelatePKDialog
     */
    public UpdateRelatePKDialog(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }

    public void initComponentsData() {
        initTables(cbxFKTable);
        initTables(cbxPKTable);
        loadParams();

    }

    //?
    private void initTables(JComboBox comboBox) {
        comboBox.removeAllItems();
        List<String> tables = migratePlan.getTargetTableNames();
        for (String table : tables) {
            comboBox.addItem(table);
        }
    }

    //?
    private void initFields(JComboBox comboBox, String table) {
        comboBox.removeAllItems();
        List<Field> fields = getMetaDao().getFieldsOfTable(getConnection(), table);
        for (Field field : fields) {
            comboBox.addItem(field.getName());
        }
    }

    /**
     * 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() {

        btnUpdatePK = new javax.swing.JButton();
        cbxFKTable = new javax.swing.JComboBox();
        cbxFKLogic = new javax.swing.JComboBox();
        cbxFK = new javax.swing.JComboBox();
        jLabel7 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        cbxPKTable = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        cbxPKLogic = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        cbxPK = new javax.swing.JComboBox();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("org/smart/migrate/ui/Bundle"); // NOI18N
        btnUpdatePK.setText(bundle.getString("UpdateRelatePKDialog.btnUpdatePK.text")); // NOI18N
        btnUpdatePK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnUpdatePKActionPerformed(evt);
            }
        });

        cbxFKTable.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbxFKTableItemStateChanged(evt);
            }
        });

        cbxFKLogic.setMaximumRowCount(20);

        cbxFK.setMaximumRowCount(20);

        jLabel7.setText(bundle.getString("UpdateRelatePKDialog.jLabel7.text")); // NOI18N

        jLabel1.setText(bundle.getString("UpdateRelatePKDialog.jLabel1.text")); // NOI18N

        jLabel2.setText(bundle.getString("UpdateRelatePKDialog.jLabel2.text")); // NOI18N

        cbxPKTable.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cbxPKTableItemStateChanged(evt);
            }
        });

        jLabel5.setText(bundle.getString("UpdateRelatePKDialog.jLabel5.text")); // NOI18N

        jLabel3.setText(bundle.getString("UpdateRelatePKDialog.jLabel3.text")); // NOI18N

        cbxPKLogic.setMaximumRowCount(20);

        jLabel4.setText(bundle.getString("UpdateRelatePKDialog.jLabel4.text")); // NOI18N

        cbxPK.setMaximumRowCount(20);

        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().addGap(32, 32, 32).addGroup(
                                                layout.createParallelGroup(
                                                        javax.swing.GroupLayout.Alignment.TRAILING)
                                                        .addComponent(jLabel1).addComponent(jLabel7))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addGroup(layout
                                                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                                .addGroup(layout.createSequentialGroup()
                                                        .addComponent(cbxFKLogic,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE, 144,
                                                                javax.swing.GroupLayout.PREFERRED_SIZE)
                                                        .addGap(38, 38, 38).addComponent(jLabel2)
                                                        .addPreferredGap(
                                                                javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                        .addComponent(cbxFK, 0,
                                                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                Short.MAX_VALUE))
                                                .addComponent(
                                                        cbxFKTable, 0, javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        Short.MAX_VALUE)))
                                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING,
                                        layout.createSequentialGroup().addGap(28, 28, 28)
                                                .addGroup(layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(jLabel5,
                                                                javax.swing.GroupLayout.Alignment.TRAILING)
                                                        .addComponent(
                                                                jLabel3,
                                                                javax.swing.GroupLayout.Alignment.TRAILING))
                                                .addPreferredGap(
                                                        javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                                .addGroup(layout
                                                        .createParallelGroup(
                                                                javax.swing.GroupLayout.Alignment.LEADING)
                                                        .addComponent(btnUpdatePK)
                                                        .addGroup(layout.createSequentialGroup()
                                                                .addComponent(cbxPKLogic,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE, 146,
                                                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                                                .addGap(38, 38, 38).addComponent(jLabel4)
                                                                .addPreferredGap(
                                                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                                                .addComponent(cbxPK, 0,
                                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                                        Short.MAX_VALUE))
                                                        .addComponent(cbxPKTable, 0, 372, Short.MAX_VALUE))))
                        .addGap(42, 42, 42)));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup().addGap(31, 31, 31)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(cbxFKTable, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel7))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(cbxFKLogic, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(cbxFK, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel1).addComponent(jLabel2))
                        .addGap(40, 40, 40)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(cbxPKTable, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel5))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                                .addComponent(jLabel3)
                                .addComponent(cbxPKLogic, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jLabel4).addComponent(cbxPK, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                        javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(24, 24, 24).addComponent(btnUpdatePK).addContainerGap(25, Short.MAX_VALUE)));

        setBounds(0, 0, 565, 303);
    }// </editor-fold>//GEN-END:initComponents

    private void btnUpdatePKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnUpdatePKActionPerformed
        //updateTargetRelatedFK(String foreignTable,String logicFK,String FK,String primaryTable,String logicPK,String PK){
        if (cbxFKTable.getSelectedItem() == null || cbxFKLogic.getSelectedItem() == null
                || cbxFK.getSelectedItem() == null || cbxPKTable.getSelectedItem() == null
                || cbxPKLogic.getSelectedItem() == null || cbxPK.getSelectedItem() == null) {
            JOptionPane.showMessageDialog(this, bundle.getString("UpdateRelatePKDialog.error.message"));
        } else {
            importManager.setMigratePlan(migratePlan);
            importManager.connectToDataBase(false, true);
            importManager.updateTargetRelatedFK((String) cbxFKTable.getSelectedItem(),
                    (String) cbxFKLogic.getSelectedItem(), (String) cbxFK.getSelectedItem(),
                    (String) cbxPKTable.getSelectedItem(), (String) cbxPKLogic.getSelectedItem(),
                    (String) cbxPK.getSelectedItem());
            importManager.closeConnection();
            JOptionPane.showMessageDialog(this, bundle.getString("UpdateRelatePKDialog.success.message"));
        }
    }//GEN-LAST:event_btnUpdatePKActionPerformed

    private void cbxFKTableItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbxFKTableItemStateChanged
        if (evt.getStateChange() == ItemEvent.SELECTED) {
            initFields(cbxFKLogic, (String) evt.getItem());
            initFields(cbxFK, (String) evt.getItem());
        }
    }//GEN-LAST:event_cbxFKTableItemStateChanged

    private void cbxPKTableItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cbxPKTableItemStateChanged
        if (evt.getStateChange() == ItemEvent.SELECTED) {
            initFields(cbxPKLogic, (String) evt.getItem());
            initFields(cbxPK, (String) evt.getItem());
        }
    }//GEN-LAST:event_cbxPKTableItemStateChanged

    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        saveParams();
    }//GEN-LAST:event_formWindowClosing

    /**
     * @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(UpdateRelatePKDialog.class.getName())
                    .log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(UpdateRelatePKDialog.class.getName())
                    .log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(UpdateRelatePKDialog.class.getName())
                    .log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(UpdateRelatePKDialog.class.getName())
                    .log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                UpdateRelatePKDialog dialog = new UpdateRelatePKDialog(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnUpdatePK;
    private javax.swing.JComboBox cbxFK;
    private javax.swing.JComboBox cbxFKLogic;
    private javax.swing.JComboBox cbxFKTable;
    private javax.swing.JComboBox cbxPK;
    private javax.swing.JComboBox cbxPKLogic;
    private javax.swing.JComboBox cbxPKTable;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    // End of variables declaration//GEN-END:variables

    /**
     * @return the importManager
     */
    public ImportManager getImportManager() {
        return importManager;
    }

    /**
     * @param importManager the importManager to set
     */
    public void setImportManager(ImportManager importManager) {
        this.importManager = importManager;
    }

    /**
     * @return the migratePlan
     */
    public MigratePlan getMigratePlan() {
        return migratePlan;
    }

    /**
     * @param migratePlan the migratePlan to set
     */
    public void setMigratePlan(MigratePlan migratePlan) {
        this.migratePlan = migratePlan;
        setConnection(ConnectionUtils.connect(migratePlan.getTargetDB()));
        setMetaDao(MetaDaoFactory.createMetaDao(migratePlan.getTargetDB().getdBType(),
                migratePlan.getTargetDB().getDatabase()));
    }

    /**
     * @return the metaDao
     */
    public MetaDao getMetaDao() {
        return metaDao;
    }

    /**
     * @param metaDao the metaDao to set
     */
    public void setMetaDao(MetaDao metaDao) {
        this.metaDao = metaDao;
    }

    /**
     * @return the connection
     */
    public Connection getConnection() {
        return connection;
    }

    /**
     * @param connection the connection to set
     */
    public void setConnection(Connection connection) {
        this.connection = connection;
    }
}