1 19 20 package org.apache.cayenne.query; 21 22 import java.io.Serializable ; 23 import java.util.Collection ; 24 import java.util.Collections ; 25 import java.util.Iterator ; 26 import java.util.Map ; 27 import java.util.StringTokenizer ; 28 29 import org.apache.cayenne.CayenneRuntimeException; 30 import org.apache.cayenne.Persistent; 31 import org.apache.cayenne.map.DataMap; 32 import org.apache.cayenne.map.DbEntity; 33 import org.apache.cayenne.map.EntityResolver; 34 import org.apache.cayenne.map.ObjEntity; 35 import org.apache.cayenne.map.Procedure; 36 import org.apache.cayenne.reflect.ClassDescriptor; 37 import org.apache.cayenne.util.Util; 38 import org.apache.cayenne.util.XMLEncoder; 39 import org.apache.cayenne.util.XMLSerializable; 40 41 47 class BaseQueryMetadata implements QueryMetadata, XMLSerializable, Serializable { 48 49 int fetchLimit = QueryMetadata.FETCH_LIMIT_DEFAULT; 50 int pageSize = QueryMetadata.PAGE_SIZE_DEFAULT; 51 boolean fetchingDataRows = QueryMetadata.FETCHING_DATA_ROWS_DEFAULT; 52 boolean refreshingObjects = QueryMetadata.REFRESHING_OBJECTS_DEFAULT; 53 boolean resolvingInherited = QueryMetadata.RESOLVING_INHERITED_DEFAULT; 54 String cachePolicy = QueryMetadata.CACHE_POLICY_DEFAULT; 55 56 PrefetchTreeNode prefetchTree; 57 String cacheKey; 58 String [] cacheGroups; 59 60 transient DbEntity dbEntity; 61 transient DataMap dataMap; 62 transient Object lastRoot; 63 transient ClassDescriptor classDescriptor; 64 transient EntityResolver lastEntityResolver; 65 66 69 void copyFromInfo(QueryMetadata info) { 70 this.lastEntityResolver = null; 71 this.lastRoot = null; 72 this.classDescriptor = null; 73 this.dbEntity = null; 74 this.dataMap = null; 75 76 this.fetchingDataRows = info.isFetchingDataRows(); 77 this.fetchLimit = info.getFetchLimit(); 78 this.pageSize = info.getPageSize(); 79 this.refreshingObjects = info.isRefreshingObjects(); 80 this.resolvingInherited = info.isResolvingInherited(); 81 this.cachePolicy = info.getCachePolicy(); 82 this.cacheKey = info.getCacheKey(); 83 84 setPrefetchTree(info.getPrefetchTree()); 85 } 86 87 boolean resolve(Object root, EntityResolver resolver, String cacheKey) { 88 89 if (lastRoot != root || lastEntityResolver != resolver) { 90 91 this.cacheKey = cacheKey; 92 93 this.classDescriptor = null; 94 this.dbEntity = null; 95 this.dataMap = null; 96 97 ObjEntity entity = null; 98 99 if (root != null) { 100 if (root instanceof Class ) { 101 entity = resolver.lookupObjEntity((Class ) root); 102 103 if (entity != null) { 104 this.dbEntity = entity.getDbEntity(); 105 this.dataMap = entity.getDataMap(); 106 } 107 } 108 else if (root instanceof ObjEntity) { 109 entity = (ObjEntity) root; 110 this.dbEntity = entity.getDbEntity(); 111 this.dataMap = entity.getDataMap(); 112 } 113 else if (root instanceof String ) { 114 entity = resolver.getObjEntity((String ) root); 115 if (entity != null) { 116 this.dbEntity = entity.getDbEntity(); 117 this.dataMap = entity.getDataMap(); 118 } 119 } 120 else if (root instanceof DbEntity) { 121 this.dbEntity = (DbEntity) root; 122 this.dataMap = dbEntity.getDataMap(); 123 } 124 else if (root instanceof DataMap) { 125 this.dataMap = (DataMap) root; 126 } 127 else if (root instanceof Persistent) { 128 entity = resolver.lookupObjEntity((Persistent) root); 129 if (entity != null) { 130 this.dbEntity = entity.getDbEntity(); 131 this.dataMap = entity.getDataMap(); 132 } 133 } 134 } 135 136 if (entity != null) { 137 this.classDescriptor = resolver.getClassDescriptor(entity.getName()); 138 } 139 140 this.lastRoot = root; 141 this.lastEntityResolver = resolver; 142 return true; 143 } 144 145 return false; 146 } 147 148 void initWithProperties(Map properties) { 149 if (properties == null) { 151 properties = Collections.EMPTY_MAP; 152 } 153 154 Object fetchLimit = properties.get(QueryMetadata.FETCH_LIMIT_PROPERTY); 155 Object pageSize = properties.get(QueryMetadata.PAGE_SIZE_PROPERTY); 156 Object refreshingObjects = properties 157 .get(QueryMetadata.REFRESHING_OBJECTS_PROPERTY); 158 Object fetchingDataRows = properties 159 .get(QueryMetadata.FETCHING_DATA_ROWS_PROPERTY); 160 161 Object resolvingInherited = properties 162 .get(QueryMetadata.RESOLVING_INHERITED_PROPERTY); 163 164 Object cachePolicy = properties.get(QueryMetadata.CACHE_POLICY_PROPERTY); 165 Object cacheGroups = properties.get(QueryMetadata.CACHE_GROUPS_PROPERTY); 166 167 this.fetchLimit = (fetchLimit != null) 169 ? Integer.parseInt(fetchLimit.toString()) 170 : QueryMetadata.FETCH_LIMIT_DEFAULT; 171 172 this.pageSize = (pageSize != null) 173 ? Integer.parseInt(pageSize.toString()) 174 : QueryMetadata.PAGE_SIZE_DEFAULT; 175 176 this.refreshingObjects = (refreshingObjects != null) 177 ? "true".equalsIgnoreCase(refreshingObjects.toString()) 178 : QueryMetadata.REFRESHING_OBJECTS_DEFAULT; 179 180 this.fetchingDataRows = (fetchingDataRows != null) 181 ? "true".equalsIgnoreCase(fetchingDataRows.toString()) 182 : QueryMetadata.FETCHING_DATA_ROWS_DEFAULT; 183 184 this.resolvingInherited = (resolvingInherited != null) 185 ? "true".equalsIgnoreCase(resolvingInherited.toString()) 186 : QueryMetadata.RESOLVING_INHERITED_DEFAULT; 187 188 this.cachePolicy = (cachePolicy != null) 189 ? cachePolicy.toString() 190 : QueryMetadata.CACHE_POLICY_DEFAULT; 191 192 this.cacheGroups = null; 193 if (cacheGroups instanceof String []) { 194 this.cacheGroups = (String []) cacheGroups; 195 } 196 else if (cacheGroups instanceof String ) { 197 StringTokenizer toks = new StringTokenizer (cacheGroups.toString(), ","); 198 this.cacheGroups = new String [toks.countTokens()]; 199 for (int i = 0; i < this.cacheGroups.length; i++) { 200 this.cacheGroups[i] = toks.nextToken(); 201 } 202 } 203 } 204 205 public void encodeAsXML(XMLEncoder encoder) { 206 if (refreshingObjects != QueryMetadata.REFRESHING_OBJECTS_DEFAULT) { 207 encoder.printProperty( 208 QueryMetadata.REFRESHING_OBJECTS_PROPERTY, 209 refreshingObjects); 210 } 211 212 if (fetchingDataRows != QueryMetadata.FETCHING_DATA_ROWS_DEFAULT) { 213 encoder.printProperty( 214 QueryMetadata.FETCHING_DATA_ROWS_PROPERTY, 215 fetchingDataRows); 216 } 217 218 if (resolvingInherited != QueryMetadata.RESOLVING_INHERITED_DEFAULT) { 219 encoder.printProperty( 220 QueryMetadata.RESOLVING_INHERITED_PROPERTY, 221 resolvingInherited); 222 } 223 224 if (fetchLimit != QueryMetadata.FETCH_LIMIT_DEFAULT) { 225 encoder.printProperty(QueryMetadata.FETCH_LIMIT_PROPERTY, fetchLimit); 226 } 227 228 if (pageSize != QueryMetadata.PAGE_SIZE_DEFAULT) { 229 encoder.printProperty(QueryMetadata.PAGE_SIZE_PROPERTY, pageSize); 230 } 231 232 if (cachePolicy != null 233 && !QueryMetadata.CACHE_POLICY_DEFAULT.equals(cachePolicy)) { 234 encoder.printProperty(QueryMetadata.CACHE_POLICY_PROPERTY, cachePolicy); 235 } 236 237 if (prefetchTree != null) { 238 prefetchTree.encodeAsXML(encoder); 239 } 240 241 if (cacheGroups != null && cacheGroups.length > 0) { 242 StringBuffer buffer = new StringBuffer (cacheGroups[0]); 243 for (int i = 1; i < cacheGroups.length; i++) { 244 buffer.append(',').append(cacheGroups[i]); 245 } 246 encoder.printProperty(QueryMetadata.CACHE_GROUPS_PROPERTY, cachePolicy); 247 } 248 } 249 250 253 public String getCacheKey() { 254 return cacheKey; 255 } 256 257 260 public DataMap getDataMap() { 261 return dataMap; 262 } 263 264 267 public Procedure getProcedure() { 268 return null; 269 } 270 271 274 public DbEntity getDbEntity() { 275 return dbEntity; 276 } 277 278 281 public ObjEntity getObjEntity() { 282 return classDescriptor != null ? classDescriptor.getEntity() : null; 283 } 284 285 288 public ClassDescriptor getClassDescriptor() { 289 return classDescriptor; 290 } 291 292 297 public SQLResultSetMapping getResultSetMapping() { 298 return null; 299 } 300 301 304 public PrefetchTreeNode getPrefetchTree() { 305 return prefetchTree; 306 } 307 308 void setPrefetchTree(PrefetchTreeNode prefetchTree) { 309 if (prefetchTree != null) { 310 try { 313 prefetchTree = (PrefetchTreeNode) Util 314 .cloneViaSerialization(prefetchTree); 315 } 316 catch (CayenneRuntimeException e) { 317 throw e; 318 } 319 catch (Exception e) { 320 throw new CayenneRuntimeException("Error cloning prefetch tree", e); 321 } 322 } 323 324 this.prefetchTree = prefetchTree; 325 } 326 327 public String getCachePolicy() { 328 return cachePolicy; 329 } 330 331 void setCachePolicy(String policy) { 332 this.cachePolicy = policy; 333 } 334 335 338 public String [] getCacheGroups() { 339 return cacheGroups; 340 } 341 342 345 void setCacheGroups(String [] groups) { 346 this.cacheGroups = groups; 347 } 348 349 public boolean isFetchingDataRows() { 350 return fetchingDataRows; 351 } 352 353 public int getFetchLimit() { 354 return fetchLimit; 355 } 356 357 public int getPageSize() { 358 return pageSize; 359 } 360 361 364 public int getFetchStartIndex() { 365 return -1; 366 } 367 368 public boolean isRefreshingObjects() { 369 return refreshingObjects; 370 } 371 372 public boolean isResolvingInherited() { 373 return resolvingInherited; 374 } 375 376 void setFetchingDataRows(boolean b) { 377 fetchingDataRows = b; 378 } 379 380 void setFetchLimit(int i) { 381 fetchLimit = i; 382 } 383 384 void setPageSize(int i) { 385 pageSize = i; 386 } 387 388 void setRefreshingObjects(boolean b) { 389 refreshingObjects = b; 390 } 391 392 void setResolvingInherited(boolean b) { 393 resolvingInherited = b; 394 } 395 396 401 PrefetchTreeNode addPrefetch(String path, int semantics) { 402 if (prefetchTree == null) { 403 prefetchTree = new PrefetchTreeNode(); 404 } 405 406 PrefetchTreeNode node = prefetchTree.addPath(path); 407 node.setSemantics(semantics); 408 node.setPhantom(false); 409 return node; 410 } 411 412 417 void addPrefetches(Collection prefetches, int semantics) { 418 419 if (prefetches != null && !prefetches.isEmpty()) { 420 421 Iterator it = prefetches.iterator(); 422 while (it.hasNext()) { 423 String prefetch = (String ) it.next(); 424 addPrefetch(prefetch, semantics); 425 } 426 } 427 } 428 429 434 void clearPrefetches() { 435 prefetchTree = null; 436 } 437 438 443 void removePrefetch(String prefetch) { 444 if (prefetchTree != null) { 445 prefetchTree.removePath(prefetch); 446 } 447 } 448 } 449 | Popular Tags |