1 21 package oracle.toplink.essentials.queryframework; 23 24 import java.security.AccessController ; 25 import java.security.PrivilegedActionException ; 26 import java.util.*; 27 import oracle.toplink.essentials.internal.expressions.*; 28 import oracle.toplink.essentials.internal.helper.Helper; 29 import oracle.toplink.essentials.mappings.DatabaseMapping; 30 import oracle.toplink.essentials.internal.security.PrivilegedAccessHelper; 31 import oracle.toplink.essentials.internal.security.PrivilegedClassForName; 32 import oracle.toplink.essentials.internal.sessions.MergeManager; 33 import oracle.toplink.essentials.exceptions.ValidationException; 34 import oracle.toplink.essentials.internal.sessions.AbstractRecord; 35 import oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl; 36 import oracle.toplink.essentials.descriptors.ClassDescriptor; 37 import oracle.toplink.essentials.internal.queryframework.JoinedAttributeManager; 38 39 50 public abstract class ObjectBuildingQuery extends ReadQuery { 51 52 53 protected Class referenceClass; 54 protected String referenceClassName; 55 56 57 protected boolean shouldRefreshIdentityMapResult; 58 protected boolean shouldRefreshRemoteIdentityMapResult; 59 60 61 protected boolean shouldRegisterResultsInUnitOfWork = true; 62 63 64 protected boolean shouldProcessResultsInUnitOfWork = true; 65 66 67 protected ForUpdateClause lockingClause; 68 public static final short NO_LOCK = 0; 69 public static final short LOCK = 1; 70 public static final short LOCK_NOWAIT = 2; 71 72 public static final short DEFAULT_LOCK_MODE = -1; 74 protected boolean isPrePrepared; 75 76 80 protected long executionTime = 0; 81 82 85 protected boolean shouldUseExclusiveConnection = false; 86 87 91 public static final String LOCK_RESULT_PROPERTY = "LOCK_RESULT"; 92 93 94 protected boolean wasDefaultLockMode = false; 95 96 100 public ObjectBuildingQuery() { 101 this.shouldRefreshIdentityMapResult = false; 102 } 103 104 111 public void convertClassNamesToClasses(ClassLoader classLoader){ 112 super.convertClassNamesToClasses(classLoader); 113 Class referenceClass = null; 114 try{ 115 if (PrivilegedAccessHelper.shouldUsePrivilegedAccess()){ 116 try { 117 referenceClass = (Class )AccessController.doPrivileged(new PrivilegedClassForName(getReferenceClassName(), true, classLoader)); 118 } catch (PrivilegedActionException exception) { 119 throw ValidationException.classNotFoundWhileConvertingClassNames(getReferenceClassName(), exception.getException()); 120 } 121 } else { 122 referenceClass = oracle.toplink.essentials.internal.security.PrivilegedAccessHelper.getClassForName(getReferenceClassName(), true, classLoader); 123 } 124 } catch (ClassNotFoundException exc){ 125 throw ValidationException.classNotFoundWhileConvertingClassNames(getReferenceClassName(), exc); 126 } 127 setReferenceClass(referenceClass); 128 }; 129 130 134 protected boolean wasDefaultLockMode() { 135 return wasDefaultLockMode; 136 } 137 138 142 protected void setWasDefaultLockMode(boolean wasDefaultLockMode) { 143 this.wasDefaultLockMode = wasDefaultLockMode; 144 } 145 146 150 public void dontRefreshIdentityMapResult() { 151 setShouldRefreshIdentityMapResult(false); 152 } 153 154 158 public void dontRefreshRemoteIdentityMapResult() { 159 setShouldRefreshRemoteIdentityMapResult(false); 160 } 161 162 167 public FetchGroup getFetchGroup() { 168 return null; 169 } 170 171 175 public short getLockMode() { 176 if (lockingClause == null) { 177 return DEFAULT_LOCK_MODE; 178 } else { 179 return lockingClause.getLockMode(); 180 } 181 } 182 183 187 public List getDataResults() { 188 return null; 189 } 190 191 195 public long getExecutionTime() { 196 return executionTime; 197 } 198 199 203 public Class getReferenceClass() { 204 return referenceClass; 205 } 206 207 211 public String getReferenceClassName() { 212 if ((referenceClassName == null) && (referenceClass != null)) { 213 referenceClassName = referenceClass.getName(); 214 } 215 return referenceClassName; 216 } 217 218 222 public boolean hasPartialAttributeExpressions() { 223 return false; 224 } 225 226 237 public boolean isLockQuery() { 238 return getLockMode() > NO_LOCK; 239 } 240 241 245 public boolean isObjectBuildingQuery() { 246 return true; 247 } 248 249 255 protected boolean isRegisteringResults() { 256 return ((shouldRegisterResultsInUnitOfWork() && getDescriptor().shouldRegisterResultsInUnitOfWork()) || isLockQuery()); 257 } 258 259 264 public void refreshIdentityMapResult() { 265 setShouldRefreshIdentityMapResult(true); 266 } 267 268 273 public void refreshRemoteIdentityMapResult() { 274 setShouldRefreshRemoteIdentityMapResult(true); 275 } 276 277 297 public Object registerIndividualResult(Object result, UnitOfWorkImpl unitOfWork, boolean buildDirectlyFromRows, JoinedAttributeManager joinManager) { 298 Object clone = null; 299 if (buildDirectlyFromRows) { 300 return buildObject((AbstractRecord)result); 304 } 305 else if (!isRegisteringResults()) { 307 clone = unitOfWork.getIdentityMapAccessorInstance().getIdentityMapManager().getFromIdentityMap(result); 308 309 if (clone == null) { 313 clone = result; 314 } 315 } else { 316 clone = unitOfWork.registerExistingObject(result, joinManager); 320 } 321 322 if (shouldRefreshIdentityMapResult()) { 324 if (unitOfWork.getParent().getIdentityMapAccessor().containsObjectInIdentityMap(clone)) { 326 if (shouldCascadeAllParts()) { 327 unitOfWork.deepRevertObject(clone); 328 } else if (shouldCascadePrivateParts()) { 329 unitOfWork.revertObject(clone); 330 } else if (shouldCascadeByMapping()) { 331 unitOfWork.revertObject(clone, MergeManager.CASCADE_BY_MAPPING); 332 } else if (!shouldCascadeParts()) { 333 unitOfWork.shallowRevertObject(clone); 334 } 335 } else { 336 if (shouldCascadeAllParts()) { 337 unitOfWork.deepMergeClone(result); 338 } else if (shouldCascadePrivateParts()) { 339 unitOfWork.mergeClone(result); 340 } else if (shouldCascadeByMapping()) { 341 unitOfWork.mergeClone(result, MergeManager.CASCADE_BY_MAPPING); 342 } else if (!shouldCascadeParts()) { 343 unitOfWork.shallowMergeClone(result); 344 } 345 } 346 } 347 348 recordCloneForPessimisticLocking(clone, unitOfWork); 350 351 return clone; 352 } 353 354 358 public void setExecutionTime(long executionTime) { 359 this.executionTime = executionTime; 360 } 361 362 384 public void setLockMode(short lockMode) { 385 lockingClause = ForUpdateClause.newInstance(lockMode); 386 } 387 388 392 public void setReferenceClass(Class aClass) { 393 referenceClass = aClass; 394 setIsPrepared(false); 395 } 396 397 401 public void setReferenceClassName(String aClass) { 402 referenceClassName = aClass; 403 setIsPrepared(false); 404 } 405 406 411 public void setShouldRefreshIdentityMapResult(boolean shouldRefreshIdentityMapResult) { 412 this.shouldRefreshIdentityMapResult = shouldRefreshIdentityMapResult; 413 if (shouldRefreshIdentityMapResult) { 414 setShouldRefreshRemoteIdentityMapResult(true); 415 } 416 } 417 418 423 public void setShouldRefreshRemoteIdentityMapResult(boolean shouldRefreshIdentityMapResult) { 424 this.shouldRefreshRemoteIdentityMapResult = shouldRefreshIdentityMapResult; 425 } 426 427 434 public void setShouldRegisterResultsInUnitOfWork(boolean shouldRegisterResultsInUnitOfWork) { 435 this.shouldRegisterResultsInUnitOfWork = shouldRegisterResultsInUnitOfWork; 436 } 437 438 453 public boolean shouldRegisterResultsInUnitOfWork() { 454 return shouldRegisterResultsInUnitOfWork; 455 } 456 457 461 public boolean shouldReadAllMappings() { 462 return true; 463 } 464 465 469 public boolean shouldReadMapping(DatabaseMapping mapping) { 470 return true; 471 } 472 473 478 public boolean shouldRefreshIdentityMapResult() { 479 return shouldRefreshIdentityMapResult; 480 } 481 482 487 public boolean shouldRefreshRemoteIdentityMapResult() { 488 return shouldRefreshRemoteIdentityMapResult; 489 } 490 491 public String toString() { 492 if (getReferenceClass() == null) { 493 return super.toString(); 494 } 495 return Helper.getShortClassName(getClass()) + "(" + getReferenceClass().getName() + ")"; 496 } 497 498 515 public void setShouldProcessResultsInUnitOfWork(boolean processResultsInUnitOfWork) { 516 this.shouldProcessResultsInUnitOfWork = processResultsInUnitOfWork; 517 } 518 519 536 public boolean shouldProcessResultsInUnitOfWork() { 537 return this.shouldProcessResultsInUnitOfWork; 538 } 539 540 544 public boolean isAttributeJoined(ClassDescriptor mappingDescriptor, String attributeName) { 545 return false; 546 } 547 548 552 public boolean isClonePessimisticLocked(Object clone, UnitOfWorkImpl uow) { 553 return false; 554 } 555 556 560 public void recordCloneForPessimisticLocking(Object clone, UnitOfWorkImpl uow) { 561 if ((isLockQuery()) && lockingClause.isReferenceClassLocked()) { 562 uow.addPessimisticLockedClone(clone); 563 } 564 } 565 566 570 public boolean isDefaultLock() { 571 return (lockingClause == null); 572 } 573 574 } 575 | Popular Tags |