1 56 package org.objectstyle.cayenne.access.util; 57 58 import java.util.ArrayList ; 59 import java.util.Collection ; 60 import java.util.Collections ; 61 import java.util.Iterator ; 62 import java.util.List ; 63 import java.util.Map ; 64 65 import org.objectstyle.cayenne.DataObject; 66 import org.objectstyle.cayenne.DataRow; 67 import org.objectstyle.cayenne.ObjectFactory; 68 69 76 final class FlatPrefetchResolver { 77 78 ObjectFactory factory; 79 80 83 FlatPrefetchResolver(ObjectFactory factory) { 84 this.factory = factory; 85 } 86 87 90 List resolveObjectTree(FlatPrefetchTreeNode rootNode, List flatRows) { 91 93 int capacity = flatRows.size(); 96 if (capacity > 100) { 97 capacity = capacity / 2; 98 } 99 100 List results = new ArrayList (capacity); 101 102 int len = flatRows.size(); 104 for (int i = 0; i < len; i++) { 105 106 DataRow flatRow = (DataRow) flatRows.get(i); 107 108 110 DataObject object = resolveRow(flatRow, rootNode, null); 111 112 if (object != null) { 114 results.add(object); 115 } 116 } 117 118 rootNode.connectToParents(); 120 121 return results; 122 } 123 124 129 private DataObject resolveRow( 130 DataRow flatRow, 131 FlatPrefetchTreeNode node, 132 DataObject parentObject) { 133 134 boolean existing = true; 135 DataObject object = null; 136 137 if (!node.isPhantom()) { 139 Map 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 node.connectToParent(object, parentObject); 152 } 153 154 Collection children = node.getChildren(); 156 if (children != null) { 157 158 DataObject parentOfChildren = (!node.isPhantom()) ? object : null; 159 160 Iterator it = children.iterator(); 161 while (it.hasNext()) { 162 FlatPrefetchTreeNode child = (FlatPrefetchTreeNode) it.next(); 163 resolveRow(flatRow, child, parentOfChildren); 164 } 165 } 166 167 return (existing) ? null : object; 169 } 170 171 private DataObject objectFromFlatRow(DataRow flatRow, FlatPrefetchTreeNode node) { 172 DataRow row = node.rowFromFlatRow(flatRow); 176 List objects = factory.objectsFromDataRows(node.getEntity(), Collections 177 .singletonList(row)); 178 return (DataObject) objects.get(0); 179 } 180 } | Popular Tags |