1 19 20 package org.apache.cayenne; 21 22 import java.util.Collection ; 23 import java.util.Iterator ; 24 25 import org.apache.cayenne.cache.QueryCacheEntryFactory; 26 import org.apache.cayenne.cache.QueryCache; 27 import org.apache.cayenne.query.Query; 28 import org.apache.cayenne.query.RefreshQuery; 29 import org.apache.cayenne.remote.RemoteIncrementalFaultList; 30 import org.apache.cayenne.util.ListResponse; 31 import org.apache.cayenne.util.ObjectContextQueryAction; 32 33 37 class CayenneContextQueryAction extends ObjectContextQueryAction { 38 39 CayenneContextQueryAction(CayenneContext actingContext, ObjectContext targetContext, 40 Query query) { 41 super(actingContext, targetContext, query); 42 } 43 44 public QueryResponse execute() { 45 46 if (interceptOIDQuery() != DONE) { 47 if (interceptRelationshipQuery() != DONE) { 48 if (interceptRefreshQuery() != DONE) { 49 if (interceptLocalCache() != DONE) { 50 if (interceptPaginatedQuery() != DONE) { 51 runQuery(); 52 } 53 } 54 } 55 } 56 } 57 58 interceptObjectConversion(); 59 return response; 60 } 61 62 private boolean interceptPaginatedQuery() { 63 if (metadata.getPageSize() > 0) { 64 response = new ListResponse(new RemoteIncrementalFaultList( 65 actingContext, 66 query)); 67 return DONE; 68 } 69 70 return !DONE; 71 } 72 73 protected QueryCache getQueryCache() { 74 return ((CayenneContext) actingContext).getQueryCache(); 75 } 76 77 protected QueryCacheEntryFactory getCacheObjectFactory() { 78 return new QueryCacheEntryFactory() { 79 80 public Object createObject() { 81 if (interceptPaginatedQuery() != DONE) { 82 runQuery(); 83 } 84 return response.firstList(); 85 } 86 }; 87 } 88 89 private boolean interceptRefreshQuery() { 90 if (query instanceof RefreshQuery) { 91 RefreshQuery refreshQuery = (RefreshQuery) query; 92 93 CayenneContext context = (CayenneContext) actingContext; 94 95 98 if (refreshQuery.isRefreshAll()) { 100 101 invalidateLocally(context.internalGraphManager(), context 102 .internalGraphManager() 103 .registeredNodes() 104 .iterator()); 105 context.getQueryCache().clear(); 106 107 return !DONE; 109 } 110 111 Collection objects = refreshQuery.getObjects(); 113 if (objects != null && !objects.isEmpty()) { 114 115 invalidateLocally(context.internalGraphManager(), objects.iterator()); 116 117 return !DONE; 119 } 120 121 if (refreshQuery.getQuery() != null) { 123 Query cachedQuery = refreshQuery.getQuery(); 124 125 String cacheKey = cachedQuery 126 .getMetaData(context.getEntityResolver()) 127 .getCacheKey(); 128 context.getQueryCache().remove(cacheKey); 129 130 this.response = context.performGenericQuery(cachedQuery); 131 132 return DONE; 134 } 135 136 if (refreshQuery.getGroupKeys() != null 138 && refreshQuery.getGroupKeys().length > 0) { 139 140 String [] groups = refreshQuery.getGroupKeys(); 141 for (int i = 0; i < groups.length; i++) { 142 context.getQueryCache().removeGroup(groups[i]); 143 } 144 145 return !DONE; 147 } 148 } 149 150 return !DONE; 151 } 152 153 private void invalidateLocally(CayenneContextGraphManager graphManager, Iterator it) { 154 while (it.hasNext()) { 155 Persistent object = (Persistent) it.next(); 156 157 if (object.getPersistenceState() == PersistenceState.NEW) { 161 continue; 162 } 163 164 object.setPersistenceState(PersistenceState.HOLLOW); 165 166 graphManager.changeLog.unregisterNode(object.getObjectId()); 168 graphManager.stateLog.unregisterNode(object.getObjectId()); 169 } 170 } 171 } 172 | Popular Tags |