Search in sources :

Example 16 with GraphDiff

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

the class ObjectStore method processSnapshotEvent.

/**
 * @since 1.2
 */
synchronized void processSnapshotEvent(SnapshotEvent event) {
    Map<ObjectId, DataRow> modifiedDiffs = event.getModifiedDiffs();
    if (modifiedDiffs != null && !modifiedDiffs.isEmpty()) {
        for (Map.Entry<ObjectId, DataRow> entry : modifiedDiffs.entrySet()) {
            processUpdatedSnapshot(entry.getKey(), entry.getValue());
        }
    }
    Collection<ObjectId> deletedIDs = event.getDeletedIds();
    if (deletedIDs != null && !deletedIDs.isEmpty()) {
        for (ObjectId deletedID : deletedIDs) {
            processDeletedID(deletedID);
        }
    }
    processInvalidatedIDs(event.getInvalidatedIds());
    processIndirectlyModifiedIDs(event.getIndirectlyModifiedIds());
    // TODO: andrus, 3/28/2006 - 'SnapshotEventDecorator' serves as a bridge (or
    // rather a noop wrapper) between old snapshot events and new GraphEvents. Once
    // SnapshotEvents are replaced with GraphEvents (in 2.0) we won't need it
    GraphDiff diff = new SnapshotEventDecorator(event);
    ObjectContext originatingContext = (event.getPostedBy() instanceof ObjectContext) ? (ObjectContext) event.getPostedBy() : null;
    context.fireDataChannelChanged(originatingContext, diff);
}
Also used : ObjectId(org.apache.cayenne.ObjectId) GraphDiff(org.apache.cayenne.graph.GraphDiff) ObjectContext(org.apache.cayenne.ObjectContext) DataRow(org.apache.cayenne.DataRow) HashMap(java.util.HashMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 17 with GraphDiff

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

the class ClientChannelServerDiffsIT method testReturnDiffClientArcChanges.

@Test
public void testReturnDiffClientArcChanges() {
    final NoopGraphChangeHandler diffReader = new NoopGraphChangeHandler();
    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);
    ClientMtTable2 o2 = context.newObject(ClientMtTable2.class);
    o.addToTable2Array(o2);
    context.commitChanges();
    assertEquals(2, diffReader.size);
    diffReader.reset();
    ClientMtTable2 o3 = context.newObject(ClientMtTable2.class);
    o3.setTable1(o);
    context.commitChanges();
    assertEquals(1, diffReader.size);
}
Also used : ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ClientMtTable2(org.apache.cayenne.testdo.mt.ClientMtTable2) GraphDiff(org.apache.cayenne.graph.GraphDiff) ObjectContext(org.apache.cayenne.ObjectContext) CayenneContext(org.apache.cayenne.CayenneContext) MockEventManager(org.apache.cayenne.event.MockEventManager) Test(org.junit.Test)

Example 18 with GraphDiff

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

the class ClientChannelServerDiffsIT method testReturnIdDiff.

@Test
public void testReturnIdDiff() {
    final Object[] ids = new Object[2];
    final GraphChangeHandler diffReader = new NoopGraphChangeHandler() {

        @Override
        public void nodeIdChanged(Object oldId, Object newId) {
            ids[0] = oldId;
            ids[1] = newId;
        }
    };
    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);
    context.newObject(ClientMtTable1.class);
    context.commitChanges();
    assertTrue(ids[0] instanceof ObjectId);
    assertTrue(((ObjectId) ids[0]).isTemporary());
    assertTrue(ids[1] instanceof ObjectId);
    assertFalse(((ObjectId) ids[1]).isTemporary());
}
Also used : GraphDiff(org.apache.cayenne.graph.GraphDiff) ObjectId(org.apache.cayenne.ObjectId) GraphChangeHandler(org.apache.cayenne.graph.GraphChangeHandler) ObjectContext(org.apache.cayenne.ObjectContext) CayenneContext(org.apache.cayenne.CayenneContext) MockEventManager(org.apache.cayenne.event.MockEventManager) Test(org.junit.Test)

Example 19 with GraphDiff

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

the class NestedCayenneContextIT method testCommitChangesToParentOneToMany.

@Test
public void testCommitChangesToParentOneToMany() {
    ObjectContext child = runtime.newContext(clientContext);
    ClientMtTable1 master = child.newObject(ClientMtTable1.class);
    ClientMtTable2 dep = child.newObject(ClientMtTable2.class);
    master.addToTable2Array(dep);
    child.commitChangesToParent();
    ClientMtTable1 masterParent = (ClientMtTable1) clientContext.getGraphManager().getNode(master.getObjectId());
    ClientMtTable2 depParent = (ClientMtTable2) clientContext.getGraphManager().getNode(dep.getObjectId());
    assertNotNull(masterParent);
    assertNotNull(depParent);
    assertSame(masterParent, depParent.getTable1());
    assertTrue(masterParent.getTable2Array().contains(depParent));
    // check that arc changes got recorded in the parent context
    GraphDiff diffs = clientContext.internalGraphManager().getDiffs();
    final int[] arcDiffs = new int[1];
    final int[] newNodes = new int[1];
    diffs.apply(new GraphChangeHandler() {

        @Override
        public void arcCreated(Object nodeId, Object targetNodeId, ArcId arcId) {
            arcDiffs[0]++;
        }

        @Override
        public void arcDeleted(Object nodeId, Object targetNodeId, ArcId arcId) {
            arcDiffs[0]--;
        }

        @Override
        public void nodeCreated(Object nodeId) {
            newNodes[0]++;
        }

        @Override
        public void nodeIdChanged(Object nodeId, Object newId) {
        }

        @Override
        public void nodePropertyChanged(Object nodeId, String property, Object oldValue, Object newValue) {
        }

        @Override
        public void nodeRemoved(Object nodeId) {
            newNodes[0]--;
        }
    });
    assertEquals(2, newNodes[0]);
    assertEquals(2, arcDiffs[0]);
}
Also used : ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ClientMtTable2(org.apache.cayenne.testdo.mt.ClientMtTable2) ArcId(org.apache.cayenne.graph.ArcId) GraphDiff(org.apache.cayenne.graph.GraphDiff) GraphChangeHandler(org.apache.cayenne.graph.GraphChangeHandler) Test(org.junit.Test)

Example 20 with GraphDiff

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

the class NestedCayenneContextTooneIT method testCommitChangesToParentOneToOne.

@Test
public void testCommitChangesToParentOneToOne() throws Exception {
    ObjectContext child = runtime.newContext(clientContext);
    ClientTooneMaster master = child.newObject(ClientTooneMaster.class);
    ClientTooneDep dep = child.newObject(ClientTooneDep.class);
    master.setToDependent(dep);
    child.commitChangesToParent();
    ClientTooneMaster masterParent = (ClientTooneMaster) clientContext.getGraphManager().getNode(master.getObjectId());
    ClientTooneDep depParent = (ClientTooneDep) clientContext.getGraphManager().getNode(dep.getObjectId());
    assertNotNull(masterParent);
    assertNotNull(depParent);
    assertSame(masterParent, depParent.getToMaster());
    assertSame(depParent, masterParent.getToDependent());
    // check that arc changes got recorded in the parent context
    GraphDiff diffs = clientContext.internalGraphManager().getDiffs();
    final int[] arcDiffs = new int[1];
    final int[] newNodes = new int[1];
    diffs.apply(new GraphChangeHandler() {

        @Override
        public void arcCreated(Object nodeId, Object targetNodeId, ArcId arcId) {
            arcDiffs[0]++;
        }

        @Override
        public void arcDeleted(Object nodeId, Object targetNodeId, ArcId arcId) {
            arcDiffs[0]--;
        }

        @Override
        public void nodeCreated(Object nodeId) {
            newNodes[0]++;
        }

        @Override
        public void nodeIdChanged(Object nodeId, Object newId) {
        }

        @Override
        public void nodePropertyChanged(Object nodeId, String property, Object oldValue, Object newValue) {
        }

        @Override
        public void nodeRemoved(Object nodeId) {
            newNodes[0]--;
        }
    });
    assertEquals(2, newNodes[0]);
    assertEquals(2, arcDiffs[0]);
}
Also used : ClientTooneMaster(org.apache.cayenne.testdo.toone.ClientTooneMaster) ArcId(org.apache.cayenne.graph.ArcId) GraphDiff(org.apache.cayenne.graph.GraphDiff) ClientTooneDep(org.apache.cayenne.testdo.toone.ClientTooneDep) GraphChangeHandler(org.apache.cayenne.graph.GraphChangeHandler) Test(org.junit.Test)

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