Search in sources :

Example 11 with GraphDiff

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

the class ObjectContextChangeLogTest method testReset.

@Test
public void testReset() {
    ObjectContextChangeLog recorder = new ObjectContextChangeLog();
    assertNotNull(recorder.getDiffs());
    assertTrue(recorder.getDiffs().isNoop());
    recorder.addOperation(new NodeCreateOperation(new Object()));
    assertNotNull(recorder.getDiffs());
    assertFalse(recorder.getDiffs().isNoop());
    recorder.reset();
    assertNotNull(recorder.getDiffs());
    assertTrue(recorder.getDiffs().isNoop());
    // now test that a diff stored before "clear" is not affected by 'clear'
    recorder.addOperation(new NodeCreateOperation(new Object()));
    GraphDiff diff = recorder.getDiffs();
    assertFalse(diff.isNoop());
    recorder.reset();
    assertFalse(diff.isNoop());
}
Also used : NodeCreateOperation(org.apache.cayenne.graph.NodeCreateOperation) GraphDiff(org.apache.cayenne.graph.GraphDiff) Test(org.junit.Test)

Example 12 with GraphDiff

use of org.apache.cayenne.graph.GraphDiff 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 13 with GraphDiff

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

the class ClientChannelServerDiffsIT method testReturnDiffInPrePersist.

@Test
public void testReturnDiffInPrePersist() {
    final List<GenericDiff> diffs = new ArrayList<>();
    final NoopGraphChangeHandler diffReader = new NoopGraphChangeHandler() {

        @Override
        public void nodePropertyChanged(Object nodeId, String property, Object oldValue, Object newValue) {
            super.nodePropertyChanged(nodeId, property, oldValue, newValue);
            diffs.add(new GenericDiff((ObjectId) nodeId, property, oldValue, newValue));
        }
    };
    LifecycleCallbackRegistry callbackRegistry = clientServerChannel.getEntityResolver().getCallbackRegistry();
    try {
        callbackRegistry.addListener(LifecycleEvent.POST_ADD, MtTable1.class, new ClientChannelServerDiffsListener1(), "prePersist");
        ClientChannel channel = new ClientChannel(connection, false, new MockEventManager(), false) {

            @Override
            public GraphDiff onSync(ObjectContext originatingContext, GraphDiff changes, int syncType) {
                GraphDiff serverDiff = super.onSync(originatingContext, changes, syncType);
                assertNotNull(serverDiff);
                serverDiff.apply(diffReader);
                return serverDiff;
            }
        };
        CayenneContext context = new CayenneContext(channel);
        ClientMtTable1 o = context.newObject(ClientMtTable1.class);
        ObjectId tempId = o.getObjectId();
        o.setServerAttribute1("YY");
        context.commitChanges();
        assertEquals(2, diffReader.size);
        assertEquals(1, diffs.size());
        assertEquals(tempId, diffs.get(0).sourceId);
        assertEquals(ClientMtTable1.GLOBAL_ATTRIBUTE1.getName(), diffs.get(0).property);
        assertNull(diffs.get(0).oldValue);
        assertEquals("XXX", diffs.get(0).newValue);
    } finally {
        callbackRegistry.clear();
    }
}
Also used : ObjectId(org.apache.cayenne.ObjectId) GraphDiff(org.apache.cayenne.graph.GraphDiff) ArrayList(java.util.ArrayList) CayenneContext(org.apache.cayenne.CayenneContext) LifecycleCallbackRegistry(org.apache.cayenne.reflect.LifecycleCallbackRegistry) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ObjectContext(org.apache.cayenne.ObjectContext) MockEventManager(org.apache.cayenne.event.MockEventManager) Test(org.junit.Test)

Example 14 with GraphDiff

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

the class SyncMessageTest method testConstructor.

@Test
public void testConstructor() {
    ObjectContext source = mock(ObjectContext.class);
    GraphDiff diff = new CompoundDiff();
    SyncMessage message = new SyncMessage(source, DataChannel.FLUSH_NOCASCADE_SYNC, diff);
    assertSame(source, message.getSource());
    assertEquals(DataChannel.FLUSH_NOCASCADE_SYNC, message.getType());
    assertSame(diff, message.getSenderChanges());
}
Also used : GraphDiff(org.apache.cayenne.graph.GraphDiff) ObjectContext(org.apache.cayenne.ObjectContext) CompoundDiff(org.apache.cayenne.graph.CompoundDiff) Test(org.junit.Test)

Example 15 with GraphDiff

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

the class CacheInvalidationFilter method onSync.

@Override
public GraphDiff onSync(ObjectContext originatingContext, GraphDiff changes, int syncType, DataChannelSyncFilterChain filterChain) {
    try {
        GraphDiff result = filterChain.onSync(originatingContext, changes, syncType);
        // no exceptions, flush...
        Collection<CacheGroupDescriptor> groupSet = groups.get();
        if (groupSet != null && !groupSet.isEmpty()) {
            QueryCache cache = cacheProvider.get();
            for (CacheGroupDescriptor group : groupSet) {
                if (group.getKeyType() != Void.class) {
                    cache.removeGroup(group.getCacheGroupName(), group.getKeyType(), group.getValueType());
                } else {
                    cache.removeGroup(group.getCacheGroupName());
                }
            }
        }
        return result;
    } finally {
        groups.set(null);
    }
}
Also used : QueryCache(org.apache.cayenne.cache.QueryCache) GraphDiff(org.apache.cayenne.graph.GraphDiff)

Aggregations

GraphDiff (org.apache.cayenne.graph.GraphDiff)32 Test (org.junit.Test)16 ObjectContext (org.apache.cayenne.ObjectContext)8 GraphChangeHandler (org.apache.cayenne.graph.GraphChangeHandler)7 ObjectId (org.apache.cayenne.ObjectId)5 CompoundDiff (org.apache.cayenne.graph.CompoundDiff)5 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)5 ArcId (org.apache.cayenne.graph.ArcId)4 CayenneContext (org.apache.cayenne.CayenneContext)3 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)3 DataChannel (org.apache.cayenne.DataChannel)3 DataChannelSyncCallbackAction (org.apache.cayenne.DataChannelSyncCallbackAction)3 MockEventManager (org.apache.cayenne.event.MockEventManager)3 EntityResolver (org.apache.cayenne.map.EntityResolver)3 ClientMtTable2 (org.apache.cayenne.testdo.mt.ClientMtTable2)3 Artist (org.apache.cayenne.testdo.testmap.Artist)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 QueryResponse (org.apache.cayenne.QueryResponse)2 QueryCache (org.apache.cayenne.cache.QueryCache)2