Search in sources :

Example 1 with MapChangeEvent

use of org.eclipse.persistence.descriptors.changetracking.MapChangeEvent in project eclipselink by eclipse-ee4j.

the class IndirectMapTest method assertRemoveAddEvents.

private void assertRemoveAddEvents(int count) {
    if (useListener) {
        int totalEvents = count * 2;
        assertEquals("events do not match", totalEvents, testMapLsn.events.size());
        for (int i = 0; i < totalEvents; ) {
            MapChangeEvent removeEvent = testMapLsn.events.get(i++);
            MapChangeEvent addEvent = testMapLsn.events.get(i++);
            assertEquals("expected remove event", CollectionChangeEvent.REMOVE, removeEvent.getChangeType());
            assertEquals("expected add event", CollectionChangeEvent.ADD, addEvent.getChangeType());
            assertFalse("removed: '" + removeEvent.getNewValue() + "', new: '" + addEvent.getNewValue() + "'", removeEvent.getNewValue().equals(addEvent.getNewValue()));
        }
    }
}
Also used : MapChangeEvent(org.eclipse.persistence.descriptors.changetracking.MapChangeEvent)

Example 2 with MapChangeEvent

use of org.eclipse.persistence.descriptors.changetracking.MapChangeEvent in project eclipselink by eclipse-ee4j.

the class DirectMapMapping method mergeIntoObject.

/**
 * INTERNAL:
 * Merge changes from the source to the target object.
 */
@Override
public void mergeIntoObject(Object target, boolean isTargetUnInitialized, Object source, MergeManager mergeManager, AbstractSession targetSession) {
    if (this.descriptor.getCachePolicy().isProtectedIsolation() && !this.isCacheable && !targetSession.isProtectedSession()) {
        setAttributeValueInObject(target, this.indirectionPolicy.buildIndirectObject(new ValueHolder<>(null)));
        return;
    }
    if (isTargetUnInitialized) {
        // This will happen if the target object was removed from the cache before the commit was attempted
        if (mergeManager.shouldMergeWorkingCopyIntoOriginal() && (!isAttributeValueInstantiated(source))) {
            setAttributeValueInObject(target, getIndirectionPolicy().getOriginalIndirectionObject(getAttributeValueFromObject(source), targetSession));
            return;
        }
    }
    if (!shouldMergeCascadeReference(mergeManager)) {
        // This is only going to happen on mergeClone, and we should not attempt to merge the reference
        return;
    }
    if (mergeManager.shouldRefreshRemoteObject() && usesIndirection()) {
        mergeRemoteValueHolder(target, source, mergeManager);
        return;
    }
    if (mergeManager.isForRefresh()) {
        if (!isAttributeValueInstantiated(target)) {
            // the refresh that attribute
            return;
        }
    } else if (!isAttributeValueInstantiated(source)) {
        // modified
        return;
    }
    Map valueOfSource = (Map) getRealCollectionAttributeValueFromObject(source, mergeManager.getSession());
    // trigger instantiation of target attribute
    Object valueOfTarget = getRealCollectionAttributeValueFromObject(target, mergeManager.getSession());
    Object newContainer = containerPolicy.containerInstance(containerPolicy.sizeFor(valueOfSource));
    boolean fireChangeEvents = false;
    if ((this.getDescriptor().getObjectChangePolicy().isObjectChangeTrackingPolicy()) && (target instanceof ChangeTracker) && (((ChangeTracker) target)._persistence_getPropertyChangeListener() != null)) {
        fireChangeEvents = true;
        // Collections may not be indirect list or may have been replaced with user collection.
        Object iterator = containerPolicy.iteratorFor(valueOfTarget);
        while (containerPolicy.hasNext(iterator)) {
            Map.Entry entry = (Map.Entry) containerPolicy.nextEntry(iterator, mergeManager.getSession());
            // make the remove change event fire.
            ((ObjectChangeListener) ((ChangeTracker) target)._persistence_getPropertyChangeListener()).internalPropertyChange(new MapChangeEvent(target, getAttributeName(), valueOfTarget, entry.getKey(), entry.getValue(), CollectionChangeEvent.REMOVE, false));
        }
        if (newContainer instanceof ChangeTracker) {
            ((ChangeTracker) newContainer)._persistence_setPropertyChangeListener(((ChangeTracker) target)._persistence_getPropertyChangeListener());
        }
        if (valueOfTarget instanceof ChangeTracker) {
            // remove listener
            ((ChangeTracker) valueOfTarget)._persistence_setPropertyChangeListener(null);
        }
    }
    valueOfTarget = newContainer;
    for (Object sourceValuesIterator = containerPolicy.iteratorFor(valueOfSource); containerPolicy.hasNext(sourceValuesIterator); ) {
        Map.Entry entry = (Map.Entry) containerPolicy.nextEntry(sourceValuesIterator, mergeManager.getSession());
        if (fireChangeEvents) {
            // Collections may not be indirect list or may have been replaced with user collection.
            // make the add change event fire.
            ((ObjectChangeListener) ((ChangeTracker) target)._persistence_getPropertyChangeListener()).internalPropertyChange(new MapChangeEvent(target, getAttributeName(), valueOfTarget, entry.getKey(), entry.getValue(), CollectionChangeEvent.ADD, false));
        }
        containerPolicy.addInto(entry.getKey(), entry.getValue(), valueOfTarget, mergeManager.getSession());
    }
    if (fireChangeEvents && (getDescriptor().getObjectChangePolicy().isAttributeChangeTrackingPolicy())) {
        // check that there were changes, if not then remove the record.
        ObjectChangeSet changeSet = ((AttributeChangeListener) ((ChangeTracker) target)._persistence_getPropertyChangeListener()).getObjectChangeSet();
        if (changeSet != null) {
            DirectMapChangeRecord changeRecord = (DirectMapChangeRecord) changeSet.getChangesForAttributeNamed(getAttributeName());
            if (changeRecord != null) {
                if (!changeRecord.isDeferred()) {
                    if (!changeRecord.hasChanges()) {
                        changeSet.removeChange(getAttributeName());
                    }
                } else {
                    // Must reset the latest collection.
                    changeRecord.setLatestCollection(valueOfTarget);
                }
            }
        }
    }
    // Must re-set variable to allow for set method to re-morph changes if the collection is not being stored directly.
    setRealAttributeValueInObject(target, valueOfTarget);
}
Also used : DirectMapChangeRecord(org.eclipse.persistence.internal.sessions.DirectMapChangeRecord) AttributeChangeListener(org.eclipse.persistence.internal.descriptors.changetracking.AttributeChangeListener) ObjectChangeSet(org.eclipse.persistence.internal.sessions.ObjectChangeSet) ObjectChangeListener(org.eclipse.persistence.internal.descriptors.changetracking.ObjectChangeListener) ValueHolder(org.eclipse.persistence.indirection.ValueHolder) MapChangeEvent(org.eclipse.persistence.descriptors.changetracking.MapChangeEvent) Map(java.util.Map) HashMap(java.util.HashMap) ChangeTracker(org.eclipse.persistence.descriptors.changetracking.ChangeTracker)

Aggregations

MapChangeEvent (org.eclipse.persistence.descriptors.changetracking.MapChangeEvent)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ChangeTracker (org.eclipse.persistence.descriptors.changetracking.ChangeTracker)1 ValueHolder (org.eclipse.persistence.indirection.ValueHolder)1 AttributeChangeListener (org.eclipse.persistence.internal.descriptors.changetracking.AttributeChangeListener)1 ObjectChangeListener (org.eclipse.persistence.internal.descriptors.changetracking.ObjectChangeListener)1 DirectMapChangeRecord (org.eclipse.persistence.internal.sessions.DirectMapChangeRecord)1 ObjectChangeSet (org.eclipse.persistence.internal.sessions.ObjectChangeSet)1