1 56 57 package org.objectstyle.cayenne.access.util; 58 59 import java.util.Collection ; 60 import java.util.HashMap ; 61 import java.util.List ; 62 import java.util.Map ; 63 64 import org.apache.log4j.Level; 65 import org.objectstyle.cayenne.CayenneRuntimeException; 66 import org.objectstyle.cayenne.ObjectFactory; 67 import org.objectstyle.cayenne.access.DataContext; 68 import org.objectstyle.cayenne.access.DataContextObjectFactory; 69 import org.objectstyle.cayenne.access.QueryLogger; 70 import org.objectstyle.cayenne.exp.Expression; 71 import org.objectstyle.cayenne.map.ObjEntity; 72 import org.objectstyle.cayenne.query.GenericSelectQuery; 73 import org.objectstyle.cayenne.query.QualifiedQuery; 74 import org.objectstyle.cayenne.query.Query; 75 import org.objectstyle.cayenne.util.Util; 76 77 95 public class SelectObserver extends DefaultOperationObserver { 96 97 protected Map results = new HashMap (); 98 protected int selectCount; 99 100 public SelectObserver() { 101 this(QueryLogger.DEFAULT_LOG_LEVEL); 102 } 103 104 public SelectObserver(Level logLevel) { 105 super.setLoggingLevel(logLevel); 106 } 107 108 112 public int getSelectCount() { 113 return selectCount; 114 } 115 116 120 public List getResults(Query q) { 121 return (List ) results.get(q); 122 } 123 124 128 public Map getResults() { 129 return results; 130 } 131 132 133 public void clear() { 134 selectCount = 0; 135 results.clear(); 136 } 137 138 141 public void nextDataRows(Query query, List dataRows) { 142 143 if (dataRows != null) { 144 results.put(query, dataRows); 145 } 146 147 selectCount++; 148 } 149 150 154 public List getResultsAsObjects(DataContext dataContext, Query rootQuery) { 155 156 if (!(rootQuery instanceof GenericSelectQuery)) { 157 throw new CayenneRuntimeException("Expected GenericSelectQuery, got: " 158 + rootQuery); 159 } 160 161 GenericSelectQuery selectQuery = (GenericSelectQuery) rootQuery; 162 163 ObjectFactory factory = new DataContextObjectFactory(dataContext, selectQuery 164 .isRefreshingObjects(), selectQuery.isResolvingInherited()); 165 ObjEntity rootEntity = dataContext.getEntityResolver().lookupObjEntity(rootQuery); 166 return getResultsAsObjects(factory, rootEntity, selectQuery); 167 } 168 169 186 public List getResultsAsObjects( 187 ObjectFactory factory, 188 ObjEntity rootEntity, 189 GenericSelectQuery rootQuery) { 190 191 if (rootEntity == null) { 193 throw new CayenneRuntimeException( 194 "Can't instantiate DataObjects from resutls. ObjEntity is undefined for query: " 195 + rootQuery); 196 } 197 198 PrefetchResolver tree = new PrefetchResolver(); 202 tree.buildTree(rootEntity, rootQuery, results); 203 204 Collection jointPrefetches = rootQuery.getJointPrefetches(); 205 if (!jointPrefetches.isEmpty()) { 206 207 Expression qualifier = null; 211 if (rootQuery instanceof QualifiedQuery) { 212 qualifier = ((QualifiedQuery) rootQuery).getQualifier(); 213 } 214 215 FlatPrefetchTreeNode flatPrefetchTree = new FlatPrefetchTreeNode( 216 rootEntity, 217 jointPrefetches, 218 qualifier); 219 220 FlatPrefetchResolver flatPrefetchResolver = new FlatPrefetchResolver(factory); 221 222 List objects = flatPrefetchResolver.resolveObjectTree(flatPrefetchTree, 223 getResults(rootQuery)); 224 225 tree.resolveObjectTree(factory, objects, true); 228 return objects; 229 } 230 231 return tree.resolveObjectTree(factory); 232 } 233 234 237 public void nextQueryException(Query query, Exception ex) { 238 super.nextQueryException(query, ex); 239 throw new CayenneRuntimeException("Query exception.", Util.unwindException(ex)); 240 } 241 242 245 public void nextGlobalException(Exception ex) { 246 super.nextGlobalException(ex); 247 throw new CayenneRuntimeException("Global exception.", Util.unwindException(ex)); 248 } 249 } | Popular Tags |