Search in sources :

Example 21 with GraphDiff

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

the class ServerRuntimeTest method testGetDataChannel_CustomModule.

@Test
public void testGetDataChannel_CustomModule() {
    final DataChannel channel = new DataChannel() {

        public EntityResolver getEntityResolver() {
            return null;
        }

        public EventManager getEventManager() {
            return null;
        }

        public QueryResponse onQuery(ObjectContext originatingContext, Query query) {
            return null;
        }

        public GraphDiff onSync(ObjectContext originatingContext, GraphDiff changes, int syncType) {
            return null;
        }
    };
    Module module = binder -> binder.bind(DataChannel.class).toInstance(channel);
    ServerRuntime runtime = new ServerRuntime(Collections.singleton(module));
    assertSame(channel, runtime.getChannel());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ObjectContext(org.apache.cayenne.ObjectContext) Arrays(java.util.Arrays) DataContext(org.apache.cayenne.access.DataContext) Module(org.apache.cayenne.di.Module) BaseTransaction(org.apache.cayenne.tx.BaseTransaction) EntityResolver(org.apache.cayenne.map.EntityResolver) Arrays.asList(java.util.Arrays.asList) GraphDiff(org.apache.cayenne.graph.GraphDiff) TransactionFactory(org.apache.cayenne.tx.TransactionFactory) Key(org.apache.cayenne.di.Key) Collection(java.util.Collection) Constants(org.apache.cayenne.configuration.Constants) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) DataChannel(org.apache.cayenne.DataChannel) Query(org.apache.cayenne.query.Query) TransactionDescriptor(org.apache.cayenne.tx.TransactionDescriptor) List(java.util.List) ObjectContextFactory(org.apache.cayenne.configuration.ObjectContextFactory) EventManager(org.apache.cayenne.event.EventManager) QueryResponse(org.apache.cayenne.QueryResponse) TransactionalOperation(org.apache.cayenne.tx.TransactionalOperation) Assert(org.junit.Assert) Collections(java.util.Collections) Mockito.mock(org.mockito.Mockito.mock) DataChannel(org.apache.cayenne.DataChannel) Query(org.apache.cayenne.query.Query) GraphDiff(org.apache.cayenne.graph.GraphDiff) ObjectContext(org.apache.cayenne.ObjectContext) Module(org.apache.cayenne.di.Module) Test(org.junit.Test)

Example 22 with GraphDiff

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

the class DataContext method flushToParent.

/**
 * Synchronizes with the parent channel, performing a flush or a commit.
 *
 * @since 1.2
 */
GraphDiff flushToParent(boolean cascade) {
    if (this.getChannel() == null) {
        throw new CayenneRuntimeException("Cannot commit changes - channel is not set.");
    }
    int syncType = cascade ? DataChannel.FLUSH_CASCADE_SYNC : DataChannel.FLUSH_NOCASCADE_SYNC;
    ObjectStore objectStore = getObjectStore();
    GraphDiff parentChanges = null;
    // prevent multiple commits occurring simultaneously
    synchronized (objectStore) {
        ObjectStoreGraphDiff changes = objectStore.getChanges();
        boolean noop = isValidatingObjectsOnCommit() ? changes.validateAndCheckNoop() : changes.isNoop();
        if (noop) {
            // need to clear phantom changes
            objectStore.postprocessAfterPhantomCommit();
        } else {
            try {
                parentChanges = getChannel().onSync(this, changes, syncType);
                // Pending better callback design .....
                if (objectStore.hasChanges()) {
                    objectStore.postprocessAfterCommit(parentChanges);
                }
                // this event is caught by peer nested DataContexts to
                // synchronize the
                // state
                fireDataChannelCommitted(this, changes);
            }// "catch" is needed to unwrap OptimisticLockExceptions
             catch (CayenneRuntimeException ex) {
                Throwable unwound = Util.unwindException(ex);
                if (unwound instanceof CayenneRuntimeException) {
                    throw (CayenneRuntimeException) unwound;
                } else {
                    throw new CayenneRuntimeException("Commit Exception", unwound);
                }
            }
        }
        // merge changes from parent as well as changes caused by lifecycle
        // event
        // callbacks/listeners...
        CompoundDiff diff = new CompoundDiff();
        diff.addAll(objectStore.getLifecycleEventInducedChanges());
        if (parentChanges != null) {
            diff.add(parentChanges);
        }
        // ObjectIds with permanent
        if (!diff.isNoop()) {
            fireDataChannelCommitted(getChannel(), diff);
        }
        return diff;
    }
}
Also used : GraphDiff(org.apache.cayenne.graph.GraphDiff) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) CompoundDiff(org.apache.cayenne.graph.CompoundDiff)

Example 23 with GraphDiff

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

the class DataContext method rollbackChanges.

/**
 * Reverts any changes that have occurred to objects registered with
 * DataContext; also performs cascading rollback of all parent DataContexts.
 */
@Override
public void rollbackChanges() {
    if (objectStore.hasChanges()) {
        GraphDiff diff = getObjectStore().getChanges();
        if (channel != null) {
            channel.onSync(this, diff, DataChannel.ROLLBACK_CASCADE_SYNC);
        }
        getObjectStore().objectsRolledBack();
        fireDataChannelRolledback(this, diff);
    } else {
        if (channel != null) {
            channel.onSync(this, new CompoundDiff(), DataChannel.ROLLBACK_CASCADE_SYNC);
        }
    }
}
Also used : GraphDiff(org.apache.cayenne.graph.GraphDiff) CompoundDiff(org.apache.cayenne.graph.CompoundDiff)

Example 24 with GraphDiff

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

the class DataContextMergeHandler method graphChanged.

// *** GraphEventListener methods
@Override
public void graphChanged(GraphEvent event) {
    // parent received external change
    if (shouldProcessEvent(event)) {
        // temp kludge - see TODO in ObjectStore.snapshotsChanged(..)
        GraphDiff diff = event.getDiff();
        if (diff instanceof SnapshotEventDecorator) {
            SnapshotEvent decoratedEvent = ((SnapshotEventDecorator) diff).getEvent();
            context.getObjectStore().processSnapshotEvent(decoratedEvent);
        } else {
            synchronized (context.getObjectStore()) {
                diff.apply(this);
            }
        }
        // repost channel change event for our own children
        context.fireDataChannelChanged(event.getPostedBy(), event.getDiff());
    }
}
Also used : GraphDiff(org.apache.cayenne.graph.GraphDiff) SnapshotEventDecorator(org.apache.cayenne.access.ObjectStore.SnapshotEventDecorator) SnapshotEvent(org.apache.cayenne.access.event.SnapshotEvent)

Example 25 with GraphDiff

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

the class CayenneContext method rollbackChangesLocally.

@Override
public void rollbackChangesLocally() {
    synchronized (graphManager) {
        if (graphManager.hasChanges()) {
            GraphDiff diff = graphManager.getDiffs();
            graphManager.graphReverted();
            fireDataChannelRolledback(this, diff);
        }
    }
}
Also used : 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