Search in sources :

Example 1 with NodeIdChangeOperation

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));
}
Also used : NodeIdChangeOperation(org.apache.cayenne.graph.NodeIdChangeOperation) GraphDiff(org.apache.cayenne.graph.GraphDiff) DefaultEventManager(org.apache.cayenne.event.DefaultEventManager) EntityResolver(org.apache.cayenne.map.EntityResolver) DataMap(org.apache.cayenne.map.DataMap) ObjEntity(org.apache.cayenne.map.ObjEntity) CompoundDiff(org.apache.cayenne.graph.CompoundDiff) Test(org.junit.Test)

Example 2 with NodeIdChangeOperation

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));
}
Also used : NodeIdChangeOperation(org.apache.cayenne.graph.NodeIdChangeOperation)

Example 3 with NodeIdChangeOperation

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));
    }
}
Also used : NodeIdChangeOperation(org.apache.cayenne.graph.NodeIdChangeOperation) ObjectId(org.apache.cayenne.ObjectId) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) Persistent(org.apache.cayenne.Persistent) AttributeProperty(org.apache.cayenne.reflect.AttributeProperty)

Example 4 with NodeIdChangeOperation

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);
                    }
                }
            }
        }
    }
}
Also used : NodeIdChangeOperation(org.apache.cayenne.graph.NodeIdChangeOperation) ArcProperty(org.apache.cayenne.reflect.ArcProperty) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ObjectId(org.apache.cayenne.ObjectId) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) ToManyMapProperty(org.apache.cayenne.reflect.ToManyMapProperty) Persistent(org.apache.cayenne.Persistent) AttributeProperty(org.apache.cayenne.reflect.AttributeProperty) DataRow(org.apache.cayenne.DataRow) DataObject(org.apache.cayenne.DataObject) ArrayList(java.util.ArrayList) List(java.util.List) DataObject(org.apache.cayenne.DataObject) HashMap(java.util.HashMap) Map(java.util.Map) CompoundDiff(org.apache.cayenne.graph.CompoundDiff)

Aggregations

NodeIdChangeOperation (org.apache.cayenne.graph.NodeIdChangeOperation)4 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)2 ObjectId (org.apache.cayenne.ObjectId)2 Persistent (org.apache.cayenne.Persistent)2 CompoundDiff (org.apache.cayenne.graph.CompoundDiff)2 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 DataObject (org.apache.cayenne.DataObject)1 DataRow (org.apache.cayenne.DataRow)1 DefaultEventManager (org.apache.cayenne.event.DefaultEventManager)1 GraphDiff (org.apache.cayenne.graph.GraphDiff)1 DataMap (org.apache.cayenne.map.DataMap)1 EntityResolver (org.apache.cayenne.map.EntityResolver)1 ObjEntity (org.apache.cayenne.map.ObjEntity)1 ArcProperty (org.apache.cayenne.reflect.ArcProperty)1 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)1 ToManyMapProperty (org.apache.cayenne.reflect.ToManyMapProperty)1