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