KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > ivata > groupware > container > persistence > hibernate > HibernateQueryFactory


1 /*
2  * Copyright (c) 2001 - 2005 ivata limited.
3  * All rights reserved.
4  * -----------------------------------------------------------------------------
5  * ivata groupware may be redistributed under the GNU General Public
6  * License as published by the Free Software Foundation;
7  * version 2 of the License.
8  *
9  * These programs are free software; you can redistribute them and/or
10  * modify them under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; version 2 of the License.
12  *
13  * These programs are distributed in the hope that they will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  *
17  * See the GNU General Public License in the file LICENSE.txt for more
18  * details.
19  *
20  * If you would like a copy of the GNU General Public License write to
21  *
22  * Free Software Foundation, Inc.
23  * 59 Temple Place - Suite 330
24  * Boston, MA 02111-1307, USA.
25  *
26  *
27  * To arrange commercial support and licensing, contact ivata at
28  * http://www.ivata.com/contact.jsp
29  * -----------------------------------------------------------------------------
30  * $Log: HibernateQueryFactory.java,v $
31  * Revision 1.3 2005/04/10 20:09:43 colinmacleod
32  * Added new themes.
33  * Changed id type to String.
34  * Changed i tag to em and b tag to strong.
35  * Improved PicoContainerFactory with NanoContainer scripts.
36  *
37  * Revision 1.2 2005/04/09 17:19:37 colinmacleod
38  * Changed copyright text to GPL v2 explicitly.
39  *
40  * Revision 1.1 2005/03/10 19:23:04 colinmacleod
41  * Moved to ivata groupware.
42  *
43  * Revision 1.1 2004/07/13 19:42:44 colinmacleod
44  * Moved project to POJOs from EJBs.
45  * Applied PicoContainer to services layer (replacing session EJBs).
46  * Applied Hibernate to persistence layer (replacing entity EJBs).
47  * -----------------------------------------------------------------------------
48  */

49 package com.ivata.groupware.container.persistence.hibernate;
50
51 import java.io.Serializable JavaDoc;
52 import java.util.HashMap JavaDoc;
53 import java.util.List JavaDoc;
54 import java.util.Map JavaDoc;
55
56 /**
57  * @author Colin MacLeod
58  * <a HREF='mailto:colin.macleod@ivata.com'>colin.macleod@ivata.com</a>
59  * @since Mar 28, 2004
60  * @version $Revision: 1.3 $
61  */

62 public class HibernateQueryFactory implements Serializable JavaDoc {
63     /**
64      * Map of query names to the string queries which will be passed to
65      * hibernate.
66      */

67     private Map JavaDoc queries;
68     /**
69      * Stores the order of arguments when passed as an object array.
70      */

71     private Map JavaDoc queryArguments;
72
73     /**
74      * Construct a new hibernate query factory with the given queries and
75      * default argument order.
76      */

77     public HibernateQueryFactory(Map JavaDoc queries, Map JavaDoc queryArguments) {
78         super();
79         this.queries = queries;
80         this.queryArguments = queryArguments;
81     }
82
83     /**
84      * @see com.ivata.groupware.container.persistence.PersistenceQueryFactory#generateQuery(String, java.util.List)
85      */

86     public HibernateQuery generateQuery(final String JavaDoc name,
87             final Map JavaDoc arguments) {
88         String JavaDoc query = (String JavaDoc) queries.get(name);
89         if (query == null) {
90             throw new NullPointerException JavaDoc("Error in HibernateQueryFactory: no query called '"
91                 + name
92                 +"'");
93         }
94         return new HibernateQuery(query, arguments);
95     }
96
97     /**
98      * @see com.ivata.groupware.container.persistence.PersistenceQueryFactory#generateQuery(String, Object[])
99      */

100     public HibernateQuery generateQuery(final String JavaDoc name,
101             final Object JavaDoc[] arguments) {
102         Object JavaDoc argumentNamesObject = queryArguments.get(name);
103         String JavaDoc[] argumentNames;
104         if (argumentNamesObject instanceof List JavaDoc) {
105             argumentNames = (String JavaDoc[]) ((List JavaDoc) argumentNamesObject)
106                 .toArray(new String JavaDoc[] {});
107         } else {
108             assert (argumentNamesObject instanceof String JavaDoc[]);
109             argumentNames = (String JavaDoc[]) argumentNamesObject;
110         }
111         if (argumentNames == null) {
112             throw new NullPointerException JavaDoc("Error in HibernateQueryFactory: no query called '"
113                 + name
114                 +"'");
115         }
116         if(arguments.length > argumentNames.length) {
117             throw new RuntimeException JavaDoc("Error in HibernateQueryFactory: too many arguments ("
118                 + arguments.length
119                 + ") provided - expected "
120                 + argumentNames.length);
121         }
122         Map JavaDoc argumentMap = new HashMap JavaDoc();
123         for (int i = 0; i < arguments.length; i++) {
124             String JavaDoc argumentName = argumentNames[i];
125             Object JavaDoc argument = arguments[i];
126             argumentMap.put(argumentName, argument);
127         }
128         return generateQuery(name, argumentMap);
129     }
130 }
131
Popular Tags