use of org.apache.cayenne.graph.GraphChangeHandler in project cayenne by apache.
the class NestedCayenneContextIT method testCommitChangesToParent.
@Test
public void testCommitChangesToParent() {
clientContext.newObject(ClientMtTable1.class);
clientContext.newObject(ClientMtTable1.class);
clientContext.newObject(ClientMtTable1.class);
clientContext.newObject(ClientMtTable1.class);
clientContext.commitChanges();
final ObjectContext child = runtime.newContext(clientContext);
List<ClientMtTable1> objects = ObjectSelect.query(ClientMtTable1.class).select(child);
assertEquals(4, objects.size());
final ClientMtTable1 childNew = child.newObject(ClientMtTable1.class);
childNew.setGlobalAttribute1("NNN");
final ClientMtTable1 childModified = objects.get(0);
childModified.setGlobalAttribute1("MMM");
final ClientMtTable1 childCommitted = objects.get(1);
final ClientMtTable1 childHollow = objects.get(3);
child.invalidateObjects(childHollow);
queryInterceptor.runWithQueriesBlocked(() -> {
child.commitChangesToParent();
// * all modified child objects must be in committed state now
// * all modifications should be propagated to the parent
// * no actual commit should occur.
assertEquals(PersistenceState.COMMITTED, childNew.getPersistenceState());
assertEquals(PersistenceState.COMMITTED, childModified.getPersistenceState());
assertEquals(PersistenceState.COMMITTED, childCommitted.getPersistenceState());
assertEquals(PersistenceState.HOLLOW, childHollow.getPersistenceState());
ClientMtTable1 parentNew = (ClientMtTable1) clientContext.getGraphManager().getNode(childNew.getObjectId());
final ClientMtTable1 parentModified = (ClientMtTable1) clientContext.getGraphManager().getNode(childModified.getObjectId());
ClientMtTable1 parentCommitted = (ClientMtTable1) clientContext.getGraphManager().getNode(childCommitted.getObjectId());
ClientMtTable1 parentHollow = (ClientMtTable1) clientContext.getGraphManager().getNode(childHollow.getObjectId());
assertNotNull(parentNew);
assertEquals(PersistenceState.NEW, parentNew.getPersistenceState());
assertEquals("NNN", parentNew.getGlobalAttribute1());
assertNotNull(parentModified);
assertEquals(PersistenceState.MODIFIED, parentModified.getPersistenceState());
assertEquals("MMM", parentModified.getGlobalAttribute1());
assertNotNull(parentCommitted);
assertEquals(PersistenceState.COMMITTED, parentCommitted.getPersistenceState());
assertNotNull(parentHollow);
// check that arc changes got recorded in the parent context
GraphDiff diffs = clientContext.internalGraphManager().getDiffs();
final int[] modifiedProperties = new int[1];
diffs.apply(new GraphChangeHandler() {
@Override
public void arcCreated(Object nodeId, Object targetNodeId, ArcId arcId) {
}
@Override
public void arcDeleted(Object nodeId, Object targetNodeId, ArcId arcId) {
}
@Override
public void nodeCreated(Object nodeId) {
}
@Override
public void nodeIdChanged(Object nodeId, Object newId) {
}
@Override
public void nodePropertyChanged(Object nodeId, String property, Object oldValue, Object newValue) {
if (nodeId.equals(parentModified.getObjectId())) {
modifiedProperties[0]++;
}
}
@Override
public void nodeRemoved(Object nodeId) {
}
});
assertEquals(1, modifiedProperties[0]);
});
}
use of org.apache.cayenne.graph.GraphChangeHandler in project cayenne by apache.
the class NestedCayenneContextIT method testNullifyToOne.
/**
* A test case for CAY-698 bug.
*/
@Test
public void testNullifyToOne() {
ClientMtTable1 a = clientContext.newObject(ClientMtTable1.class);
ClientMtTable2 b = clientContext.newObject(ClientMtTable2.class);
a.addToTable2Array(b);
clientContext.commitChanges();
ObjectContext child = runtime.newContext(clientContext);
ObjectContext childPeer = runtime.newContext(clientContext);
ClientMtTable2 childP1 = SelectById.query(ClientMtTable2.class, b.getObjectId()).selectOne(child);
// trigger object creation in the peer nested DC
Cayenne.objectForPK(childPeer, b.getObjectId());
childP1.setTable1(null);
queryInterceptor.runWithQueriesBlocked(() -> {
child.commitChangesToParent();
assertEquals(PersistenceState.COMMITTED, childP1.getPersistenceState());
ClientMtTable2 parentP1 = (ClientMtTable2) clientContext.getGraphManager().getNode(childP1.getObjectId());
assertNotNull(parentP1);
assertEquals(PersistenceState.MODIFIED, parentP1.getPersistenceState());
assertNull(parentP1.getTable1());
// check that arc changes got recorded in the parent context
GraphDiff diffs = clientContext.internalGraphManager().getDiffs();
final int[] arcDiffs = new int[1];
diffs.apply(new GraphChangeHandler() {
@Override
public void arcCreated(Object nodeId, Object targetNodeId, ArcId arcId) {
arcDiffs[0]++;
}
@Override
public void arcDeleted(Object nodeId, Object targetNodeId, ArcId arcId) {
arcDiffs[0]--;
}
@Override
public void nodeCreated(Object nodeId) {
}
@Override
public void nodeIdChanged(Object nodeId, Object newId) {
}
@Override
public void nodePropertyChanged(Object nodeId, String property, Object oldValue, Object newValue) {
}
@Override
public void nodeRemoved(Object nodeId) {
}
});
assertEquals(-2, arcDiffs[0]);
});
}
use of org.apache.cayenne.graph.GraphChangeHandler in project cayenne by apache.
the class DataContextCommitIT method testFlushToParent_Commit_Mix.
@Test
public void testFlushToParent_Commit_Mix() {
Artist a = context.newObject(Artist.class);
a.setArtistName("Test");
context.flushToParent(true);
// commit a mix of new and modified
Painting p = context.newObject(Painting.class);
p.setPaintingTitle("PT");
p.setToArtist(a);
a.setArtistName("Test_");
ObjectId beforeId = p.getObjectId();
GraphDiff diff = context.flushToParent(true);
ObjectId afterId = p.getObjectId();
assertNotNull(diff);
assertFalse(context.hasChanges());
GraphChangeHandler diffChecker = mock(GraphChangeHandler.class);
diff.apply(diffChecker);
verify(diffChecker).nodeIdChanged(beforeId, afterId);
verifyZeroInteractions(diffChecker);
}
use of org.apache.cayenne.graph.GraphChangeHandler in project cayenne by apache.
the class CommitLogFilter method beforeCommit.
private void beforeCommit(MutableChangeMap changes, DataChannel channel, GraphDiff contextDiff) {
// capture snapshots of deleted objects before they are purged from cache
GraphChangeHandler handler = new DiffFilter(entityFactory, new DeletedDiffProcessor(changes, channel, entityFactory));
contextDiff.apply(handler);
}
use of org.apache.cayenne.graph.GraphChangeHandler in project cayenne by apache.
the class ObjectStore method postprocessAfterCommit.
/**
* Internal unsynchronized method to process objects state after commit.
*
* @since 1.2
*/
public void postprocessAfterCommit(GraphDiff parentChanges) {
// scan through changed objects, set persistence state to committed
for (Object id : changes.keySet()) {
Persistent object = objectMap.get(id);
switch(object.getPersistenceState()) {
case PersistenceState.DELETED:
objectMap.remove(id);
object.setObjectContext(null);
object.setPersistenceState(PersistenceState.TRANSIENT);
break;
case PersistenceState.NEW:
case PersistenceState.MODIFIED:
object.setPersistenceState(PersistenceState.COMMITTED);
break;
}
}
// re-register changed object ids
if (!parentChanges.isNoop()) {
parentChanges.apply(new GraphChangeHandler() {
@Override
public void arcCreated(Object nodeId, Object targetNodeId, ArcId arcId) {
}
@Override
public void arcDeleted(Object nodeId, Object targetNodeId, ArcId arcId) {
}
@Override
public void nodeCreated(Object nodeId) {
}
@Override
public void nodeIdChanged(Object nodeId, Object newId) {
processIdChange(nodeId, newId);
}
@Override
public void nodePropertyChanged(Object nodeId, String property, Object oldValue, Object newValue) {
}
@Override
public void nodeRemoved(Object nodeId) {
}
});
}
// create new instance of changes map so that event listeners who stored the
// original diff don't get affected
this.changes = new HashMap<>();
}
Aggregations