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);
}
}
}
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;
}
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;
}
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);
}
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());
}
Aggregations