1 21 package oracle.toplink.essentials.internal.sessions; 23 24 import java.util.*; 25 import java.io.*; 26 import oracle.toplink.essentials.internal.helper.*; 27 import oracle.toplink.essentials.descriptors.ClassDescriptor; 28 29 38 public class UnitOfWorkChangeSet implements Serializable, oracle.toplink.essentials.changesets.UnitOfWorkChangeSet { 39 40 41 42 transient protected java.util.Hashtable objectChanges; 44 45 transient protected java.util.Hashtable newObjectChangeSets; 47 transient protected oracle.toplink.essentials.internal.helper.IdentityHashtable cloneToObjectChangeSet; 48 transient protected oracle.toplink.essentials.internal.helper.IdentityHashtable objectChangeSetToUOWClone; 49 protected IdentityHashtable aggregateList; 50 protected IdentityHashtable allChangeSets; 51 protected IdentityHashtable deletedObjects; 52 53 54 protected boolean hasChanges; 55 protected boolean hasForcedChanges; 56 57 58 private transient Vector sdkAllChangeSets; 59 private transient int objectChangeSetCounter = 0; 60 61 65 public UnitOfWorkChangeSet() { 66 super(); 67 this.setHasChanges(false); 68 } 69 70 75 public UnitOfWorkChangeSet(byte[] bytes) throws java.io.IOException , ClassNotFoundException { 76 java.io.ByteArrayInputStream byteIn = new java.io.ByteArrayInputStream (bytes); 77 ObjectInputStream objectIn = new ObjectInputStream(byteIn); 78 allChangeSets = (IdentityHashtable)objectIn.readObject(); 80 deletedObjects = (IdentityHashtable)objectIn.readObject(); 81 } 82 83 88 public void addDeletedObjects(IdentityHashtable deletedObjects, AbstractSession session) { 89 Enumeration enumtr = deletedObjects.keys(); 90 while (enumtr.hasMoreElements()) { 91 Object object = enumtr.nextElement(); 92 93 this.addDeletedObject(object, session); 94 } 95 } 96 97 102 public void addDeletedObject(Object object, AbstractSession session) { 103 ClassDescriptor descriptor = session.getDescriptor(object); 105 if (!descriptor.isAggregateCollectionDescriptor()) { 106 ObjectChangeSet set = descriptor.getObjectBuilder().createObjectChangeSet(object, this, false, session); 107 108 set.setShouldBeDeleted(true); 110 getDeletedObjects().put(set, set); 111 } 112 } 113 114 123 public void addObjectChangeSet(ObjectChangeSet objectChanges) { 124 if ((objectChanges == null)) { 125 return; 126 } 127 128 boolean objectChangeSetHasChanges = objectChanges.hasChanges(); 133 if (objectChangeSetHasChanges) { 134 this.setHasChanges(true); 135 this.hasForcedChanges = this.hasForcedChanges || objectChanges.hasForcedChanges(); 136 } else { 137 this.hasForcedChanges = true; 139 } 140 141 if (!objectChanges.isAggregate()) { 142 if (objectChangeSetHasChanges) { 143 getAllChangeSets().put(objectChanges, objectChanges); 148 } 149 150 if (objectChanges.getCacheKey() != null) { 151 Hashtable table = (Hashtable)getObjectChanges().get(objectChanges.getClassName()); 152 153 if (table == null) { 154 table = new Hashtable(2); 155 getObjectChanges().put(objectChanges.getClassName(), table); 156 table.put(objectChanges, objectChanges); 157 } else { 158 table.put(objectChanges, objectChanges); 159 } 160 } 161 } 162 } 163 164 173 public void addNewObjectChangeSet(ObjectChangeSet objectChanges, AbstractSession session) { 174 if ((objectChanges == null)) { 175 return; 176 } 177 IdentityHashtable changeSetTable = (IdentityHashtable)getNewObjectChangeSets().get(objectChanges.getClassType(session)); 178 if (changeSetTable == null) { 179 changeSetTable = new IdentityHashtable(); 181 getNewObjectChangeSets().put(objectChanges.getClassType(session), changeSetTable); 182 } 183 changeSetTable.put(objectChanges, objectChanges); 184 } 185 186 192 public ObjectChangeSet findObjectChangeSet(ObjectChangeSet changeSet, UnitOfWorkChangeSet mergeFromChangeSet) { 193 Hashtable changes = (Hashtable)getObjectChanges().get(changeSet.getClassName()); 194 ObjectChangeSet potential = null; 195 if (changes != null) { 196 potential = (ObjectChangeSet)changes.get(changeSet); 197 } 198 if (potential == null) { 199 potential = (ObjectChangeSet)this.getObjectChangeSetForClone(changeSet.getUnitOfWorkClone()); 200 } 201 return potential; 202 } 203 204 209 public ObjectChangeSet findOrIntegrateObjectChangeSet(ObjectChangeSet tofind, UnitOfWorkChangeSet mergeFromChangeSet) { 210 if (tofind == null) { 211 return tofind; 212 } 213 ObjectChangeSet localChangeSet = this.findObjectChangeSet(tofind, mergeFromChangeSet); 214 if (localChangeSet == null) { localChangeSet = new ObjectChangeSet(tofind.getPrimaryKeys(), tofind.getUnitOfWorkClone(), this, tofind.isNew()); 216 this.addObjectChangeSetForIdentity(localChangeSet, localChangeSet.getUnitOfWorkClone()); 217 } 218 return localChangeSet; 219 } 220 221 228 public void addObjectChangeSetForIdentity(ObjectChangeSet objectChanges, Object object) { 229 if ((objectChanges == null) || (object == null)) { 230 return; 231 } 232 233 if (objectChanges.isAggregate()) { 234 getAggregateList().put(objectChanges, objectChanges); 235 } 236 237 getObjectChangeSetToUOWClone().put(objectChanges, object); 238 getCloneToObjectChangeSet().put(object, objectChanges); 239 240 } 241 242 247 protected IdentityHashtable getAggregateList() { 248 if (aggregateList == null) { 249 aggregateList = new IdentityHashtable(); 250 } 251 return aggregateList; 252 } 253 254 259 public oracle.toplink.essentials.internal.helper.IdentityHashtable getAllChangeSets() { 260 if (this.allChangeSets == null) { 261 this.allChangeSets = new IdentityHashtable(); 263 } 264 return allChangeSets; 265 } 266 267 272 public oracle.toplink.essentials.internal.helper.IdentityHashtable getCloneToObjectChangeSet() { 273 if (cloneToObjectChangeSet == null) { 274 cloneToObjectChangeSet = new IdentityHashtable(); 275 } 276 return cloneToObjectChangeSet; 277 } 278 279 284 public oracle.toplink.essentials.internal.helper.IdentityHashtable getDeletedObjects() { 285 if (this.deletedObjects == null) { 286 this.deletedObjects = new IdentityHashtable(); 288 } 289 return deletedObjects; 290 } 291 292 297 public Hashtable getObjectChanges() { 298 if (objectChanges == null) { 299 objectChanges = new Hashtable(2); 300 } 301 return objectChanges; 302 } 303 304 309 public oracle.toplink.essentials.changesets.ObjectChangeSet getObjectChangeSetForClone(Object clone) { 310 if ((clone == null) || (getCloneToObjectChangeSet() == null)) { 311 return null; 312 } 313 return (oracle.toplink.essentials.changesets.ObjectChangeSet)getCloneToObjectChangeSet().get(clone); 314 } 315 316 321 protected oracle.toplink.essentials.internal.helper.IdentityHashtable getObjectChangeSetToUOWClone() { 322 if (this.objectChangeSetToUOWClone == null) { 323 this.objectChangeSetToUOWClone = new IdentityHashtable(); 325 } 326 return objectChangeSetToUOWClone; 327 } 328 329 334 public Object getUOWCloneForObjectChangeSet(oracle.toplink.essentials.changesets.ObjectChangeSet changeSet) { 335 if ((changeSet == null) || (getObjectChangeSetToUOWClone() == null)) { 336 return null; 337 } 338 return getObjectChangeSetToUOWClone().get(changeSet); 339 } 340 341 345 public boolean hasChanges() { 346 return (this.hasChanges || (!getDeletedObjects().isEmpty())); 349 } 350 351 355 public void setHasChanges(boolean flag) { 356 this.hasChanges = flag; 357 } 358 359 365 public boolean hasForcedChanges() { 366 return this.hasForcedChanges; 367 } 368 369 374 public ObjectChangeSet mergeObjectChanges(ObjectChangeSet objectChangeSet, UnitOfWorkChangeSet mergeFromChangeSet) { 375 ObjectChangeSet localChangeSet = this.findOrIntegrateObjectChangeSet(objectChangeSet, mergeFromChangeSet); 376 if (localChangeSet != null) { 377 localChangeSet.mergeObjectChanges(objectChangeSet, this, mergeFromChangeSet); 378 } 379 return localChangeSet; 380 } 381 382 389 public void mergeUnitOfWorkChangeSet(UnitOfWorkChangeSet mergeFromChangeSet, AbstractSession session, boolean postCommit) { 390 Iterator iterator = mergeFromChangeSet.getObjectChanges().values().iterator(); 391 while (iterator.hasNext()) { 392 Hashtable table = (Hashtable)iterator.next(); 394 Iterator changes = table.values().iterator(); 395 while (changes.hasNext()) { 396 ObjectChangeSet objectChangeSet = (ObjectChangeSet)changes.next(); 397 objectChangeSet = mergeObjectChanges(objectChangeSet, mergeFromChangeSet); 398 if (objectChangeSet.isNew() && !postCommit) { this.addNewObjectChangeSet(objectChangeSet, session); 400 } else { 401 this.addObjectChangeSet(objectChangeSet); 402 } 403 } 404 } 405 406 Enumeration deletedEnum = mergeFromChangeSet.getDeletedObjects().elements(); 409 while (deletedEnum.hasMoreElements()) { 410 ObjectChangeSet objectChangeSet = (ObjectChangeSet)deletedEnum.nextElement(); 411 ObjectChangeSet localObjectChangeSet = findObjectChangeSet(objectChangeSet, mergeFromChangeSet); 412 if (localObjectChangeSet == null) { 413 localObjectChangeSet = objectChangeSet; 414 } 415 this.getDeletedObjects().put(localObjectChangeSet, localObjectChangeSet); 416 } 417 } 418 419 423 public void putNewObjectInChangesList(ObjectChangeSet objectChangeSet, AbstractSession session) { 424 this.addObjectChangeSet(objectChangeSet); 425 removeObjectChangeSetFromNewList(objectChangeSet, session); 426 } 427 428 433 public void removeObjectChangeSetFromNewList(ObjectChangeSet objectChangeSet, AbstractSession session) { 434 IdentityHashtable table = (IdentityHashtable)getNewObjectChangeSets().get(objectChangeSet.getClassType(session)); 435 if (table != null) { 436 table.remove(objectChangeSet); 437 } 438 } 439 440 445 public void removeObjectChangeSet(ObjectChangeSet objectChanges) { 446 if (objectChanges == null) { 447 return; 448 } 449 Object object = getObjectChangeSetToUOWClone().get(objectChanges); 450 if (objectChanges.isAggregate()) { 451 getAggregateList().remove(objectChanges); 452 } else { 453 Hashtable table = (Hashtable)getObjectChanges().get(object.getClass().getName()); 455 if (table != null) { 456 table.remove(objectChanges); 457 } 458 } 459 getObjectChangeSetToUOWClone().remove(objectChanges); 460 if (object != null) { 461 getCloneToObjectChangeSet().remove(object); 462 } 463 getAllChangeSets().remove(objectChanges); 464 } 465 466 471 protected void setCloneToObjectChangeSet(oracle.toplink.essentials.internal.helper.IdentityHashtable newCloneToObjectChangeSet) { 472 cloneToObjectChangeSet = newCloneToObjectChangeSet; 473 } 474 475 480 protected void setObjectChanges(Hashtable objectChanges) { 481 this.objectChanges = objectChanges; 482 } 483 484 489 protected void setObjectChangeSetToUOWClone(oracle.toplink.essentials.internal.helper.IdentityHashtable newObjectChangeSetToUOWClone) { 490 objectChangeSetToUOWClone = newObjectChangeSetToUOWClone; 491 } 492 493 497 public java.util.Hashtable getNewObjectChangeSets() { 498 if (this.newObjectChangeSets == null) { 499 this.newObjectChangeSets = new java.util.Hashtable (); 500 } 501 return this.newObjectChangeSets; 502 } 503 504 508 public void setInternalAllChangeSets(Vector objectChangeSets) { 509 if (objectChangeSets == null) { 510 return; 511 } 512 sdkAllChangeSets = objectChangeSets; 513 514 for (int i = 0; i < objectChangeSets.size(); i++) { 515 ObjectChangeSet objChangeSet = (ObjectChangeSet)objectChangeSets.elementAt(i); 516 objChangeSet.setUOWChangeSet(this); 517 518 if (objChangeSet.isAggregate()) { 519 getAggregateList().put(objChangeSet, objChangeSet); 520 521 } else if (objChangeSet.shouldBeDeleted()) { 522 getDeletedObjects().put(objChangeSet, objChangeSet); 523 } else { 524 getAllChangeSets().put(objChangeSet, objChangeSet); 525 } 526 if (objChangeSet.getCacheKey() != null) { 527 Hashtable table = (Hashtable)getObjectChanges().get(objChangeSet.getClassName()); 528 if (table == null) { 529 table = new Hashtable(2); 530 getObjectChanges().put(objChangeSet.getClassName(), table); 531 } 532 table.put(objChangeSet, objChangeSet); 533 } 534 } 535 } 536 } 537 | Popular Tags |