Search in sources :

Example 11 with CompoundDiff

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

Example 12 with CompoundDiff

use of org.apache.cayenne.graph.CompoundDiff 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)

Example 13 with CompoundDiff

use of org.apache.cayenne.graph.CompoundDiff in project cayenne by apache.

the class ObjectContextChangeLogSubListMessageFactoryTest method testGetDiffsSerializable.

@Test
public void testGetDiffsSerializable() throws Exception {
    ObjectContextChangeLog recorder = new ObjectContextChangeLog();
    recorder.addOperation(new NodeCreateOperation(new ObjectId("test")));
    CompoundDiff diff = (CompoundDiff) recorder.getDiffs();
    byte[] data = serializationService.serialize(diff);
    CompoundDiff diff0 = serializationService.deserialize(data, CompoundDiff.class);
    assertNotNull(diff0);
    assertEquals(1, diff0.getDiffs().size());
}
Also used : NodeCreateOperation(org.apache.cayenne.graph.NodeCreateOperation) CompoundDiff(org.apache.cayenne.graph.CompoundDiff) Test(org.junit.Test)

Example 14 with CompoundDiff

use of org.apache.cayenne.graph.CompoundDiff in project cayenne by apache.

the class CayenneContext method onContextFlush.

@Override
protected GraphDiff onContextFlush(ObjectContext originatingContext, GraphDiff changes, boolean cascade) {
    boolean childContext = this != originatingContext && changes != null;
    if (childContext) {
        changes.apply(new CayenneContextChildDiffLoader(this));
        fireDataChannelChanged(originatingContext, changes);
    }
    return (cascade) ? doCommitChanges(true) : new CompoundDiff();
}
Also used : CompoundDiff(org.apache.cayenne.graph.CompoundDiff)

Example 15 with CompoundDiff

use of org.apache.cayenne.graph.CompoundDiff in project cayenne by apache.

the class ObjectContextChangeLogTest method testGetDiffsSerializableWithHessian.

@Test
public void testGetDiffsSerializableWithHessian() throws Exception {
    ObjectContextChangeLog recorder = new ObjectContextChangeLog();
    // id must be a serializable object
    recorder.addOperation(new NodeCreateOperation("id-string"));
    CompoundDiff diff = (CompoundDiff) recorder.getDiffs();
    Object clone = HessianUtil.cloneViaClientServerSerialization(diff, new EntityResolver());
    assertNotNull(clone);
    assertTrue(clone instanceof CompoundDiff);
    CompoundDiff d1 = (CompoundDiff) clone;
    assertEquals(1, d1.getDiffs().size());
}
Also used : NodeCreateOperation(org.apache.cayenne.graph.NodeCreateOperation) EntityResolver(org.apache.cayenne.map.EntityResolver) CompoundDiff(org.apache.cayenne.graph.CompoundDiff) Test(org.junit.Test)

Aggregations

CompoundDiff (org.apache.cayenne.graph.CompoundDiff)15 Test (org.junit.Test)7 GraphDiff (org.apache.cayenne.graph.GraphDiff)5 NodeCreateOperation (org.apache.cayenne.graph.NodeCreateOperation)4 ArrayList (java.util.ArrayList)2 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)2 ObjectContext (org.apache.cayenne.ObjectContext)2 NodeIdChangeOperation (org.apache.cayenne.graph.NodeIdChangeOperation)2 EntityResolver (org.apache.cayenne.map.EntityResolver)2 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 DataChannelSyncCallbackAction (org.apache.cayenne.DataChannelSyncCallbackAction)1 DataObject (org.apache.cayenne.DataObject)1 DataRow (org.apache.cayenne.DataRow)1 ObjectId (org.apache.cayenne.ObjectId)1 Persistent (org.apache.cayenne.Persistent)1 DefaultEventManager (org.apache.cayenne.event.DefaultEventManager)1 EventSubject (org.apache.cayenne.event.EventSubject)1 ChildDiffLoader (org.apache.cayenne.graph.ChildDiffLoader)1