KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.Iterator JavaDoc;
23
24 import org.apache.cayenne.CayenneRuntimeException;
25 import org.apache.cayenne.exp.Expression;
26 import org.apache.cayenne.map.EntityInheritanceTree;
27 import org.apache.cayenne.map.EntityResolver;
28 import org.apache.cayenne.map.ObjEntity;
29 import org.apache.cayenne.map.ObjRelationship;
30
31 /**
32  * Preprocessor and router of SelectQuery prefetches.
33  *
34  * @since 1.2
35  * @author Andrus Adamchik
36  */

37 class SelectQueryPrefetchRouterAction implements PrefetchProcessor {
38
39     SelectQuery query;
40     QueryRouter router;
41     EntityResolver resolver;
42     ObjEntity entity;
43     EntityInheritanceTree inheritanceTree;
44
45     /**
46      * Routes query prefetches, but not the query itself.
47      */

48     void route(SelectQuery query, QueryRouter router, EntityResolver resolver) {
49         if (!query.isFetchingDataRows() && query.getPrefetchTree() != null) {
50
51             this.query = query;
52             this.router = router;
53             this.resolver = resolver;
54             this.entity = query.getMetaData(resolver).getObjEntity();
55             this.inheritanceTree = resolver.lookupInheritanceTree(entity);
56
57             query.getPrefetchTree().traverse(this);
58         }
59     }
60
61     public boolean startPhantomPrefetch(PrefetchTreeNode node) {
62         return true;
63     }
64
65     public boolean startDisjointPrefetch(PrefetchTreeNode node) {
66         // don't do anything to root
67
if (node == query.getPrefetchTree()) {
68             return true;
69         }
70
71         String JavaDoc prefetchPath = node.getPath();
72
73         // find last relationship
74
Iterator JavaDoc it = entity.resolvePathComponents(prefetchPath);
75
76         ObjRelationship relationship = null;
77         while (it.hasNext()) {
78             relationship = (ObjRelationship) it.next();
79         }
80
81         if (relationship == null) {
82             throw new CayenneRuntimeException("Invalid prefetch '"
83                     + prefetchPath
84                     + "' for entity: "
85                     + entity.getName());
86         }
87
88         // chain query and entity qualifiers
89
Expression queryQualifier = query.getQualifier();
90
91         Expression entityQualifier = (inheritanceTree != null) ? inheritanceTree
92                 .qualifierForEntityAndSubclasses() : entity.getDeclaredQualifier();
93
94         if (entityQualifier != null) {
95             queryQualifier = (queryQualifier != null) ? queryQualifier
96                     .andExp(entityQualifier) : entityQualifier;
97         }
98
99         // create and configure PrefetchSelectQuery
100
PrefetchSelectQuery prefetchQuery = new PrefetchSelectQuery(
101                 query,
102                 prefetchPath,
103                 relationship);
104
105         prefetchQuery.setQualifier(entity.translateToRelatedEntity(
106                 queryQualifier,
107                 prefetchPath));
108
109         // setup extra result columns to be able to relate result rows to the parent
110
// result objects.
111
if (relationship.isFlattened()
112                 || (relationship.isToMany() && relationship.getReverseRelationship() == null)) {
113
114             prefetchQuery.addResultPath("db:"
115                     + relationship.getReverseDbRelationshipPath());
116         }
117
118         // pass prefetch subtree to enable joint prefetches...
119
prefetchQuery.setPrefetchTree(node);
120
121         // route...
122
prefetchQuery.route(router, resolver, null);
123         return true;
124     }
125
126     public boolean startJointPrefetch(PrefetchTreeNode node) {
127         // simply pass through
128
return true;
129     }
130
131     public boolean startUnknownPrefetch(PrefetchTreeNode node) {
132         // don't do anything to root
133
if (node == query.getPrefetchTree()) {
134             return true;
135         }
136
137         // route unknown as disjoint...
138
return startDisjointPrefetch(node);
139     }
140
141     public void finishPrefetch(PrefetchTreeNode node) {
142     }
143 }
144
Popular Tags