Search in sources :

Example 1 with CompoundDiff

use of org.apache.cayenne.graph.CompoundDiff in project cayenne by apache.

the class DataContext method onContextFlush.

@Override
protected GraphDiff onContextFlush(ObjectContext originatingContext, GraphDiff changes, boolean cascade) {
    boolean childContext = this != originatingContext && changes != null;
    try {
        if (childContext) {
            getObjectStore().childContextSyncStarted();
            changes.apply(new ChildDiffLoader(this));
            fireDataChannelChanged(originatingContext, changes);
        }
        return (cascade) ? flushToParent(true) : new CompoundDiff();
    } finally {
        if (childContext) {
            getObjectStore().childContextSyncStopped();
        }
    }
}
Also used : ChildDiffLoader(org.apache.cayenne.graph.ChildDiffLoader) CompoundDiff(org.apache.cayenne.graph.CompoundDiff)

Example 2 with CompoundDiff

use of org.apache.cayenne.graph.CompoundDiff in project cayenne by apache.

the class DataDomainFlushAction method flush.

GraphDiff flush(DataContext context, GraphDiff changes) {
    if (changes == null) {
        return new CompoundDiff();
    }
    // TODO: Andrus, 3/13/2006 - support categorizing an arbitrary diff
    if (!(changes instanceof ObjectStoreGraphDiff)) {
        throw new IllegalArgumentException("Expected 'ObjectStoreGraphDiff', got: " + changes.getClass().getName());
    }
    this.context = context;
    // ObjectStoreGraphDiff contains changes already categorized by objectId...
    this.changesByObjectId = ((ObjectStoreGraphDiff) changes).getChangesByObjectId();
    this.insertBucket = new DataDomainInsertBucket(this);
    this.deleteBucket = new DataDomainDeleteBucket(this);
    this.updateBucket = new DataDomainUpdateBucket(this);
    this.flattenedBucket = new DataDomainFlattenedBucket(this);
    this.queries = new ArrayList<>();
    this.resultIndirectlyModifiedIds = new HashSet<>();
    preprocess(context, changes);
    if (queries.isEmpty()) {
        return new CompoundDiff();
    }
    this.resultDiff = new CompoundDiff();
    this.resultDeletedIds = new ArrayList<>();
    this.resultModifiedSnapshots = new HashMap<>();
    runQueries();
    postprocess(context);
    return resultDiff;
}
Also used : CompoundDiff(org.apache.cayenne.graph.CompoundDiff)

Example 3 with CompoundDiff

use of org.apache.cayenne.graph.CompoundDiff in project cayenne by apache.

the class ObjectStoreGraphDiff method resolveDiff.

/**
 * Converts diffs organized by ObjectId in a collection of diffs sorted by
 * diffId (same as creation order).
 */
private void resolveDiff() {
    // changed the the last time we cached the changes.
    if (resolvedDiff == null || lastSeenDiffId < objectStore.currentDiffId) {
        CompoundDiff diff = new CompoundDiff();
        Map<Object, ObjectDiff> changes = getChangesByObjectId();
        if (!changes.isEmpty()) {
            List<NodeDiff> allChanges = new ArrayList<>(changes.size() * 2);
            for (final ObjectDiff objectDiff : changes.values()) {
                objectDiff.appendDiffs(allChanges);
            }
            Collections.sort(allChanges);
            diff.addAll(allChanges);
        }
        this.lastSeenDiffId = objectStore.currentDiffId;
        this.resolvedDiff = diff;
    }
}
Also used : NodeDiff(org.apache.cayenne.graph.NodeDiff) ArrayList(java.util.ArrayList) CompoundDiff(org.apache.cayenne.graph.CompoundDiff)

Example 4 with CompoundDiff

use of org.apache.cayenne.graph.CompoundDiff in project cayenne by apache.

the class ClientChannel method onSync.

public GraphDiff onSync(ObjectContext originatingContext, GraphDiff changes, int syncType) {
    DataChannelSyncCallbackAction callbackAction = DataChannelSyncCallbackAction.getCallbackAction(getEntityResolver().getCallbackRegistry(), originatingContext.getGraphManager(), changes, syncType);
    callbackAction.applyPreCommit();
    changes = diffCompressor.compress(changes);
    GraphDiff replyDiff = send(new SyncMessage(originatingContext, syncType, changes), GraphDiff.class);
    if (channelEventsEnabled) {
        EventSubject subject;
        switch(syncType) {
            case DataChannel.ROLLBACK_CASCADE_SYNC:
                subject = DataChannel.GRAPH_ROLLEDBACK_SUBJECT;
                break;
            case DataChannel.FLUSH_NOCASCADE_SYNC:
                subject = DataChannel.GRAPH_CHANGED_SUBJECT;
                break;
            case DataChannel.FLUSH_CASCADE_SYNC:
                subject = DataChannel.GRAPH_FLUSHED_SUBJECT;
                break;
            default:
                subject = null;
        }
        if (subject != null) {
            // combine message sender changes and message receiver changes into a
            // single event
            boolean sentNoop = changes == null || changes.isNoop();
            boolean receivedNoop = replyDiff == null || replyDiff.isNoop();
            if (!sentNoop || !receivedNoop) {
                CompoundDiff notification = new CompoundDiff();
                if (!sentNoop) {
                    notification.add(changes);
                }
                if (!receivedNoop) {
                    notification.add(replyDiff);
                }
                GraphEvent e = new GraphEvent(this, originatingContext, notification);
                eventManager.postEvent(e, subject);
            }
        }
    }
    callbackAction.applyPostCommit();
    return replyDiff;
}
Also used : GraphEvent(org.apache.cayenne.graph.GraphEvent) GraphDiff(org.apache.cayenne.graph.GraphDiff) DataChannelSyncCallbackAction(org.apache.cayenne.DataChannelSyncCallbackAction) EventSubject(org.apache.cayenne.event.EventSubject) CompoundDiff(org.apache.cayenne.graph.CompoundDiff)

Example 5 with CompoundDiff

use of org.apache.cayenne.graph.CompoundDiff in project cayenne by apache.

the class CayenneContextIT method testCommitChangesNew.

@Test
public void testCommitChangesNew() {
    final CompoundDiff diff = new CompoundDiff();
    final Object newObjectId = new ObjectId("test", "key", "generated");
    eventManager = new DefaultEventManager(0);
    // test that ids that are passed back are actually propagated to the
    // right
    // objects...
    MockDataChannel channel = new MockDataChannel() {

        @Override
        public GraphDiff onSync(ObjectContext originatingContext, GraphDiff changes, int syncType) {
            return diff;
        }

        // must provide a channel with working event manager
        @Override
        public EventManager getEventManager() {
            return eventManager;
        }
    };
    CayenneContext context = new CayenneContext(channel);
    ObjEntity entity = new ObjEntity("test_entity");
    entity.setClassName(MockPersistentObject.class.getName());
    DataMap dataMap = new DataMap("test");
    dataMap.addObjEntity(entity);
    Collection<DataMap> entities = Collections.singleton(dataMap);
    context.setEntityResolver(new EntityResolver(entities));
    Persistent object = context.newObject(MockPersistentObject.class);
    // record change here to make it available to the anonymous connector
    // method..
    diff.add(new NodeIdChangeOperation(object.getObjectId(), newObjectId));
    // check that a generated object ID is assigned back to the object...
    assertNotSame(newObjectId, object.getObjectId());
    context.commitChanges();
    assertSame(newObjectId, object.getObjectId());
    assertSame(object, context.graphManager.getNode(newObjectId));
}
Also used : NodeIdChangeOperation(org.apache.cayenne.graph.NodeIdChangeOperation) GraphDiff(org.apache.cayenne.graph.GraphDiff) DefaultEventManager(org.apache.cayenne.event.DefaultEventManager) EntityResolver(org.apache.cayenne.map.EntityResolver) DataMap(org.apache.cayenne.map.DataMap) ObjEntity(org.apache.cayenne.map.ObjEntity) CompoundDiff(org.apache.cayenne.graph.CompoundDiff) Test(org.junit.Test)

Aggregations

CompoundDiff (org.apache.cayenne.graph.CompoundDiff)15 Test (org.junit.Test)7 GraphDiff (org.apache.cayenne.graph.GraphDiff)5 NodeCreateOperation (org.apache.cayenne.graph.NodeCreateOperation)4 ArrayList (java.util.ArrayList)2 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)2 ObjectContext (org.apache.cayenne.ObjectContext)2 NodeIdChangeOperation (org.apache.cayenne.graph.NodeIdChangeOperation)2 EntityResolver (org.apache.cayenne.map.EntityResolver)2 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 DataChannelSyncCallbackAction (org.apache.cayenne.DataChannelSyncCallbackAction)1 DataObject (org.apache.cayenne.DataObject)1 DataRow (org.apache.cayenne.DataRow)1 ObjectId (org.apache.cayenne.ObjectId)1 Persistent (org.apache.cayenne.Persistent)1 DefaultEventManager (org.apache.cayenne.event.DefaultEventManager)1 EventSubject (org.apache.cayenne.event.EventSubject)1 ChildDiffLoader (org.apache.cayenne.graph.ChildDiffLoader)1