1 21 package oracle.toplink.essentials.mappings; 23 24 import java.util.*; 25 import oracle.toplink.essentials.exceptions.*; 26 import oracle.toplink.essentials.indirection.ValueHolderInterface; 27 import oracle.toplink.essentials.internal.descriptors.*; 28 import oracle.toplink.essentials.internal.helper.*; 29 import oracle.toplink.essentials.internal.identitymaps.CacheKey; 30 import oracle.toplink.essentials.internal.indirection.*; 31 import oracle.toplink.essentials.internal.sessions.*; 32 import oracle.toplink.essentials.queryframework.*; 33 import oracle.toplink.essentials.sessions.ObjectCopyingPolicy; 34 import oracle.toplink.essentials.internal.sessions.AbstractRecord; 35 import oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl; 36 import oracle.toplink.essentials.internal.sessions.AbstractSession; 37 import oracle.toplink.essentials.descriptors.ClassDescriptor; 38 39 42 public abstract class ObjectReferenceMapping extends ForeignReferenceMapping { 43 44 45 protected boolean isForeignKeyRelationship; 46 47 48 protected Vector foreignKeyFields; 49 50 protected ObjectReferenceMapping() { 51 super(); 52 } 53 54 59 public Object buildBackupCloneForPartObject(Object attributeValue, Object clone, Object backup, UnitOfWorkImpl unitOfWork) { 60 return attributeValue; 61 } 62 63 68 public Object buildCloneForPartObject(Object attributeValue, Object original, Object clone, UnitOfWorkImpl unitOfWork, boolean isExisting) { 69 if (isExisting) { 71 return unitOfWork.registerExistingObject(attributeValue); 72 } else { 73 return unitOfWork.registerObject(attributeValue); 75 } 76 } 77 78 83 public void buildCopy(Object copy, Object original, ObjectCopyingPolicy policy) { 84 Object attributeValue = getRealAttributeValueFromObject(original, policy.getSession()); 85 if ((attributeValue != null) && (policy.shouldCascadeAllParts() || (policy.shouldCascadePrivateParts() && isPrivateOwned()))) { 86 attributeValue = policy.getSession().copyObject(attributeValue, policy); 87 } else if (attributeValue != null) { 88 Object copyValue = policy.getCopies().get(attributeValue); 90 if (copyValue != null) { 91 attributeValue = copyValue; 92 } 93 } 94 setRealAttributeValueInObject(copy, attributeValue); 95 } 96 97 102 public ChangeRecord compareForChange(Object clone, Object backUp, ObjectChangeSet owner, AbstractSession session) { 103 Object cloneAttribute = null; 104 Object backUpAttribute = null; 105 106 cloneAttribute = getAttributeValueFromObject(clone); 107 108 if (!owner.isNew()) { 109 backUpAttribute = getAttributeValueFromObject(backUp); 110 if ((backUpAttribute == null) && (cloneAttribute == null)) { 111 return null; 112 } 113 } 114 115 if ((cloneAttribute != null) && (!getIndirectionPolicy().objectIsInstantiated(cloneAttribute))) { 116 return null; 118 } 119 Object cloneAttributeValue = null; 120 Object backUpAttributeValue = null; 121 122 if (cloneAttribute != null) { 123 cloneAttributeValue = getRealAttributeValueFromObject(clone, session); 124 } 125 if (backUpAttribute != null) { 126 backUpAttributeValue = getRealAttributeValueFromObject(backUp, session); 127 } 128 129 if ((cloneAttributeValue == backUpAttributeValue) && (!owner.isNew())) { return null; 131 } 132 133 ObjectReferenceChangeRecord record = internalBuildChangeRecord(cloneAttributeValue, owner, session); 134 if (!owner.isNew()) { 135 record.setOldValue(backUpAttributeValue); 136 } 137 return record; 138 } 139 140 144 public ObjectReferenceChangeRecord internalBuildChangeRecord(Object newValue, ObjectChangeSet owner, AbstractSession session) { 145 ObjectReferenceChangeRecord changeRecord = new ObjectReferenceChangeRecord(owner); 146 changeRecord.setAttribute(getAttributeName()); 147 changeRecord.setMapping(this); 148 setNewValueInChangeRecord(newValue, changeRecord, owner, session); 149 return changeRecord; 150 } 151 152 156 public void setNewValueInChangeRecord(Object newValue, ObjectReferenceChangeRecord changeRecord, ObjectChangeSet owner, AbstractSession session) { 157 if (newValue != null) { 158 ObjectChangeSet newSet = getDescriptorForTarget(newValue, session).getObjectBuilder().createObjectChangeSet(newValue, (UnitOfWorkChangeSet)owner.getUOWChangeSet(), session); 160 changeRecord.setNewValue(newSet); 161 } else { 162 changeRecord.setNewValue(null); 163 } 164 } 165 166 172 protected boolean compareObjectsWithoutPrivateOwned(Object firstObject, Object secondObject, AbstractSession session) { 173 Object firstReferencedObject = getRealAttributeValueFromObject(firstObject, session); 174 Object secondReferencedObject = getRealAttributeValueFromObject(secondObject, session); 175 176 if ((firstReferencedObject == null) && (secondReferencedObject == null)) { 177 return true; 178 } 179 180 if ((firstReferencedObject == null) || (secondReferencedObject == null)) { 181 return false; 182 } 183 184 Vector firstKey = getReferenceDescriptor().getObjectBuilder().extractPrimaryKeyFromObject(firstReferencedObject, session); 185 Vector secondKey = getReferenceDescriptor().getObjectBuilder().extractPrimaryKeyFromObject(secondReferencedObject, session); 186 187 for (int index = 0; index < firstKey.size(); index++) { 188 Object firstValue = firstKey.elementAt(index); 189 Object secondValue = secondKey.elementAt(index); 190 191 if (!((firstValue == null) && (secondValue == null))) { 192 if ((firstValue == null) || (secondValue == null)) { 193 return false; 194 } 195 if (!firstValue.equals(secondValue)) { 196 return false; 197 } 198 } 199 } 200 201 return true; 202 } 203 204 210 protected boolean compareObjectsWithPrivateOwned(Object firstObject, Object secondObject, AbstractSession session) { 211 Object firstPrivateObject = getRealAttributeValueFromObject(firstObject, session); 212 Object secondPrivateObject = getRealAttributeValueFromObject(secondObject, session); 213 214 return session.compareObjects(firstPrivateObject, secondPrivateObject); 215 } 216 217 223 public ClassDescriptor getDescriptorForTarget(Object object, AbstractSession session) { 224 return getReferenceDescriptor(); 225 } 226 227 231 public Object getRealAttributeValueFromObject(Object object, AbstractSession session) { 232 Object value = super.getRealAttributeValueFromObject(object, session); 233 value = getReferenceDescriptor().getObjectBuilder().unwrapObject(value, session); 234 235 return value; 236 } 237 238 242 public boolean isObjectReferenceMapping() { 243 return true; 244 } 245 246 251 public void iterateOnRealAttributeValue(DescriptorIterator iterator, Object realAttributeValue) { 252 Object unwrappedAttributeValue = getReferenceDescriptor().getObjectBuilder().unwrapObject(realAttributeValue, iterator.getSession()); 254 iterator.iterateReferenceObjectForMapping(unwrappedAttributeValue, this); 255 } 256 257 261 public void mergeChangesIntoObject(Object target, ChangeRecord changeRecord, Object source, MergeManager mergeManager) { 262 Object targetValueOfSource = null; 263 264 if (shouldMergeCascadeParts(mergeManager)) { 267 ObjectChangeSet set = (ObjectChangeSet)((ObjectReferenceChangeRecord)changeRecord).getNewValue(); 268 if (set != null) { 269 if (mergeManager.shouldMergeChangesIntoDistributedCache()) { 270 targetValueOfSource = set.getTargetVersionOfSourceObject(mergeManager.getSession(), false); 273 if ((targetValueOfSource == null) && (set.isNew() || set.isAggregate()) && set.containsChangesFromSynchronization()) { 274 if (!mergeManager.getObjectsAlreadyMerged().containsKey(set)) { 275 Class objectClass = set.getClassType(mergeManager.getSession()); 280 targetValueOfSource = mergeManager.getSession().getDescriptor(objectClass).getObjectBuilder().buildNewInstance(); 281 mergeManager.getObjectsAlreadyMerged().put(set, targetValueOfSource); 283 } else { 284 targetValueOfSource = mergeManager.getObjectsAlreadyMerged().get(set); 288 } 289 } else { 290 targetValueOfSource = set.getTargetVersionOfSourceObject(mergeManager.getSession(), true); 292 } 293 if (set.containsChangesFromSynchronization()) { 294 mergeManager.mergeChanges(targetValueOfSource, set); 295 } 296 if (targetValueOfSource ==null) 298 { 299 mergeManager.getSession().getIdentityMapAccessorInstance().invalidateObject(target); 300 return; 301 } 302 } else { 303 mergeManager.mergeChanges(set.getUnitOfWorkClone(), set); 304 } 305 } 306 } 307 if ((targetValueOfSource == null) && (((ObjectReferenceChangeRecord)changeRecord).getNewValue() != null)) { 308 targetValueOfSource = ((ObjectChangeSet)((ObjectReferenceChangeRecord)changeRecord).getNewValue()).getTargetVersionOfSourceObject(mergeManager.getSession()); 309 } 310 311 if (isPrivateOwned() && (source != null)) { 314 mergeManager.registerRemovedNewObjectIfRequired(getRealAttributeValueFromObject(source, mergeManager.getSession())); 315 } 316 317 targetValueOfSource = getReferenceDescriptor().getObjectBuilder().wrapObject(targetValueOfSource, mergeManager.getSession()); 318 setRealAttributeValueInObject(target, targetValueOfSource); 319 } 320 321 325 public void mergeIntoObject(Object target, boolean isTargetUnInitialized, Object source, MergeManager mergeManager) { 326 if (isTargetUnInitialized) { 327 if (mergeManager.shouldMergeWorkingCopyIntoOriginal() && (!isAttributeValueInstantiated(source))) { 329 setAttributeValueInObject(target, getIndirectionPolicy().getOriginalIndirectionObject(getAttributeValueFromObject(source), mergeManager.getSession())); 330 return; 331 } 332 } 333 if (!shouldMergeCascadeReference(mergeManager)) { 334 return; 336 } 337 if (mergeManager.shouldMergeOriginalIntoWorkingCopy()) { 338 if (!isAttributeValueInstantiated(target)) { 339 return; 342 } 343 } else if (!isAttributeValueInstantiated(source)) { 344 return; 347 } 348 349 Object valueOfSource = getRealAttributeValueFromObject(source, mergeManager.getSession()); 350 351 Object targetValueOfSource = null; 352 353 if (shouldMergeCascadeParts(mergeManager) && (valueOfSource != null)) { 356 if ((mergeManager.getSession().isUnitOfWork()) && (((UnitOfWorkImpl)mergeManager.getSession()).getUnitOfWorkChangeSet() != null)) { 357 mergeManager.mergeChanges(mergeManager.getObjectToMerge(valueOfSource), (ObjectChangeSet)((UnitOfWorkChangeSet)((UnitOfWorkImpl)mergeManager.getSession()).getUnitOfWorkChangeSet()).getObjectChangeSetForClone(valueOfSource)); 359 } else { 360 mergeManager.mergeChanges(mergeManager.getObjectToMerge(valueOfSource), null); 361 } 362 } 363 364 if (valueOfSource != null) { 365 targetValueOfSource = mergeManager.getTargetVersionOfSourceObject(valueOfSource); 367 } 368 369 if (this.getDescriptor().getObjectChangePolicy().isObjectChangeTrackingPolicy()) { 370 Object valueOfTarget = getRealAttributeValueFromObject(target, mergeManager.getSession()); 372 if ( valueOfTarget != targetValueOfSource ) { this.getDescriptor().getObjectChangePolicy().raiseInternalPropertyChangeEvent(target, getAttributeName(), valueOfTarget, targetValueOfSource); 374 } 375 } 376 377 targetValueOfSource = getReferenceDescriptor().getObjectBuilder().wrapObject(targetValueOfSource, mergeManager.getSession()); 378 setRealAttributeValueInObject(target, targetValueOfSource); 379 } 380 381 385 protected Vector collectFields() { 386 return getForeignKeyFields(); 387 } 388 389 394 public Vector getForeignKeyFields() { 395 return foreignKeyFields; 396 } 397 398 403 protected void setForeignKeyFields(Vector foreignKeyFields) { 404 this.foreignKeyFields = foreignKeyFields; 405 if (!foreignKeyFields.isEmpty()) { 406 setIsForeignKeyRelationship(true); 407 } 408 } 409 410 416 public boolean isForeignKeyRelationship() { 417 return isForeignKeyRelationship; 418 } 419 420 426 public void setIsForeignKeyRelationship(boolean isForeignKeyRelationship) { 427 this.isForeignKeyRelationship = isForeignKeyRelationship; 428 } 429 430 434 public void preInsert(WriteObjectQuery query) throws DatabaseException, OptimisticLockException { 435 if (isForeignKeyRelationship()) { 436 insert(query); 437 } 438 } 439 440 444 protected Object readPrivateOwnedForObject(ObjectLevelModifyQuery modifyQuery) throws DatabaseException { 445 if (modifyQuery.getSession().isUnitOfWork()) { 446 if (modifyQuery.getObjectChangeSet() != null) { 447 ObjectReferenceChangeRecord record = (ObjectReferenceChangeRecord) modifyQuery.getObjectChangeSet().getChangesForAttributeNamed(getAttributeName()); 448 if (record != null) { 449 return record.getOldValue(); 450 } 451 } else { return getRealAttributeValueFromObject(modifyQuery.getBackupClone(), modifyQuery.getSession()); 453 } 454 } 455 456 return null; 457 } 458 459 463 public void preUpdate(WriteObjectQuery query) throws DatabaseException, OptimisticLockException { 464 if (!isAttributeValueInstantiated(query.getObject())) { 465 return; 466 } 467 468 if (isPrivateOwned()) { 469 Object objectInDatabase = readPrivateOwnedForObject(query); 470 if (objectInDatabase != null) { 471 query.setProperty(this, objectInDatabase); 472 } 473 } 474 475 if (!isForeignKeyRelationship()) { 476 return; 477 } 478 479 update(query); 480 } 481 482 486 public void postDelete(DeleteObjectQuery query) throws DatabaseException, OptimisticLockException { 487 if (!shouldObjectModifyCascadeToParts(query)) { 489 return; 490 } 491 492 Object object = query.getProperty(this); 493 494 if (isForeignKeyRelationship()) { 496 if (object != null) { 497 query.removeProperty(this); 498 499 if (query.isCascadeOfAggregateDelete()) { 505 query.getSession().getCommitManager().addObjectToDelete(object); 506 } else { 507 DeleteObjectQuery deleteQuery = new DeleteObjectQuery(); 508 deleteQuery.setObject(object); 509 deleteQuery.setCascadePolicy(query.getCascadePolicy()); 510 query.getSession().executeQuery(deleteQuery); 511 } 512 } 513 } 514 } 515 516 520 public void postInsert(WriteObjectQuery query) throws DatabaseException, OptimisticLockException { 521 if (!isForeignKeyRelationship()) { 522 insert(query); 523 } 524 } 525 526 530 public void postUpdate(WriteObjectQuery query) throws DatabaseException, OptimisticLockException { 531 if (!isAttributeValueInstantiated(query.getObject())) { 532 return; 533 } 534 535 if (!isForeignKeyRelationship()) { 536 update(query); 537 } 538 539 Object objectInDatabase = query.getProperty(this); 541 if (objectInDatabase != null) { 542 query.removeProperty(this); 543 } else { 544 return; 545 } 546 547 if (query.getObjectChangeSet() == null) { 549 Object objectInMemory = getRealAttributeValueFromObject(query.getObject(), query.getSession()); 550 551 if (objectInDatabase != objectInMemory) { 553 CacheKey cacheKeyForObjectInDatabase = null; 554 CacheKey cacheKeyForObjectInMemory = new CacheKey(new Vector()); 555 556 cacheKeyForObjectInDatabase = new CacheKey(getPrimaryKeyForObject(objectInDatabase, query.getSession())); 557 558 if (objectInMemory != null) { 559 cacheKeyForObjectInMemory = new CacheKey(getPrimaryKeyForObject(objectInMemory, query.getSession())); 560 } 561 562 if (cacheKeysAreEqual(cacheKeyForObjectInDatabase, cacheKeyForObjectInMemory)) { 563 return; 564 } 565 } else { 566 return; 567 } 568 } 569 570 if (query.shouldCascadeOnlyDependentParts()) { 571 query.getSession().getCommitManager().addObjectToDelete(objectInDatabase); 572 } else { 573 query.getSession().deleteObject(objectInDatabase); 574 } 575 } 576 577 581 public void preDelete(DeleteObjectQuery query) throws DatabaseException, OptimisticLockException { 582 if (!shouldObjectModifyCascadeToParts(query)) { 584 return; 585 } 586 587 Object objectInMemory = getRealAttributeValueFromObject(query.getObject(), query.getSession()); 589 Object objectFromDatabase = null; 590 591 objectFromDatabase = readPrivateOwnedForObject(query); 593 594 if ((objectFromDatabase != null) && (objectFromDatabase != objectInMemory)) { 596 CacheKey cacheKeyForObjectInDatabase = null; 598 CacheKey cacheKeyForObjectInMemory = new CacheKey(new Vector()); 599 600 cacheKeyForObjectInDatabase = new CacheKey(getPrimaryKeyForObject(objectFromDatabase, query.getSession())); 601 602 if (objectInMemory != null) { 603 cacheKeyForObjectInMemory = new CacheKey(getPrimaryKeyForObject(objectInMemory, query.getSession())); 604 } 605 if (!cacheKeysAreEqual(cacheKeyForObjectInMemory, cacheKeyForObjectInDatabase)) { 606 if (objectFromDatabase != null) { 607 DeleteObjectQuery deleteQuery = new DeleteObjectQuery(); 608 deleteQuery.setObject(objectFromDatabase); 609 deleteQuery.setCascadePolicy(query.getCascadePolicy()); 610 query.getSession().executeQuery(deleteQuery); 611 } 612 } 613 } 614 615 if (!isForeignKeyRelationship()) { 616 if (objectInMemory != null) { 617 DeleteObjectQuery deleteQuery = new DeleteObjectQuery(); 618 deleteQuery.setObject(objectInMemory); 619 deleteQuery.setCascadePolicy(query.getCascadePolicy()); 620 query.getSession().executeQuery(deleteQuery); 621 } 622 } else { 623 if (objectInMemory != null) { 625 query.setProperty(this, objectInMemory); 626 } 627 } 628 } 629 630 634 public void cascadePerformRemoveIfRequired(Object object, UnitOfWorkImpl uow, IdentityHashtable visitedObjects){ 635 Object attributeValue = getAttributeValueFromObject(object); 636 if (attributeValue != null && this.isCascadeRemove() ){ 637 Object reference = getIndirectionPolicy().getRealAttributeValueFromObject(attributeValue); 638 if (reference != null && (! visitedObjects.contains(reference)) ){ 639 visitedObjects.put(reference, reference); 640 uow.performRemove(reference, visitedObjects); 641 } 642 } 643 } 644 645 649 public void cascadeRegisterNewIfRequired(Object object, UnitOfWorkImpl uow, IdentityHashtable visitedObjects){ 650 Object attributeValue = getAttributeValueFromObject(object); 651 if (attributeValue != null && this.isCascadePersist() && getIndirectionPolicy().objectIsInstantiated(attributeValue)){ 652 Object reference = getIndirectionPolicy().getRealAttributeValueFromObject(attributeValue); 653 if (reference != null && (! visitedObjects.contains(reference)) ){ 654 visitedObjects.put(reference, reference); 655 uow.registerNewObjectForPersist(reference, visitedObjects); 656 } 657 } 658 } 659 660 663 protected boolean cacheKeysAreEqual(CacheKey cacheKey1, CacheKey cacheKey2) { 664 return cacheKey1.equals(cacheKey2); 665 } 666 667 670 protected Vector getPrimaryKeyForObject(Object object, AbstractSession session) { 671 return getReferenceDescriptor().getObjectBuilder().extractPrimaryKeyFromObject(object, session); 672 } 673 674 678 public boolean hasConstraintDependency() { 679 return isForeignKeyRelationship(); 680 } 681 682 689 public UnitOfWorkValueHolder createUnitOfWorkValueHolder(ValueHolderInterface attributeValue, Object original, Object clone, AbstractRecord row, UnitOfWorkImpl unitOfWork, boolean buildDirectlyFromRow) { 690 UnitOfWorkQueryValueHolder valueHolder = null; 691 if ((row == null) && (getDescriptor().getObjectBuilder().isPrimaryKeyMapping(this))) { 692 AbstractRecord rowFromTargetObject = extractPrimaryKeyRowForSourceObject(original, unitOfWork); 694 valueHolder = new UnitOfWorkQueryValueHolder(attributeValue, clone, this, rowFromTargetObject, unitOfWork); 695 } else { 696 valueHolder = new UnitOfWorkQueryValueHolder(attributeValue, clone, this, row, unitOfWork); 697 } 698 699 if (buildDirectlyFromRow && attributeValue.isInstantiated()) { 705 Object cloneAttributeValue = attributeValue.getValue(); 706 valueHolder.privilegedSetValue(cloneAttributeValue); 707 valueHolder.setInstantiated(); 708 709 } 711 return valueHolder; 712 } 713 714 718 public AbstractRecord extractPrimaryKeyRowForSourceObject(Object domainObject, AbstractSession session) { 719 AbstractRecord databaseRow = getDescriptor().getObjectBuilder().createRecord(); 720 writeFromObjectIntoRow(domainObject, databaseRow, session); 721 return databaseRow; 722 } 723 724 728 public Vector extractPrimaryKeysForReferenceObject(Object domainObject, AbstractSession session) { 729 return getIndirectionPolicy().extractPrimaryKeyForReferenceObject(getAttributeValueFromObject(domainObject), session); 730 } 731 732 738 public Vector extractPrimaryKeysForReferenceObjectFromRow(AbstractRecord row) { 739 return new Vector(1); 740 } 741 742 746 public Vector extractPrimaryKeysFromRealReferenceObject(Object object, AbstractSession session) { 747 if (object == null) { 748 return new Vector(1); 749 } else { 750 Object implementation = getReferenceDescriptor().getObjectBuilder().unwrapObject(object, session); 751 return getReferenceDescriptor().getObjectBuilder().extractPrimaryKeyFromObject(implementation, session); 752 } 753 } 754 755 759 protected void insert(WriteObjectQuery query) throws DatabaseException, OptimisticLockException { 760 if (!shouldObjectModifyCascadeToParts(query)) { 762 return; 763 } 764 765 Object object = getRealAttributeValueFromObject(query.getObject(), query.getSession()); 767 768 if (object == null) { 769 return; 770 } 771 ObjectChangeSet changeSet = query.getObjectChangeSet(); 772 if (changeSet != null) { 773 ObjectReferenceChangeRecord changeRecord = (ObjectReferenceChangeRecord)query.getObjectChangeSet().getChangesForAttributeNamed(getAttributeName()); 774 if (changeRecord != null) { 775 changeSet = (ObjectChangeSet)changeRecord.getNewValue(); 776 } else { 777 return; 779 } 780 } else { 781 UnitOfWorkChangeSet uowChangeSet = null; 782 783 if (query.getSession().isUnitOfWork() && (((UnitOfWorkImpl)query.getSession()).getUnitOfWorkChangeSet() != null)) { 785 uowChangeSet = (UnitOfWorkChangeSet)((UnitOfWorkImpl)query.getSession()).getUnitOfWorkChangeSet(); 786 changeSet = (ObjectChangeSet)uowChangeSet.getObjectChangeSetForClone(object); 787 } 788 } 789 790 WriteObjectQuery writeQuery = null; 791 if (isPrivateOwned()) { 792 writeQuery = new InsertObjectQuery(); 794 } else { 795 writeQuery = new WriteObjectQuery(); 796 } 797 writeQuery.setObject(object); 798 writeQuery.setObjectChangeSet(changeSet); 799 writeQuery.setCascadePolicy(query.getCascadePolicy()); 800 query.getSession().executeQuery(writeQuery); 801 } 802 803 807 protected void update(WriteObjectQuery query) throws DatabaseException, OptimisticLockException { 808 if (!shouldObjectModifyCascadeToParts(query)) { 809 return; 810 } 811 812 if (!isAttributeValueInstantiated(query.getObject())) { 814 return; 815 } 816 817 Object object = getRealAttributeValueFromObject(query.getObject(), query.getSession()); 819 if (object != null) { 820 ObjectChangeSet changeSet = query.getObjectChangeSet(); 821 if (changeSet != null) { 822 ObjectReferenceChangeRecord changeRecord = (ObjectReferenceChangeRecord)query.getObjectChangeSet().getChangesForAttributeNamed(getAttributeName()); 823 if (changeRecord != null) { 824 changeSet = (ObjectChangeSet)changeRecord.getNewValue(); 825 } else { 826 return; 828 } 829 } else { 830 UnitOfWorkChangeSet uowChangeSet = null; 831 832 if (query.getSession().isUnitOfWork() && (((UnitOfWorkImpl)query.getSession()).getUnitOfWorkChangeSet() != null)) { 834 uowChangeSet = (UnitOfWorkChangeSet)((UnitOfWorkImpl)query.getSession()).getUnitOfWorkChangeSet(); 835 changeSet = (ObjectChangeSet)uowChangeSet.getObjectChangeSetForClone(object); 836 } 837 } 838 WriteObjectQuery writeQuery = new WriteObjectQuery(); 839 writeQuery.setObject(object); 840 writeQuery.setObjectChangeSet(changeSet); 841 writeQuery.setCascadePolicy(query.getCascadePolicy()); 842 query.getSession().executeQuery(writeQuery); 843 } 844 } 845 846 850 public boolean verifyDelete(Object object, AbstractSession session) throws DatabaseException { 851 if (isPrivateOwned()) { 852 Object attributeValue = getRealAttributeValueFromObject(object, session); 853 854 if (attributeValue != null) { 855 return session.verifyDelete(attributeValue); 856 } 857 } 858 859 return true; 860 } 861 862 867 public void writeFromObjectIntoRowForUpdate(WriteObjectQuery query, AbstractRecord databaseRow) { 868 Object object = query.getObject(); 869 AbstractSession session = query.getSession(); 870 871 if (!isAttributeValueInstantiated(object)) { 872 return; 873 } 874 875 if (session.isUnitOfWork()) { 876 if (compareObjectsWithoutPrivateOwned(query.getBackupClone(), object, session)) { 877 return; 878 } 879 } 880 881 writeFromObjectIntoRow(object, databaseRow, session); 882 } 883 884 888 public void writeFromObjectIntoRowForWhereClause(ObjectLevelModifyQuery query, AbstractRecord databaseRow) { 889 if (isReadOnly()) { 890 return; 891 } 892 893 if (query.isDeleteObjectQuery()) { 894 writeFromObjectIntoRow(query.getObject(), databaseRow, query.getSession()); 895 } else { 896 if (isAttributeValueInstantiated(query.getObject())) { 899 writeFromObjectIntoRow(query.getBackupClone(), databaseRow, query.getSession()); 900 } else { 901 writeFromObjectIntoRow(query.getObject(), databaseRow, query.getSession()); 902 } 903 } 904 } 905 906 910 public boolean isChangeTrackingSupported() { 911 return true; 912 } 913 914 919 public void updateChangeRecord(Object clone, Object newValue, Object oldValue, ObjectChangeSet objectChangeSet, UnitOfWorkImpl uow) { 920 Object unwrappedNewValue = newValue; 922 Object unwrappedOldValue = oldValue; 923 if (newValue != null) { 924 unwrappedNewValue = getReferenceDescriptor().getObjectBuilder().unwrapObject(newValue, uow); 925 } 926 if (oldValue != null) { 927 unwrappedOldValue = getReferenceDescriptor().getObjectBuilder().unwrapObject(oldValue, uow); 928 } 929 ObjectReferenceChangeRecord changeRecord = (ObjectReferenceChangeRecord)objectChangeSet.getChangesForAttributeNamed(this.getAttributeName()); 930 if (changeRecord == null) { 931 changeRecord = internalBuildChangeRecord(unwrappedNewValue, objectChangeSet, uow); 932 changeRecord.setOldValue(unwrappedOldValue); 933 objectChangeSet.addChange(changeRecord); 934 935 } else { 936 setNewValueInChangeRecord(unwrappedNewValue, changeRecord, objectChangeSet, uow); 937 } 938 } 939 940 944 public ChangeRecord buildChangeRecord(Object clone, ObjectChangeSet owner, AbstractSession session) { 945 return internalBuildChangeRecord(getRealAttributeValueFromObject(clone, session), owner, session); 946 } 947 } 948 | Popular Tags |