1 21 package oracle.toplink.essentials.descriptors; 23 24 import java.util.*; 25 import oracle.toplink.essentials.mappings.DatabaseMapping; 26 import oracle.toplink.essentials.exceptions.ValidationException; 27 import oracle.toplink.essentials.exceptions.DescriptorException; 28 import oracle.toplink.essentials.queryframework.*; 29 import oracle.toplink.essentials.internal.sessions.*; 30 import oracle.toplink.essentials.sessions.Record; 31 import oracle.toplink.essentials.internal.sessions.AbstractRecord; 32 import oracle.toplink.essentials.internal.sessions.UnitOfWorkImpl; 33 import oracle.toplink.essentials.internal.sessions.AbstractSession; 34 35 48 public class DescriptorEvent extends EventObject { 49 53 protected int eventCode; 54 55 56 protected DatabaseQuery query; 57 58 59 protected Record record; 60 protected ClassDescriptor descriptor; 61 62 66 protected Object originalObject; 67 68 71 protected ObjectChangeSet changeSet; 72 73 74 protected AbstractSession session; 75 76 77 protected static String [] eventNames; 78 79 80 static { 81 eventNames = new String [DescriptorEventManager.NumberOfEvents]; 82 83 eventNames[DescriptorEventManager.PreWriteEvent] = "PreWriteEvent"; 84 eventNames[DescriptorEventManager.PostWriteEvent] = "PostWriteEvent"; 85 eventNames[DescriptorEventManager.PreDeleteEvent] = "PostDeleteEvent"; 86 eventNames[DescriptorEventManager.PostDeleteEvent] = "PostDeleteEvent"; 87 eventNames[DescriptorEventManager.PreInsertEvent] = "PreInsertEvent"; 88 eventNames[DescriptorEventManager.PostInsertEvent] = "PostInsertEvent"; 89 eventNames[DescriptorEventManager.PreUpdateEvent] = "PreUpdateEvent"; 90 eventNames[DescriptorEventManager.PostUpdateEvent] = "PostUpdateEvent"; 91 eventNames[DescriptorEventManager.PostBuildEvent] = "PostBuildEvent"; 92 eventNames[DescriptorEventManager.PostRefreshEvent] = "PostRefreshEvent"; 93 eventNames[DescriptorEventManager.PostCloneEvent] = "PostCloneEvent"; 94 eventNames[DescriptorEventManager.PostMergeEvent] = "PostMergeEvent"; 95 eventNames[DescriptorEventManager.AboutToInsertEvent] = "AboutToInsertEvent"; 96 eventNames[DescriptorEventManager.AboutToUpdateEvent] = "AboutToUpdateEvent"; 97 } 98 99 103 public DescriptorEvent(int eventCode, ObjectLevelModifyQuery query) { 104 this(query.getObject()); 105 this.query = query; 106 this.eventCode = eventCode; 107 this.session = query.getSession(); 108 this.descriptor = query.getDescriptor(); 109 } 110 111 115 public DescriptorEvent(Object sourceObject) { 116 super(sourceObject); 117 } 118 119 125 public void applyAttributeValuesIntoRow(String attributeName) { 126 ClassDescriptor descriptor = getSession().getDescriptor(getSource()); 127 DatabaseMapping mapping = descriptor.getMappingForAttributeName(attributeName); 128 129 if (mapping == null) { 130 throw ValidationException.missingMappingForAttribute(descriptor, attributeName, this.toString()); 131 } 132 if (getRecord() != null) { 133 mapping.writeFromObjectIntoRow(getSource(), (AbstractRecord)getRecord(), getSession()); 134 } 135 } 136 137 141 public ObjectChangeSet getChangeSet() { 142 return changeSet; 143 } 144 145 149 public ClassDescriptor getDescriptor() { 150 return descriptor; 151 } 152 153 157 public ClassDescriptor getClassDescriptor() { 158 ClassDescriptor desc = getDescriptor(); 159 if (desc instanceof ClassDescriptor) { 160 return (ClassDescriptor)desc; 161 } else { 162 throw ValidationException.cannotCastToClass(desc, desc.getClass(), ClassDescriptor.class); 163 } 164 } 165 166 171 public int getEventCode() { 172 return eventCode; 173 } 174 175 179 public Object getObject() { 180 return getSource(); 181 } 182 183 190 public Object getOriginalObject() { 191 if ((originalObject == null) && getSession().isUnitOfWork() && (getQuery() != null) && (getQuery().isObjectLevelModifyQuery())) { 193 setOriginalObject(((UnitOfWorkImpl)getSession()).getOriginalVersionOfObject(getSource())); 194 } 195 return originalObject; 196 } 197 198 202 public DatabaseQuery getQuery() { 203 return query; 204 } 205 206 211 public Record getRecord() { 212 return record; 213 } 214 215 219 public AbstractSession getSession() { 220 return session; 221 } 222 223 227 public void setChangeSet(ObjectChangeSet newChangeSet) { 228 changeSet = newChangeSet; 229 } 230 231 235 public void setDescriptor(ClassDescriptor descriptor) { 236 this.descriptor = descriptor; 237 } 238 239 244 public void setEventCode(int eventCode) { 245 this.eventCode = eventCode; 246 } 247 248 253 public void setOriginalObject(Object originalObject) { 254 this.originalObject = originalObject; 255 } 256 257 261 public void setQuery(DatabaseQuery query) { 262 this.query = query; 263 } 264 265 269 public void setRecord(Record record) { 270 this.record = record; 271 } 272 273 277 public void setSession(AbstractSession session) { 278 this.session = session; 279 } 280 281 284 public String toString() { 285 String eventName = "UnkownEvent"; 286 287 if ((getEventCode() >= 0) && (getEventCode() < DescriptorEventManager.NumberOfEvents)) { 288 eventName = eventNames[getEventCode()]; 289 } 290 291 return eventName + "(" + getSource().getClass() + ")"; 292 } 293 294 301 public void updateAttributeWithObject(String attributeName, Object value) { 302 DatabaseMapping mapping = this.query.getDescriptor().getMappingForAttributeName(attributeName); 303 if (mapping == null) { 304 throw DescriptorException.mappingForAttributeIsMissing(attributeName, getDescriptor()); 305 } 306 307 Object clone = this.getObject(); 308 Object cloneValue = value; 309 Object original = null; 310 311 if ((this.eventCode == DescriptorEventManager.PostCloneEvent) || (this.eventCode == DescriptorEventManager.PostMergeEvent)) { 313 original = this.getOriginalObject(); 314 } 315 Object originalValue = value; 316 ObjectChangeSet eventChangeSet = this.getChangeSet(); 317 Object valueForChangeSet = value; 318 319 if ((this.query != null) && this.query.isObjectLevelModifyQuery()) { 320 clone = ((ObjectLevelModifyQuery)this.query).getObject(); 321 eventChangeSet = ((ObjectLevelModifyQuery)this.query).getObjectChangeSet(); 322 } 323 ClassDescriptor descriptor = getSession().getDescriptor(value.getClass()); 324 325 if (descriptor != null) { 326 if (eventChangeSet != null) { 329 valueForChangeSet = descriptor.getObjectBuilder().createObjectChangeSet(value, (UnitOfWorkChangeSet)eventChangeSet.getUOWChangeSet(), getSession()); 330 } 331 if (original != null) { 332 originalValue = ((UnitOfWorkImpl)getSession()).getOriginalVersionOfObject(value); 334 } 335 } 336 if (clone != null) { 337 mapping.setRealAttributeValueInObject(clone, cloneValue); 338 } 339 if (original != null) { 340 mapping.setRealAttributeValueInObject(original, originalValue); 341 } 342 if (getRecord() != null) { 343 AbstractRecord tempRow = getDescriptor().getObjectBuilder().createRecord(); 344 345 mapping.writeFromObjectIntoRow(clone, tempRow, getSession()); 349 ((AbstractRecord)getRecord()).mergeFrom(tempRow); 350 } 351 if (eventChangeSet != null) { 352 eventChangeSet.removeChange(attributeName); 353 eventChangeSet.addChange(mapping.compareForChange(clone, ((UnitOfWorkImpl)getSession()).getBackupClone(clone), eventChangeSet, getSession())); 354 } 355 } 356 357 365 public void updateAttributeAddObjectToCollection(String attributeName, Object mapKey, Object value) { 366 DatabaseMapping mapping = this.query.getDescriptor().getMappingForAttributeName(attributeName); 367 if (mapping == null) { 368 throw DescriptorException.mappingForAttributeIsMissing(attributeName, getDescriptor()); 369 } 370 371 Object clone = this.getObject(); 372 Object cloneValue = value; 373 Object original = null; 374 375 if ((this.eventCode == DescriptorEventManager.PostCloneEvent) || (this.eventCode == DescriptorEventManager.PostMergeEvent)) { 377 original = this.getOriginalObject(); 378 } 379 Object originalValue = value; 380 ObjectChangeSet eventChangeSet = this.getChangeSet(); 381 Object valueForChangeSet = value; 382 383 if ((this.query != null) && this.query.isObjectLevelModifyQuery()) { 384 clone = ((ObjectLevelModifyQuery)this.query).getObject(); 385 eventChangeSet = ((ObjectLevelModifyQuery)this.query).getObjectChangeSet(); 386 } 387 ClassDescriptor descriptor = getSession().getDescriptor(value.getClass()); 388 389 if (descriptor != null) { 390 if (eventChangeSet != null) { 393 valueForChangeSet = descriptor.getObjectBuilder().createObjectChangeSet(value, (UnitOfWorkChangeSet)eventChangeSet.getUOWChangeSet(), getSession()); 394 } 395 if (original != null) { 396 originalValue = ((UnitOfWorkImpl)getSession()).getOriginalVersionOfObject(value); 398 } 399 } 400 401 if (clone != null) { 402 Object collection = mapping.getRealCollectionAttributeValueFromObject(clone, getSession()); 403 mapping.getContainerPolicy().addInto(mapKey, cloneValue, collection, getSession()); 404 } 405 if (original != null) { 406 Object collection = mapping.getRealCollectionAttributeValueFromObject(original, getSession()); 407 mapping.getContainerPolicy().addInto(mapKey, originalValue, collection, getSession()); 408 } 409 if (getRecord() != null) { 410 AbstractRecord tempRow = getDescriptor().getObjectBuilder().createRecord(); 411 412 mapping.writeFromObjectIntoRow(clone, tempRow, getSession()); 416 ((AbstractRecord)getRecord()).mergeFrom(tempRow); 417 } 418 if (eventChangeSet != null) { 419 mapping.simpleAddToCollectionChangeRecord(mapKey, valueForChangeSet, eventChangeSet, getSession()); 420 } 421 } 422 423 430 public void updateAttributeRemoveObjectFromCollection(String attributeName, Object mapKey, Object value) { 431 DatabaseMapping mapping = this.query.getDescriptor().getMappingForAttributeName(attributeName); 432 if (mapping == null) { 433 throw DescriptorException.mappingForAttributeIsMissing(attributeName, getDescriptor()); 434 } 435 436 Object clone = this.getObject(); 437 Object cloneValue = value; 438 Object original = null; 439 440 if ((this.eventCode == DescriptorEventManager.PostCloneEvent) || (this.eventCode == DescriptorEventManager.PostMergeEvent)) { 442 original = this.getOriginalObject(); 443 } 444 Object originalValue = value; 445 ObjectChangeSet eventChangeSet = this.getChangeSet(); 446 Object valueForChangeSet = value; 447 448 if ((this.query != null) && this.query.isObjectLevelModifyQuery()) { 449 clone = ((ObjectLevelModifyQuery)this.query).getObject(); 450 eventChangeSet = ((ObjectLevelModifyQuery)this.query).getObjectChangeSet(); 451 } 452 ClassDescriptor descriptor = getSession().getDescriptor(value.getClass()); 453 454 if (descriptor != null) { 455 if (eventChangeSet != null) { 458 valueForChangeSet = descriptor.getObjectBuilder().createObjectChangeSet(value, (UnitOfWorkChangeSet)eventChangeSet.getUOWChangeSet(), getSession()); 459 } 460 if (original != null) { 461 originalValue = ((UnitOfWorkImpl)getSession()).getOriginalVersionOfObject(value); 463 } 464 } 465 if (clone != null) { 466 Object collection = mapping.getRealCollectionAttributeValueFromObject(clone, getSession()); 467 mapping.getContainerPolicy().removeFrom(mapKey, cloneValue, collection, getSession()); 468 } 469 if (original != null) { 470 Object collection = mapping.getRealCollectionAttributeValueFromObject(original, getSession()); 471 mapping.getContainerPolicy().removeFrom(mapKey, originalValue, collection, getSession()); 472 } 473 if (getRecord() != null) { 474 AbstractRecord tempRow = getDescriptor().getObjectBuilder().createRecord(); 475 476 mapping.writeFromObjectIntoRow(clone, tempRow, getSession()); 480 ((AbstractRecord)getRecord()).mergeFrom(tempRow); 481 } 482 if (eventChangeSet != null) { 483 mapping.simpleRemoveFromCollectionChangeRecord(mapKey, valueForChangeSet, eventChangeSet, getSession()); 484 } 485 } 486 } 487 | Popular Tags |