use of org.apache.cayenne.graph.NodeIdChangeOperation in project cayenne by apache.
the class CayenneContextIT method testCommitChangesNew.
@Test
public void testCommitChangesNew() {
final CompoundDiff diff = new CompoundDiff();
final Object newObjectId = ObjectId.of("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));
}
use of org.apache.cayenne.graph.NodeIdChangeOperation in project cayenne by apache.
the class CayenneContextGraphManager method nodeIdChanged.
// ****** GraphChangeHandler API ******
// =====================================================
@Override
public synchronized void nodeIdChanged(Object nodeId, Object newId) {
stateLog.nodeIdChanged(nodeId, newId);
processChange(new NodeIdChangeOperation(nodeId, newId));
}
use of org.apache.cayenne.graph.NodeIdChangeOperation in project cayenne by apache.
the class ReplacementIdVisitor method updateId.
private void updateId(DbRowOp dbRow) {
ObjectId id = dbRow.getChangeId();
Persistent object = dbRow.getObject();
// check that PK was generated or set properly
if (!id.isReplacementIdAttached()) {
if (id == dbRow.getObject().getObjectId() && id.isTemporary()) {
throw new CayenneRuntimeException("PK for the object %s is not set during insert.", object);
}
return;
}
Map<String, Object> replacement = id.getReplacementIdMap();
replacement.forEach((attr, val) -> {
if (val instanceof IdGenerationMarker) {
throw new CayenneRuntimeException("PK for the object %s is not set during insert.", object);
}
});
ObjectId replacementId = id.createReplacementId();
if (object.getObjectId() == id && !replacementId.getEntityName().startsWith(ASTDbPath.DB_PREFIX)) {
object.setObjectId(replacementId);
// update meaningful PKs
for (AttributeProperty property : resolver.getClassDescriptor(replacementId.getEntityName()).getIdProperties()) {
if (property.getAttribute() != null) {
Object value = replacement.get(property.getAttribute().getDbAttributeName());
if (value != null) {
property.writePropertyDirectly(object, null, value);
}
}
}
result.add(new NodeIdChangeOperation(id, replacementId));
}
}
use of org.apache.cayenne.graph.NodeIdChangeOperation in project cayenne by apache.
the class DataDomainSyncBucket method postprocess.
void postprocess() {
if (!objectsByDescriptor.isEmpty()) {
CompoundDiff result = parent.getResultDiff();
Map<ObjectId, DataRow> modifiedSnapshots = parent.getResultModifiedSnapshots();
Collection<ObjectId> deletedIds = parent.getResultDeletedIds();
for (Map.Entry<ClassDescriptor, List<Persistent>> entry : objectsByDescriptor.entrySet()) {
ClassDescriptor descriptor = entry.getKey();
for (Persistent object : entry.getValue()) {
ObjectId id = object.getObjectId();
ObjectId finalId;
// record id change and update attributes for generated ids
if (id.isReplacementIdAttached()) {
Map<String, Object> replacement = id.getReplacementIdMap();
for (AttributeProperty property : descriptor.getIdProperties()) {
Object value = replacement.get(property.getAttribute().getDbAttributeName());
// if the id wasn't generated. We may need to optimize it...
if (value != null) {
property.writePropertyDirectly(object, null, value);
}
}
ObjectId replacementId = id.createReplacementId();
result.add(new NodeIdChangeOperation(id, replacementId));
// DataRowCache has no notion of replaced id...
if (!id.isTemporary()) {
deletedIds.add(id);
}
finalId = replacementId;
} else if (id.isTemporary()) {
throw new CayenneRuntimeException("Temporary ID hasn't been replaced on commit: %s", object);
} else {
finalId = id;
}
// do not take the snapshot until generated columns are processed (see code above)
DataRow dataRow = parent.getContext().currentSnapshot(object);
if (object instanceof DataObject) {
DataObject dataObject = (DataObject) object;
dataRow.setReplacesVersion(dataObject.getSnapshotVersion());
dataObject.setSnapshotVersion(dataRow.getVersion());
}
modifiedSnapshots.put(finalId, dataRow);
// update Map reverse relationships
for (ArcProperty arc : descriptor.getMapArcProperties()) {
ToManyMapProperty reverseArc = (ToManyMapProperty) arc.getComplimentaryReverseArc();
// must resolve faults... hopefully for to-one this will not cause extra fetches...
Object source = arc.readProperty(object);
if (source != null && !reverseArc.isFault(source)) {
remapTarget(reverseArc, source, object);
}
}
}
}
}
}
Aggregations