KickJava   Java API By Example, From Geeks To Geeks.

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


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.Collection JavaDoc;
60 import java.util.Collections JavaDoc;
61 import java.util.Iterator JavaDoc;
62 import java.util.List JavaDoc;
63 import java.util.Map JavaDoc;
64
65 import org.objectstyle.cayenne.DataObject;
66 import org.objectstyle.cayenne.DataRow;
67 import org.objectstyle.cayenne.ObjectFactory;
68
69 /**
70  * Encapsulates resolving of a cartesian product result set. Uses FlatPrefetchTreeNode
71  * tree to obtain each entity metadata and store intermediary results.
72  *
73  * @since 1.2
74  * @author Andrei Adamchik
75  */

76 final class FlatPrefetchResolver {
77
78     ObjectFactory factory;
79
80     /**
81      * Create and configure an operation.
82      */

83     FlatPrefetchResolver(ObjectFactory factory) {
84         this.factory = factory;
85     }
86
87     /**
88      * Main method that runs resloving of the flattened result set.
89      */

90     List JavaDoc resolveObjectTree(FlatPrefetchTreeNode rootNode, List JavaDoc flatRows) {
91         // prepare...
92

93         // list may shrink as a result of duplicates in flattened rows.. so don't
94
// allocate too much space
95
int capacity = flatRows.size();
96         if (capacity > 100) {
97             capacity = capacity / 2;
98         }
99
100         List JavaDoc results = new ArrayList JavaDoc(capacity);
101
102         // run
103
int len = flatRows.size();
104         for (int i = 0; i < len; i++) {
105
106             DataRow flatRow = (DataRow) flatRows.get(i);
107
108             // pass each row through the tree
109

110             DataObject object = resolveRow(flatRow, rootNode, null);
111
112             // null will be returned for duplicates...
113
if (object != null) {
114                 results.add(object);
115             }
116         }
117
118         // assemble fetched objects tree
119
rootNode.connectToParents();
120
121         return results;
122     }
123
124     /**
125      * Recursively resolves a flat row by processing it for each tree node, going
126      * depth-first. Returns a root object of the hierarchy, or null if this object is a
127      * duplicate of a previously resolved one.
128      */

129     private DataObject resolveRow(
130             DataRow flatRow,
131             FlatPrefetchTreeNode node,
132             DataObject parentObject) {
133
134         boolean existing = true;
135         DataObject object = null;
136
137         // resolve for a given node...
138
if (!node.isPhantom()) {
139             // find existing object, if found skip further processing
140
Map JavaDoc id = node.idFromFlatRow(flatRow);
141             object = node.getResolved(id);
142
143             if (object == null) {
144                 existing = false;
145                 object = objectFromFlatRow(flatRow, node);
146                 node.putResolved(id, object);
147             }
148
149             // categorization by parent needed even if an object is already there
150
// (many-to-many case)
151
node.connectToParent(object, parentObject);
152         }
153
154         // recursively resolve for children
155
Collection JavaDoc children = node.getChildren();
156         if (children != null) {
157
158             DataObject parentOfChildren = (!node.isPhantom()) ? object : null;
159
160             Iterator JavaDoc it = children.iterator();
161             while (it.hasNext()) {
162                 FlatPrefetchTreeNode child = (FlatPrefetchTreeNode) it.next();
163                 resolveRow(flatRow, child, parentOfChildren);
164             }
165         }
166
167         // return root object if it was newly resolved
168
return (existing) ? null : object;
169     }
170
171     private DataObject objectFromFlatRow(DataRow flatRow, FlatPrefetchTreeNode node) {
172         // TODO: this should be optimized - DataContext.objectsFromDataRows does
173
// some batching that we should do once instead of N * M times (e.g.
174
// synchronization blocks, etc.)
175
DataRow row = node.rowFromFlatRow(flatRow);
176         List JavaDoc objects = factory.objectsFromDataRows(node.getEntity(), Collections
177                 .singletonList(row));
178         return (DataObject) objects.get(0);
179     }
180 }
Popular Tags