KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.HashSet JavaDoc;
25
26 import org.apache.cayenne.map.EntityResolver;
27 import org.apache.cayenne.map.ObjRelationship;
28 import org.apache.cayenne.util.Util;
29
30 /**
31  * A SelectQuery to perform a prefetch based on another query. Used internally by Cayenne
32  * and is normally never used directly.
33  *
34  * @author Craig Miskell, Andrus Adamchik
35  */

36 public class PrefetchSelectQuery extends SelectQuery {
37
38     protected SelectQuery parentQuery;
39
40     /**
41      * The relationship path from root objects to the objects being prefetched.
42      */

43     protected String JavaDoc prefetchPath;
44
45     /**
46      * Stores the last ObjRelationship in the prefetch path.
47      */

48     protected ObjRelationship lastPrefetchHint;
49
50     // TODO, Andrus 11/17/2005 - i guess we should deprecate
51
// SelectQuery.customDbAttribute, replacing it with "resultPaths" mechanism.
52
protected Collection JavaDoc resultPaths;
53
54     /**
55      * Creates a new disjoint prefetch select query.
56      *
57      * @since 1.2
58      */

59     public PrefetchSelectQuery(SelectQuery parentQuery, String JavaDoc prefetchPath,
60             ObjRelationship lastPrefetchHint) {
61
62         setRoot(lastPrefetchHint.getTargetEntity());
63         this.parentQuery = parentQuery;
64         this.prefetchPath = prefetchPath;
65         this.lastPrefetchHint = lastPrefetchHint;
66     }
67
68     /**
69      * Overrides super implementation to suppress disjoint prefetch routing, as the parent
70      * query should take care of that.
71      *
72      * @since 1.2
73      */

74     void routePrefetches(QueryRouter router, EntityResolver resolver) {
75         // noop - intentional.
76
}
77
78     /**
79      * Returns the prefetchPath.
80      *
81      * @return String
82      */

83     public String JavaDoc getPrefetchPath() {
84         return prefetchPath;
85     }
86
87     /**
88      * Sets the prefetchPath.
89      *
90      * @param prefetchPath The prefetchPath to set
91      */

92     public void setPrefetchPath(String JavaDoc prefetchPath) {
93         this.prefetchPath = prefetchPath;
94     }
95
96     /**
97      * @since 1.1
98      */

99     public SelectQuery getParentQuery() {
100         return parentQuery;
101     }
102
103     /**
104      * @since 1.1
105      */

106     public void setParentQuery(SelectQuery parentQuery) {
107         this.parentQuery = parentQuery;
108     }
109
110     /**
111      * Retunrs last incoming ObjRelationship in the prefetch relationship chain.
112      *
113      * @since 1.1
114      */

115     public ObjRelationship getLastPrefetchHint() {
116         return lastPrefetchHint;
117     }
118
119     /**
120      * @since 1.1
121      */

122     public void setLastPrefetchHint(ObjRelationship relationship) {
123         lastPrefetchHint = relationship;
124     }
125
126     /**
127      * Configures an "extra" path that will resolve to an extra column (or columns) in the
128      * result set.
129      *
130      * @param path A valid path expression. E.g. "abc" or "db:ABC" or "abc.xyz".
131      * @since 1.2
132      */

133     public void addResultPath(String JavaDoc path) {
134         if (Util.isEmptyString(path)) {
135             throw new IllegalArgumentException JavaDoc("Invalid path: " + path);
136         }
137
138         nonNullResultPaths().add(path);
139     }
140
141     /**
142      * Removes an extra result path. Note that this method doesn't check for expression
143      * invariants, as it doesn't have a proper context to do so. E.g. for the purspose of
144      * this method "db:ARTIST_NAME" and "obj:artistName" are not the same, though both
145      * will resolve to the same column name.
146      */

147     public void removeResultPath(String JavaDoc path) {
148         if (resultPaths != null) {
149             resultPaths.remove(path);
150         }
151     }
152
153     /**
154      * Returns extra result paths.
155      *
156      * @since 1.2
157      */

158     public Collection JavaDoc getResultPaths() {
159         return resultPaths != null
160                 ? Collections.unmodifiableCollection(resultPaths)
161                 : Collections.EMPTY_SET;
162     }
163
164     /**
165      * Returns a Collection that internally stores extra result paths, creating it on
166      * demand.
167      *
168      * @since 1.2
169      */

170     Collection JavaDoc nonNullResultPaths() {
171         if (resultPaths == null) {
172             resultPaths = new HashSet JavaDoc();
173         }
174
175         return resultPaths;
176     }
177 }
178
Popular Tags