Search in sources :

Example 26 with GraphDiff

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

the class CayenneContext method rollbackChanges.

@Override
public void rollbackChanges() {
    synchronized (graphManager) {
        if (graphManager.hasChanges()) {
            GraphDiff diff = graphManager.getDiffs();
            graphManager.graphReverted();
            channel.onSync(this, diff, DataChannel.ROLLBACK_CASCADE_SYNC);
            fireDataChannelRolledback(this, diff);
        }
    }
}
Also used : GraphDiff(org.apache.cayenne.graph.GraphDiff)

Example 27 with GraphDiff

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

the class CayenneContext method doCommitChanges.

GraphDiff doCommitChanges(boolean cascade) {
    int syncType = cascade ? DataChannel.FLUSH_CASCADE_SYNC : DataChannel.FLUSH_NOCASCADE_SYNC;
    GraphDiff commitDiff = null;
    synchronized (graphManager) {
        if (graphManager.hasChanges()) {
            if (isValidatingObjectsOnCommit()) {
                ValidationResult result = new ValidationResult();
                Iterator<?> it = graphManager.dirtyNodes().iterator();
                while (it.hasNext()) {
                    Persistent p = (Persistent) it.next();
                    if (p instanceof Validating) {
                        switch(p.getPersistenceState()) {
                            case PersistenceState.NEW:
                                ((Validating) p).validateForInsert(result);
                                break;
                            case PersistenceState.MODIFIED:
                                ((Validating) p).validateForUpdate(result);
                                break;
                            case PersistenceState.DELETED:
                                ((Validating) p).validateForDelete(result);
                                break;
                        }
                    }
                }
                if (result.hasFailures()) {
                    throw new ValidationException(result);
                }
            }
            graphManager.graphCommitStarted();
            GraphDiff changes = graphManager.getDiffsSinceLastFlush();
            try {
                commitDiff = channel.onSync(this, changes, syncType);
            } catch (Throwable th) {
                graphManager.graphCommitAborted();
                if (th instanceof CayenneRuntimeException) {
                    throw (CayenneRuntimeException) th;
                } else {
                    throw new CayenneRuntimeException("Commit error", th);
                }
            }
            graphManager.graphCommitted(commitDiff);
            // this event is caught by peer nested ObjectContexts to
            // synchronize the
            // state
            fireDataChannelCommitted(this, changes);
        }
    }
    return commitDiff;
}
Also used : ValidationException(org.apache.cayenne.validation.ValidationException) GraphDiff(org.apache.cayenne.graph.GraphDiff) ValidationResult(org.apache.cayenne.validation.ValidationResult)

Example 28 with GraphDiff

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

the class TransactionFilter method onSync.

@Override
public GraphDiff onSync(ObjectContext originatingContext, GraphDiff changes, int syncType, DataChannelSyncFilterChain filterChain) {
    DataChannelSyncCallbackAction callbackAction = DataChannelSyncCallbackAction.getCallbackAction(originatingContext.getChannel().getEntityResolver().getCallbackRegistry(), originatingContext.getGraphManager(), changes, syncType);
    callbackAction.applyPreCommit();
    GraphDiff result;
    switch(syncType) {
        case DataChannel.ROLLBACK_CASCADE_SYNC:
            result = filterChain.onSync(originatingContext, changes, syncType);
            break;
        // including transaction handling logic
        case DataChannel.FLUSH_NOCASCADE_SYNC:
        case DataChannel.FLUSH_CASCADE_SYNC:
            result = transactionManager.performInTransaction(() -> filterChain.onSync(originatingContext, changes, syncType));
            break;
        default:
            throw new CayenneRuntimeException("Invalid synchronization type: %d", syncType);
    }
    callbackAction.applyPostCommit();
    return result;
}
Also used : GraphDiff(org.apache.cayenne.graph.GraphDiff) DataChannelSyncCallbackAction(org.apache.cayenne.DataChannelSyncCallbackAction) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException)

Example 29 with GraphDiff

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

the class DataContextCommitIT method testFlushToParent_Commit_New.

@Test
public void testFlushToParent_Commit_New() {
    // commit new object
    Artist a = context.newObject(Artist.class);
    a.setArtistName("Test");
    assertTrue(context.hasChanges());
    ObjectId beforeId = a.getObjectId();
    GraphDiff diff = context.flushToParent(true);
    ObjectId afterId = a.getObjectId();
    assertNotNull(diff);
    assertFalse(context.hasChanges());
    assertNotEquals(beforeId, afterId);
    GraphChangeHandler diffChecker = mock(GraphChangeHandler.class);
    diff.apply(diffChecker);
    verify(diffChecker).nodeIdChanged(beforeId, afterId);
    verifyZeroInteractions(diffChecker);
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) ObjectId(org.apache.cayenne.ObjectId) GraphDiff(org.apache.cayenne.graph.GraphDiff) GraphChangeHandler(org.apache.cayenne.graph.GraphChangeHandler) Test(org.junit.Test)

Example 30 with GraphDiff

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

the class SyncMessageTest method testHessianSerialization.

@Test
public void testHessianSerialization() throws Exception {
    // id must be a serializable object; source doesn't have to be
    ObjectContext source = mock(ObjectContext.class);
    GraphDiff diff = new NodeCreateOperation("id-string");
    SyncMessage message = new SyncMessage(source, DataChannel.FLUSH_NOCASCADE_SYNC, diff);
    Object d = HessianUtil.cloneViaClientServerSerialization(message, new EntityResolver());
    assertNotNull(d);
    assertTrue(d instanceof SyncMessage);
    SyncMessage ds = (SyncMessage) d;
    assertNull(ds.getSource());
    assertEquals(message.getType(), ds.getType());
    assertNotNull(ds.getSenderChanges());
}
Also used : NodeCreateOperation(org.apache.cayenne.graph.NodeCreateOperation) GraphDiff(org.apache.cayenne.graph.GraphDiff) ObjectContext(org.apache.cayenne.ObjectContext) EntityResolver(org.apache.cayenne.map.EntityResolver) 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