001: /*
002: * Copyright (c) 2001 - 2005 ivata limited.
003: * All rights reserved.
004: * -----------------------------------------------------------------------------
005: * ivata groupware may be redistributed under the GNU General Public
006: * License as published by the Free Software Foundation;
007: * version 2 of the License.
008: *
009: * These programs are free software; you can redistribute them and/or
010: * modify them under the terms of the GNU General Public License
011: * as published by the Free Software Foundation; version 2 of the License.
012: *
013: * These programs are distributed in the hope that they will be useful,
014: * but WITHOUT ANY WARRANTY; without even the implied warranty of
015: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: *
017: * See the GNU General Public License in the file LICENSE.txt for more
018: * details.
019: *
020: * If you would like a copy of the GNU General Public License write to
021: *
022: * Free Software Foundation, Inc.
023: * 59 Temple Place - Suite 330
024: * Boston, MA 02111-1307, USA.
025: *
026: *
027: * To arrange commercial support and licensing, contact ivata at
028: * http://www.ivata.com/contact.jsp
029: * -----------------------------------------------------------------------------
030: * $Log: HibernateQueryFactory.java,v $
031: * Revision 1.6 2005/10/11 18:51:38 colinmacleod
032: * Fixed some checkstyle and javadoc issues.
033: *
034: * Revision 1.5 2005/10/02 14:08:56 colinmacleod
035: * Added/improved log4j logging.
036: *
037: * Revision 1.4 2005/09/14 15:17:30 colinmacleod
038: * Improved assertions for query argument checking.
039: * Removed unused local and class variables.
040: * Added serialVersionUID.
041: *
042: * Revision 1.3 2005/04/10 20:09:43 colinmacleod
043: * Added new themes.
044: * Changed id type to String.
045: * Changed i tag to em and b tag to strong.
046: * Improved PicoContainerFactory with NanoContainer scripts.
047: *
048: * Revision 1.2 2005/04/09 17:19:37 colinmacleod
049: * Changed copyright text to GPL v2 explicitly.
050: *
051: * Revision 1.1 2005/03/10 19:23:04 colinmacleod
052: * Moved to ivata groupware.
053: *
054: * Revision 1.1 2004/07/13 19:42:44 colinmacleod
055: * Moved project to POJOs from EJBs.
056: * Applied PicoContainer to services layer (replacing session EJBs).
057: * Applied Hibernate to persistence layer (replacing entity EJBs).
058: * -----------------------------------------------------------------------------
059: */
060: package com.ivata.groupware.container.persistence.hibernate;
061:
062: import org.apache.log4j.Logger;
063:
064: import java.io.Serializable;
065: import java.util.HashMap;
066: import java.util.List;
067: import java.util.Map;
068:
069: /**
070: * This factory is used for generating instances pf {@link
071: * com.ivata.groupware.container.persistence.hibernate.HibernateQuery}.
072: *
073: * @author Colin MacLeod
074: * <a href='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
075: * @since ivata groupware 0.10 (Mar 28, 2004)
076: * @version $Revision: 1.6 $
077: */
078: public class HibernateQueryFactory implements Serializable {
079: /**
080: * Logger for this class.
081: */
082: private static final Logger logger = Logger
083: .getLogger(HibernateQueryFactory.class);
084:
085: /**
086: * Serialization version (for <code>Serializable</code> interface).
087: */
088: private static final long serialVersionUID = 1L;
089: /**
090: * <copyDoc>Refer to {@link HibernateQueryFactory()}.</copyDoc>
091: */
092: private Map queries;
093: /**
094: * <copyDoc>Refer to {@link HibernateQueryFactory()}.</copyDoc>
095: */
096: private Map queryArguments;
097:
098: /**
099: * Construct a new Hibernate query factory with the given queries and
100: * default argument order.
101: *
102: * @param queriesParam
103: * Map of query names to the string queries which will be passed to
104: * hibernate.
105: * @param queryArgumentsParam
106: * Stores the order of arguments when passed as an object array. Keyed by
107: * the same name parameter of <code>queries</code>.
108: */
109: public HibernateQueryFactory(final Map queriesParam,
110: final Map queryArgumentsParam) {
111: super ();
112: this .queries = queriesParam;
113: this .queryArguments = queryArgumentsParam;
114: }
115:
116: /**
117: * Generate a single instance of {@link HibernateQuery}.
118: *
119: * @param name Name of the query to be generated.
120: * @param arguments Mappng of argument names to values to be used in parsing
121: * the query.
122: * @return New {@link HibernateQuery} instance for this query name.
123: */
124: public HibernateQuery generateQuery(final String name,
125: final Map arguments) {
126: if (logger.isDebugEnabled()) {
127: logger.debug("generateQuery(String name = " + name
128: + ", Map arguments = " + arguments + ") - start");
129: }
130:
131: String query = (String) queries.get(name);
132: if (query == null) {
133: throw new NullPointerException(
134: "Error in HibernateQueryFactory: no query called '"
135: + name + "'");
136: }
137: HibernateQuery returnHibernateQuery = new HibernateQuery(query,
138: arguments);
139: if (logger.isDebugEnabled()) {
140: logger
141: .debug("generateQuery(String, Map) - end - return value = "
142: + returnHibernateQuery);
143: }
144: return returnHibernateQuery;
145: }
146:
147: /**
148: * Generate a single instance of {@link HibernateQuery}.
149: *
150: * @param name Name of the query to be generated.
151: * @param arguments Arguments values, in the order in which they appear in
152: * the query string.
153: * @return New {@link HibernateQuery} instance for this query name.
154: */
155: public HibernateQuery generateQuery(final String name,
156: final Object[] arguments) {
157: if (logger.isDebugEnabled()) {
158: logger.debug("generateQuery(String name = " + name
159: + ", Object[] arguments = " + arguments
160: + ") - start");
161: }
162:
163: Object argumentNamesObject = queryArguments.get(name);
164: String[] argumentNames;
165: if (argumentNamesObject instanceof List) {
166: argumentNames = (String[]) ((List) argumentNamesObject)
167: .toArray(new String[] {});
168: } else {
169: assert (argumentNamesObject != null) : "argumentNamesObject is null for query '"
170: + name + "'";
171: assert (argumentNamesObject instanceof String[]) : "The arguments for query '"
172: + name
173: + "' of unknown type '"
174: + argumentNamesObject.getClass().getName() + "'";
175: argumentNames = (String[]) argumentNamesObject;
176: }
177: if (argumentNames == null) {
178: throw new NullPointerException(
179: "Error in HibernateQueryFactory: no query called '"
180: + name + "'");
181: }
182: if (arguments.length > argumentNames.length) {
183: throw new RuntimeException(
184: "Error in HibernateQueryFactory: too many arguments ("
185: + arguments.length
186: + ") provided - expected "
187: + argumentNames.length);
188: }
189: Map argumentMap = new HashMap();
190: for (int i = 0; i < arguments.length; i++) {
191: String argumentName = argumentNames[i];
192: Object argument = arguments[i];
193: argumentMap.put(argumentName, argument);
194: }
195: HibernateQuery returnHibernateQuery = generateQuery(name,
196: argumentMap);
197: if (logger.isDebugEnabled()) {
198: logger.debug("generateQuery - end - return value = "
199: + returnHibernateQuery);
200: }
201: return returnHibernateQuery;
202: }
203: }
|