HibernateQueryFactory.java :  » Groupware » ivatagroupware » com » ivata » groupware » container » persistence » hibernate » Java Open Source

Java Open Source » Groupware » ivatagroupware 
ivatagroupware » com » ivata » groupware » container » persistence » hibernate » HibernateQueryFactory.java
/*
 * Copyright (c) 2001 - 2005 ivata limited.
 * All rights reserved.
 * -----------------------------------------------------------------------------
 * ivata groupware may be redistributed under the GNU General Public
 * License as published by the Free Software Foundation;
 * version 2 of the License.
 *
 * These programs are free software; you can redistribute them and/or
 * modify them under the terms of the GNU General Public License
 * as published by the Free Software Foundation; version 2 of the License.
 *
 * These programs are distributed in the hope that they will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * See the GNU General Public License in the file LICENSE.txt for more
 * details.
 *
 * If you would like a copy of the GNU General Public License write to
 *
 * Free Software Foundation, Inc.
 * 59 Temple Place - Suite 330
 * Boston, MA 02111-1307, USA.
 *
 *
 * To arrange commercial support and licensing, contact ivata at
 *                  http://www.ivata.com/contact.jsp
 * -----------------------------------------------------------------------------
 * $Log: HibernateQueryFactory.java,v $
 * Revision 1.6  2005/10/11 18:51:38  colinmacleod
 * Fixed some checkstyle and javadoc issues.
 *
 * Revision 1.5  2005/10/02 14:08:56  colinmacleod
 * Added/improved log4j logging.
 *
 * Revision 1.4  2005/09/14 15:17:30  colinmacleod
 * Improved assertions for query argument checking.
 * Removed unused local and class variables.
 * Added serialVersionUID.
 *
 * Revision 1.3  2005/04/10 20:09:43  colinmacleod
 * Added new themes.
 * Changed id type to String.
 * Changed i tag to em and b tag to strong.
 * Improved PicoContainerFactory with NanoContainer scripts.
 *
 * Revision 1.2  2005/04/09 17:19:37  colinmacleod
 * Changed copyright text to GPL v2 explicitly.
 *
 * Revision 1.1  2005/03/10 19:23:04  colinmacleod
 * Moved to ivata groupware.
 *
 * Revision 1.1  2004/07/13 19:42:44  colinmacleod
 * Moved project to POJOs from EJBs.
 * Applied PicoContainer to services layer (replacing session EJBs).
 * Applied Hibernate to persistence layer (replacing entity EJBs).
 * -----------------------------------------------------------------------------
 */
package com.ivata.groupware.container.persistence.hibernate;

import org.apache.log4j.Logger;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * This factory is used for generating instances pf {@link
 * com.ivata.groupware.container.persistence.hibernate.HibernateQuery}.
 *
 * @author Colin MacLeod
 * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
 * @since ivata groupware 0.10 (Mar 28, 2004)
 * @version $Revision: 1.6 $
 */
public class HibernateQueryFactory implements Serializable {
    /**
     * Logger for this class.
     */
    private static final Logger logger = Logger
            .getLogger(HibernateQueryFactory.class);

    /**
     * Serialization version (for <code>Serializable</code> interface).
     */
    private static final long serialVersionUID = 1L;
    /**
     * <copyDoc>Refer to {@link HibernateQueryFactory()}.</copyDoc>
     */
    private Map queries;
    /**
     * <copyDoc>Refer to {@link HibernateQueryFactory()}.</copyDoc>
     */
    private Map queryArguments;

    /**
     * Construct a new Hibernate query factory with the given queries and
     * default argument order.
     *
     * @param queriesParam
     * Map of query names to the string queries which will be passed to
     * hibernate.
     * @param queryArgumentsParam
     * Stores the order of arguments when passed as an object array. Keyed by
     * the same name parameter of <code>queries</code>.
     */
    public HibernateQueryFactory(final Map queriesParam,
            final Map queryArgumentsParam) {
        super();
        this.queries = queriesParam;
        this.queryArguments = queryArgumentsParam;
    }

    /**
     * Generate a single instance of {@link HibernateQuery}.
     *
     * @param name Name of the query to be generated.
     * @param arguments Mappng of argument names to values to be used in parsing
     * the query.
     * @return New {@link HibernateQuery} instance for this query name.
     */
    public HibernateQuery generateQuery(final String name,
            final Map arguments) {
        if (logger.isDebugEnabled()) {
            logger.debug("generateQuery(String name = " + name
                    + ", Map arguments = " + arguments + ") - start");
        }

        String query = (String) queries.get(name);
        if (query == null) {
            throw new NullPointerException(
                    "Error in HibernateQueryFactory: no query called '"
                    + name
                    + "'");
        }
        HibernateQuery returnHibernateQuery = new HibernateQuery(query,
                arguments);
        if (logger.isDebugEnabled()) {
            logger.debug("generateQuery(String, Map) - end - return value = "
                    + returnHibernateQuery);
        }
        return returnHibernateQuery;
    }

    /**
     * Generate a single instance of {@link HibernateQuery}.
     *
     * @param name Name of the query to be generated.
     * @param arguments Arguments values, in the order in which they appear in
     * the query string.
     * @return New {@link HibernateQuery} instance for this query name.
     */
    public HibernateQuery generateQuery(final String name,
            final Object[] arguments) {
        if (logger.isDebugEnabled()) {
            logger.debug("generateQuery(String name = " + name
                    + ", Object[] arguments = " + arguments + ") - start");
        }

        Object argumentNamesObject = queryArguments.get(name);
        String[] argumentNames;
        if (argumentNamesObject instanceof List) {
            argumentNames = (String[]) ((List) argumentNamesObject)
                .toArray(new String[] {});
        } else {
            assert (argumentNamesObject != null)
            : "argumentNamesObject is null for query '"
                + name
                + "'";
            assert (argumentNamesObject instanceof String[])
            : "The arguments for query '"
                + name
                + "' of unknown type '"
                + argumentNamesObject.getClass().getName()
                + "'";
            argumentNames = (String[]) argumentNamesObject;
        }
        if (argumentNames == null) {
            throw new NullPointerException(
                    "Error in HibernateQueryFactory: no query called '"
                    + name
                    + "'");
        }
        if (arguments.length > argumentNames.length) {
            throw new RuntimeException(
                    "Error in HibernateQueryFactory: too many arguments ("
                    + arguments.length
                    + ") provided - expected "
                    + argumentNames.length);
        }
        Map argumentMap = new HashMap();
        for (int i = 0; i < arguments.length; i++) {
            String argumentName = argumentNames[i];
            Object argument = arguments[i];
            argumentMap.put(argumentName, argument);
        }
        HibernateQuery returnHibernateQuery = generateQuery(name, argumentMap);
        if (logger.isDebugEnabled()) {
            logger.debug("generateQuery - end - return value = "
                            + returnHibernateQuery);
        }
        return returnHibernateQuery;
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.