ca.on.gov.jus.icon.common.iconcodetables.IconCodeTablesManager.java Source code

Java tutorial

Introduction

Here is the source code for ca.on.gov.jus.icon.common.iconcodetables.IconCodeTablesManager.java

Source

/*
 * Created on Nov 1, 2007
 *
 * To change the template for this generated file go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
package ca.on.gov.jus.icon.common.iconcodetables;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.faces.context.FacesContext;

import org.apache.commons.lang.StringUtils;

import pagecode.RPTs.ReportsConnectionManager;
import pagecode.PageCodeBase;
import pagecode.jsp.tablemanagement.managedbeans.SingleRecordDataContainerBean;
import IntegrationObject.BackToMenu_Access;
import IntegrationObject.TableManagementSystemGetData_Access;
import IntegrationObject.TableManagementSystemGoFromIcon_Access;
import IntegrationObject.TableManagementSystemGoIconMain_Access;
import IntegrationObject.TableManagementSystemGoMain_Access;
import IntegrationObject.TableManagementSystemMain2_Access;
import IntegrationObject.TableManagementSystemPageDown_Access;
import IntegrationObject.TableManagementSystemUnlock_Access;

/**
 * @author Anan Mazzawi
 *
 * To change the template for this generated type comment go to 
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class IconCodeTablesManager {
    private static final Logger logger = Logger.getLogger(IconCodeTablesManager.class.getName());
    private static IconCodeTablesManager instance;
    private SortedMap iconCodeTables;

    private IconCodeTablesManager() {
        logger.info("IN IconCodeTablesManager::IconCodeTablesManager");
        iconCodeTables = new TreeMap();
        populateHardcodedTables();

        FacesContext facesContext = FacesContext.getCurrentInstance();
        if (null != facesContext) {
            Map applicationScope = (Map) facesContext.getApplication().createValueBinding("#{applicationScope}")
                    .getValue(facesContext);

            applicationScope.put("iconCodeTablesManager", this);
        }
    }

    public static IconCodeTablesManager getInstance() {
        if (null == instance) {
            instance = new IconCodeTablesManager();
        }
        return instance;
    }

    public IconCodeTable getIconCodeTable(String iconCodeTableKey) throws Exception {
        IconCodeTable retVal = null;
        if (null != iconCodeTableKey && iconCodeTables.containsKey(iconCodeTableKey.trim())) {
            retVal = (IconCodeTable) iconCodeTables.get(iconCodeTableKey.trim());
        } else {
            retVal = (IconCodeTable) populateIconCodeTable(iconCodeTableKey);
            if (null != retVal) {
                iconCodeTables.put(iconCodeTableKey, retVal);
            }
        }
        return retVal;
    }

    public IconCodeTable getIconCodeTable(String iconCodeTableKey, boolean forceDbReload) throws Exception {
        IconCodeTable retVal = null;
        if (null != iconCodeTableKey && true == forceDbReload) {
            retVal = (IconCodeTable) populateIconCodeTable(iconCodeTableKey);
            if (null != retVal) {
                iconCodeTables.put(iconCodeTableKey, retVal);
            }
        } else if (null != iconCodeTableKey && false == forceDbReload) {
            retVal = getIconCodeTable(iconCodeTableKey);
        }
        return retVal;
    }

    public IconCodeTable getIconCodeTable(String iconCodeTableKey, boolean forceDbReload, boolean cacheOnlyLookup)
            throws Exception {
        //if cache only look up and the key does not exist in the cache then
        //throw exception i.e. dont do a look up
        if (true == cacheOnlyLookup && !iconCodeTables.containsKey(iconCodeTableKey.trim())) {
            throw new Exception(
                    "Code list is missing!  Please use the ICON Code Tables option to view the table first then try again.");
        }

        return getIconCodeTable(iconCodeTableKey, forceDbReload);
    }

    private Object populateIconCodeTable(String iconCodeTableKey) throws Exception {
        logger.info("IN populateIconCodeTable");
        boolean transactionRejected = false;
        IconCodeTable iconCodeTable = null;

        if ("ICON_CodeTablesList".equalsIgnoreCase(iconCodeTableKey)) {
            iconCodeTable = getICONCodesTableList();
            iconCodeTables.put(iconCodeTableKey, iconCodeTable);
            return iconCodeTable;
        }

        //This will go to ICON Main menu first then to table Man main menu
        //Simply call this
        this.goToTableManagementMainScreen();

        //Create an instance of the main Macro
        TableManagementSystemMain2_Access tableManagementSystemMain2_Access = new TableManagementSystemMain2_Access();

        //Set the required values on it
        tableManagementSystemMain2_Access.setTableId(StringUtils.rightPad(iconCodeTableKey.trim(), 8));
        tableManagementSystemMain2_Access
                .setTablePass(StringUtils.rightPad(getTablePassword(iconCodeTableKey).trim(), 8));
        //Execute the action/Macro
        PageCodeBase.executeEJB(tableManagementSystemMain2_Access, getSessionScope());

        //Get the returned data
        String returnMessage = tableManagementSystemMain2_Access.getReturnMessage().trim();

        try {
            //Error or bad password - display msg
            if (!returnMessage.equals("")) {
                logger.log(Level.SEVERE, "ERROR: " + returnMessage);
                //found TRANSACTION REJECTED
                if (returnMessage.indexOf("TRANSACTION REJECTED") != -1) {
                    transactionRejected = true;
                    TableManagementSystemUnlock_Access tableManagementSystemUnlock_Access = new TableManagementSystemUnlock_Access();
                    PageCodeBase.executeEJB(tableManagementSystemUnlock_Access, getSessionScope());
                } else {
                    //Go back to the main menu after error
                    TableManagementSystemGoMain_Access tableManagementSystemGoMain_Access = new TableManagementSystemGoMain_Access();
                    PageCodeBase.executeEJB(tableManagementSystemGoMain_Access, getSessionScope());
                }
                throw new Exception(returnMessage);
            }

            String iconCodeTableName = ((IconCodeTable) getIconCodeTable("ICON_CodeTablesList").getCodeTableValues()
                    .get(iconCodeTableKey)).getCodeTableName();

            iconCodeTable = new IconCodeTable(iconCodeTableKey, iconCodeTableName);
            String hasMore = "";
            do {
                //NO Error, we can go ahead then get the data  
                TableManagementSystemGetData_Access tableManagementSystemGetData_Access = new TableManagementSystemGetData_Access();

                //Execute the action/Macro
                PageCodeBase.executeEJB(tableManagementSystemGetData_Access, getSessionScope());

                hasMore = tableManagementSystemGetData_Access.getMore().trim();
                //Must declare a local variable to get the number
                //better for performance vs. calling EJB all the time
                int numberOfEntries = tableManagementSystemGetData_Access.getTableEntryListEntryCode().length;

                for (int i = 0; i < numberOfEntries; i++) {
                    if (tableManagementSystemGetData_Access.getTableEntryListEntryCode(i).trim().length() > 0) {

                        iconCodeTable.getCodeTableValues().put(
                                tableManagementSystemGetData_Access.getTableEntryListEntryCode(i).trim(),
                                tableManagementSystemGetData_Access.getTableEntryListEntryValue(i).trim());

                        /* Since the table "Disbursement GL Acct X-Ref" (ICDSXREF)  
                         * is an exception to all the other tables and has an  
                         * additional column then we need to do some additional  
                         * work to extract the third column. For easier solution  
                         * the choice was to extract the value in the Java app  
                         * vs. creating a new Macro or modifying the existing one.
                         */
                        //                        if("ICDSXREF".equalsIgnoreCase(this.getViewIconTablesManagedBean().getSelectedTableId())) {
                        //                           this.processCustomRecord(tableManagementSystemGetData_Access, singleRecord, i);
                        //                  }
                        //                  this.getViewIconTablesManagedBean().getIconTableDataRecordList().add(singleRecord);
                    }
                } //end for

                //if there is more data then call Page Down Macro
                if (!hasMore.equals("")) {
                    TableManagementSystemPageDown_Access tableManagementSystemPageDown_Access = new TableManagementSystemPageDown_Access();
                    PageCodeBase.executeEJB(tableManagementSystemPageDown_Access, getSessionScope());
                }

            } while (!hasMore.equals("")); //end do

            iconCodeTables.put(iconCodeTableKey, iconCodeTable);

            //At end of the while (no more data) we go back to the main screen
            TableManagementSystemGoMain_Access tableManagementSystemGoMain_Access = new TableManagementSystemGoMain_Access();
            PageCodeBase.executeEJB(tableManagementSystemGoMain_Access, getSessionScope());

        } finally {
            //Finally go to ICON main menu so that it is ready for next request
            if (false == transactionRejected) {
                TableManagementSystemGoIconMain_Access tableManagementSystemGoIconMain_Access = new TableManagementSystemGoIconMain_Access();
                PageCodeBase.executeEJB(tableManagementSystemGoIconMain_Access, getSessionScope());
            }
        }
        return iconCodeTable;
    }

    private IconCodeTable getICONCodesTableList() {
        IconCodeTable iconCodesTableList = null;
        String selectSql = null;
        Connection oracleConnection = ReportsConnectionManager.getPooledOracleConnection();
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        if (null != oracleConnection) {
            selectSql = "" + "SELECT " + "    ICONADMIN.ICON_TABLES_CODE.CODE, "
                    + "    ICONADMIN.ICON_TABLES_CODE.DESCRIPTION, " + "    ICONADMIN.ICON_TABLES_CODE.TABLE_PASS  "
                    + "FROM  " + "    ICONADMIN.ICON_TABLES_CODE " + "ORDER BY  "
                    + "    ICONADMIN.ICON_TABLES_CODE.DESCRIPTION ASC ";

            try {

                preparedStatement = oracleConnection.prepareStatement(selectSql, ResultSet.TYPE_SCROLL_INSENSITIVE,
                        ResultSet.CONCUR_READ_ONLY);
                resultSet = preparedStatement.executeQuery();

                resultSet.last();
                int resultSetCount = resultSet.getRow();
                //logger.info("resultSetCount: " + resultSetCount);
                resultSet.beforeFirst();

                if (resultSetCount > 0) {
                    iconCodesTableList = new IconCodeTable("ICON_CodeTablesList", "ICON Codes Table List");
                }

                while (resultSet.next()) {
                    IconCodeTable iconCodeTable = new IconCodeTable(resultSet.getString("CODE"),
                            resultSet.getString("DESCRIPTION"));
                    iconCodeTable.setCodeTablePass(resultSet.getString("TABLE_PASS"));

                    //Null it so that it can not be used that way
                    iconCodeTable.setCodeTableValues(null);

                    iconCodesTableList.getCodeTableValues().put(resultSet.getString("CODE"), iconCodeTable);
                }

            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    //clean up
                    if (null != resultSet) {
                        resultSet.close();
                        resultSet = null;
                    }
                    if (null != preparedStatement) {
                        preparedStatement.close();
                        preparedStatement = null;
                    }
                    //Should never close the pooled connection
                    //               if(null != oracleConnection){ 
                    //                  oracleConnection.close();
                    //                  oracleConnection = null;
                    //               }
                } catch (SQLException e1) {
                }
            }
        }

        return iconCodesTableList;
    }

    private void goToTableManagementMainScreen() {
        //First go to ICON main screen
        BackToMenu_Access backToMenu_Access = new BackToMenu_Access();
        PageCodeBase.executeEJB(backToMenu_Access, getSessionScope());

        //Now go to Table Management Main Screen from ICON
        TableManagementSystemGoFromIcon_Access tableManagementSystemGoFromIcon_Access = new TableManagementSystemGoFromIcon_Access();
        PageCodeBase.executeEJB(tableManagementSystemGoFromIcon_Access, getSessionScope());
    }

    private String getTablePassword(String iconCodeTableKey) throws Exception {
        logger.info("IN IconCodeTablesManager::getTablePassword");
        String tablePass = null;

        tablePass = ((IconCodeTable) getIconCodeTable("ICON_CodeTablesList").getCodeTableValues()
                .get(iconCodeTableKey)).getCodeTablePass();

        return tablePass;
    }

    private Map getSessionScope() {
        FacesContext facesContext = FacesContext.getCurrentInstance();
        Map sessionScope = (Map) facesContext.getApplication().createValueBinding("#{sessionScope}")
                .getValue(facesContext);
        return sessionScope;
    }

    private void processCustomRecord(TableManagementSystemGetData_Access accessBean,
            SingleRecordDataContainerBean singleRecord, int i) {
        //set the first col
        singleRecord.setColumn01DataValue(accessBean.getTableEntryListEntryCode(i).trim());

        //second and third cols need work i.e. need to extract the second column
        String listEntryValue = accessBean.getTableEntryListEntryValue(i).trim();

        //Delimiter is any number of whitespace characters
        String[] result = listEntryValue.split("\\s+");

        singleRecord.setColumn02DataValue(result[1]);
        singleRecord.setColumn03DataValue(result[0]);

    }

    private void populateHardcodedTables() {
        logger.info("In IconCodeTablesManager::populateHardcodedTables");
        /* The following four (4) tables are hardcode in the COBOL programs and 
         * do not exist in the ATM system for live inquiries.  Therefore we have 
         * to hardcode them in the Java environment as well.
         */
        IconCodeTable IHCURRCD = new IconCodeTable("IHCURRCD", "Currency Code");
        IHCURRCD.getCodeTableValues().put("01", "CANADIAN");
        IHCURRCD.getCodeTableValues().put("02", "US");
        IHCURRCD.getCodeTableValues().put("03", "SPLIT PREPAY");
        iconCodeTables.put("IHCURRCD", IHCURRCD);

        IconCodeTable IHMEDIUM = new IconCodeTable("IHMEDIUM", "Appearance Medium");
        IHMEDIUM.getCodeTableValues().put("AU", "AUDIO");
        IHMEDIUM.getCodeTableValues().put("IP", "IN PERSON");
        IHMEDIUM.getCodeTableValues().put("IW", "IN WRITING");
        IHMEDIUM.getCodeTableValues().put("TE", "BY TELEPHONE");
        IHMEDIUM.getCodeTableValues().put("VI", "VIDEO");
        iconCodeTables.put("IHMEDIUM", IHMEDIUM);

        IconCodeTable IHOFDESQ = new IconCodeTable("IHOFDESQ", "Offence Description Qualifiers");
        IHOFDESQ.getCodeTableValues().put("A", "ATTEMPT");
        IHOFDESQ.getCodeTableValues().put("B", "AID AND ABET");
        IHOFDESQ.getCodeTableValues().put("C", "CONSPIRACY");
        IHOFDESQ.getCodeTableValues().put("D", "DOMESTIC VIOLENCE");
        iconCodeTables.put("IHOFDESQ", IHOFDESQ);

        IconCodeTable IHLEGREP = new IconCodeTable("IHLEGREP", "Legal Representation Code");
        IHLEGREP.getCodeTableValues().put("NRC", "NOT REPRESENTED BY COUNSEL");
        IHLEGREP.getCodeTableValues().put("RDC", "REPRESENTED BY DUTY COUNSEL");
        IHLEGREP.getCodeTableValues().put("RRC", "REPRESENTED BY RETAINED OR REPORTING COUNSEL");
        iconCodeTables.put("IHLEGREP", IHLEGREP);

        //Modified August 6, 2008 by Anan
        //Added the following table
        IconCodeTable IHFINREL = new IconCodeTable("IHFINREL", "Fine Relationship Code");
        IHFINREL.getCodeTableValues().put("L", "LICENSE");
        IHFINREL.getCodeTableValues().put("N", "COMPLETE NAME, BIRTH DATE, AND SEX");
        IHFINREL.getCodeTableValues().put("S", "SURNAME, BIRTH DATE, AND SEX");
        IHFINREL.getCodeTableValues().put("U", "UNRELATED");
        iconCodeTables.put("IHFINREL", IHFINREL);

        //Modified August 6, 2008 by Anan
        //Added the following table
        IconCodeTable IHPAYTYP = new IconCodeTable("IHPAYTYP", "Payment Type Code");
        IHPAYTYP.getCodeTableValues().put("P", "PREPAID FINE");
        IHPAYTYP.getCodeTableValues().put("F", "FINE");
        IHPAYTYP.getCodeTableValues().put("N", "NSF - NON-SUFFICIENT FUNDS");
        IHPAYTYP.getCodeTableValues().put("R", "RESTITUTION");
        iconCodeTables.put("IHPAYTYP", IHPAYTYP);

    }

}