KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectstyle > cayenne > access > util > PrefetchResolver


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.access.util;
57
58 import java.util.ArrayList JavaDoc;
59 import java.util.HashMap JavaDoc;
60 import java.util.Iterator JavaDoc;
61 import java.util.List JavaDoc;
62 import java.util.Map JavaDoc;
63
64 import org.apache.commons.collections.Factory;
65 import org.apache.commons.collections.MapUtils;
66 import org.apache.commons.collections.map.LinkedMap;
67 import org.apache.log4j.Logger;
68 import org.objectstyle.cayenne.DataObject;
69 import org.objectstyle.cayenne.ObjectFactory;
70 import org.objectstyle.cayenne.ObjectId;
71 import org.objectstyle.cayenne.PersistenceState;
72 import org.objectstyle.cayenne.access.DataContext;
73 import org.objectstyle.cayenne.access.ObjectStore;
74 import org.objectstyle.cayenne.access.ToManyList;
75 import org.objectstyle.cayenne.conf.Configuration;
76 import org.objectstyle.cayenne.map.DbRelationship;
77 import org.objectstyle.cayenne.map.ObjEntity;
78 import org.objectstyle.cayenne.map.ObjRelationship;
79 import org.objectstyle.cayenne.query.PrefetchSelectQuery;
80 import org.objectstyle.cayenne.query.Query;
81
82 /**
83  * A tree structure that allows to resolve prefetch query dependencies and build an object
84  * tree out of multiple query results.
85  *
86  * @since 1.2 moved from the parent class to a standalone class.
87  * @author Andrei Adamchik
88  */

89 class PrefetchResolver {
90
91     private static final Logger logObj = Logger.getLogger(PrefetchResolver.class);
92
93     List JavaDoc dataRows;
94     ObjEntity entity;
95     ObjRelationship incoming;
96     Map JavaDoc children;
97
98     ObjRelationship getIncoming() {
99         return incoming;
100     }
101
102     void setIncoming(ObjRelationship incoming) {
103         this.incoming = incoming;
104     }
105
106     /**
107      * Initializes a prefetch tree for the map of query results.
108      */

109     void buildTree(ObjEntity entity, Query rootQuery, Map JavaDoc resultsByQuery) {
110         this.entity = entity;
111
112         // add children
113
Iterator JavaDoc it = resultsByQuery.entrySet().iterator();
114
115         while (it.hasNext()) {
116             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
117
118             Query query = (Query) entry.getKey();
119             List JavaDoc dataRows = (List JavaDoc) entry.getValue();
120
121             if (dataRows == null) {
122                 logObj.warn("Can't find prefetch results for query: " + query);
123                 continue;
124
125                 // ignore null result (this shouldn't happen), however do not ignore
126
// empty result, since it should be used to
127
// update the source objects...
128
}
129
130             if (rootQuery == query) {
131                 this.dataRows = dataRows;
132                 continue;
133             }
134
135             // add prefetch queries to the tree
136
if (query instanceof PrefetchSelectQuery) {
137                 PrefetchSelectQuery prefetchQuery = (PrefetchSelectQuery) query;
138
139                 if (prefetchQuery.getParentQuery() == rootQuery) {
140                     addChildWithPath(prefetchQuery.getPrefetchPath(), dataRows);
141                 }
142             }
143         }
144     }
145
146     /**
147      * Adds a (possibly indirect) child to this node.
148      */

149     PrefetchResolver addChildWithPath(String JavaDoc prefetchPath, List JavaDoc dataRows) {
150         Iterator JavaDoc it = entity.resolvePathComponents(prefetchPath);
151
152         if (!it.hasNext()) {
153             return null;
154         }
155
156         PrefetchResolver lastChild = this;
157
158         while (it.hasNext()) {
159             ObjRelationship r = (ObjRelationship) it.next();
160             lastChild = lastChild.addChild(r);
161         }
162
163         lastChild.dataRows = dataRows;
164         return lastChild;
165     }
166
167     /**
168      * Adds a direct child to this node.
169      */

170     PrefetchResolver addChild(ObjRelationship outgoing) {
171         PrefetchResolver child = null;
172
173         if (children == null) {
174             children = new LinkedMap();
175         }
176         else {
177             child = (PrefetchResolver) children.get(outgoing.getName());
178         }
179
180         if (child == null) {
181             child = new PrefetchResolver();
182             child.setIncoming(outgoing);
183             children.put(outgoing.getName(), child);
184         }
185
186         return child;
187     }
188
189     /**
190      * Recursively resolves a hierarchy of prefetched data rows.
191      */

192     List JavaDoc resolveObjectTree(ObjectFactory factory) {
193
194         // resolve the tree recursively...
195
List JavaDoc objects = resolveObjectTree(factory, null, false);
196         return (objects != null) ? objects : new ArrayList JavaDoc(1);
197     }
198
199     /**
200      * Recursively resolves a hierarchy of prefetched data rows starting from a child tree
201      * node. Allows to skip the resolution of this node and only do children. This is
202      * useful in chaining various prefetch resolving strategies.
203      */

204     List JavaDoc resolveObjectTree(ObjectFactory factory, List JavaDoc parentObjects, boolean skipSelf) {
205
206         List JavaDoc objects = null;
207
208         // skip most operations on a "phantom" node that had no prefetch query
209
if (!skipSelf && dataRows != null) {
210             ObjEntity entity = (incoming != null) ? (ObjEntity) incoming
211                     .getTargetEntity() : this.entity;
212
213             // resolve objects;
214
objects = factory.objectsFromDataRows(entity, dataRows);
215
216             // connect to parent - to-many only, as to-one are connected by Cayenne
217
// automatically.
218
if (incoming != null && incoming.isToMany()) {
219
220                 Map JavaDoc partitioned = partitionBySource(objects);
221
222                 // depending on whether parent is a "phantom" node,
223
// use different strategy
224

225                 if (parentObjects != null && parentObjects.size() > 0) {
226                     connectToNodeParents(parentObjects, partitioned);
227                 }
228                 else {
229                     connectToFaultedParents(partitioned);
230                 }
231             }
232         }
233
234         // resolve children
235
if (children != null) {
236             Iterator JavaDoc it = children.entrySet().iterator();
237             while (it.hasNext()) {
238                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
239                 PrefetchResolver node = (PrefetchResolver) entry.getValue();
240                 node.resolveObjectTree(factory, objects, false);
241             }
242         }
243
244         return objects;
245     }
246
247     void connectToNodeParents(List JavaDoc parentObjects, Map JavaDoc partitioned) {
248
249         // destinationObjects has now been partitioned into a list per
250
// source object... Now init their "toMany"
251

252         Iterator JavaDoc it = parentObjects.iterator();
253         while (it.hasNext()) {
254             DataObject root = (DataObject) it.next();
255             List JavaDoc related = (List JavaDoc) partitioned.get(root);
256
257             if (related == null) {
258                 related = new ArrayList JavaDoc(1);
259             }
260
261             ToManyList toManyList = (ToManyList) root.readProperty(incoming.getName());
262             toManyList.setObjectList(related);
263         }
264     }
265
266     void connectToFaultedParents(Map JavaDoc partitioned) {
267         Iterator JavaDoc it = partitioned.entrySet().iterator();
268         while (it.hasNext()) {
269             Map.Entry JavaDoc entry = (Map.Entry JavaDoc) it.next();
270
271             DataObject root = (DataObject) entry.getKey();
272             List JavaDoc related = (List JavaDoc) entry.getValue();
273
274             ToManyList toManyList = (ToManyList) root.readProperty(incoming.getName());
275
276             // TODO: if a list is modified, should we
277
// merge to-many instead of simply overwriting it?
278
toManyList.setObjectList(related);
279         }
280     }
281
282     /**
283      * Organizes this node objects in a map keyed by the source related object for the
284      * node "incoming" relationship.
285      */

286     Map JavaDoc partitionBySource(List JavaDoc objects) {
287         Class JavaDoc sourceObjectClass = ((ObjEntity) incoming.getSourceEntity())
288                 .getJavaClass(Configuration.getResourceLoader());
289         ObjRelationship reverseRelationship = incoming.getReverseRelationship();
290
291         // Might be used later on... obtain and cast only once
292
DbRelationship dbRelationship = (DbRelationship) incoming
293                 .getDbRelationships()
294                 .get(0);
295
296         Factory listFactory = new Factory() {
297
298             public Object JavaDoc create() {
299                 return new ArrayList JavaDoc();
300             }
301         };
302
303         Map JavaDoc toManyLists = MapUtils.lazyMap(new HashMap JavaDoc(), listFactory);
304         Iterator JavaDoc destIterator = objects.iterator();
305         while (destIterator.hasNext()) {
306             DataObject destinationObject = (DataObject) destIterator.next();
307             DataObject sourceObject = null;
308             if (reverseRelationship != null) {
309                 sourceObject = (DataObject) destinationObject
310                         .readProperty(reverseRelationship.getName());
311             }
312             else {
313                 // Reverse relationship doesn't exist... match objects manually
314
DataContext context = destinationObject.getDataContext();
315                 ObjectStore objectStore = context.getObjectStore();
316
317                 Map JavaDoc sourcePk = dbRelationship.srcPkSnapshotWithTargetSnapshot(objectStore
318                         .getSnapshot(destinationObject.getObjectId(), context));
319
320                 // if object does not exist yet, don't create it
321
// the reason for its absense is likely due to the absent intermediate
322
// prefetch
323
sourceObject = objectStore.getObject(new ObjectId(
324                         sourceObjectClass,
325                         sourcePk));
326             }
327
328             // don't attach to hollow objects
329
if (sourceObject != null
330                     && sourceObject.getPersistenceState() != PersistenceState.HOLLOW) {
331                 List JavaDoc relatedObjects = (List JavaDoc) toManyLists.get(sourceObject);
332                 relatedObjects.add(destinationObject);
333             }
334         }
335
336         return toManyLists;
337     }
338 }
Popular Tags