1 21 package oracle.toplink.essentials.internal.sessions; 23 24 import oracle.toplink.essentials.internal.helper.IdentityHashtable; 25 import java.util.*; 26 import oracle.toplink.essentials.descriptors.ClassDescriptor; 27 28 37 public class CollectionChangeRecord extends ChangeRecord implements oracle.toplink.essentials.changesets.CollectionChangeRecord { 38 39 42 protected IdentityHashtable addObjectList; 43 44 47 protected transient Vector orderedAddObjects; 48 49 52 protected IdentityHashtable orderedAddObjectIndices; 53 54 57 protected Hashtable orderedRemoveObjects; 58 59 62 protected transient Vector orderedRemoveObjectIndices; 63 64 69 protected transient List addOverFlow; 70 71 74 protected IdentityHashtable removeObjectList; 75 76 79 protected transient Vector sdkAddObjects; 80 81 84 protected transient Vector sdkRemoveObjects; 85 86 89 protected transient Object originalCollection; 90 91 94 protected transient Object latestCollection; 95 96 99 public CollectionChangeRecord() { 100 super(); 101 } 102 103 107 public CollectionChangeRecord(ObjectChangeSet owner) { 108 this.owner = owner; 109 } 110 111 116 public void addAdditionChange(IdentityHashMap objectChanges, UnitOfWorkChangeSet changeSet, AbstractSession session) { 117 Iterator enumtr = objectChanges.keySet().iterator(); 118 while (enumtr.hasNext()) { 119 Object object = enumtr.next(); 120 ObjectChangeSet change = session.getDescriptor(object.getClass()).getObjectBuilder().createObjectChangeSet(object, changeSet, session); 121 if (change.hasKeys()){ 122 getAddObjectList().put(change, change); 126 }else{ 127 if (getRemoveObjectList().contains(change)){ 128 getRemoveObjectList().remove(change); 129 }else{ 130 getAddObjectList().put(change, change); 131 } 132 } 133 } 134 } 135 136 143 public void addOrderedAdditionChange(Vector objectChanges, IdentityHashtable objectChangesIndices, UnitOfWorkChangeSet changeSet, AbstractSession session) { 144 Enumeration e = objectChanges.elements(); 145 146 while (e.hasMoreElements()) { 147 Object object = e.nextElement(); 148 ObjectChangeSet change = session.getDescriptor(object.getClass()).getObjectBuilder().createObjectChangeSet(object, changeSet, session); 149 150 getOrderedAddObjects().add(change); 151 getOrderedAddObjectIndices().put(change, (Integer ) objectChangesIndices.get(object)); 152 } 153 } 154 155 162 public void addOrderedRemoveChange(Vector indicesToRemove, Hashtable objectChanges, UnitOfWorkChangeSet changeSet, AbstractSession session) { 163 orderedRemoveObjectIndices = indicesToRemove; 164 Enumeration e = orderedRemoveObjectIndices.elements(); 165 166 while (e.hasMoreElements()) { 167 Integer index = (Integer ) e.nextElement(); 168 Object object = objectChanges.get(index); 169 ObjectChangeSet change = session.getDescriptor(object.getClass()).getObjectBuilder().createObjectChangeSet(object, changeSet, session); 170 171 getOrderedRemoveObjects().put(index, change); 172 } 173 } 174 175 179 public void addRemoveChange(IdentityHashMap objectChanges, UnitOfWorkChangeSet changeSet, AbstractSession session) { 180 Iterator enumtr = objectChanges.keySet().iterator(); 183 while (enumtr.hasNext()) { 184 Object object = enumtr.next(); 185 ClassDescriptor descriptor = session.getDescriptor(object.getClass()); 186 ObjectChangeSet change = descriptor.getObjectBuilder().createObjectChangeSet(object, changeSet, session); 187 if (change.hasKeys()){ 188 getRemoveObjectList().put(change, change); 192 }else{ 193 if (getAddObjectList().contains(change)){ 194 getAddObjectList().remove(change); 195 }else{ 196 getRemoveObjectList().put(change, change); 197 } 198 } 199 } 200 } 201 202 207 public IdentityHashtable getAddObjectList() { 208 if (addObjectList == null) { 209 addObjectList = new IdentityHashtable(); 211 } 212 return addObjectList; 213 } 214 215 220 public List getAddOverFlow() { 221 if (addOverFlow == null) { 222 addOverFlow = new ArrayList(); 223 } 224 return addOverFlow; 225 } 226 227 233 public IdentityHashtable getRemoveObjectList() { 234 if (removeObjectList == null) { 235 removeObjectList = new IdentityHashtable(); 237 } 238 return removeObjectList; 239 } 240 241 244 public boolean hasChanges() { 245 return (!( getAddObjectList().isEmpty() && 246 getRemoveObjectList().isEmpty() && 247 getOrderedAddObjects().isEmpty() && 248 getOrderedRemoveObjects().isEmpty())) 249 || getOwner().isNew(); 250 } 251 252 256 public void mergeRecord(ChangeRecord mergeFromRecord, UnitOfWorkChangeSet mergeToChangeSet, UnitOfWorkChangeSet mergeFromChangeSet) { 257 Enumeration addEnum = ((CollectionChangeRecord)mergeFromRecord).getAddObjectList().keys(); 258 while (addEnum.hasMoreElements()) { 259 ObjectChangeSet mergingObject = (ObjectChangeSet)addEnum.nextElement(); 260 ObjectChangeSet localChangeSet = mergeToChangeSet.findOrIntegrateObjectChangeSet(mergingObject, mergeFromChangeSet); 261 if (getRemoveObjectList().containsKey(localChangeSet)) { 262 getRemoveObjectList().remove(localChangeSet); 263 } else { 264 getAddObjectList().put(localChangeSet, localChangeSet); 265 } 266 } 267 Enumeration removeEnum = ((CollectionChangeRecord)mergeFromRecord).getRemoveObjectList().keys(); 268 while (removeEnum.hasMoreElements()) { 269 ObjectChangeSet mergingObject = (ObjectChangeSet)removeEnum.nextElement(); 270 ObjectChangeSet localChangeSet = mergeToChangeSet.findOrIntegrateObjectChangeSet(mergingObject, mergeFromChangeSet); 271 if (getAddObjectList().containsKey(localChangeSet)) { 272 getAddObjectList().remove(localChangeSet); 273 } else { 274 getRemoveObjectList().put(localChangeSet, localChangeSet); 275 } 276 } 277 } 278 279 283 public void setAddObjectList(IdentityHashtable objectChangesList) { 284 this.addObjectList = objectChangesList; 285 } 286 287 291 public void setRemoveObjectList(IdentityHashtable objectChangesList) { 292 this.removeObjectList = objectChangesList; 293 } 294 295 299 public void updateReferences(UnitOfWorkChangeSet mergeToChangeSet, UnitOfWorkChangeSet mergeFromChangeSet) { 300 IdentityHashtable addList = new IdentityHashtable(getAddObjectList().size()); 301 IdentityHashtable removeList = new IdentityHashtable(getRemoveObjectList().size()); 302 303 if (getOrderedAddObjects().size() > 0 || getOrderedRemoveObjectIndices().size() > 0) { 305 Vector orderedAddList = new Vector(getOrderedAddObjects().size()); 307 IdentityHashtable orderedAddListIndices = new IdentityHashtable(getOrderedAddObjectIndices().size()); 308 309 for (int i = 0; i < getOrderedAddObjects().size(); i++) { 310 ObjectChangeSet changeSet = (ObjectChangeSet) getOrderedAddObjects().elementAt(i); 311 ObjectChangeSet localChangeSet = mergeToChangeSet.findOrIntegrateObjectChangeSet(changeSet, mergeFromChangeSet); 312 313 orderedAddList.add(localChangeSet); 314 orderedAddListIndices.put(localChangeSet, getOrderedAddObjectIndices().get(changeSet)); 315 316 if (getAddObjectList().contains(changeSet)) { 318 addList.put(localChangeSet, localChangeSet); 319 } 320 } 321 322 setOrderedAddObjects(orderedAddList); 323 setOrderedAddObjectIndices(orderedAddListIndices); 324 325 Hashtable orderedRemoveList = new Hashtable(getOrderedRemoveObjects().size()); 327 Enumeration changes = getOrderedRemoveObjects().keys(); 328 329 while (changes.hasMoreElements()) { 330 Object index = changes.nextElement(); 331 ObjectChangeSet changeSet = (ObjectChangeSet) getOrderedRemoveObjects().get(index); 332 ObjectChangeSet localChangeSet = mergeToChangeSet.findOrIntegrateObjectChangeSet(changeSet, mergeFromChangeSet); 333 334 orderedRemoveList.put(index, localChangeSet); 335 336 if (getRemoveObjectList().contains(changeSet)) { 338 removeList.put(localChangeSet, localChangeSet); 339 } 340 } 341 342 setOrderedRemoveObjects(orderedRemoveList); 343 } else { 345 Enumeration changes = getAddObjectList().elements(); 346 while (changes.hasMoreElements()) { 347 ObjectChangeSet localChangeSet = mergeToChangeSet.findOrIntegrateObjectChangeSet((ObjectChangeSet)changes.nextElement(), mergeFromChangeSet); 348 addList.put(localChangeSet, localChangeSet); 349 } 350 351 changes = getRemoveObjectList().elements(); 352 while (changes.hasMoreElements()) { 353 ObjectChangeSet localChangeSet = mergeToChangeSet.findOrIntegrateObjectChangeSet((ObjectChangeSet)changes.nextElement(), mergeFromChangeSet); 354 removeList.put(localChangeSet, localChangeSet); 355 } 356 } 357 358 setAddObjectList(addList); 359 setRemoveObjectList(removeList); 360 } 361 362 368 public Vector getAddObjectsForSDK() { 369 if (sdkAddObjects == null) { 370 sdkAddObjects = new Vector(); 371 372 for (Enumeration enumtr = this.getAddObjectList().keys(); enumtr.hasMoreElements();) { 373 sdkAddObjects.add(enumtr.nextElement()); 374 } 375 } 376 return sdkAddObjects; 377 } 378 379 385 public void setAddObjectsForSDK(Vector addObjects) { 386 sdkAddObjects = addObjects; 387 388 IdentityHashtable newList = new IdentityHashtable(); 390 for (int i = 0; i < sdkAddObjects.size(); i++) { 391 Object change = sdkAddObjects.elementAt(i); 392 newList.put(change, change); 393 } 394 this.setAddObjectList(newList); 395 } 396 397 403 public Vector getRemoveObjectsForSDK() { 404 if (sdkRemoveObjects == null) { 405 sdkRemoveObjects = new Vector(); 406 407 for (Enumeration enumtr = this.getRemoveObjectList().keys(); enumtr.hasMoreElements();) { 408 sdkRemoveObjects.add(enumtr.nextElement()); 409 } 410 } 411 return sdkRemoveObjects; 412 } 413 414 420 public void setRemoveObjectsForSDK(Vector removeObjects) { 421 sdkRemoveObjects = removeObjects; 422 423 IdentityHashtable newList = new IdentityHashtable(); 425 for (int i = 0; i < sdkRemoveObjects.size(); i++) { 426 Object change = sdkRemoveObjects.elementAt(i); 427 newList.put(change, change); 428 } 429 this.setRemoveObjectList(newList); 430 } 431 432 436 public Object getLatestCollection() { 437 return latestCollection; 438 } 439 440 446 public Vector getOrderedAddObjects() { 447 if (orderedAddObjects == null) { 448 orderedAddObjects = new Vector(); 449 } 450 451 return orderedAddObjects; 452 } 453 454 458 public Integer getOrderedAddObjectIndex(ObjectChangeSet changes) { 459 return (Integer ) getOrderedAddObjectIndices().get(changes); 460 } 461 462 467 public IdentityHashtable getOrderedAddObjectIndices() { 468 if (orderedAddObjectIndices == null) { 469 orderedAddObjectIndices = new IdentityHashtable(); 470 } 471 472 return orderedAddObjectIndices; 473 } 474 475 480 public Vector getOrderedRemoveObjectIndices() { 481 if (orderedRemoveObjectIndices == null) { 482 orderedRemoveObjectIndices = new Vector(); 483 } 484 485 return orderedRemoveObjectIndices; 486 } 487 488 492 public Object getOrderedRemoveObject(Integer index) { 493 return getOrderedRemoveObjects().get(index); 494 } 495 496 501 public Hashtable getOrderedRemoveObjects() { 502 if (orderedRemoveObjects == null) { 503 orderedRemoveObjects = new Hashtable(); 504 } 505 506 return orderedRemoveObjects; 507 } 508 509 513 public void setLatestCollection(Object latestCollection) { 514 this.latestCollection = latestCollection; 515 } 516 517 522 public void setOrderedAddObjectIndices(IdentityHashtable orderedAddObjectIndices) { 523 this.orderedAddObjectIndices = orderedAddObjectIndices; 524 } 525 526 530 public void setOrderedAddObjects(Vector orderedAddObjects) { 531 this.orderedAddObjects = orderedAddObjects; 532 } 533 534 538 public void setOrderedRemoveObjects(Hashtable orderedRemoveObjects) { 539 this.orderedRemoveObjects = orderedRemoveObjects; 540 } 541 542 546 public Object getOriginalCollection() { 547 return originalCollection; 548 } 549 550 554 public void setOriginalCollection(Object originalCollection) { 555 this.originalCollection = originalCollection; 556 } 557 } 558 | Popular Tags |