ca.on.gov.jus.icon.common.util.JDBCUtils.java Source code

Java tutorial

Introduction

Here is the source code for ca.on.gov.jus.icon.common.util.JDBCUtils.java

Source

/** 
 * Copyright 2005 The Ontario Lottery Gamming Corporation, Inc. 
 * Yonge 4120, Toronto, Ontario, M2P 2B8, Canada. 
 * All rights reserved. 
 * OLGC PROPRIETARY/CONFIDENTIAL. 
 * Use is subject to license terms. 
 * Created on Jul 14, 2005 
 */

package ca.on.gov.jus.icon.common.util;

import java.io.IOException;
import java.util.Map;
import java.util.StringTokenizer;

import org.apache.commons.dbutils.QueryLoader;

import ca.on.gov.jus.icon.common.util.DAOException;

/**
 * Class containing JDBC utility methods used by both the
 * BaseJDBCDAO and LookupManager classes.
 *   
 * <p>
 * In the long description, you can use <b>HTML</b> tags to spice it
 * up.  If you use Java keywords, package names, variables, code examples,
 * class names or method names (e.g., <code>method()</code>), place
 * 'code' tags around them.  After the description, edit the tags below.
 * </P>
 *             
 * @see          AnotherClass
 * @see          AnotherClass#methodName
 *
 *  Revision History: 
 * 
 *  Ver.   Date             Author          Description
 *  -----  ----------       --------------  ------------------------------------
 *   1.0   Jul 14, 2007    truongs       Initial Creation
 */
public class JDBCUtils implements DatabaseConstants {

    /**
     * Loads a Map of query names to SQL values.
     * The Maps are cached so a subsequent request to load queries
     * from the same path will return the cached Map.
     * 
     * The default "/SQLStatements.properties" path will be used
     * if the propFilePath parameter is set to null.
     * 
     * Note: The property file path not a file system path
     * If you had a jarred SQLStatements.properties file in the
     * ca.olgc.ci.common package you would pass
     * "/ca/olgc/ci/common/SQLStatements.properties" to this method. 
     * 
     * @param        propFilePath      The path that the ClassLoader will use to find the file.
     * @return       Map            Collection of SQL statements.
     * @exception    IOException      Thrown if loading the properties file fails.
     */
    static public Map getAllSQLStatements(String propFilePath) throws IOException {
        Map sqlStatments = null;

        // Use the default property file path if it is not provided.
        if (propFilePath == null) {
            propFilePath = SQL_STATEMENTS_PROP_FILE;
        }

        // Get the singleton Query Loader object.
        QueryLoader loader = QueryLoader.instance();

        // Get the collection of SQL statements from the Query Loader.
        sqlStatments = loader.load(propFilePath);

        return sqlStatments;
    }

    /**
     * Obtains the SQL statement corresponding to the given key
     * from the collection of SQL statements.
     * Throws a DAOException if the statment is not found or the file cannot be loaded.  
     *
     * @param       propFilePath      The path that the ClassLoader will use to find the file. 
     * @param       key            The identifier of the required statement in the collection.
     * @return       String            The SQL statement corresponding to the key.
     * @exception    DAOOException      Thrown if loading the properties file fails or the statement is not found.
     */
    static public String getSQLStatement(String propFilePath, String key) throws DAOException {
        Map sqlStatements = null;

        try {
            // Use the default SQL statements property file path to get the collection.
            sqlStatements = getAllSQLStatements(propFilePath);
        } catch (IOException ie) {
            throw new DAOException("Error loading the SQL statements property file: " + propFilePath == null
                    ? SQL_STATEMENTS_PROP_FILE
                    : propFilePath, ie);
        }

        // Get the SQL statement from the collection.
        String sqlStatement = (String) sqlStatements.get(key);

        // Throw a DAOException if a corresponding SQL statement is not in the collection.
        if (sqlStatement == null) {
            throw new DAOException("Unable to find the SQL statement with the key: " + key);
        }

        return sqlStatement;
    }

    /**
     * Checks a String for single-quote characters and escapes
     * them with another single-quote.  This is necessary for any
     * String used as a parameter in an SQL query.
     *
     * @param   parameter   The String checked for single-quote characters.
     * @return   The String with escaped single-quote characters.
     */
    static public String toDBString(String parameter) {
        final String delimiter = "'";
        boolean returnDelims = true;
        StringBuffer buffer = null;
        StringTokenizer tokenizer = null;
        String dbString = null;

        if (parameter != null) {
            buffer = new StringBuffer();
            tokenizer = new StringTokenizer(parameter, delimiter, returnDelims);

            // Iterater through each occurance or a single-quote.
            while (tokenizer.hasMoreTokens()) {
                String token = tokenizer.nextToken();
                buffer.append(token);

                // Check if the current token is the single-quote,
                // and escape it with another single-quote.
                if (token.equals(delimiter)) {
                    buffer.append(delimiter);
                }
            }
            dbString = buffer.toString();
        }

        return dbString;
    }
}