1 56 package org.objectstyle.cayenne.service; 57 58 import java.util.List ; 59 60 import org.objectstyle.cayenne.CayenneRuntimeException; 61 import org.objectstyle.cayenne.ObjectFactory; 62 import org.objectstyle.cayenne.access.DataContextObjectFactory; 63 import org.objectstyle.cayenne.access.IncrementalFaultList; 64 import org.objectstyle.cayenne.access.util.SelectObserver; 65 import org.objectstyle.cayenne.map.ObjEntity; 66 import org.objectstyle.cayenne.query.GenericSelectQuery; 67 import org.objectstyle.cayenne.query.Query; 68 import org.objectstyle.cayenne.query.QueryExecutionPlan; 69 70 76 class ObjectDataContextSelectAction { 79 80 ObjectDataContext context; 81 82 ObjectDataContextSelectAction(ObjectDataContext context) { 83 this.context = context; 84 } 85 86 List performQuery(QueryExecutionPlan queryPlan) { 87 GenericSelectQuery selectQuery = resolveQuery(queryPlan); 88 89 if (selectQuery.getPageSize() > 0) { 92 return new IncrementalFaultList(context, selectQuery); 93 } 94 95 String cacheKey = selectQuery.getName(); 96 boolean cacheResults = GenericSelectQuery.LOCAL_CACHE.equals(selectQuery 97 .getCachePolicy()); 98 99 if (cacheResults) { 101 102 if (cacheKey == null) { 104 throw new CayenneRuntimeException( 105 "Caching of unnamed queries is not supported."); 106 } 107 108 List results = context.getObjectStore().getCachedQueryResult(cacheKey); 111 if (results != null) { 112 return results; 113 } 114 } 115 116 SelectObserver observer = new SelectObserver(selectQuery.getLoggingLevel()); 118 context.getParentContext().performQuery(selectQuery, observer); 119 120 List results; 121 122 if (selectQuery.isFetchingDataRows()) { 123 results = observer.getResults(selectQuery); 124 } 125 else { 126 ObjectFactory factory = new DataContextObjectFactory(context, selectQuery 127 .isRefreshingObjects(), selectQuery.isResolvingInherited()); 128 ObjEntity rootEntity = context.getEntityResolver().lookupObjEntity( 129 selectQuery); 130 results = observer.getResultsAsObjects(factory, rootEntity, selectQuery); 131 } 132 133 if (cacheResults) { 135 context.getObjectStore().cacheQueryResult(cacheKey, results); 136 } 137 138 return results; 139 } 140 141 144 GenericSelectQuery resolveQuery(QueryExecutionPlan queryPlan) { 145 Query resolved = queryPlan.resolve(context.getEntityResolver()); 146 147 if (!(resolved instanceof GenericSelectQuery)) { 148 throw new CayenneRuntimeException( 149 "QueryExecutionPlan was resolved to a query that is not a GenericSelectQuery: " 150 + resolved); 151 } 152 153 return (GenericSelectQuery) resolved; 154 } 155 } 156 | Popular Tags |