1 19 20 package org.apache.cayenne.access; 21 22 import java.util.Collection ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 26 import org.apache.cayenne.ObjectContext; 27 import org.apache.cayenne.PersistenceState; 28 import org.apache.cayenne.Persistent; 29 import org.apache.cayenne.QueryResponse; 30 import org.apache.cayenne.cache.QueryCache; 31 import org.apache.cayenne.query.ObjectIdQuery; 32 import org.apache.cayenne.query.Query; 33 import org.apache.cayenne.query.RefreshQuery; 34 import org.apache.cayenne.util.ListResponse; 35 import org.apache.cayenne.util.ObjectContextQueryAction; 36 37 44 class DataContextQueryAction extends ObjectContextQueryAction { 47 48 public DataContextQueryAction(DataContext actingContext, ObjectContext targetContext, 49 Query query) { 50 super(actingContext, targetContext, query); 51 } 52 53 public QueryResponse execute() { 54 if (interceptPaginatedQuery() != DONE) { 55 if (interceptOIDQuery() != DONE) { 56 if (interceptRelationshipQuery() != DONE) { 57 if (interceptRefreshQuery() != DONE) { 58 if (interceptLocalCache() != DONE) { 59 runQuery(); 60 } 61 } 62 } 63 } 64 } 65 66 interceptObjectConversion(); 67 return response; 68 } 69 70 73 protected boolean interceptOIDQuery() { 74 if (query instanceof ObjectIdQuery) { 75 ObjectIdQuery oidQuery = (ObjectIdQuery) query; 76 77 if (!oidQuery.isFetchMandatory()) { 78 Object object = actingContext.getGraphManager().getNode( 79 oidQuery.getObjectId()); 80 if (object != null) { 81 82 if (oidQuery.isFetchingDataRows()) { 88 object = ((DataContext) actingContext) 89 .currentSnapshot((Persistent) object); 90 } 91 else if (((Persistent) object).getPersistenceState() == PersistenceState.HOLLOW) { 93 return !DONE; 94 } 95 96 this.response = new ListResponse(object); 97 return DONE; 98 } 99 } 100 } 101 102 return !DONE; 103 } 104 105 private boolean interceptPaginatedQuery() { 106 if (metadata.getPageSize() > 0) { 107 response = new ListResponse(new IncrementalFaultList( 108 (DataContext) actingContext, 109 query)); 110 return DONE; 111 } 112 113 return !DONE; 114 } 115 116 protected QueryCache getQueryCache() { 117 return ((DataContext) actingContext).getQueryCache(); 118 } 119 120 private boolean interceptRefreshQuery() { 121 if (query instanceof RefreshQuery) { 122 RefreshQuery refreshQuery = (RefreshQuery) query; 123 124 DataContext context = (DataContext) actingContext; 125 126 129 if (refreshQuery.isRefreshAll()) { 131 synchronized (context.getObjectStore()) { 132 133 invalidateLocally(context.getObjectStore(), context 134 .getObjectStore() 135 .getObjectIterator()); 136 137 context.getQueryCache().clear(); 138 } 139 140 return !DONE; 142 } 143 144 Collection objects = refreshQuery.getObjects(); 146 if (objects != null && !objects.isEmpty()) { 147 148 synchronized (context.getObjectStore()) { 149 invalidateLocally(context.getObjectStore(), objects.iterator()); 150 } 151 152 return !DONE; 154 } 155 156 Query cachedQuery = refreshQuery.getQuery(); 158 if (cachedQuery != null) { 159 160 String cacheKey = cachedQuery 161 .getMetaData(context.getEntityResolver()) 162 .getCacheKey(); 163 context.getQueryCache().remove(cacheKey); 164 165 this.response = context.performGenericQuery(cachedQuery); 166 167 return DONE; 169 } 170 171 String [] groups = refreshQuery.getGroupKeys(); 173 if (groups != null && groups.length > 0) { 174 175 for (int i = 0; i < groups.length; i++) { 176 context.getQueryCache().removeGroup(groups[i]); 177 } 178 179 return !DONE; 181 } 182 183 return DONE; 185 } 186 187 return !DONE; 188 } 189 190 private void invalidateLocally(ObjectStore objectStore, Iterator it) { 191 Map diffMap = objectStore.getChangesByObjectId(); 192 193 while (it.hasNext()) { 194 Persistent object = (Persistent) it.next(); 195 196 int state = object.getPersistenceState(); 197 198 if (state == PersistenceState.NEW) { 202 continue; 203 } 204 205 if (state == PersistenceState.MODIFIED || state == PersistenceState.DELETED) { 206 diffMap.remove(object.getObjectId()); 208 } 209 210 object.setPersistenceState(PersistenceState.HOLLOW); 211 } 212 } 213 } 214 | Popular Tags |