1 56 57 package org.objectstyle.cayenne.access; 58 59 import java.util.Collections ; 60 import java.util.List ; 61 62 import org.objectstyle.cayenne.CayenneRuntimeException; 63 import org.objectstyle.cayenne.ObjectFactory; 64 import org.objectstyle.cayenne.access.util.SelectObserver; 65 import org.objectstyle.cayenne.map.ObjEntity; 66 import org.objectstyle.cayenne.query.GenericSelectQuery; 67 68 74 class DataContextSelectAction { 75 76 DataContext context; 77 78 DataContextSelectAction(DataContext context) { 79 this.context = context; 80 } 81 82 List performQuery(GenericSelectQuery query) { 83 return performQuery(query, query.getName(), query.isRefreshingObjects()); 84 } 85 86 List performQuery(GenericSelectQuery query, String cacheKey, boolean refreshCache) { 87 88 if (query.getPageSize() > 0) { 91 return new IncrementalFaultList(context, query); 92 } 93 94 boolean localCache = GenericSelectQuery.LOCAL_CACHE 95 .equals(query.getCachePolicy()); 96 boolean sharedCache = GenericSelectQuery.SHARED_CACHE.equals(query 97 .getCachePolicy()); 98 boolean useCache = localCache || sharedCache; 99 100 String name = query.getName(); 101 102 if (useCache && name == null) { 104 throw new CayenneRuntimeException( 105 "Caching of unnamed queries is not supported."); 106 } 107 108 if (!refreshCache && useCache) { 110 List results = null; 111 112 if (localCache) { 113 results = context.getObjectStore().getCachedQueryResult(cacheKey); 116 } 117 else if (sharedCache) { 118 119 List rows = context 120 .getObjectStore() 121 .getDataRowCache() 122 .getCachedSnapshots(cacheKey); 123 if (rows != null) { 124 125 if (rows.size() == 0) { 128 results = Collections.EMPTY_LIST; 129 } 130 else if (query.isFetchingDataRows()) { 131 results = Collections.unmodifiableList(rows); 132 } 133 else { 134 ObjEntity root = context.getEntityResolver().lookupObjEntity( 135 query); 136 results = context.objectsFromDataRows(root, rows, query 137 .isRefreshingObjects(), query.isResolvingInherited()); 138 } 139 } 140 } 141 142 if (results != null) { 143 return results; 144 } 145 } 146 147 SelectObserver observer = new SelectObserver(query.getLoggingLevel()); 149 context.performQueries(Collections.singleton(query), observer); 150 151 List results; 152 153 if (query.isFetchingDataRows()) { 154 results = observer.getResults(query); 155 } 156 else { 157 ObjectFactory factory = new DataContextObjectFactory(context, query 158 .isRefreshingObjects(), query.isResolvingInherited()); 159 ObjEntity rootEntity = context.getEntityResolver().lookupObjEntity(query); 160 results = observer.getResultsAsObjects(factory, rootEntity, query); 161 } 162 163 if (useCache) { 165 if (localCache) { 166 context.getObjectStore().cacheQueryResult(cacheKey, results); 167 } 168 else if (sharedCache) { 169 context.getObjectStore().getDataRowCache().cacheSnapshots( 170 cacheKey, 171 observer.getResults(query)); 172 } 173 } 174 175 return results; 176 } 177 } | Popular Tags |