KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

19
20 package org.apache.cayenne.map;
21
22 import java.util.ArrayList JavaDoc;
23 import java.util.HashMap JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import org.apache.cayenne.CayenneRuntimeException;
28 import org.apache.cayenne.exp.Expression;
29 import org.apache.cayenne.query.Ordering;
30 import org.apache.cayenne.query.Query;
31
32 /**
33  * A builder that constructs Cayenne queries from abstract configuration information
34  * defined in cayenne-data-map*.dtd. This abstract builder supports values declared in the
35  * DTD, allowing subclasses to define their own Query creation logic.
36  *
37  * @since 1.1
38  * @author Andrus Adamchik
39  */

40 public abstract class QueryBuilder {
41
42     public static final String JavaDoc OBJ_ENTITY_ROOT = "obj-entity";
43     public static final String JavaDoc DB_ENTITY_ROOT = "db-entity";
44     public static final String JavaDoc PROCEDURE_ROOT = "procedure";
45     public static final String JavaDoc DATA_MAP_ROOT = "data-map";
46     public static final String JavaDoc JAVA_CLASS_ROOT = "java-class";
47
48     protected String JavaDoc name;
49     protected Map JavaDoc properties;
50     protected List JavaDoc resultColumns;
51     protected String JavaDoc sql;
52     protected Map JavaDoc adapterSql;
53     protected Expression qualifier;
54     protected List JavaDoc orderings;
55     protected List JavaDoc prefetches;
56     protected DataMap dataMap;
57     protected String JavaDoc rootType;
58     protected String JavaDoc rootName;
59     protected String JavaDoc resultEntity;
60
61     /**
62      * Builds a Query object based on internal configuration information.
63      */

64     public abstract Query getQuery();
65
66     public void setName(String JavaDoc name) {
67         this.name = name;
68     }
69
70     /**
71      * Determines query root based on configuration info, falls back to a DataMap root if
72      * the data is invalid.
73      *
74      * @throws CayenneRuntimeException if a valid root can't be established.
75      */

76     protected Object JavaDoc getRoot() {
77
78         Object JavaDoc root = null;
79
80         if (rootType == null || DATA_MAP_ROOT.equals(rootType) || rootName == null) {
81             root = dataMap;
82         }
83         else if (OBJ_ENTITY_ROOT.equals(rootType)) {
84             root = dataMap.getObjEntity(rootName);
85         }
86         else if (DB_ENTITY_ROOT.equals(rootType)) {
87             root = dataMap.getDbEntity(rootName);
88         }
89         else if (PROCEDURE_ROOT.equals(rootType)) {
90             root = dataMap.getProcedure(rootName);
91         }
92         else if (JAVA_CLASS_ROOT.equals(rootType)) {
93             // setting root to ObjEntity, since creating a Class requires
94
// the knowledge of the ClassLoader
95
root = dataMap.getObjEntityForJavaClass(rootName);
96         }
97
98         return (root != null) ? root : dataMap;
99     }
100
101     public void setResultEntity(String JavaDoc resultEntity) {
102         this.resultEntity = resultEntity;
103     }
104
105     /**
106      * Sets the information pertaining to the root of the query.
107      */

108     public void setRoot(DataMap dataMap, String JavaDoc rootType, String JavaDoc rootName) {
109         this.dataMap = dataMap;
110         this.rootType = rootType;
111         this.rootName = rootName;
112     }
113
114     /**
115      * Adds raw sql. If adapterClass parameter is not null, sets the SQL string to be
116      * adapter-specific. Otherwise it is used as a default SQL string.
117      */

118     public void addSql(String JavaDoc sql, String JavaDoc adapterClass) {
119         if (adapterClass == null) {
120             this.sql = sql;
121         }
122         else {
123             if (adapterSql == null) {
124                 adapterSql = new HashMap JavaDoc();
125             }
126
127             adapterSql.put(adapterClass, sql);
128         }
129     }
130
131     public void setQualifier(String JavaDoc qualifier) {
132         if (qualifier == null || qualifier.trim().length() == 0) {
133             this.qualifier = null;
134         }
135         else {
136             this.qualifier = Expression.fromString(qualifier.trim());
137         }
138     }
139
140     public void addProperty(String JavaDoc name, String JavaDoc value) {
141         if (properties == null) {
142             properties = new HashMap JavaDoc();
143         }
144
145         properties.put(name, value);
146     }
147
148     public void addOrdering(String JavaDoc path, String JavaDoc descending, String JavaDoc ignoreCase) {
149         if (orderings == null) {
150             orderings = new ArrayList JavaDoc();
151         }
152
153         if (path != null && path.trim().length() == 0) {
154             path = null;
155         }
156         boolean isDescending = "true".equalsIgnoreCase(descending);
157         boolean isIgnoringCase = "true".equalsIgnoreCase(ignoreCase);
158         orderings.add(new Ordering(path, !isDescending, isIgnoringCase));
159     }
160
161     public void addPrefetch(String JavaDoc path) {
162         if (path == null || path != null && path.trim().length() == 0) {
163             // throw??
164
return;
165         }
166
167         if (prefetches == null) {
168             prefetches = new ArrayList JavaDoc();
169         }
170         prefetches.add(path.trim());
171     }
172 }
173
Popular Tags