1 package oracle.toplink.essentials.queryframework; 3 4 import java.util.*; 5 import oracle.toplink.essentials.internal.helper.*; 6 import oracle.toplink.essentials.internal.queryframework.*; 7 import oracle.toplink.essentials.exceptions.*; 8 import oracle.toplink.essentials.expressions.*; 9 import oracle.toplink.essentials.internal.sessions.AbstractRecord; 10 import oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl; 11 import oracle.toplink.essentials.internal.sessions.AbstractSession; 12 13 24 public class ReadAllQuery extends ObjectLevelReadQuery { 25 26 27 protected Vector orderByExpressions; 28 29 30 protected ContainerPolicy containerPolicy; 31 32 39 public ReadAllQuery() { 40 super(); 41 this.useCollectionClass(ClassConstants.Vector_class); 42 } 43 44 50 public ReadAllQuery(Class classToRead) { 51 this(); 52 setReferenceClass(classToRead); 53 } 54 55 59 public ReadAllQuery(Class classToRead, Expression selectionCriteria) { 60 this(); 61 setReferenceClass(classToRead); 62 setSelectionCriteria(selectionCriteria); 63 } 64 65 70 public ReadAllQuery(Class classToRead, ExpressionBuilder builder) { 71 this(); 72 this.defaultBuilder = builder; 73 setReferenceClass(classToRead); 74 } 75 76 81 public ReadAllQuery(Class classToRead, Call call) { 82 this(); 83 setReferenceClass(classToRead); 84 setCall(call); 85 } 86 87 91 public ReadAllQuery(ExpressionBuilder builder) { 92 this(); 93 this.defaultBuilder = builder; 94 } 95 96 100 public ReadAllQuery(Call call) { 101 this(); 102 setCall(call); 103 } 104 105 109 public void addAscendingOrdering(String queryKeyName) { 110 addOrdering(getExpressionBuilder().get(queryKeyName).ascending()); 111 } 112 113 117 public void addDescendingOrdering(String queryKeyName) { 118 addOrdering(getExpressionBuilder().get(queryKeyName).descending()); 119 } 120 121 126 public void addOrdering(Expression orderingExpression) { 127 getOrderByExpressions().addElement(orderingExpression); 128 setIsPrepared(false); 130 } 131 132 136 protected Object checkEarlyReturnImpl(AbstractSession session, AbstractRecord translationRow) { 137 if (shouldCheckCacheOnly()) { 139 if (shouldUseWrapperPolicy()) { 141 getContainerPolicy().setElementDescriptor(getDescriptor()); 142 } 143 144 AbstractSession rootSession = session; 149 while (rootSession.isUnitOfWork()) { 150 rootSession = ((UnitOfWorkImpl)rootSession).getParent(); 151 } 152 Vector allCachedVector = rootSession.getIdentityMapAccessor().getAllFromIdentityMap(getSelectionCriteria(), getReferenceClass(), translationRow, getInMemoryQueryIndirectionPolicy(), false); 153 154 if (session.isUnitOfWork()) { 156 allCachedVector = ((UnitOfWorkImpl)session).registerAllObjects(allCachedVector); 157 } 158 159 return getContainerPolicy().buildContainerFromVector(allCachedVector, session); 160 } else { 161 return null; 162 } 163 } 164 165 171 protected DatabaseQuery checkForCustomQuery(AbstractSession session, AbstractRecord translationRow) { 172 checkDescriptor(session); 173 174 if ((!isUserDefined()) && isExpressionQuery() && (getSelectionCriteria() == null) && (!hasOrderByExpressions()) && (getDescriptor().getQueryManager().hasReadAllQuery())) { 176 return getDescriptor().getQueryManager().getReadAllQuery(); 177 } else { 178 return null; 179 } 180 } 181 182 186 public Object clone() { 187 ReadAllQuery cloneQuery = (ReadAllQuery)super.clone(); 188 189 if (hasOrderByExpressions()) { 191 cloneQuery.orderByExpressions = (Vector)getOrderByExpressions().clone(); 192 } 193 cloneQuery.containerPolicy = getContainerPolicy().clone(cloneQuery); 194 195 return cloneQuery; 196 } 197 198 202 protected Object conformResult(Object result, UnitOfWorkImpl unitOfWork, AbstractRecord arguments, boolean buildDirectlyFromRows) { 203 ContainerPolicy cp; 204 IdentityHashtable indexedInterimResult = null; 205 Expression selectionCriteriaClone = null; 206 207 if (getSelectionCriteria() != null) { 208 selectionCriteriaClone = (Expression)getSelectionCriteria().clone(); 209 selectionCriteriaClone.getBuilder().setSession(unitOfWork); 210 selectionCriteriaClone.getBuilder().setQueryClass(getReferenceClass()); 211 } 212 cp = getContainerPolicy(); 213 214 indexedInterimResult = unitOfWork.scanForConformingInstances(selectionCriteriaClone, getReferenceClass(), arguments, this); 224 225 Vector fromDatabase = null; 230 231 if (buildDirectlyFromRows) { 235 Vector rows = (Vector)result; 236 Set identitySet = null; 237 fromDatabase = new Vector(rows.size()); 238 for (int i = 0; i < rows.size(); i++) { 239 Object object = rows.elementAt(i); 240 241 if (object != null) { 243 Object clone = conformIndividualResult(object, unitOfWork, arguments, selectionCriteriaClone, indexedInterimResult, buildDirectlyFromRows); 244 if (clone != null) { 245 if (getJoinedAttributeManager().isToManyJoin()) { 247 if (identitySet == null) { 248 identitySet = new TopLinkIdentityHashSet(rows.size()); 249 } 250 if (!identitySet.contains(clone)) { 251 identitySet.add(clone); 252 fromDatabase.addElement(clone); 253 } 254 } else { 255 fromDatabase.addElement(clone); 256 } 257 } 258 } 259 } 260 } else { 261 fromDatabase = new Vector(cp.sizeFor(result)); 262 AbstractSession sessionToUse = unitOfWork.getParent(); 263 for (Object iter = cp.iteratorFor(result); cp.hasNext(iter);) { 264 Object object = cp.next(iter, sessionToUse); 265 Object clone = conformIndividualResult(object, unitOfWork, arguments, selectionCriteriaClone, indexedInterimResult, buildDirectlyFromRows); 266 if (clone != null) { 267 fromDatabase.addElement(clone); 268 } 269 } 270 } 271 272 Object conformedResult = cp.containerInstance(indexedInterimResult.size() + fromDatabase.size()); 277 Object eachClone; 278 for (Enumeration enumtr = indexedInterimResult.elements(); enumtr.hasMoreElements();) { 279 eachClone = enumtr.nextElement(); 280 cp.addInto(eachClone, conformedResult, unitOfWork); 281 } 282 for (Enumeration enumtr = fromDatabase.elements(); enumtr.hasMoreElements();) { 283 eachClone = enumtr.nextElement(); 284 cp.addInto(eachClone, conformedResult, unitOfWork); 285 } 286 return conformedResult; 287 } 288 289 296 protected Object executeObjectLevelReadQuery() throws DatabaseException { 297 Object result = null; 298 if (getContainerPolicy().overridesRead()) { 299 return getContainerPolicy().execute(); 300 } 301 302 Vector rows = getQueryMechanism().selectAllRows(); 303 setExecutionTime(System.currentTimeMillis()); 304 if (getJoinedAttributeManager().isToManyJoin()) { 306 getJoinedAttributeManager().setDataResults(rows, getSession()); 307 } 308 309 if (getSession().isUnitOfWork()) { 310 result = registerResultInUnitOfWork(rows, (UnitOfWorkImpl)getSession(), getTranslationRow(), true); } else { 312 result = getQueryMechanism().buildObjectsFromRows(rows); 313 } 314 315 if (shouldIncludeData()) { 316 ComplexQueryResult complexResult = new ComplexQueryResult(); 317 complexResult.setResult(result); 318 complexResult.setData(rows); 319 return complexResult; 320 } 321 322 return result; 323 } 324 325 330 public ContainerPolicy getContainerPolicy() { 331 return containerPolicy; 332 } 333 334 338 public Vector getOrderByExpressions() { 339 if (orderByExpressions == null) { 340 orderByExpressions = new Vector(); 341 } 342 return orderByExpressions; 343 } 344 345 349 public boolean hasOrderByExpressions() { 350 return orderByExpressions != null; 351 } 352 353 357 public boolean hasHierarchicalExpressions() { 358 return false; 359 } 360 361 365 public boolean hasBatchReadAttributes() { 366 return false; 367 } 368 369 373 public boolean isAttributeBatchRead(String attributeName) { 374 return false; 375 } 376 377 381 public boolean isReadAllQuery() { 382 return true; 383 } 384 385 389 protected void prepare() throws QueryException { 390 super.prepare(); 391 392 getContainerPolicy().prepare(this, getSession()); 393 394 if (getContainerPolicy().overridesRead()) { 395 return; 396 } 397 398 prepareSelectAllRows(); 399 } 400 401 405 protected void prepareCustomQuery(DatabaseQuery customQuery) { 406 ReadAllQuery customReadQuery = (ReadAllQuery)customQuery; 407 customReadQuery.setContainerPolicy(getContainerPolicy()); 408 customReadQuery.setCascadePolicy(getCascadePolicy()); 409 customReadQuery.setShouldRefreshIdentityMapResult(shouldRefreshIdentityMapResult()); 410 customReadQuery.setShouldMaintainCache(shouldMaintainCache()); 411 customReadQuery.setShouldUseWrapperPolicy(shouldUseWrapperPolicy()); 412 } 413 414 418 public void prepareForExecution() throws QueryException { 419 super.prepareForExecution(); 420 421 getContainerPolicy().prepareForExecution(); 422 423 } 424 425 429 protected void prepareSelectAllRows() { 430 getQueryMechanism().prepareSelectAllRows(); 431 } 432 433 453 public Object registerResultInUnitOfWork(Object result, UnitOfWorkImpl unitOfWork, AbstractRecord arguments, boolean buildDirectlyFromRows) { 454 if (shouldConformResultsInUnitOfWork() || getDescriptor().shouldAlwaysConformResultsInUnitOfWork()) { 458 return conformResult(result, unitOfWork, arguments, buildDirectlyFromRows); 459 } 460 461 if (buildDirectlyFromRows) { 466 Vector rows = (Vector)result; 467 Set identitySet = null; 468 ContainerPolicy cp = getContainerPolicy(); 469 Object clones = cp.containerInstance(rows.size()); 470 for (Enumeration enumtr = rows.elements(); enumtr.hasMoreElements();) { 471 Object row = enumtr.nextElement(); 472 473 if (row != null) { 475 Object clone = registerIndividualResult(row, unitOfWork, buildDirectlyFromRows, null); 476 477 if (getJoinedAttributeManager().isToManyJoin()) { 479 if (identitySet == null) { 480 identitySet = new TopLinkIdentityHashSet(rows.size()); 481 } 482 if (!identitySet.contains(clone)) { 483 identitySet.add(clone); 484 cp.addInto(clone, clones, unitOfWork); 485 } 486 } else { 487 cp.addInto(clone, clones, unitOfWork); 488 } 489 } 490 } 491 return clones; 492 } 493 494 ContainerPolicy cp; 495 cp = getContainerPolicy(); 496 497 Object clones = cp.containerInstance(cp.sizeFor(result)); 498 AbstractSession sessionToUse = unitOfWork.getParent(); 499 for (Object iter = cp.iteratorFor(result); cp.hasNext(iter);) { 500 Object object = cp.next(iter, sessionToUse); 501 Object clone = registerIndividualResult(object, unitOfWork, buildDirectlyFromRows, null); 502 cp.addInto(clone, clones, unitOfWork); 503 } 504 return clones; 505 } 506 507 512 public void setContainerPolicy(ContainerPolicy containerPolicy) { 513 if (containerPolicy == null) { 516 return; 517 } 518 this.containerPolicy = containerPolicy; 519 setIsPrepared(false); 520 } 521 522 526 public void setOrderByExpressions(Vector orderByExpressions) { 527 this.orderByExpressions = orderByExpressions; 528 } 529 530 537 public void useCollectionClass(Class concreteClass) { 538 setContainerPolicy(ContainerPolicy.buildPolicyFor(concreteClass)); 540 541 } 542 543 554 public void useMapClass(Class concreteClass, String methodName) { 555 if (getReferenceClass() == null) { 557 throw QueryException.referenceClassMissing(this); 558 } 559 ContainerPolicy policy = ContainerPolicy.buildPolicyFor(concreteClass); 560 policy.setKeyName(methodName, getReferenceClass().getName()); 561 setContainerPolicy(policy); 562 } 563 } 564 | Popular Tags |