1 21 package oracle.toplink.essentials.queryframework; 23 24 import java.util.*; 25 import oracle.toplink.essentials.internal.helper.*; 26 import oracle.toplink.essentials.exceptions.*; 27 import oracle.toplink.essentials.expressions.*; 28 import oracle.toplink.essentials.internal.descriptors.*; 29 import oracle.toplink.essentials.descriptors.DescriptorQueryManager; 30 import oracle.toplink.essentials.internal.sessions.AbstractRecord; 31 import oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl; 32 import oracle.toplink.essentials.internal.sessions.AbstractSession; 33 import oracle.toplink.essentials.descriptors.ClassDescriptor; 34 35 46 public class ReadObjectQuery extends ObjectLevelReadQuery { 47 48 49 protected transient Object selectionObject; 50 51 52 protected Vector selectionKey; 53 54 55 protected boolean shouldLoadResultIntoSelectionObject = false; 56 57 64 public ReadObjectQuery() { 65 super(); 66 } 67 68 79 public ReadObjectQuery(Class classToRead) { 80 this(); 81 setReferenceClass(classToRead); 82 } 83 84 88 public ReadObjectQuery(Class classToRead, Expression selectionCriteria) { 89 this(); 90 setReferenceClass(classToRead); 91 setSelectionCriteria(selectionCriteria); 92 } 93 94 99 public ReadObjectQuery(Class classToRead, ExpressionBuilder builder) { 100 this(); 101 this.defaultBuilder = builder; 102 setReferenceClass(classToRead); 103 } 104 105 110 public ReadObjectQuery(Class classToRead, Call call) { 111 this(); 112 setReferenceClass(classToRead); 113 setCall(call); 114 } 115 116 121 public ReadObjectQuery(Call call) { 122 this(); 123 setCall(call); 124 } 125 126 131 public ReadObjectQuery(Object objectToRead) { 132 this(); 133 setSelectionObject(objectToRead); 134 } 135 136 140 public ReadObjectQuery(ExpressionBuilder builder) { 141 this(); 142 this.defaultBuilder = builder; 143 } 144 145 152 public void checkCacheByExactPrimaryKey() { 153 setCacheUsage(CheckCacheByExactPrimaryKey); 154 } 155 156 163 public void checkCacheByPrimaryKey() { 164 setCacheUsage(CheckCacheByPrimaryKey); 165 } 166 167 174 public void checkCacheThenDatabase() { 175 setCacheUsage(CheckCacheThenDatabase); 176 } 177 178 182 public void checkDescriptor(AbstractSession session) throws QueryException { 183 if (getReferenceClass() == null) { 184 throw QueryException.referenceClassMissing(this); 185 } 186 187 if (getDescriptor() == null) { 188 ClassDescriptor referenceDescriptor; 189 if (getSelectionObject() != null && session.getProject().hasProxyIndirection()) { 191 referenceDescriptor = session.getDescriptor(getSelectionObject()); 192 } else { 193 referenceDescriptor = session.getDescriptor(getReferenceClass()); 194 } 195 if (referenceDescriptor == null) { 196 throw QueryException.descriptorIsMissing(getReferenceClass(), this); 197 } 198 setDescriptor(referenceDescriptor); 199 } 200 } 201 202 206 protected Object checkEarlyReturnImpl(AbstractSession session, AbstractRecord translationRow) { 207 if (shouldMaintainCache() && (!shouldRefreshIdentityMapResult()) && (!(shouldCheckDescriptorForCacheUsage() && getDescriptor().shouldDisableCacheHits())) && (shouldCheckCache())) { 209 Object cachedObject = getQueryMechanism().checkCacheForObject(translationRow, session); 210 211 if (cachedObject == InvalidObject.instance) { 214 return cachedObject; 215 } 216 if (cachedObject != null) { 217 if (shouldLoadResultIntoSelectionObject()) { 218 ObjectBuilder builder = getDescriptor().getObjectBuilder(); 219 builder.copyInto(cachedObject, getSelectionObject()); 220 session.getIdentityMapAccessorInstance().putInIdentityMap(getSelectionObject()); 222 cachedObject = getSelectionObject(); 223 } 224 225 if (isLockQuery() && (session.isUnitOfWork() && !((UnitOfWorkImpl)session).isPessimisticLocked(cachedObject))) { 227 return null; 228 } 229 } 230 if (shouldUseWrapperPolicy()) { 231 cachedObject = getDescriptor().getObjectBuilder().wrapObject(cachedObject, session); 232 } 233 return cachedObject; 234 } else { 235 return null; 236 } 237 } 238 239 245 protected DatabaseQuery checkForCustomQuery(AbstractSession session, AbstractRecord translationRow) { 246 checkDescriptor(session); 247 248 if (!isUserDefined()) { 250 if (isCallQuery()) { 251 return null; 253 } 254 DescriptorQueryManager descriptorQueryManager = getDescriptor().getQueryManager(); 255 256 if (descriptorQueryManager.hasReadObjectQuery()) { 259 if (getJoinedAttributeManager().hasJoinedAttributeExpressions() || hasPartialAttributeExpressions() || hasAsOfClause() || hasNonDefaultFetchGroup() || (!wasDefaultLockMode()) || (!shouldIgnoreBindAllParameters())) { 262 return null; 263 } 264 265 if ((getSelectionKey() != null) || (getSelectionObject() != null)) { return descriptorQueryManager.getReadObjectQuery(); 267 } 268 269 if (getSelectionCriteria() != null) { 270 AbstractRecord primaryKeyRow = getDescriptor().getObjectBuilder().extractPrimaryKeyRowFromExpression(getSelectionCriteria(), translationRow, session); 271 272 if (primaryKeyRow != null) { 274 return descriptorQueryManager.getReadObjectQuery(); 275 } 276 } 277 } 278 } 279 280 return null; 281 } 282 283 287 protected Object conformResult(Object result, UnitOfWorkImpl unitOfWork, AbstractRecord databaseRow, boolean buildDirectlyFromRows) { 288 Object implementation = null; 294 if (buildDirectlyFromRows) { 295 implementation = result; 296 } else { 297 implementation = getDescriptor().getObjectBuilder().unwrapObject(result, unitOfWork.getParent()); 298 } 299 300 Expression selectionCriteriaClone = null; 301 if ((getSelectionCriteria() != null) && (getSelectionKey() == null) && (getSelectionObject() == null)) { 302 selectionCriteriaClone = (Expression)getSelectionCriteria().clone(); 303 selectionCriteriaClone.getBuilder().setSession(unitOfWork); 304 selectionCriteriaClone.getBuilder().setQueryClass(getReferenceClass()); 305 } 306 307 Object clone = conformIndividualResult(implementation, unitOfWork, databaseRow, selectionCriteriaClone, null, buildDirectlyFromRows); 308 if (clone == null) { 309 return clone; 310 } 311 312 if (shouldUseWrapperPolicy()) { 313 return getDescriptor().getObjectBuilder().wrapObject(clone, unitOfWork); 314 } else { 315 return clone; 316 } 317 } 318 319 328 public void dontLoadResultIntoSelectionObject() { 329 setShouldLoadResultIntoSelectionObject(false); 330 } 331 332 339 protected Object executeObjectLevelReadQuery() throws DatabaseException { 340 AbstractRecord row = null; 341 342 if (getJoinedAttributeManager().isToManyJoin()) { 344 List rows = getQueryMechanism().selectAllRows(); 345 if (rows.size() > 0) { 346 row = (AbstractRecord)rows.get(0); 347 } 348 getJoinedAttributeManager().setDataResults(rows, getSession()); 349 } else { 350 row = getQueryMechanism().selectOneRow(); 351 } 352 setExecutionTime(System.currentTimeMillis()); 353 Object result = null; 354 355 if (getSession().isUnitOfWork()) { 356 result = registerResultInUnitOfWork(row, (UnitOfWorkImpl)getSession(), getTranslationRow(), true); 357 } else { 358 if (row != null) { 359 result = buildObject(row); 360 } 361 } 362 363 if (shouldIncludeData()) { 364 ComplexQueryResult complexResult = new ComplexQueryResult(); 365 complexResult.setResult(result); 366 complexResult.setData(row); 367 return complexResult; 368 } 369 370 return result; 371 } 372 373 378 public Vector getSelectionKey() { 379 return selectionKey; 380 381 } 382 383 392 public Object getSelectionObject() { 393 return selectionObject; 394 } 395 396 400 public boolean isReadObjectQuery() { 401 return true; 402 } 403 404 412 public void loadResultIntoSelectionObject() { 413 setShouldLoadResultIntoSelectionObject(true); 414 } 415 416 420 protected void prepare() throws QueryException { 421 super.prepare(); 422 423 if ((getSelectionKey() != null) || (getSelectionObject() != null)) { 424 setSelectionCriteria(getDescriptor().getObjectBuilder().getPrimaryKeyExpression()); 426 if (!shouldPrepare()) { 428 if (getSelectionKey() != null) { 429 setTranslationRow(getDescriptor().getObjectBuilder().buildRowFromPrimaryKeyValues(getSelectionKey(), getSession())); 431 } else { setTranslationRow(getDescriptor().getObjectBuilder().buildRowForTranslation(getSelectionObject(), getSession())); 433 } 434 } 435 } 436 437 if (getJoinedAttributeManager().isToManyJoin()) { 439 getQueryMechanism().prepareSelectAllRows(); 440 } else { 441 getQueryMechanism().prepareSelectOneRow(); 442 } 443 } 444 445 449 protected void prepareCustomQuery(DatabaseQuery customQuery) { 450 ReadObjectQuery customReadQuery = (ReadObjectQuery)customQuery; 451 customReadQuery.setShouldRefreshIdentityMapResult(shouldRefreshIdentityMapResult()); 452 customReadQuery.setCascadePolicy(getCascadePolicy()); 453 customReadQuery.setShouldMaintainCache(shouldMaintainCache()); 454 customReadQuery.setShouldUseWrapperPolicy(shouldUseWrapperPolicy()); 455 customReadQuery.setQueryId(getQueryId()); 457 customReadQuery.setExecutionTime(getExecutionTime()); 458 customReadQuery.setShouldLoadResultIntoSelectionObject(shouldLoadResultIntoSelectionObject()); 459 AbstractRecord primaryKeyRow; 460 if (getSelectionObject() != null) { 461 customReadQuery.setSelectionObject(getSelectionObject()); 463 } else if (getSelectionKey() != null) { 465 customReadQuery.setSelectionKey(getSelectionKey()); 466 } else { 467 primaryKeyRow = customQuery.getDescriptor().getObjectBuilder().extractPrimaryKeyRowFromExpression(getSelectionCriteria(), customQuery.getTranslationRow(), customReadQuery.getSession()); 469 customReadQuery.setTranslationRow(primaryKeyRow); 470 } 471 } 472 473 477 public void prepareForExecution() throws QueryException { 478 super.prepareForExecution(); 479 480 if (shouldPrepare()) { 482 if (getSelectionKey() != null) { 483 setTranslationRow(getDescriptor().getObjectBuilder().buildRowFromPrimaryKeyValues(getSelectionKey(), getSession())); 485 } else if (getSelectionObject() != null) { 486 setTranslationRow(getDescriptor().getObjectBuilder().buildRowForTranslation(getSelectionObject(), getSession())); 488 } 489 } 490 } 491 492 510 public Object registerResultInUnitOfWork(Object result, UnitOfWorkImpl unitOfWork, AbstractRecord arguments, boolean buildDirectlyFromRows) { 511 if (result == null) { 512 return null; 513 } 514 if (shouldConformResultsInUnitOfWork() || getDescriptor().shouldAlwaysConformResultsInUnitOfWork()) { 515 return conformResult(result, unitOfWork, arguments, buildDirectlyFromRows); 516 } 517 518 Object clone = registerIndividualResult(result, unitOfWork, buildDirectlyFromRows, null); 519 520 if (shouldUseWrapperPolicy()) { 521 clone = getDescriptor().getObjectBuilder().wrapObject(clone, unitOfWork); 522 } 523 return clone; 524 } 525 526 531 public void setSelectionKey(Vector selectionKey) { 532 this.selectionKey = selectionKey; 533 setIsPrepared(false); 534 } 535 536 545 public void setSelectionObject(Object selectionObject) { 546 if (selectionObject == null) { 547 throw QueryException.selectionObjectCannotBeNull(this); 548 } 549 setSelectionKey(null); 550 setReferenceClass(selectionObject.getClass()); 552 this.selectionObject = selectionObject; 553 } 554 555 563 public void setShouldLoadResultIntoSelectionObject(boolean shouldLoadResultIntoSelectionObject) { 564 this.shouldLoadResultIntoSelectionObject = shouldLoadResultIntoSelectionObject; 565 } 566 567 571 public void setSingletonSelectionKey(Object selectionKey) { 572 Vector key = new Vector(); 573 key.addElement(selectionKey); 574 setSelectionKey(key); 575 576 } 577 578 582 public boolean shouldCheckCache() { 583 return getCacheUsage() != DoNotCheckCache; 584 } 585 586 590 public boolean shouldCheckCacheByExactPrimaryKey() { 591 return getCacheUsage() == CheckCacheByExactPrimaryKey; 592 } 593 594 598 public boolean shouldCheckCacheByPrimaryKey() { 599 return (getCacheUsage() == CheckCacheByPrimaryKey) || (getCacheUsage() == UseDescriptorSetting); 600 } 601 602 606 public boolean shouldCheckCacheThenDatabase() { 607 return getCacheUsage() == CheckCacheThenDatabase; 608 } 609 610 614 public boolean shouldLoadResultIntoSelectionObject() { 615 return shouldLoadResultIntoSelectionObject; 616 } 617 618 622 protected boolean hasNonDefaultFetchGroup() { 623 return getDescriptor().hasFetchGroupManager() && ((this.getFetchGroup() != null) || (this.getFetchGroupName() != null) || (!this.shouldUseDefaultFetchGroup())); 624 625 } 626 } 627 | Popular Tags |