use of org.apache.cayenne.graph.CompoundDiff in project cayenne by apache.
the class DataContext method onContextFlush.
@Override
protected GraphDiff onContextFlush(ObjectContext originatingContext, GraphDiff changes, boolean cascade) {
boolean childContext = this != originatingContext && changes != null;
try {
if (childContext) {
getObjectStore().childContextSyncStarted();
changes.apply(new ChildDiffLoader(this));
fireDataChannelChanged(originatingContext, changes);
}
return (cascade) ? flushToParent(true) : new CompoundDiff();
} finally {
if (childContext) {
getObjectStore().childContextSyncStopped();
}
}
}
use of org.apache.cayenne.graph.CompoundDiff in project cayenne by apache.
the class DataDomainFlushAction method flush.
GraphDiff flush(DataContext context, GraphDiff changes) {
if (changes == null) {
return new CompoundDiff();
}
// TODO: Andrus, 3/13/2006 - support categorizing an arbitrary diff
if (!(changes instanceof ObjectStoreGraphDiff)) {
throw new IllegalArgumentException("Expected 'ObjectStoreGraphDiff', got: " + changes.getClass().getName());
}
this.context = context;
// ObjectStoreGraphDiff contains changes already categorized by objectId...
this.changesByObjectId = ((ObjectStoreGraphDiff) changes).getChangesByObjectId();
this.insertBucket = new DataDomainInsertBucket(this);
this.deleteBucket = new DataDomainDeleteBucket(this);
this.updateBucket = new DataDomainUpdateBucket(this);
this.flattenedBucket = new DataDomainFlattenedBucket(this);
this.queries = new ArrayList<>();
this.resultIndirectlyModifiedIds = new HashSet<>();
preprocess(context, changes);
if (queries.isEmpty()) {
return new CompoundDiff();
}
this.resultDiff = new CompoundDiff();
this.resultDeletedIds = new ArrayList<>();
this.resultModifiedSnapshots = new HashMap<>();
runQueries();
postprocess(context);
return resultDiff;
}
use of org.apache.cayenne.graph.CompoundDiff in project cayenne by apache.
the class ObjectStoreGraphDiff method resolveDiff.
/**
* Converts diffs organized by ObjectId in a collection of diffs sorted by
* diffId (same as creation order).
*/
private void resolveDiff() {
// changed the the last time we cached the changes.
if (resolvedDiff == null || lastSeenDiffId < objectStore.currentDiffId) {
CompoundDiff diff = new CompoundDiff();
Map<Object, ObjectDiff> changes = getChangesByObjectId();
if (!changes.isEmpty()) {
List<NodeDiff> allChanges = new ArrayList<>(changes.size() * 2);
for (final ObjectDiff objectDiff : changes.values()) {
objectDiff.appendDiffs(allChanges);
}
Collections.sort(allChanges);
diff.addAll(allChanges);
}
this.lastSeenDiffId = objectStore.currentDiffId;
this.resolvedDiff = diff;
}
}
use of org.apache.cayenne.graph.CompoundDiff in project cayenne by apache.
the class ClientChannel method onSync.
public GraphDiff onSync(ObjectContext originatingContext, GraphDiff changes, int syncType) {
DataChannelSyncCallbackAction callbackAction = DataChannelSyncCallbackAction.getCallbackAction(getEntityResolver().getCallbackRegistry(), originatingContext.getGraphManager(), changes, syncType);
callbackAction.applyPreCommit();
changes = diffCompressor.compress(changes);
GraphDiff replyDiff = send(new SyncMessage(originatingContext, syncType, changes), GraphDiff.class);
if (channelEventsEnabled) {
EventSubject subject;
switch(syncType) {
case DataChannel.ROLLBACK_CASCADE_SYNC:
subject = DataChannel.GRAPH_ROLLEDBACK_SUBJECT;
break;
case DataChannel.FLUSH_NOCASCADE_SYNC:
subject = DataChannel.GRAPH_CHANGED_SUBJECT;
break;
case DataChannel.FLUSH_CASCADE_SYNC:
subject = DataChannel.GRAPH_FLUSHED_SUBJECT;
break;
default:
subject = null;
}
if (subject != null) {
// combine message sender changes and message receiver changes into a
// single event
boolean sentNoop = changes == null || changes.isNoop();
boolean receivedNoop = replyDiff == null || replyDiff.isNoop();
if (!sentNoop || !receivedNoop) {
CompoundDiff notification = new CompoundDiff();
if (!sentNoop) {
notification.add(changes);
}
if (!receivedNoop) {
notification.add(replyDiff);
}
GraphEvent e = new GraphEvent(this, originatingContext, notification);
eventManager.postEvent(e, subject);
}
}
}
callbackAction.applyPostCommit();
return replyDiff;
}
use of org.apache.cayenne.graph.CompoundDiff in project cayenne by apache.
the class CayenneContextIT method testCommitChangesNew.
@Test
public void testCommitChangesNew() {
final CompoundDiff diff = new CompoundDiff();
final Object newObjectId = new ObjectId("test", "key", "generated");
eventManager = new DefaultEventManager(0);
// test that ids that are passed back are actually propagated to the
// right
// objects...
MockDataChannel channel = new MockDataChannel() {
@Override
public GraphDiff onSync(ObjectContext originatingContext, GraphDiff changes, int syncType) {
return diff;
}
// must provide a channel with working event manager
@Override
public EventManager getEventManager() {
return eventManager;
}
};
CayenneContext context = new CayenneContext(channel);
ObjEntity entity = new ObjEntity("test_entity");
entity.setClassName(MockPersistentObject.class.getName());
DataMap dataMap = new DataMap("test");
dataMap.addObjEntity(entity);
Collection<DataMap> entities = Collections.singleton(dataMap);
context.setEntityResolver(new EntityResolver(entities));
Persistent object = context.newObject(MockPersistentObject.class);
// record change here to make it available to the anonymous connector
// method..
diff.add(new NodeIdChangeOperation(object.getObjectId(), newObjectId));
// check that a generated object ID is assigned back to the object...
assertNotSame(newObjectId, object.getObjectId());
context.commitChanges();
assertSame(newObjectId, object.getObjectId());
assertSame(object, context.graphManager.getNode(newObjectId));
}
Aggregations