KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > map > QueryBuilder


1 /* ====================================================================
2  *
3  * The ObjectStyle Group Software License, version 1.1
4  * ObjectStyle Group - http://objectstyle.org/
5  *
6  * Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
7  * of the software. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution, if any,
22  * must include the following acknowlegement:
23  * "This product includes software developed by independent contributors
24  * and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
25  * Alternately, this acknowlegement may appear in the software itself,
26  * if and wherever such third-party acknowlegements normally appear.
27  *
28  * 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
29  * or promote products derived from this software without prior written
30  * permission. For written permission, email
31  * "andrus at objectstyle dot org".
32  *
33  * 5. Products derived from this software may not be called "ObjectStyle"
34  * or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
35  * names without prior written permission.
36  *
37  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40  * DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
41  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48  * SUCH DAMAGE.
49  * ====================================================================
50  *
51  * This software consists of voluntary contributions made by many
52  * individuals and hosted on ObjectStyle Group web site. For more
53  * information on the ObjectStyle Group, please see
54  * <http://objectstyle.org/>.
55  */

56 package org.objectstyle.cayenne.map;
57
58 import java.util.ArrayList JavaDoc;
59 import java.util.HashMap JavaDoc;
60 import java.util.List JavaDoc;
61 import java.util.Map JavaDoc;
62
63 import org.objectstyle.cayenne.CayenneRuntimeException;
64 import org.objectstyle.cayenne.exp.Expression;
65 import org.objectstyle.cayenne.query.Ordering;
66 import org.objectstyle.cayenne.query.Query;
67
68 /**
69  * A builder that constructs Cayenne queries from abstract configuration information
70  * defined in cayenne-data-map*.dtd. This abstract builder supports values declared in the
71  * DTD, allowing subclasses to define their own Query creation logic.
72  *
73  * @since 1.1
74  * @author Andrei Adamchik
75  */

76 public abstract class QueryBuilder {
77
78     public static final String JavaDoc OBJ_ENTITY_ROOT = "obj-entity";
79     public static final String JavaDoc DB_ENTITY_ROOT = "db-entity";
80     public static final String JavaDoc PROCEDURE_ROOT = "procedure";
81     public static final String JavaDoc DATA_MAP_ROOT = "data-map";
82     public static final String JavaDoc JAVA_CLASS_ROOT = "java-class";
83
84     protected String JavaDoc name;
85     protected Map JavaDoc properties;
86     protected List JavaDoc resultColumns;
87     protected String JavaDoc sql;
88     protected Map JavaDoc adapterSql;
89     protected Expression qualifier;
90     protected List JavaDoc orderings;
91     protected List JavaDoc prefetches;
92     protected DataMap dataMap;
93     protected String JavaDoc rootType;
94     protected String JavaDoc rootName;
95     protected String JavaDoc resultType;
96     protected boolean selecting = true;
97
98     /**
99      * Builds a Query object based on internal configuration information.
100      */

101     public abstract Query getQuery();
102
103     public void setName(String JavaDoc name) {
104         this.name = name;
105     }
106
107     /**
108      * Determines query root based on configuration info.
109      *
110      * @throws CayenneRuntimeException if a valid root can't be established.
111      */

112     protected Object JavaDoc getRoot() {
113
114         if (rootType == null || DATA_MAP_ROOT.equals(rootType)) {
115             return dataMap;
116         }
117         else if (rootName == null) {
118             return null;
119         }
120         else if (OBJ_ENTITY_ROOT.equals(rootType)) {
121             return dataMap.getObjEntity(rootName);
122         }
123         else if (DB_ENTITY_ROOT.equals(rootType)) {
124             return dataMap.getDbEntity(rootName);
125         }
126         else if (PROCEDURE_ROOT.equals(rootType)) {
127             return dataMap.getProcedure(rootName);
128         }
129         else if (JAVA_CLASS_ROOT.equals(rootType)) {
130             // setting root to ObjEntity, since creating a Class requires
131
// the knowledge of the ClassLoader
132
return dataMap.getObjEntityForJavaClass(rootName);
133         }
134
135         return null;
136     }
137
138     public void setSelecting(String JavaDoc selecting) {
139         // "true" is a default per DTD
140
this.selecting = ("false".equalsIgnoreCase(selecting)) ? false : true;
141     }
142     
143     public void setResultType(String JavaDoc resultType) {
144         this.resultType = resultType;
145     }
146
147     /**
148      * Sets the information pertaining to the root of the query.
149      */

150     public void setRoot(DataMap dataMap, String JavaDoc rootType, String JavaDoc rootName) {
151         this.dataMap = dataMap;
152         this.rootType = rootType;
153         this.rootName = rootName;
154     }
155
156     /**
157      * Adds raw sql. If adapterClass parameter is not null, sets the SQL string to be
158      * adapter-specific. Otherwise it is used as a default SQL string.
159      */

160     public void addSql(String JavaDoc sql, String JavaDoc adapterClass) {
161         if (adapterClass == null) {
162             this.sql = sql;
163         }
164         else {
165             if (adapterSql == null) {
166                 adapterSql = new HashMap JavaDoc();
167             }
168
169             adapterSql.put(adapterClass, sql);
170         }
171     }
172
173     public void setQualifier(String JavaDoc qualifier) {
174         if (qualifier == null || qualifier.trim().length() == 0) {
175             this.qualifier = null;
176         }
177         else {
178             this.qualifier = Expression.fromString(qualifier.trim());
179         }
180     }
181
182     public void addProperty(String JavaDoc name, String JavaDoc value) {
183         if (properties == null) {
184             properties = new HashMap JavaDoc();
185         }
186
187         properties.put(name, value);
188     }
189
190     public void addResultColumn(String JavaDoc label, String JavaDoc dbType, String JavaDoc objectType) {
191         if (resultColumns == null) {
192             resultColumns = new ArrayList JavaDoc();
193         }
194
195         resultColumns.add(new ResultColumn(label, dbType, objectType));
196     }
197
198     public void addOrdering(String JavaDoc path, String JavaDoc descending, String JavaDoc ignoreCase) {
199         if (orderings == null) {
200             orderings = new ArrayList JavaDoc();
201         }
202
203         if (path != null && path.trim().length() == 0) {
204             path = null;
205         }
206         boolean isDescending = "true".equalsIgnoreCase(descending);
207         boolean isIgnoringCase = "true".equalsIgnoreCase(ignoreCase);
208         orderings.add(new Ordering(path, !isDescending, isIgnoringCase));
209     }
210
211     public void addPrefetch(String JavaDoc path) {
212         if (path == null || path != null && path.trim().length() == 0) {
213             // throw??
214
return;
215         }
216
217         if (prefetches == null) {
218             prefetches = new ArrayList JavaDoc();
219         }
220         prefetches.add(path.trim());
221     }
222
223     static class ResultColumn {
224
225         String JavaDoc label;
226         String JavaDoc dbType;
227         String JavaDoc objectType;
228
229         ResultColumn(String JavaDoc label, String JavaDoc dbType, String JavaDoc objectType) {
230             this.label = label;
231             this.dbType = dbType;
232             this.objectType = objectType;
233         }
234     }
235 }
Popular Tags