1 21 package oracle.toplink.essentials.internal.indirection; 23 24 import oracle.toplink.essentials.indirection.*; 25 import oracle.toplink.essentials.internal.helper.ClassConstants; 26 import oracle.toplink.essentials.mappings.*; 27 import oracle.toplink.essentials.internal.sessions.AbstractRecord; 28 import oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl; 29 import oracle.toplink.essentials.descriptors.ClassDescriptor; 30 31 39 public class UnitOfWorkQueryValueHolder extends UnitOfWorkValueHolder { 40 protected UnitOfWorkQueryValueHolder(ValueHolderInterface attributeValue, Object clone, DatabaseMapping mapping, UnitOfWorkImpl unitOfWork) { 41 super(attributeValue, clone, mapping, unitOfWork); 42 } 43 44 public UnitOfWorkQueryValueHolder(ValueHolderInterface attributeValue, Object clone, ForeignReferenceMapping mapping, AbstractRecord row, UnitOfWorkImpl unitOfWork) { 45 this(attributeValue, clone, mapping, unitOfWork); 46 this.row = row; 47 } 48 49 52 protected Object buildBackupCloneFor(Object cloneAttributeValue) { 53 return getMapping().buildBackupCloneForPartObject(cloneAttributeValue, null, null, getUnitOfWork()); 54 } 55 56 59 public Object buildCloneFor(Object originalAttributeValue) { 60 return getMapping().buildCloneForPartObject(originalAttributeValue, null, this.relationshipSourceObject, getUnitOfWork(), true); 61 } 62 63 66 public void setValue(Object theValue) { 67 if (!isInstantiated()) { 69 instantiate(); 70 } 71 Object oldValue = getValue(); 72 super.setValue(theValue); 73 updateForeignReferenceSet(theValue, oldValue); 74 } 75 76 82 public void updateForeignReferenceRemove(Object value) { 83 DatabaseMapping sourceMapping = this.getMapping(); 84 if (sourceMapping == null) { 85 return; 87 } 88 89 if (sourceMapping.isPrivateOwned()) { 90 return; 93 } 94 95 ForeignReferenceMapping partner = this.getRelationshipPartnerFor(value); 97 if (partner != null) { 98 if (value != null) { 99 Object unwrappedValue = partner.getDescriptor().getObjectBuilder().unwrapObject(value, getSession()); 100 Object oldParent = partner.getRealAttributeValueFromObject(unwrappedValue, getSession()); 101 Object sourceObject = getRelationshipSourceObject(); 102 103 if ((oldParent == null) || (partner.isCollectionMapping() && !(partner.getContainerPolicy().contains(sourceObject, oldParent, getSession())))) { 104 return; 106 } 107 108 if (partner.isObjectReferenceMapping()) { 109 partner.setRealAttributeValueInObject(unwrappedValue, null); 111 } else if (partner.isCollectionMapping()) { 112 partner.getContainerPolicy().removeFrom(sourceObject, oldParent, getSession()); 114 } 115 } 116 } 117 } 118 119 125 public void updateForeignReferenceSet(Object value, Object oldValue) { 126 if ((value != null) && (ClassConstants.Collection_Class.isAssignableFrom(value.getClass()))) { 127 return; 129 } 130 if (getMapping() == null) { 131 return; 133 } 134 135 ForeignReferenceMapping partner = this.getRelationshipPartnerFor(value); 137 if (partner != null) { 138 if (value != null) { 139 Object unwrappedValue = partner.getDescriptor().getObjectBuilder().unwrapObject(value, getSession()); 140 Object oldParent = partner.getRealAttributeValueFromObject(unwrappedValue, getSession()); 141 Object sourceObject = getRelationshipSourceObject(); 142 Object wrappedSource = getMapping().getDescriptor().getObjectBuilder().wrapObject(sourceObject, getSession()); 143 144 if ((oldParent == sourceObject) || (partner.isCollectionMapping() && partner.getContainerPolicy().contains(sourceObject, oldParent, getSession()))) { 145 return; 147 } 148 149 if (oldParent != null) { 151 if (getMapping().isObjectReferenceMapping()) { 152 if (!partner.isCollectionMapping()) { 153 ((ObjectReferenceMapping)getMapping()).setRealAttributeValueInObject(oldParent, null); 155 } 156 } else if (getMapping().isCollectionMapping() && (!partner.isManyToManyMapping())) { 157 getMapping().getContainerPolicy().removeFrom(unwrappedValue, getMapping().getRealAttributeValueFromObject(oldParent, getSession()), getSession()); 158 } 159 } 160 161 if (oldValue != null) { 162 Object unwrappedOldValue = partner.getDescriptor().getObjectBuilder().unwrapObject(oldValue, getSession()); 164 165 if (partner.isObjectReferenceMapping()) { 167 partner.setRealAttributeValueInObject(unwrappedOldValue, null); 168 } else if (partner.isCollectionMapping()) { 169 partner.getContainerPolicy().removeFrom(sourceObject, partner.getRealAttributeValueFromObject(unwrappedOldValue, getSession()), getSession()); 170 } 171 } 172 173 if (partner.isObjectReferenceMapping()) { 175 partner.setRealAttributeValueInObject(unwrappedValue, wrappedSource); 176 } else if (partner.isCollectionMapping()) { 177 partner.getContainerPolicy().addInto(wrappedSource, oldParent, getSession()); 178 } 179 } else { 180 updateForeignReferenceRemove(oldValue); 181 } 182 } 183 } 184 185 190 private ForeignReferenceMapping getRelationshipPartnerFor(Object partnerObject) { 191 ForeignReferenceMapping partner = (ForeignReferenceMapping)getMapping().getRelationshipPartner(); 192 if ((partner == null) || (partnerObject == null)) { 193 return partner; 195 } 196 197 if (partner.getDescriptor().hasInheritance()) { 201 ClassDescriptor partnerObjectDescriptor = this.getSession().getDescriptor(partnerObject); 202 if (!(partner.getDescriptor().getJavaClass().isAssignableFrom(partnerObjectDescriptor.getJavaClass()))) { 203 return (ForeignReferenceMapping)partnerObjectDescriptor.getMappingForAttributeName(partner.getAttributeName()); 204 } 205 } 206 return partner; 207 } 208 } 209 | Popular Tags |