Package realcix20.guis.relationships

Source Code of realcix20.guis.relationships.RelationManager$Relation

package realcix20.guis.relationships;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.Vector;

import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JFormattedTextField;
import javax.swing.JTextField;
import javax.swing.text.DefaultFormatterFactory;
import javax.swing.text.NumberFormatter;

import realcix20.classes.basic.Column;
import realcix20.classes.basic.ColumnManager;
import realcix20.classes.plugins.CurrencyPlugin;
import realcix20.classes.plugins.UomPlugin;
import realcix20.guis.components.DetailList;
import realcix20.guis.components.XrCalculator;
import realcix20.guis.models.ListTableModel;
import realcix20.guis.utils.ComponentExt;
import realcix20.guis.utils.ComponentManager;
import realcix20.guis.utils.I18NManager;
import realcix20.guis.utils.Item;
import realcix20.utils.DAO;
import realcix20.utils.GlobalValueManager;
import realcix20.utils.ObjectUtil;

//Will be modified, 7-7
public class RelationManager {
   
    private static RelationManager instance;
   
    private Vector relationTable;
   
    //restore all components(include head & item components)
    private Vector componentExts;
       
    private String stat = "initing";
   
    private RelationManager(Vector componentExts) {
       
            this.componentExts = componentExts;
            createRelationship()
           
            Iterator relationsIter = relationTable.iterator();
            while (relationsIter.hasNext()) {
                Relation relation = (Relation)relationsIter.next();
//                System.err.println("Relation : " + relation.toString());
            }
                       
            init81And82ComponentsActions();
       
    }
   
    public void init81Or82Or83ComponentAction(ComponentExt componentExt) {
                   
            final Column column = componentExt.getColumn();
            JComponent component = componentExt.getComponent();
            if (column.getInputType() == 83) {
                final XrCalculator xrCalculator = (XrCalculator)component;
                xrCalculator.setRelationManager(this);
                final Vector parameters = getParameters(componentExt);
                final String sql = getSQL(componentExt, parameters);
                xrCalculator.addActionListener(
                    new ActionAdapter() {
                        public void actionPerformed(ActionEvent e) {

                            DAO dao = DAO.getInstance();
                            dao.query(sql);
                            for (int i = 0; i < parameters.size(); i++) {

                                String parameter = (String)parameters.get(i);
                                StringTokenizer st = new StringTokenizer(parameter, ".");
                                String tableName = (String)st.nextElement();
                                String columnName = (String)st.nextElement();
                                ComponentExt componentExt = findComponentExt(tableName, columnName);
                                JComponent component = componentExt.getComponent();
                                dao.setObject(i+1, ComponentManager.getValue(component));

                            }

                            ResultSet rs = dao.executeQuery();
                            try {
                                ResultSetMetaData rsmd = rs.getMetaData();
                                Vector columns = new Vector();
                                for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                                    ComponentExt tempComponentExt = findComponentExt(column.getTableName(), rsmd.getColumnName(i));
                                    boolean isThisObjectColumn = (tempComponentExt != null);
                                    if (isThisObjectColumn)
                                        columns.add(column.getTableName() + "-" + rsmd.getColumnName(i));
                                    else
                                        columns.add(rsmd.getTableName(i) + "-" + rsmd.getColumnName(i));

                                }                                                                               
                                xrCalculator.setColumns(columns);
                            } catch (Exception ee) {
                                ee.printStackTrace();
                            }                                     

                        }     
                });
            } else if ( (column.getInputType() == 81) || (column.getInputType() == 82) ) {                    
                final DetailList detailList = (DetailList)component;
                detailList.setRelationManager(this);
                final Vector parameters = getParameters(componentExt);
                final String sql = getSQL(componentExt, parameters);
                detailList.addActionListener(
                    new ActionAdapter() {
                        public void actionPerformed(ActionEvent e) {

//                            System.err.println("81 = 82");
                            DAO dao = DAO.getInstance();
                            dao.query(sql);
                            for (int i = 0; i < parameters.size(); i++) {
                                String parameter = (String)parameters.get(i);
                                StringTokenizer st = new StringTokenizer(parameter, ".");
                                String tableName = (String)st.nextElement();
                                String columnName = (String)st.nextElement();
//                                System.err.println("TABLENAME=" + tableName + "  COLUMNNAME=" + columnName);
                                ComponentExt componentExt = findComponentExt(tableName, columnName);
//                                System.err.println("componentExt = " + componentExt);
                                JComponent component = componentExt.getComponent();
                                dao.setObject(i+1, ComponentManager.getValue(component));
                            }
                            ResultSet rs = dao.executeQuery();                           
                            try {
//                                System.err.println(rs.getStatement());
                                ResultSetMetaData rsmd = rs.getMetaData();
                                Vector columns = new Vector();
                                Vector columnNames = new Vector();
                                Vector datas = new Vector();
                                detailList.tDataVector = new Vector();
                                for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                                    ComponentExt tempComponentExt = findComponentExt(column.getTableName(), rsmd.getColumnName(i));
                                    boolean isThisObjectColumn = (tempComponentExt != null);
                                    if (isThisObjectColumn)
                                        columns.add(column.getTableName() + "-" + rsmd.getColumnName(i));
                                    else
                                        columns.add(rsmd.getTableName(i) + "-" + rsmd.getColumnName(i));

                                    Column tempColumn = null;
                                    if (isThisObjectColumn)
                                        tempColumn = ColumnManager.getColumn(column.getTableName(), rsmd.getColumnName(i));
                                    else
                                        tempColumn = ColumnManager.getColumn(rsmd.getTableName(i), rsmd.getColumnName(i));
                                    if (tempColumn != null) {
                                        String columnName = ObjectUtil.findColumnTxt(tempColumn.getClsId(), tempColumn.getTableName(), tempColumn.getColumnName());
                                        if (columnName != null)
                                            columnNames.add(columnName);
                                        else {
                                            columnNames.add("Txt");
                                        }
                                    } else {
                                        columnNames.add("Txt");
                                    }
                                }                                       
                                columnNames.add("value");//restore value
                                detailList.setColumns(columns);
                               
                                while (rs.next()) {
                                    Vector rowDatas = new Vector();
                                    detailList.tRowDatas = new Vector();
                                    for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                                        Object columnValue = rs.getObject(rsmd.getColumnName(i));
                                        ComponentExt tempComponentExt = findComponentExt(column.getTableName(), rsmd.getColumnName(i));
                                        boolean isThisObjectColumn = (tempComponentExt != null);
                                        Column tempColumn = null;
                                        if (isThisObjectColumn) {
                                            tempColumn = ColumnManager.getColumn(column.getTableName(), rsmd.getColumnName(i));
                                        } else {
                                            tempColumn = ColumnManager.getColumn(rsmd.getTableName(i), rsmd.getColumnName(i));
                                        }
                                        if (tempColumn != null) {
                                            if (tempColumn.isI18N()) {
                                                String i18nString = I18NManager.getI18NString(tempColumn.getI18nPrefix(), columnValue);
                                                if (GlobalValueManager.getValue("APPLICATION.ZIC").equals("1")) {
                                                    rowDatas.add(i18nString);
                                                } else if (GlobalValueManager.getValue("APPLICATION.ZIC").equals("2")) {
                                                    rowDatas.add(columnValue);
                                                } else if (GlobalValueManager.getValue("APPLICATION.ZIC").equals("3")) {
                                                    rowDatas.add(columnValue + " - " + i18nString);
                                                }
                                                detailList.tRowDatas.add(columnValue);
                                            } else {
                                                rowDatas.add(columnValue);
                                                detailList.tRowDatas.add(columnValue);
                                            }
                                        } else {
                                            rowDatas.add(columnValue);
                                            detailList.tRowDatas.add(columnValue);
                                        }                                                                                                                                 
                                    }
                                    rowDatas.add(rs.getObject(column.getColumnName()));//value
                                    datas.add(rowDatas);
                                    detailList.tDataVector.add(detailList.tRowDatas);
                                }
                                ListTableModel model = new ListTableModel(columnNames, datas);
                                detailList.updateTable(model);
                            } catch (Exception ee) {
                                ee.printStackTrace();
                            }                                     

                        }     
                });
            }
       
    }
   
    public void init81And82ComponentsActions() {
       
            Iterator componentExtIter = componentExts.iterator();
            while (componentExtIter.hasNext()) {
               
                    ComponentExt componentExt = (ComponentExt)componentExtIter.next();
                    final Column column = componentExt.getColumn();
                    JComponent component = componentExt.getComponent();
                    if (column.getInputType() == 83) {
                        final XrCalculator xrCalculator = (XrCalculator)component;
                        xrCalculator.setRelationManager(this);
                        final Vector parameters = getParameters(componentExt);
                        final String sql = getSQL(componentExt, parameters);
                        xrCalculator.addActionListener(
                            new ActionAdapter() {
                                public void actionPerformed(ActionEvent e) {                                                           
                                    DAO dao = DAO.getInstance();
                                    dao.query(sql);
                                    for (int i = 0; i < parameters.size(); i++) {
                                   
                                        String parameter = (String)parameters.get(i);
                                        StringTokenizer st = new StringTokenizer(parameter, ".");
                                        String tableName = (String)st.nextElement();
                                        String columnName = (String)st.nextElement();
                                        ComponentExt componentExt = findComponentExt(tableName, columnName);
                                        JComponent component = componentExt.getComponent();
                                        dao.setObject(i+1, ComponentManager.getValue(component));
                                   
                                    }
                                   
                                    ResultSet rs = dao.executeQuery();
                                    try {
                                        ResultSetMetaData rsmd = rs.getMetaData();
                                        Vector columns = new Vector();
                                        for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                                            ComponentExt tempComponentExt = findComponentExt(column.getTableName(), rsmd.getColumnName(i));
                                            boolean isThisObjectColumn = (tempComponentExt != null);
                                            if (isThisObjectColumn)
                                                columns.add(column.getTableName() + "-" + rsmd.getColumnName(i));
                                            else
                                                columns.add(rsmd.getTableName(i) + "-" + rsmd.getColumnName(i));
                                           
                                        }                                                                               
                                        xrCalculator.setColumns(columns);
                                    } catch (Exception ee) {
                                        ee.printStackTrace();
                                    }                                     
                                   
                                }     
                        });
                    } else if ( (column.getInputType() == 81) || (column.getInputType() == 82) ) {                       
                        final DetailList detailList = (DetailList)component;
                        detailList.setRelationManager(this);
                        final Vector parameters = getParameters(componentExt);
                        final String sql = getSQL(componentExt, parameters);
                        detailList.addActionListener(
                            new ActionAdapter() {
                                public void actionPerformed(ActionEvent e) {                                                           
                                    DAO dao = DAO.getInstance();
                                    dao.query(sql);
                                    for (int i = 0; i < parameters.size(); i++) {
                                   
                                        String parameter = (String)parameters.get(i);
                                        StringTokenizer st = new StringTokenizer(parameter, ".");
                                        String tableName = (String)st.nextElement();
                                        String columnName = (String)st.nextElement();
                                        ComponentExt componentExt = findComponentExt(tableName, columnName);
                                        JComponent component = componentExt.getComponent();
                                        dao.setObject(i+1, ComponentManager.getValue(component));
                                   
                                    }
                                   
                                    ResultSet rs = dao.executeQuery();
                                    try {
                                        ResultSetMetaData rsmd = rs.getMetaData();
                                        Vector columns = new Vector();
                                        Vector columnNames = new Vector();
                                        Vector datas = new Vector();
                                        detailList.tDataVector = new Vector();
                                        for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                                            ComponentExt tempComponentExt = findComponentExt(column.getTableName(), rsmd.getColumnName(i));
                                            boolean isThisObjectColumn = (tempComponentExt != null);
                                            if (isThisObjectColumn)
                                                columns.add(column.getTableName() + "-" + rsmd.getColumnName(i));
                                            else
                                                columns.add(rsmd.getTableName(i) + "-" + rsmd.getColumnName(i));

                                            Column tempColumn = null;
                                            if (isThisObjectColumn)
                                                tempColumn = ColumnManager.getColumn(column.getTableName(), rsmd.getColumnName(i));
                                            else
                                                tempColumn = ColumnManager.getColumn(rsmd.getTableName(i), rsmd.getColumnName(i));
                                            if (tempColumn != null) {
                                                String columnName = ObjectUtil.findColumnTxt(tempColumn.getClsId(), tempColumn.getTableName(), tempColumn.getColumnName());
                                                if (columnName != null)
                                                    columnNames.add(columnName);
                                                else {
                                                    columnNames.add("Txt");
                                                }
                                            } else {
                                                columnNames.add("Txt");
                                            }
                                        }                                       
                                        columnNames.add("value");//restore value
                                        detailList.setColumns(columns);
                                        while (rs.next()) {
                                            detailList.tRowDatas = new Vector();
                                            Vector rowDatas = new Vector();
                                            for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                                                Object columnValue = rs.getObject(rsmd.getColumnName(i));
                                                ComponentExt tempComponentExt = findComponentExt(column.getTableName(), rsmd.getColumnName(i));
                                                boolean isThisObjectColumn = (tempComponentExt != null);
                                                Column tempColumn = null;
                                                if (isThisObjectColumn) {
                                                    tempColumn = ColumnManager.getColumn(column.getTableName(), rsmd.getColumnName(i));
                                                } else {
                                                    tempColumn = ColumnManager.getColumn(rsmd.getTableName(i), rsmd.getColumnName(i));
                                                }
                                                if (tempColumn != null) {
                                                    if (tempColumn.isI18N()) {
                                                        String i18nString = I18NManager.getI18NString(tempColumn.getI18nPrefix(), columnValue);
                                                        if (GlobalValueManager.getValue("APPLICATION.ZIC").equals("1")) {
                                                            rowDatas.add(i18nString);
                                                        } else if (GlobalValueManager.getValue("APPLICATION.ZIC").equals("2")) {
                                                            rowDatas.add(columnValue);
                                                        } else if (GlobalValueManager.getValue("APPLICATION.ZIC").equals("3")) {
                                                            rowDatas.add(columnValue + " - " + i18nString);
                                                        }     
                                                        detailList.tRowDatas.add(columnValue);
                                                    } else {
                                                        rowDatas.add(columnValue);
                                                        detailList.tRowDatas.add(columnValue);
                                                    }
                                                } else {
                                                    rowDatas.add(columnValue);
                                                    detailList.tRowDatas.add(columnValue);
                                                }                                                                                                                                 
                                            }
                                            rowDatas.add(rs.getObject(column.getColumnName()));//value
                                            datas.add(rowDatas);
                                            detailList.tDataVector.add(detailList.tRowDatas);
                                        }
                                        ListTableModel model = new ListTableModel(columnNames, datas);
                                        detailList.updateTable(model);
                                    } catch (Exception ee) {
                                        ee.printStackTrace();
                                    }                                     
                                   
                                }     
                        });
                    }
               
            }
       
    }
   
    public static RelationManager getInstance(Vector componentExts) {
       
        instance = new RelationManager(componentExts);
       
        return instance;
       
    }
   
    //Create relationship for components   
    private void createRelationship() {
                       
            relationTable = createRelationTable()

            /*Iterator relationIter = relationTable.iterator();
          
            while (relationIter.hasNext()) {

                Relation relation = (Relation)relationIter.next();

                ComponentExt pComponentExt = relation.getPComponentExt();
                ComponentExt cComponentExt = relation.getCComponentExt();
               
                connectComponents(pComponentExt, cComponentExt);

            }
                       
            initialNotBeControledComponents();*/
       
    }
   
      
    //Create relationship Table
    private Vector createRelationTable() {
       
        Vector relations = new Vector();
       
        Iterator componentExtIter = componentExts.iterator();
        while (componentExtIter.hasNext()) {
           
            ComponentExt componentExt = (ComponentExt)componentExtIter.next();
            Column column = componentExt.getColumn();
            if ( (column.getCControls() != null) && (!column.getCControls().trim().equals("")) ) {
               
                StringTokenizer st = new StringTokenizer(column.getCControls(), "!!");
                while (st.hasMoreElements()) {
                    StringTokenizer stt = new StringTokenizer((String)st.nextElement(), ".");
                    String tableName = (String)stt.nextElement();
                    String columnName = (String) stt.nextElement();
                    ComponentExt cComponentExt = findComponentExt(tableName, columnName);
                    Relation relation = new Relation(componentExt, cComponentExt);
                    relations.add(relation);
                }
               
            }
           
        }               
       
        return relations;
       
    }
   
    //help
    public Vector getChildComponentExts(Column column) {
       
            Vector childComponentExts = new Vector();
            //Column column = componentExt.getColumn();
            if ( (column.getCControls() != null) && (!column.getCControls().trim().equals("")) ) {
                StringTokenizer st = new StringTokenizer(column.getCControls(), "!!");
                while (st.hasMoreElements()) {
                    StringTokenizer stt = new StringTokenizer((String)st.nextElement(), ".");
                    String tableName = (String)stt.nextElement();
                    String columnName = (String) stt.nextElement();
                    ComponentExt cComponentExt = findComponentExt(tableName, columnName);
                    childComponentExts.add(cComponentExt);
                }
            }
           
            return childComponentExts;
       
    }
   
   
    //initial not be controled components
    private void initialNotBeControledComponents() {
       
            Iterator componentExtIter = componentExts.iterator();
            while (componentExtIter.hasNext()) {

                ComponentExt componentExt = (ComponentExt)componentExtIter.next();
                Column column = componentExt.getColumn();
                JComponent component = componentExt.getComponent();

                switch (column.getInputType()) {

                    case 1:
                    case 2:                   

                        if ( (column.getPControls() == null) || (column.getPControls().equals("")) ) {
                           
                            DAO dao = DAO.getInstance();
                            dao.query(column.getInputPar());
                            ResultSet rs = dao.executeQuery();
                            try {
                               
                                //add null
                                Item nilItem = new Item("", "");
                                ((JComboBox)component).addItem(nilItem);
                               
                                ResultSetMetaData rsmd = rs.getMetaData();
                                if (rsmd.getColumnCount() == 1) {
                                    while (rs.next()) {
                                        Item item = new Item(rs.getObject(1), rs.getObject(1));
                                        ((JComboBox)component).addItem(item);
                                    }
                                } else {
                                    while (rs.next()) {

                                        Object value = null;
                                        StringBuffer sb = new StringBuffer();
                                        sb.append("<html>");                                       
                                        for (int i = 1; i < rsmd.getColumnCount(); i++) {
                                            if (rsmd.getColumnName(i).equals(column.getColumnName())) {
                                                sb.append("<b>" + rs.getObject(i) + "</b> | ");
                                                value = rs.getObject(i);
                                            } else {
                                                sb.append(rs.getObject(i) + " | ");
                                            }
                                        }
                                                                                       
                                        if (rsmd.getColumnName(rsmd.getColumnCount()).equals(column.getColumnName())) {
                                            sb.append("<b>" + rs.getObject(rsmd.getColumnCount()) + "</b>");
                                            value = rs.getObject(rsmd.getColumnCount());
                                        } else {
                                            sb.append(rs.getObject(rsmd.getColumnCount()) + "");
                                        }
                                        sb.append("</html>");
                                       
                                        Item item = new Item(value, sb.toString());
                                        ((JComboBox)component).addItem(item);
                                    }
                                }
                                rs.close();

                            }
                            catch (SQLException sqle) {

                            }

                        }

                        break;

                    case 81:
                    case 82:
                       
                        if ( (column.getPControls() == null) || (column.getPControls().equals("")) ) {
                           
                            DAO dao = DAO.getInstance();
                            dao.query(column.getInputPar());
                            ResultSet rs = dao.executeQuery();
                            try {
                                ResultSetMetaData rsmd = rs.getMetaData();
                                Vector columnNames = new Vector();
                                Vector datas = new Vector();
                                for (int i = 1; i <= rsmd.getColumnCount(); i++) {                                  
                                    columnNames.add(ObjectUtil.findColumnTxt(column.getClsId(), column.getTableName(), rsmd.getColumnName(i)));
                                }
                                columnNames.add("value");//restore value
                                while (rs.next()) {
                                    Vector rowDatas = new Vector();
                                    for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                                        Object columnValue = rs.getObject(rsmd.getColumnName(i));
                                        if (column.isI18N()) {
                                            String i18nString = I18NManager.getI18NString(column.getI18nPrefix(), columnValue);
                                            rowDatas.add(i18nString + "(" + columnValue + ")");
                                        } else {
                                            rowDatas.add(columnValue);
                                        }                                     
                                    }
                                    rowDatas.add(rs.getObject(column.getColumnName()));//value
                                    datas.add(rowDatas);
                                }
                                ListTableModel model = new ListTableModel(columnNames, datas);
                                ((DetailList)component).updateTable(model);
                               
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                           
                        }
                       
                        break;
                       
                    case 87:
                       
                        if (column.getInputPar().indexOf("[") == -1) {
                           
                            DAO dao = DAO.getInstance();
                            dao.query(column.getInputPar());
                            ResultSet rs = dao.executeQuery();
                            try {
                                ResultSetMetaData rsmd = rs.getMetaData();
                                Vector columnNames = new Vector();
                                Vector datas = new Vector();
                                for (int i = 1; i <= rsmd.getColumnCount(); i++) {                                  
                                    columnNames.add(ObjectUtil.findColumnTxt(column.getClsId(), column.getTableName(), rsmd.getColumnName(i)));
                                }
                                columnNames.add("value");//restore value
                                while (rs.next()) {
                                    Vector rowDatas = new Vector();
                                    for (int i = 1; i <= rsmd.getColumnCount(); i++) {
                                        Object columnValue = rs.getObject(rsmd.getColumnName(i));
                                        if (column.isI18N()) {
                                            String i18nString = I18NManager.getI18NString(column.getI18nPrefix(), columnValue);
                                            rowDatas.add(i18nString + "(" + columnValue + ")");
                                        } else {
                                            rowDatas.add(columnValue);
                                        }                                     
                                    }
                                    rowDatas.add(rs.getObject(column.getColumnName()));//value
                                    datas.add(rowDatas);
                                }
                                ListTableModel model = new ListTableModel(columnNames, datas);
                                ((DetailList)component).updateTable(model);
                               
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                           
                        }
                       
                        break;
                       
                    case 7:
                       
                        if (column.getInputPar().indexOf("[") == -1) {
                            DAO dao = DAO.getInstance();
                            dao.query(column.getInputPar());
                            ResultSet rs = dao.executeQuery();
                            try {
                               
                                while (rs.next()) {

                                    ResultSetMetaData rsmd = rs.getMetaData();
                                    Object value = null;
                                    String displayValue = "";
                                    displayValue += "<html>";
                                    for (int i = 1; i < rsmd.getColumnCount(); i++) {
                                        if (rsmd.getColumnName(i).equals(column.getColumnName())) {
                                            displayValue += "<i><b>";
                                            displayValue += rs.getObject(i);
                                            displayValue += "</i></b> ";
                                            value = rs.getObject(i);
                                        } else {
                                            displayValue += rs.getObject(i);
                                            displayValue += " ";
                                        }                                      
                                    }
                                   
                                    if (rsmd.getColumnName(rsmd.getColumnCount()).equals(column.getColumnName())) {
                                            displayValue += "<i><b>";
                                            displayValue += rs.getObject(rsmd.getColumnCount());
                                            displayValue += "</i></b> ";
                                            value = rs.getObject(rsmd.getColumnCount());
                                        } else {
                                            displayValue += rs.getObject(rsmd.getColumnCount());
                                            displayValue += " ";
                                        }    
                                   
                                    displayValue += "</html>";
                                    Item item = new Item(value, displayValue);
                                    ((JComboBox)component).addItem(item);
                                }
                                                               
                                rs.close();

                            }
                            catch (SQLException sqle) {

                            }
                        }                        
                       
                        break;
                       
                    default:
                        break;

                }

            }
           
            stat = "inited";
       
    }
   
    //connect components
    private void connectComponents(ComponentExt pComponentExt, ComponentExt cComponentExt) {
       
            Column pColumn = pComponentExt.getColumn();
            Column cColumn = cComponentExt.getColumn();
            JComponent pComponent = pComponentExt.getComponent();
            JComponent cComponent = cComponentExt.getComponent();
           
//            System.err.print(pColumn.getTableName() + "." + pColumn.getColumnName() +
//                    " ------> " + cColumn.getTableName() + "." + cColumn.getColumnName() + " ==   ");
           
            if ( (pColumn.getInputType() == 1) && (cColumn.getInputType() == 1) ) {
//                System.err.println("connectComponents1_1");               
                Vector parameters = getParameters(cComponentExt);
                String sql = getSQL(cComponentExt, parameters);
                connectComponents1_1(pComponent, cComponent, sql, parameters);
            } else if ( (pColumn.getInputType() == 1) && (cColumn.getInputType() == 7) ) {
//                System.err.println("connectComponents1_7");               
                Vector parameters = getParameters(cComponentExt);
                String sql = getSQL(cComponentExt, parameters);
                connectComponents1_7(pComponent, cComponent, sql, parameters);
            } else if ( (pColumn.getInputType() == 1) && (cColumn.getInputType() == 11) ) {               
//                System.err.println("connectComponents1_11");               
                if (cColumn.getDefValue() != null) {                                   
                    Vector parameters = getParameters_Defvalue(cComponentExt);
                    String sql = getSQL_Defvalue(cComponentExt, parameters);
                    connectComponents1_3(pComponent, cComponent, sql, parameters);
                } else {
                    connectComponents1_11(pComponent, cComponent);
                }
            } else if ( (pColumn.getInputType() == 1) && (cColumn.getInputType() == 12) ) {
//                System.err.println("connectComponents1_12");
                connectComponents1_12(pComponent, cComponent);
            } else if ( (pColumn.getInputType() == 1) && (cColumn.getInputType() == 3) ) {
//                System.err.println("connectComponents1_3");
                Vector parameters = getParameters(cComponentExt);
                String sql = getSQL(cComponentExt, parameters);
                connectComponents1_3(pComponent, cComponent, sql, parameters);
            } else if ( (pColumn.getInputType() == 3) && (cColumn.getInputType() == 3) ) {
//                System.err.println("connectComponents3_3");
                Vector parameters = getParameters(cComponentExt);
                String sql = getSQL(cComponentExt, parameters);
                connectComponents3_3(pComponent, cComponent, sql, parameters);
            } else if ( (pColumn.getInputType() == 5) && (cColumn.getInputType() == 3) ) {
//                System.err.println("connectComponents5_3");
                Vector parameters = getParameters(cComponentExt);
                String sql = getSQL(cComponentExt, parameters);
                connectComponents5_3(pComponent, cComponent, sql, parameters);
            } else if ( (pColumn.getInputType() == 7) && (cColumn.getInputType() == 1) ) {               
//                System.err.println("connectComponents7_1");
                Vector parameters = getParameters(cComponentExt);
                String sql = getSQL(cComponentExt, parameters);
                connectComponents7_1(pComponent, cComponent, sql, parameters);
            } else if ( (pColumn.getInputType() == 7) && (cColumn.getInputType() == 7) ) {
//                System.err.println("connectComponents7_7");              
                connectComponents7_7(pComponent, cComponent);
            } else if ( (pColumn.getInputType() == 1) && (cColumn.getInputType() == 16) ) {
//                System.err.println("connectComponents1_16");
                Vector parameters = getParameters_Defvalue(cComponentExt);
                String sql = getSQL_Defvalue(cComponentExt, parameters);
                connectComponents1_16(pComponent, cComponent, sql, parameters);
            } else if ( (pColumn.getInputType() == 87) && (cColumn.getInputType() == 87) ) {
//                System.err.println("connectComponents87_87");
                connectComponents87_87(pComponent, cComponent);
            }
            else {
//                System.err.println("Not Supported!");
            }            
       
    }

    private void connectComponents87_87(JComponent pComponent, JComponent cComponent) {
       
          
       
    }
   
    private void connectComponents1_16(final JComponent pComponent, final JComponent cComponent, final String sql, final Vector parameters) {
       
            connectComponents1_3(pComponent, cComponent, sql, parameters);
       
    }
   
    private void connectComponents1_1(final JComponent pComponent, final JComponent cComponent, final String sql, final Vector parameters) {
       
            ((JComboBox)pComponent).addActionListener(
                    new ActionAdapter() {

                        public void actionPerformed(ActionEvent e) {                           
                           
                            if ( (ComponentManager.getValue(pComponent) != null) && (pComponent.isFocusOwner() || (stat.equals("initing")))   ) {
                                                               
                                ((JComboBox)cComponent).removeAllItems();
                               
                                DAO dao = DAO.getInstance();
                                dao.query(sql);
                                for (int i = 0; i < parameters.size(); i++) {
                                   
                                    String parameter = (String)parameters.get(i);
                                    StringTokenizer st = new StringTokenizer(parameter, ".");
                                    String tableName = (String)st.nextElement();
                                    String columnName = (String)st.nextElement();
                                    ComponentExt componentExt = findComponentExt(tableName, columnName);
                                    JComponent component = componentExt.getComponent();
                                    dao.setObject(i+1, ComponentManager.getValue(component));
                                   
                                }
                               
                                ComponentExt cComponentExt = findComponentExt(cComponent);
                                Column cColumn = cComponentExt.getColumn();
                                if (cColumn.getInputType() != 7) {
                                
                                    //set null                               
                                    ResultSet rs = dao.executeQuery();                               
                                    try {
                                        ResultSetMetaData rsmd = rs.getMetaData();
                                        ((JComboBox)cComponent).addItem(new Item("null", " "));
                                        if (rsmd.getColumnCount() == 1) {
                                            while (rs.next()) {
                                                Item item = new Item(rs.getObject(1), rs.getObject(1));                                           
                                                ((JComboBox)cComponent).addItem(item);
                                            }
                                        } else {
                                            while (rs.next()) {

                                                Object value = null;
                                                StringBuffer sb = new StringBuffer();
                                                sb.append("<html>");                                       
                                                for (int i = 1; i < rsmd.getColumnCount(); i++) {
                                                    if (rsmd.getColumnName(i).equals(cColumn.getColumnName())) {
                                                        sb.append("<b>" + rs.getObject(i) + "</b> | ");
                                                        value = rs.getObject(i);
                                                    } else {
                                                        sb.append(rs.getObject(i) + " | ");
                                                    }
                                                }

                                                if (rsmd.getColumnName(rsmd.getColumnCount()).equals(cColumn.getColumnName())) {
                                                    sb.append("<b>" + rs.getObject(rsmd.getColumnCount()) + "</b>");
                                                    value = rs.getObject(rsmd.getColumnCount());
                                                } else {
                                                    sb.append(rs.getObject(rsmd.getColumnCount()) + "");
                                                }
                                                sb.append("</html>");

                                                Item item = new Item(value, sb.toString());
                                                ((JComboBox)cComponent).addItem(item);
                                            }
                                        }
                                        rs.close();                                                                       

                                    } catch (Exception ee) {

                                    }
                                   
                                } else {
                                   
                                    ResultSet rs = dao.executeQuery();
                                    try {

                                        while (rs.next()) {

                                            ResultSetMetaData rsmd = rs.getMetaData();
                                            Object value = null;
                                            String displayValue = "";
                                            displayValue += "<html>";
                                            for (int i = 1; i < rsmd.getColumnCount(); i++) {
                                                if (rsmd.getColumnName(i).equals(cColumn.getColumnName())) {
                                                    displayValue += "<i><b>";
                                                    displayValue += rs.getObject(i);
                                                    displayValue += "</i></b> ";
                                                    value = rs.getObject(i);
                                                } else {
                                                    displayValue += rs.getObject(i);
                                                    displayValue += " ";
                                                }
                                            }
                                           
                                            if (rsmd.getColumnName(rsmd.getColumnCount()).equals(cColumn.getColumnName())) {
                                                    displayValue += "<i><b>";
                                                    displayValue += rs.getObject(rsmd.getColumnCount());
                                                    displayValue += "</i></b> ";
                                                    value = rs.getObject(rsmd.getColumnCount());
                                                } else {
                                                    displayValue += rs.getObject(rsmd.getColumnCount());
                                                    displayValue += " ";
                                                }
                                           
                                            displayValue += "</html>";
                                            Item item = new Item(value, displayValue);
                                            ((JComboBox)cComponent).addItem(item);
                                        }

                                        rs.close();

                                    }
                                    catch (SQLException sqle) {

                                    }
                                   
                                }                                                               
                               
                            }

                        }

                    }
            );
       
    }
   
    private void connectComponents1_7(final JComponent pComponent, final JComponent cComponent, final String sql, final Vector parameters) {
       
            connectComponents1_1(pComponent, cComponent, sql, parameters);
       
    }
   
    private void connectComponents7_1(final JComponent pComponent, final JComponent cComponent, final String sql, final Vector parameters) {       
            connectComponents1_1(pComponent, cComponent, sql, parameters);       
    }
           
    private void connectComponents1_11(final JComponent pComponent, final JComponent cComponent) {                                    
       
            ((JComboBox)pComponent).addActionListener(
                    new ActionAdapter() {

                        public void actionPerformed(ActionEvent e) {

                            if ( (ComponentManager.getValue(pComponent) != null) && (pComponent.isFocusOwner()) ) {
                                CurrencyPlugin cp = CurrencyPlugin.getInstance();
                                StringBuffer sb = new StringBuffer("###" + cp.getSepor() + "###");
                                int fraction = cp.getFraction(ComponentManager.getValue(pComponent).toString());
                                if (fraction > 0) {
                                    sb.append(cp.getFpoint());
                                }
                                for (int i = 1; i <= fraction; i++) {
                                    sb.append("0");
                                }
                                DecimalFormat df = new DecimalFormat(sb.toString());
                                NumberFormatter nf = new NumberFormatter(df);
                                DefaultFormatterFactory factory = new DefaultFormatterFactory(nf);
                                ((JFormattedTextField)cComponent).setFormatterFactory(factory);
                            }

                        }

                    }
            );
       
    }
   
    private void connectComponents1_12(final JComponent pComponent, final JComponent cComponent) {
       
            ((JComboBox)pComponent).addActionListener(
                    new ActionAdapter() {

                        public void actionPerformed(ActionEvent e) {

                            if ( (ComponentManager.getValue(pComponent) != null) && (pComponent.isFocusOwner()) ) {
                                CurrencyPlugin cp = CurrencyPlugin.getInstance();
                                UomPlugin up = UomPlugin.getInstance();
                                StringBuffer sb = new StringBuffer("###" + cp.getSepor() + "###");
                                int fraction = up.getFraction(ComponentManager.getValue(pComponent).toString());
                                if (fraction > 0) {
                                    sb.append(".");
                                }
                                for (int i = 1; i <= fraction; i++) {
                                    sb.append("0");
                                }
                                DecimalFormat df = new DecimalFormat(sb.toString());
                                NumberFormatter nf = new NumberFormatter(df);
                                DefaultFormatterFactory factory = new DefaultFormatterFactory(nf);
                                ((JFormattedTextField)cComponent).setFormatterFactory(factory);
                            }

                        }

                    }
            );
       
    }
   
    private void connectComponents7_7(final JComponent pComponent, final JComponent cComponent) {
       
            ((JComboBox)pComponent).addActionListener(
                    new ActionAdapter() {
                       
                        public void actionPerformed(ActionEvent e) {
                           
                             if ( (ComponentManager.getValue(pComponent) != null) && (pComponent.isFocusOwner() ) ) {
                                
                                ComponentExt pComponentExt = findComponentExt(pComponent);
                                Column pColumn = pComponentExt.getColumn();
                                ComponentExt cComponentExt = findComponentExt(cComponent);
                                Column cColumn = cComponentExt.getColumn();
                                if ( (((JComboBox)pComponent).getSelectedIndex() != -1) && (((JComboBox)cComponent).getItemCount() >=
                                        ((JComboBox)pComponent).getSelectedIndex()) ) {
                                    ((JComboBox)cComponent).setSelectedIndex(((JComboBox)pComponent).getSelectedIndex());
                                }                               
                                
                             }
                           
                        }

                    }
            );
       
    }

    private void connectComponents1_3(final JComponent pComponent, final JComponent cComponent, final String sql, final Vector parameters) {
       
            ((JComboBox)pComponent).addActionListener(
                    new ActionAdapter() {

                        public void actionPerformed(ActionEvent e) {

                            if (ComponentManager.getValue(pComponent) != null) {                              
                               
                                DAO dao = DAO.getInstance();
                                dao.query(sql);
                                for (int i = 0; i < parameters.size(); i++) {
                                    String parameter = (String)parameters.get(i);
                                    StringTokenizer st = new StringTokenizer(parameter, ".");
                                    String tableName = (String)st.nextElement();
                                    String columnName = (String)st.nextElement();
                                    ComponentExt componentExt = findComponentExt(tableName, columnName);
                                    JComponent component = componentExt.getComponent();
                                    dao.setObject(i+1, ComponentManager.getValue(component));                                   
                                }
                               
                                ResultSet rs = dao.executeQuery();                               
                                try {
                                    if (rs.next()) {
                                        if (cComponent instanceof JTextField) {
                                            if (cComponent instanceof JFormattedTextField) {                                               
                                                ((JFormattedTextField)cComponent).setValue(rs.getObject(1));
                                            } else {
                                                ((JTextField)cComponent).setText(rs.getObject(1).toString());
                                            }
                                        }
                                    }                                                                       
                                    rs.close();
                                   
                                } catch (Exception ee) {
                                   
                                }                                                               
                               
                            }

                        }

                    }
            );
       
    }
   
    private void connectComponents3_3(final JComponent pComponent, final JComponent cComponent, final String sql, final Vector parameters) {
       
            /*((JTextField)pComponent).addKeyListener(
                    new KeyAdapter() {

                        public void keyTyped(KeyEvent e) {

                            if (componentManager.getValue(pComponent) != null) {                              
                               
                                DAO dao = DAO.getInstance();
                                dao.query(sql);
                               
                                for (int i = 0; i < parameters.size(); i++) {
                                    String parameter = (String)parameters.get(i);
                                    System.err.println("parameter = " + parameter);
                                }
                               
                                for (int i = 0; i < parameters.size(); i++) {
                                   
                                    String parameter = (String)parameters.get(i);
                                    StringTokenizer st = new StringTokenizer(parameter, ".");
                                    String tableName = (String)st.nextElement();
                                    String columnName = (String)st.nextElement();
                                    ComponentExt componentExt = findComponentExt(tableName, columnName);
                                    JComponent component = componentExt.getComponent(); 
                                    System.err.println("component.value = " + componentManager.getValue(component));
                                    dao.setObject(i+1, componentManager.getValue(component));
                                   
                                }
                                ResultSet rs = dao.executeQuery();                               
                                try {
                                    System.err.println("rs = " + rs.getStatement());
                                    if (rs.next()) {
                                        ((JTextField)cComponent).setText(rs.getObject(1).toString());
                                    }                                                                       
                                    rs.close();
                                   
                                } catch (Exception ee) {
                                   
                                }
                               
                            }

                        }

                    }
            );*/
       
    }
   
    private void connectComponents5_3(final JComponent pComponent, final JComponent cComponent, final String sql, final Vector parameters) {
       
        connectComponents1_3(pComponent, cComponent, sql, parameters);
       
    }
   
    //Help methods   
    class Relation {
       
            private ComponentExt pComponentExt;
            private ComponentExt cComponentExt;

            public Relation(ComponentExt pComponentExt, ComponentExt cComponentExt) {
                this.pComponentExt = pComponentExt;
                this.cComponentExt = cComponentExt;
            }
           
            public String toString() {
               
                Column pColumn = pComponentExt.getColumn();
                Column cColumn = cComponentExt.getColumn();
               
                return (pColumn.getTableName() + "." + pColumn.getColumnName() + " -----> " + cColumn.getTableName() + "." + cColumn.getColumnName());
               
            }
           
            public ComponentExt getPComponentExt() {
                return pComponentExt;
            }

            public ComponentExt getCComponentExt() {
                return cComponentExt;
            }                      
       
    }
   
    abstract class ActionAdapter extends Object implements ActionListener{
       
        public ActionAdapter() {
        }

        public abstract void actionPerformed(ActionEvent ae);
       
    }
   
    public ComponentExt findComponentExt(String tableName, String columnName) {
       
            ComponentExt componentExt = null;
       
            Iterator componentExtIter = componentExts.iterator();
            while (componentExtIter.hasNext()) {
                ComponentExt tempComponentExt = (ComponentExt)componentExtIter.next();
                Column tempColumn = tempComponentExt.getColumn();
                if ( (tempColumn.getTableName().equals(tableName)) && (tempColumn.getColumnName().equals(columnName)) ) {
                    componentExt = tempComponentExt;
                    break;
                }
            }

            return componentExt;
       
    }
   
    private ComponentExt findComponentExt(JComponent component) {
       
            ComponentExt componentExt = null;
           
            Iterator componentExtIter = componentExts.iterator();
            while (componentExtIter.hasNext()) {
                ComponentExt tempComponentExt = (ComponentExt)componentExtIter.next();
                JComponent tempComponent = tempComponentExt.getComponent();
                if (component == tempComponent) {
                    componentExt = tempComponentExt;
                    break;
                }
            }
           
            return componentExt;
       
    }
   
    public Vector getParameters_Defvalue(ComponentExt componentExt) {
       
            Vector parameters = null;
       
            String sql = componentExt.getColumn().getDefValue();           
            if ( (sql != null) && (sql.trim().length() > 0) ) {
                parameters = new Vector();
//                System.err.println("sql = " + sql);                          
                StringTokenizer st = new StringTokenizer(sql, "[]");
                boolean odd = true;
                while (st.hasMoreElements()) {
                    if (!odd)
                        parameters.add(st.nextElement());
                    else
                        st.nextElement();
                    odd = !odd;               
                }
            }
              
            return parameters;
       
    }
   
    public Vector getParameters(ComponentExt componentExt) {
       
            Vector parameters = null;
       
            String sql = componentExt.getColumn().getInputPar();           
            if ( (sql != null) && (sql.trim().length() > 0) ) {
                parameters = new Vector();
//                System.err.println("sql = " + sql);                          
                StringTokenizer st = new StringTokenizer(sql, "[]");
                boolean odd = true;
                while (st.hasMoreElements()) {
                    if (!odd)
                        parameters.add(st.nextElement());
                    else
                        st.nextElement();
                    odd = !odd;               
                }
            }
              
            return parameters;
       
    }
   
    public String getSQL_Defvalue(ComponentExt componentExt, Vector parameters) {
       
            String sql = componentExt.getColumn().getDefValue();           
            Iterator parameterIter = parameters.iterator();           
            while (parameterIter.hasNext()) {               
                String parameter = (String)parameterIter.next();
                parameter = "[" + parameter + "]";              
                if (sql.indexOf(parameter) != -1) {
                    StringBuffer sb = new StringBuffer(sql);
                    sb.replace(sql.indexOf(parameter), sql.indexOf(parameter) + parameter.length(), "?");
                    sql = sb.toString();
                }               
            }
           
            return sql;
       
    }
       
    public String getSQL(ComponentExt componentExt, Vector parameters) {
       
            String sql = componentExt.getColumn().getInputPar();
           
            Iterator parameterIter = parameters.iterator();
           
            while (parameterIter.hasNext()) {
               
                String parameter = (String)parameterIter.next();
                parameter = "[" + parameter + "]";
               
                if (sql.indexOf(parameter) != -1) {
                    StringBuffer sb = new StringBuffer(sql);
                    sb.replace(sql.indexOf(parameter), sql.indexOf(parameter) + parameter.length(), "?");
                    sql = sb.toString();
                }
               
            }
           
            return sql;
       
    }
   
    private void printRelationTable() {
       
            Iterator relationIter = relationTable.iterator();

            while (relationIter.hasNext()) {

                Relation relation = (Relation)relationIter.next();

//                System.err.println(relation);

            }
       
    }
   
}
TOP

Related Classes of realcix20.guis.relationships.RelationManager$Relation

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.