KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > query > BaseQueryMetadata


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.query;
21
22 import java.io.Serializable JavaDoc;
23 import java.util.Collection JavaDoc;
24 import java.util.Collections JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.Map JavaDoc;
27 import java.util.StringTokenizer JavaDoc;
28
29 import org.apache.cayenne.CayenneRuntimeException;
30 import org.apache.cayenne.Persistent;
31 import org.apache.cayenne.map.DataMap;
32 import org.apache.cayenne.map.DbEntity;
33 import org.apache.cayenne.map.EntityResolver;
34 import org.apache.cayenne.map.ObjEntity;
35 import org.apache.cayenne.map.Procedure;
36 import org.apache.cayenne.reflect.ClassDescriptor;
37 import org.apache.cayenne.util.Util;
38 import org.apache.cayenne.util.XMLEncoder;
39 import org.apache.cayenne.util.XMLSerializable;
40
41 /**
42  * Default mutable implementation of {@link QueryMetadata}.
43  *
44  * @author Andrus Adamchik
45  * @since 1.1
46  */

47 class BaseQueryMetadata implements QueryMetadata, XMLSerializable, Serializable JavaDoc {
48
49     int fetchLimit = QueryMetadata.FETCH_LIMIT_DEFAULT;
50     int pageSize = QueryMetadata.PAGE_SIZE_DEFAULT;
51     boolean fetchingDataRows = QueryMetadata.FETCHING_DATA_ROWS_DEFAULT;
52     boolean refreshingObjects = QueryMetadata.REFRESHING_OBJECTS_DEFAULT;
53     boolean resolvingInherited = QueryMetadata.RESOLVING_INHERITED_DEFAULT;
54     String JavaDoc cachePolicy = QueryMetadata.CACHE_POLICY_DEFAULT;
55
56     PrefetchTreeNode prefetchTree;
57     String JavaDoc cacheKey;
58     String JavaDoc[] cacheGroups;
59
60     transient DbEntity dbEntity;
61     transient DataMap dataMap;
62     transient Object JavaDoc lastRoot;
63     transient ClassDescriptor classDescriptor;
64     transient EntityResolver lastEntityResolver;
65
66     /**
67      * Copies values of another QueryMetadata object to this object.
68      */

69     void copyFromInfo(QueryMetadata info) {
70         this.lastEntityResolver = null;
71         this.lastRoot = null;
72         this.classDescriptor = null;
73         this.dbEntity = null;
74         this.dataMap = null;
75
76         this.fetchingDataRows = info.isFetchingDataRows();
77         this.fetchLimit = info.getFetchLimit();
78         this.pageSize = info.getPageSize();
79         this.refreshingObjects = info.isRefreshingObjects();
80         this.resolvingInherited = info.isResolvingInherited();
81         this.cachePolicy = info.getCachePolicy();
82         this.cacheKey = info.getCacheKey();
83
84         setPrefetchTree(info.getPrefetchTree());
85     }
86
87     boolean resolve(Object JavaDoc root, EntityResolver resolver, String JavaDoc cacheKey) {
88
89         if (lastRoot != root || lastEntityResolver != resolver) {
90
91             this.cacheKey = cacheKey;
92
93             this.classDescriptor = null;
94             this.dbEntity = null;
95             this.dataMap = null;
96
97             ObjEntity entity = null;
98
99             if (root != null) {
100                 if (root instanceof Class JavaDoc) {
101                     entity = resolver.lookupObjEntity((Class JavaDoc) root);
102
103                     if (entity != null) {
104                         this.dbEntity = entity.getDbEntity();
105                         this.dataMap = entity.getDataMap();
106                     }
107                 }
108                 else if (root instanceof ObjEntity) {
109                     entity = (ObjEntity) root;
110                     this.dbEntity = entity.getDbEntity();
111                     this.dataMap = entity.getDataMap();
112                 }
113                 else if (root instanceof String JavaDoc) {
114                     entity = resolver.getObjEntity((String JavaDoc) root);
115                     if (entity != null) {
116                         this.dbEntity = entity.getDbEntity();
117                         this.dataMap = entity.getDataMap();
118                     }
119                 }
120                 else if (root instanceof DbEntity) {
121                     this.dbEntity = (DbEntity) root;
122                     this.dataMap = dbEntity.getDataMap();
123                 }
124                 else if (root instanceof DataMap) {
125                     this.dataMap = (DataMap) root;
126                 }
127                 else if (root instanceof Persistent) {
128                     entity = resolver.lookupObjEntity((Persistent) root);
129                     if (entity != null) {
130                         this.dbEntity = entity.getDbEntity();
131                         this.dataMap = entity.getDataMap();
132                     }
133                 }
134             }
135
136             if (entity != null) {
137                 this.classDescriptor = resolver.getClassDescriptor(entity.getName());
138             }
139
140             this.lastRoot = root;
141             this.lastEntityResolver = resolver;
142             return true;
143         }
144
145         return false;
146     }
147
148     void initWithProperties(Map JavaDoc properties) {
149         // must init defaults even if properties are empty
150
if (properties == null) {
151             properties = Collections.EMPTY_MAP;
152         }
153
154         Object JavaDoc fetchLimit = properties.get(QueryMetadata.FETCH_LIMIT_PROPERTY);
155         Object JavaDoc pageSize = properties.get(QueryMetadata.PAGE_SIZE_PROPERTY);
156         Object JavaDoc refreshingObjects = properties
157                 .get(QueryMetadata.REFRESHING_OBJECTS_PROPERTY);
158         Object JavaDoc fetchingDataRows = properties
159                 .get(QueryMetadata.FETCHING_DATA_ROWS_PROPERTY);
160
161         Object JavaDoc resolvingInherited = properties
162                 .get(QueryMetadata.RESOLVING_INHERITED_PROPERTY);
163
164         Object JavaDoc cachePolicy = properties.get(QueryMetadata.CACHE_POLICY_PROPERTY);
165         Object JavaDoc cacheGroups = properties.get(QueryMetadata.CACHE_GROUPS_PROPERTY);
166
167         // init ivars from properties
168
this.fetchLimit = (fetchLimit != null)
169                 ? Integer.parseInt(fetchLimit.toString())
170                 : QueryMetadata.FETCH_LIMIT_DEFAULT;
171
172         this.pageSize = (pageSize != null)
173                 ? Integer.parseInt(pageSize.toString())
174                 : QueryMetadata.PAGE_SIZE_DEFAULT;
175
176         this.refreshingObjects = (refreshingObjects != null)
177                 ? "true".equalsIgnoreCase(refreshingObjects.toString())
178                 : QueryMetadata.REFRESHING_OBJECTS_DEFAULT;
179
180         this.fetchingDataRows = (fetchingDataRows != null)
181                 ? "true".equalsIgnoreCase(fetchingDataRows.toString())
182                 : QueryMetadata.FETCHING_DATA_ROWS_DEFAULT;
183
184         this.resolvingInherited = (resolvingInherited != null)
185                 ? "true".equalsIgnoreCase(resolvingInherited.toString())
186                 : QueryMetadata.RESOLVING_INHERITED_DEFAULT;
187
188         this.cachePolicy = (cachePolicy != null)
189                 ? cachePolicy.toString()
190                 : QueryMetadata.CACHE_POLICY_DEFAULT;
191
192         this.cacheGroups = null;
193         if (cacheGroups instanceof String JavaDoc[]) {
194             this.cacheGroups = (String JavaDoc[]) cacheGroups;
195         }
196         else if (cacheGroups instanceof String JavaDoc) {
197             StringTokenizer JavaDoc toks = new StringTokenizer JavaDoc(cacheGroups.toString(), ",");
198             this.cacheGroups = new String JavaDoc[toks.countTokens()];
199             for (int i = 0; i < this.cacheGroups.length; i++) {
200                 this.cacheGroups[i] = toks.nextToken();
201             }
202         }
203     }
204
205     public void encodeAsXML(XMLEncoder encoder) {
206         if (refreshingObjects != QueryMetadata.REFRESHING_OBJECTS_DEFAULT) {
207             encoder.printProperty(
208                     QueryMetadata.REFRESHING_OBJECTS_PROPERTY,
209                     refreshingObjects);
210         }
211
212         if (fetchingDataRows != QueryMetadata.FETCHING_DATA_ROWS_DEFAULT) {
213             encoder.printProperty(
214                     QueryMetadata.FETCHING_DATA_ROWS_PROPERTY,
215                     fetchingDataRows);
216         }
217
218         if (resolvingInherited != QueryMetadata.RESOLVING_INHERITED_DEFAULT) {
219             encoder.printProperty(
220                     QueryMetadata.RESOLVING_INHERITED_PROPERTY,
221                     resolvingInherited);
222         }
223
224         if (fetchLimit != QueryMetadata.FETCH_LIMIT_DEFAULT) {
225             encoder.printProperty(QueryMetadata.FETCH_LIMIT_PROPERTY, fetchLimit);
226         }
227
228         if (pageSize != QueryMetadata.PAGE_SIZE_DEFAULT) {
229             encoder.printProperty(QueryMetadata.PAGE_SIZE_PROPERTY, pageSize);
230         }
231
232         if (cachePolicy != null
233                 && !QueryMetadata.CACHE_POLICY_DEFAULT.equals(cachePolicy)) {
234             encoder.printProperty(QueryMetadata.CACHE_POLICY_PROPERTY, cachePolicy);
235         }
236
237         if (prefetchTree != null) {
238             prefetchTree.encodeAsXML(encoder);
239         }
240
241         if (cacheGroups != null && cacheGroups.length > 0) {
242             StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(cacheGroups[0]);
243             for (int i = 1; i < cacheGroups.length; i++) {
244                 buffer.append(',').append(cacheGroups[i]);
245             }
246             encoder.printProperty(QueryMetadata.CACHE_GROUPS_PROPERTY, cachePolicy);
247         }
248     }
249
250     /**
251      * @since 1.2
252      */

253     public String JavaDoc getCacheKey() {
254         return cacheKey;
255     }
256
257     /**
258      * @since 1.2
259      */

260     public DataMap getDataMap() {
261         return dataMap;
262     }
263
264     /**
265      * @since 1.2
266      */

267     public Procedure getProcedure() {
268         return null;
269     }
270
271     /**
272      * @since 1.2
273      */

274     public DbEntity getDbEntity() {
275         return dbEntity;
276     }
277
278     /**
279      * @since 1.2
280      */

281     public ObjEntity getObjEntity() {
282         return classDescriptor != null ? classDescriptor.getEntity() : null;
283     }
284
285     /**
286      * @since 3.0
287      */

288     public ClassDescriptor getClassDescriptor() {
289         return classDescriptor;
290     }
291     
292     /**
293      * Always returns null, as this is not supported for most classic queries.
294      *
295      * @since 3.0
296      */

297     public SQLResultSetMapping getResultSetMapping() {
298         return null;
299     }
300
301     /**
302      * @since 1.2
303      */

304     public PrefetchTreeNode getPrefetchTree() {
305         return prefetchTree;
306     }
307
308     void setPrefetchTree(PrefetchTreeNode prefetchTree) {
309         if (prefetchTree != null) {
310             // importnat: make a clone to allow modification independent from the
311
// caller...
312
try {
313                 prefetchTree = (PrefetchTreeNode) Util
314                         .cloneViaSerialization(prefetchTree);
315             }
316             catch (CayenneRuntimeException e) {
317                 throw e;
318             }
319             catch (Exception JavaDoc e) {
320                 throw new CayenneRuntimeException("Error cloning prefetch tree", e);
321             }
322         }
323
324         this.prefetchTree = prefetchTree;
325     }
326
327     public String JavaDoc getCachePolicy() {
328         return cachePolicy;
329     }
330
331     void setCachePolicy(String JavaDoc policy) {
332         this.cachePolicy = policy;
333     }
334
335     /**
336      * @since 3.0
337      */

338     public String JavaDoc[] getCacheGroups() {
339         return cacheGroups;
340     }
341
342     /**
343      * @since 3.0
344      */

345     void setCacheGroups(String JavaDoc[] groups) {
346         this.cacheGroups = groups;
347     }
348
349     public boolean isFetchingDataRows() {
350         return fetchingDataRows;
351     }
352
353     public int getFetchLimit() {
354         return fetchLimit;
355     }
356
357     public int getPageSize() {
358         return pageSize;
359     }
360
361     /**
362      * Always returns -1.
363      */

364     public int getFetchStartIndex() {
365         return -1;
366     }
367
368     public boolean isRefreshingObjects() {
369         return refreshingObjects;
370     }
371
372     public boolean isResolvingInherited() {
373         return resolvingInherited;
374     }
375
376     void setFetchingDataRows(boolean b) {
377         fetchingDataRows = b;
378     }
379
380     void setFetchLimit(int i) {
381         fetchLimit = i;
382     }
383
384     void setPageSize(int i) {
385         pageSize = i;
386     }
387
388     void setRefreshingObjects(boolean b) {
389         refreshingObjects = b;
390     }
391
392     void setResolvingInherited(boolean b) {
393         resolvingInherited = b;
394     }
395
396     /**
397      * Adds a joint prefetch.
398      *
399      * @since 1.2
400      */

401     PrefetchTreeNode addPrefetch(String JavaDoc path, int semantics) {
402         if (prefetchTree == null) {
403             prefetchTree = new PrefetchTreeNode();
404         }
405
406         PrefetchTreeNode node = prefetchTree.addPath(path);
407         node.setSemantics(semantics);
408         node.setPhantom(false);
409         return node;
410     }
411
412     /**
413      * Adds all prefetches from a provided collection.
414      *
415      * @since 1.2
416      */

417     void addPrefetches(Collection JavaDoc prefetches, int semantics) {
418
419         if (prefetches != null && !prefetches.isEmpty()) {
420
421             Iterator JavaDoc it = prefetches.iterator();
422             while (it.hasNext()) {
423                 String JavaDoc prefetch = (String JavaDoc) it.next();
424                 addPrefetch(prefetch, semantics);
425             }
426         }
427     }
428
429     /**
430      * Clears all joint prefetches.
431      *
432      * @since 1.2
433      */

434     void clearPrefetches() {
435         prefetchTree = null;
436     }
437
438     /**
439      * Removes joint prefetch.
440      *
441      * @since 1.2
442      */

443     void removePrefetch(String JavaDoc prefetch) {
444         if (prefetchTree != null) {
445             prefetchTree.removePath(prefetch);
446         }
447     }
448 }
449
Popular Tags