/** * INTERNAL: * This method will be used to merge one record into another */ public void mergeRecord(ChangeRecord mergeFromRecord, UnitOfWorkChangeSet mergeToChangeSet, UnitOfWorkChangeSet mergeFromChangeSet) { HashMap addMapToMerge = ((DirectCollectionChangeRecord)mergeFromRecord).getAddObjectMap(); HashMap removeMapToMerge = ((DirectCollectionChangeRecord)mergeFromRecord).getRemoveObjectMap(); //merge additions for (Iterator iterator = addMapToMerge.keySet().iterator(); iterator.hasNext();){ Object added = iterator.next(); if (!((DirectCollectionChangeRecord)mergeFromRecord).getCommitAddMap().containsKey(added)){ // we have not recorded a change of this type in this class before so add it this.getCommitAddMap().put(added, ((DirectCollectionChangeRecord)mergeFromRecord).getCommitAddMap().get(added)); } this.addAdditionChange(added, (Integer)addMapToMerge.get(added)); } //merge removals for (Iterator iterator = removeMapToMerge.keySet().iterator(); iterator.hasNext();){ Object removed = iterator.next(); if (!((DirectCollectionChangeRecord)mergeFromRecord).getCommitAddMap().containsKey(removed)){ // we have not recorded a change of this type in this class before so add it this.getCommitAddMap().put(removed, ((DirectCollectionChangeRecord)mergeFromRecord).getCommitAddMap().get(removed)); } this.addRemoveChange(removed, (Integer)addMapToMerge.get(removed)); } }
/** * INTERNAL: * Used by AttributeLevelChangeTracking to update a changeRecord with calculated changes * as apposed to detected changes. If an attribute can not be change tracked it's * changes can be detected through this process. */ public void calculateDeferredChanges(ChangeRecord changeRecord, AbstractSession session){ DirectCollectionChangeRecord collectionRecord = (DirectCollectionChangeRecord) changeRecord; compareCollectionsForChange(collectionRecord.getOriginalCollection(), collectionRecord.getLatestCollection(), collectionRecord, session); }
/** * returns true if the change set has changes */ public boolean hasChanges() { return (!(getAddObjectMap().isEmpty() && getRemoveObjectMap().isEmpty())) || getOwner().isNew(); }
/** * This method takes a hastable of primitive objects and adds them to the add list. * the hashtable stores the number of times the object is in the list * @param objectChanges prototype.changeset.ObjectChangeSet */ public void addAdditionChange(HashMap additions, HashMap databaseCount) { Iterator enumtr = additions.keySet().iterator(); while (enumtr.hasNext()) { Object object = enumtr.next(); if (databaseCount.containsKey(object)){ getCommitAddMap().put(object, databaseCount.get(object)); } addAdditionChange(object, (Integer)additions.get(object)); } }
/** * INTERNAL: * Either create a new change record or update with the new value. This is used * by attribute change tracking. * Specifically in a collection mapping this will be called when the customer * Set a new collection. In this case we will need to mark the change record * with the new and the old versions of the collection. * And mark the ObjectChangeSet with the attribute name then when the changes are calculated * force a compare on the collections to determine changes. */ public void updateChangeRecord(Object clone, Object newValue, Object oldValue, ObjectChangeSet objectChangeSet, UnitOfWorkImpl uow) { DirectCollectionChangeRecord collectionChangeRecord = (DirectCollectionChangeRecord)objectChangeSet.getChangesForAttributeNamed(this.getAttributeName()); if (collectionChangeRecord == null) { collectionChangeRecord = new DirectCollectionChangeRecord(objectChangeSet); collectionChangeRecord.setAttribute(getAttributeName()); collectionChangeRecord.setMapping(this); objectChangeSet.addChange(collectionChangeRecord); } if (collectionChangeRecord.getOriginalCollection() == null){ collectionChangeRecord.setOriginalCollection(oldValue); } collectionChangeRecord.setLatestCollection(newValue); objectChangeSet.deferredDetectionRequiredOn(getAttributeName()); }
/** * ADVANCED: * This method is used to have an object removed from a collection once the changeSet is applied * The referenceKey parameter should only be used for direct Maps. */ public void simpleRemoveFromCollectionChangeRecord(Object referenceKey, Object objectToRemove, ObjectChangeSet changeSet, AbstractSession session) { DirectCollectionChangeRecord collectionChangeRecord = (DirectCollectionChangeRecord)changeSet.getChangesForAttributeNamed(getAttributeName()); if (collectionChangeRecord == null) { collectionChangeRecord = new DirectCollectionChangeRecord(changeSet); collectionChangeRecord.setAttribute(getAttributeName()); collectionChangeRecord.setMapping(this); changeSet.addChange(collectionChangeRecord); Object collection = getRealAttributeValueFromObject(changeSet.getUnitOfWorkClone(), session); collectionChangeRecord.storeDatabaseCounts(collection, getContainerPolicy(), session); } collectionChangeRecord.addRemoveChange(objectToRemove, new Integer(1)); }
/** * ADVANCED: * This method is used to have an object add to a collection once the changeSet is applied * The referenceKey parameter should only be used for direct Maps. */ public void simpleAddToCollectionChangeRecord(Object referenceKey, Object objectToAdd, ObjectChangeSet changeSet, AbstractSession session) { DirectCollectionChangeRecord collectionChangeRecord = (DirectCollectionChangeRecord)changeSet.getChangesForAttributeNamed(getAttributeName()); if (collectionChangeRecord == null) { collectionChangeRecord = new DirectCollectionChangeRecord(changeSet); collectionChangeRecord.setAttribute(getAttributeName()); collectionChangeRecord.setMapping(this); changeSet.addChange(collectionChangeRecord); Object collection = getRealAttributeValueFromObject(changeSet.getUnitOfWorkClone(), session); collectionChangeRecord.storeDatabaseCounts(collection, getContainerPolicy(), session); } collectionChangeRecord.addAdditionChange(objectToAdd, new Integer(1)); }
if (getAddObjectMap().containsKey(key)){ int removeValue = ((Integer)getAddObjectMap().get(key)).intValue(); int addition = count.intValue(); int result = removeValue - addition; if (result > 0 ) { // more removes still getAddObjectMap().put(key, new Integer(result)); }else if (result < 0) { // more adds now getAddObjectMap().remove(key); getRemoveObjectMap().put(key, new Integer(Math.abs(result))); }else{ // equal getAddObjectMap().remove(key); if (this.getRemoveObjectMap().containsKey(key)){ int addValue = ((Integer)this.getRemoveObjectMap().get(key)).intValue(); addValue += count.intValue(); this.getRemoveObjectMap().put(key, new Integer(addValue)); }else{ this.getRemoveObjectMap().put(key, count); if (getCommitAddMap().containsKey(key)){ commitValue = ((Integer)getCommitAddMap().get(key)).intValue(); getCommitAddMap().put(key, new Integer(commitValue - removeValue));
DirectCollectionChangeRecord changeRecord = new DirectCollectionChangeRecord(owner); changeRecord.setAttribute(getAttributeName()); changeRecord.setMapping(this); compareCollectionsForChange(backUpCollection, cloneObjectCollection, changeRecord, session); if (changeRecord.hasChanges()){ return changeRecord;
return; ((DirectCollectionChangeRecord)changeRecord).addAdditionChange(cloneKeyValues, databaseCount); ((DirectCollectionChangeRecord)changeRecord).addRemoveChange(originalKeyValues, databaseCount); ((DirectCollectionChangeRecord)changeRecord).getCommitAddMap().put(DirectCollectionChangeRecord.Null, new Integer(databaseNullCount)); if (numberOfNewNulls > 0) { ((DirectCollectionChangeRecord)changeRecord).addAdditionChange(DirectCollectionChangeRecord.Null, new Integer(numberOfNewNulls)); } else { numberOfNewNulls *= -1; ((DirectCollectionChangeRecord)changeRecord).addRemoveChange(DirectCollectionChangeRecord.Null, new Integer(numberOfNewNulls));
/** * This method takes a hashtable of primitive objects and adds them to the remove list. * Each reference in the hashtable lists the number of this object that needs to be removed from the * collection. * @param objectChanges prototype.changeset.ObjectChangeSet */ public void addRemoveChange(HashMap additions, HashMap databaseCount) { Iterator enumtr = additions.keySet().iterator(); while (enumtr.hasNext()) { Object object = enumtr.next(); if (databaseCount.containsKey(object)){ getCommitAddMap().put(object, databaseCount.get(object)); } addRemoveChange(object, (Integer)additions.get(object)); } }
HashMap addObjects = ((DirectCollectionChangeRecord)changeRecord).getAddObjectMap(); HashMap removeObjects = ((DirectCollectionChangeRecord)changeRecord).getRemoveObjectMap();
/** * This method takes a hashtable of primitives and adds them to the commit list. * This count value provided is the number of instances that will need to be * inserted into the database once a remove has occured. This is only set * once for each object type */ public void setCommitAddition(Hashtable additions){ Enumeration enumtr = additions.keys(); while (enumtr.hasMoreElements()) { Object object = enumtr.nextElement(); getCommitAddMap().put(object, additions.get(object)); } }
/** * INTERNAL: * This method sets the list of added objects. It should only be used in RCM */ public void setAddObjectList(Vector list){ for (Iterator iterator = list.iterator(); iterator.hasNext();){ Object object = iterator.next(); this.addAdditionChange(object, new Integer(1)); } }
/** * INTERNAL: * This method sets the list of added objects. It should only be used in RCM */ public void setRemoveObjectList(Vector list){ for (Iterator iterator = list.iterator(); iterator.hasNext();){ Object object = iterator.next(); this.addRemoveChange(object, new Integer(1)); } }
/** * ADVANCED: * This method returns the list of added objects */ public Vector getAddObjectList(){ Vector vector = new Vector(); for (Iterator iterator = getAddObjectMap().keySet().iterator(); iterator.hasNext();){ Object object = iterator.next(); int count = ((Integer)getAddObjectMap().get(object)).intValue(); while (count > 0){ vector.add(object); --count; } } return vector; }
/** * ADVANCED: * This method returns the list of removed objects */ public Vector getRemoveObjectList(){ Vector vector = new Vector(); for (Iterator iterator = getRemoveObjectMap().keySet().iterator(); iterator.hasNext();){ Object object = iterator.next(); int count = ((Integer)getRemoveObjectMap().get(object)).intValue(); while (count > 0){ vector.add(object); --count; } } return vector; }
/** * INTERNAL: * Remove a value and its change set from the collection change record. This is used by * attribute change tracking. */ public void removeFromCollectionChangeRecord(Object newKey, Object newValue, ObjectChangeSet objectChangeSet, UnitOfWorkImpl uow) { if (newValue == null) { newValue = DirectCollectionChangeRecord.Null; } ClassDescriptor descriptor; DirectCollectionChangeRecord collectionChangeRecord = (DirectCollectionChangeRecord)objectChangeSet.getChangesForAttributeNamed(this.getAttributeName()); if (collectionChangeRecord == null) { collectionChangeRecord = new DirectCollectionChangeRecord(objectChangeSet); collectionChangeRecord.setAttribute(getAttributeName()); collectionChangeRecord.setMapping(this); objectChangeSet.addChange(collectionChangeRecord); Object collection = getRealAttributeValueFromObject(objectChangeSet.getUnitOfWorkClone(), uow); collectionChangeRecord.storeDatabaseCounts(collection, getContainerPolicy(), uow); } collectionChangeRecord.addRemoveChange(newValue, new Integer(1)); }
/** * INTERNAL: * Add a new value and its change set to the collection change record. This is used by * attribute change tracking. */ public void addToCollectionChangeRecord(Object newKey, Object newValue, ObjectChangeSet objectChangeSet, UnitOfWorkImpl uow) { if (newValue == null) { newValue = DirectCollectionChangeRecord.Null; } ClassDescriptor descriptor; DirectCollectionChangeRecord collectionChangeRecord = (DirectCollectionChangeRecord)objectChangeSet.getChangesForAttributeNamed(this.getAttributeName()); if (collectionChangeRecord == null) { collectionChangeRecord = new DirectCollectionChangeRecord(objectChangeSet); collectionChangeRecord.setAttribute(getAttributeName()); collectionChangeRecord.setMapping(this); objectChangeSet.addChange(collectionChangeRecord); Object collection = getRealAttributeValueFromObject(objectChangeSet.getUnitOfWorkClone(), uow); collectionChangeRecord.storeDatabaseCounts(collection, getContainerPolicy(), uow); } collectionChangeRecord.addAdditionChange(newValue, new Integer(1)); }
if (getRemoveObjectMap().containsKey(key)){ int removeValue = ((Integer)getRemoveObjectMap().get(key)).intValue(); int addition = count.intValue(); int result = removeValue - addition; if (result > 0 ) { // more removes still getRemoveObjectMap().put(key, new Integer(result)); }else if (result < 0) { // more adds now getRemoveObjectMap().remove(key); getAddObjectMap().put(key, new Integer(Math.abs(result))); }else{ // equal getRemoveObjectMap().remove(key); if (this.getAddObjectMap().containsKey(key)){ int addValue = ((Integer)this.getAddObjectMap().get(key)).intValue(); addValue += count.intValue(); this.getAddObjectMap().put(key, new Integer(addValue)); }else{ this.getAddObjectMap().put(key, count); if (getCommitAddMap().containsKey(key)){ commitValue = ((Integer)getCommitAddMap().get(key)).intValue(); getCommitAddMap().put(key, new Integer(addValue+commitValue));