Search in sources :

Example 31 with GraphDiff

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

the class CommitLogFilter method onSync.

@Override
public GraphDiff onSync(ObjectContext originatingContext, GraphDiff beforeDiff, int syncType, DataChannelSyncFilterChain filterChain) {
    // process commits only; skip rollback
    if (syncType != DataChannel.FLUSH_CASCADE_SYNC && syncType != DataChannel.FLUSH_NOCASCADE_SYNC) {
        return filterChain.onSync(originatingContext, beforeDiff, syncType);
    }
    // don't collect changes if there are no listeners
    if (listeners.isEmpty()) {
        return filterChain.onSync(originatingContext, beforeDiff, syncType);
    }
    MutableChangeMap changes = new MutableChangeMap();
    // passing DataDomain, not ObjectContext to speed things up
    // and avoid capturing changed state when fetching snapshots
    DataChannel channel = originatingContext.getChannel();
    beforeCommit(changes, channel, beforeDiff);
    GraphDiff afterDiff = filterChain.onSync(originatingContext, beforeDiff, syncType);
    afterCommit(changes, channel, beforeDiff, afterDiff);
    notifyListeners(originatingContext, changes);
    return afterDiff;
}
Also used : DataChannel(org.apache.cayenne.DataChannel) GraphDiff(org.apache.cayenne.graph.GraphDiff) MutableChangeMap(org.apache.cayenne.commitlog.model.MutableChangeMap)

Example 32 with GraphDiff

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

the class DataDomainFiltersIT method testOnSync_FilterOrdering.

@Test
public void testOnSync_FilterOrdering() {
    DataDomain domain = runtime.getDataDomain();
    List<String> results = new ArrayList<>();
    DataChannelSyncFilter f1 = (originatingContext, changes, syncType, filterChain) -> {
        results.add("f1start");
        GraphDiff response = filterChain.onSync(originatingContext, changes, syncType);
        results.add("f1end");
        return response;
    };
    DataChannelSyncFilter f2 = (originatingContext, changes, syncType, filterChain) -> {
        results.add("f2start");
        GraphDiff response = filterChain.onSync(originatingContext, changes, syncType);
        results.add("f2end");
        return response;
    };
    domain.syncFilters.add(f1);
    domain.syncFilters.add(f2);
    Artist a = context.newObject(Artist.class);
    a.setArtistName("AAA");
    // testing domain.onSync indirectly
    context.commitChanges();
    assertEquals(4, results.size());
    assertEquals("f2start", results.get(0));
    assertEquals("f1start", results.get(1));
    assertEquals("f1end", results.get(2));
    assertEquals("f2end", results.get(3));
}
Also used : ObjectContext(org.apache.cayenne.ObjectContext) DataChannelSyncFilter(org.apache.cayenne.DataChannelSyncFilter) DataChannelSyncFilterChain(org.apache.cayenne.DataChannelSyncFilterChain) ServerCase(org.apache.cayenne.unit.di.server.ServerCase) Assert.assertNotNull(org.junit.Assert.assertNotNull) Test(org.junit.Test) CayenneProjects(org.apache.cayenne.unit.di.server.CayenneProjects) ArrayList(java.util.ArrayList) Query(org.apache.cayenne.query.Query) Artist(org.apache.cayenne.testdo.testmap.Artist) Assert.assertSame(org.junit.Assert.assertSame) DataChannelQueryFilterChain(org.apache.cayenne.DataChannelQueryFilterChain) List(java.util.List) PostPersist(org.apache.cayenne.annotation.PostPersist) Inject(org.apache.cayenne.di.Inject) GraphDiff(org.apache.cayenne.graph.GraphDiff) QueryResponse(org.apache.cayenne.QueryResponse) DataChannelQueryFilter(org.apache.cayenne.DataChannelQueryFilter) ObjectSelect(org.apache.cayenne.query.ObjectSelect) ServerRuntime(org.apache.cayenne.configuration.server.ServerRuntime) UseServerRuntime(org.apache.cayenne.unit.di.server.UseServerRuntime) Assert.assertEquals(org.junit.Assert.assertEquals) ListResponse(org.apache.cayenne.util.ListResponse) Artist(org.apache.cayenne.testdo.testmap.Artist) GraphDiff(org.apache.cayenne.graph.GraphDiff) ArrayList(java.util.ArrayList) DataChannelSyncFilter(org.apache.cayenne.DataChannelSyncFilter) 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