Search in sources :

Example 1 with Persistent

use of org.apache.cayenne.Persistent in project cayenne by apache.

the class DataDomainFlushAction method preprocess.

private void preprocess(DataContext context, GraphDiff changes) {
    // categorize dirty objects by state
    ObjectStore objectStore = context.getObjectStore();
    for (Object o : changesByObjectId.keySet()) {
        ObjectId id = (ObjectId) o;
        Persistent object = (Persistent) objectStore.getNode(id);
        ClassDescriptor descriptor = context.getEntityResolver().getClassDescriptor(id.getEntityName());
        switch(object.getPersistenceState()) {
            case PersistenceState.NEW:
                insertBucket.addDirtyObject(object, descriptor);
                break;
            case PersistenceState.MODIFIED:
                updateBucket.addDirtyObject(object, descriptor);
                break;
            case PersistenceState.DELETED:
                deleteBucket.addDirtyObject(object, descriptor);
                break;
        }
    }
    new DataDomainIndirectDiffBuilder(this).processIndirectChanges(changes);
    insertBucket.appendQueries(queries);
    // TODO: the following line depends on the "queries" collection filled by insertBucket.. Moreover it may
    // potentially remove values from the passed collection. Replace with something with fewer unobvious
    // side-effects...
    flattenedBucket.appendInserts(queries);
    updateBucket.appendQueries(queries);
    flattenedBucket.appendDeletes(queries);
    deleteBucket.appendQueries(queries);
}
Also used : ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ObjectId(org.apache.cayenne.ObjectId) Persistent(org.apache.cayenne.Persistent)

Example 2 with Persistent

use of org.apache.cayenne.Persistent in project cayenne by apache.

the class ChildDiffLoader method arcDeleted.

public void arcDeleted(Object nodeId, final Object targetNodeId, Object arcId) {
    final Persistent source = findObject(nodeId);
    // changing their relationships
    if (source == null) {
        return;
    }
    ClassDescriptor descriptor = context.getEntityResolver().getClassDescriptor(((ObjectId) nodeId).getEntityName());
    PropertyDescriptor property = descriptor.getProperty(arcId.toString());
    setExternalChange(Boolean.TRUE);
    try {
        property.visit(new PropertyVisitor() {

            public boolean visitAttribute(AttributeProperty property) {
                return false;
            }

            public boolean visitToMany(ToManyProperty property) {
                // connect reverse arc if the relationship is marked as
                // "runtime"
                ArcProperty reverseArc = property.getComplimentaryReverseArc();
                boolean autoConnectReverse = reverseArc != null && reverseArc.getRelationship().isRuntime();
                Persistent target = findObject(targetNodeId);
                if (target == null) {
                    // this is usually the case when a NEW object was
                    // deleted and then
                    // its
                    // relationships were manipulated; so try to locate the
                    // object in
                    // the
                    // collection ...
                    // the performance of this is rather dubious of
                    // course...
                    target = findObjectInCollection(targetNodeId, property.readProperty(source));
                }
                if (target == null) {
                // ignore?
                } else {
                    property.removeTarget(source, target, autoConnectReverse);
                }
                return false;
            }

            public boolean visitToOne(ToOneProperty property) {
                property.setTarget(source, null, false);
                return false;
            }
        });
    } finally {
        setExternalChange(Boolean.FALSE);
    }
}
Also used : ArcProperty(org.apache.cayenne.reflect.ArcProperty) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) PropertyDescriptor(org.apache.cayenne.reflect.PropertyDescriptor) ToManyProperty(org.apache.cayenne.reflect.ToManyProperty) Persistent(org.apache.cayenne.Persistent) AttributeProperty(org.apache.cayenne.reflect.AttributeProperty) PropertyVisitor(org.apache.cayenne.reflect.PropertyVisitor) ToOneProperty(org.apache.cayenne.reflect.ToOneProperty)

Example 3 with Persistent

use of org.apache.cayenne.Persistent in project cayenne by apache.

the class ChildDiffLoader method arcCreated.

public void arcCreated(Object nodeId, Object targetNodeId, Object arcId) {
    final Persistent source = findObject(nodeId);
    final Persistent target = findObject(targetNodeId);
    // can result in NULL target here.
    if (target == null) {
        return;
    }
    ClassDescriptor descriptor = context.getEntityResolver().getClassDescriptor(((ObjectId) nodeId).getEntityName());
    ArcProperty property = (ArcProperty) descriptor.getProperty(arcId.toString());
    setExternalChange(Boolean.TRUE);
    try {
        property.visit(new PropertyVisitor() {

            public boolean visitAttribute(AttributeProperty property) {
                return false;
            }

            public boolean visitToMany(ToManyProperty property) {
                // connect reverse arc if the relationship is marked as
                // "runtime"
                ArcProperty reverseArc = property.getComplimentaryReverseArc();
                boolean autoConnectReverse = reverseArc != null && reverseArc.getRelationship().isRuntime();
                property.addTarget(source, target, autoConnectReverse);
                return false;
            }

            public boolean visitToOne(ToOneProperty property) {
                property.setTarget(source, target, false);
                return false;
            }
        });
    } finally {
        setExternalChange(Boolean.FALSE);
    }
}
Also used : ArcProperty(org.apache.cayenne.reflect.ArcProperty) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ToManyProperty(org.apache.cayenne.reflect.ToManyProperty) Persistent(org.apache.cayenne.Persistent) AttributeProperty(org.apache.cayenne.reflect.AttributeProperty) PropertyVisitor(org.apache.cayenne.reflect.PropertyVisitor) ToOneProperty(org.apache.cayenne.reflect.ToOneProperty)

Example 4 with Persistent

use of org.apache.cayenne.Persistent in project cayenne by apache.

the class DefaultDataDomainFlushActionTest method mockObject.

private Persistent mockObject(ObjectId id) {
    Persistent persistent = mock(Persistent.class);
    when(persistent.getObjectId()).thenReturn(id);
    when(persistent.getPersistenceState()).thenReturn(PersistenceState.MODIFIED);
    return persistent;
}
Also used : Persistent(org.apache.cayenne.Persistent)

Example 5 with Persistent

use of org.apache.cayenne.Persistent in project cayenne by apache.

the class DefaultDbRowOpSorterTest method mockObject.

private Persistent mockObject(ObjectId id) {
    Persistent persistent = mock(Persistent.class);
    when(persistent.getObjectId()).thenReturn(id);
    when(persistent.getPersistenceState()).thenReturn(PersistenceState.MODIFIED);
    return persistent;
}
Also used : Persistent(org.apache.cayenne.Persistent)

Aggregations

Persistent (org.apache.cayenne.Persistent)111 ObjectId (org.apache.cayenne.ObjectId)48 Test (org.junit.Test)40 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)20 ArrayList (java.util.ArrayList)18 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)17 Map (java.util.Map)13 DataObject (org.apache.cayenne.DataObject)13 ObjEntity (org.apache.cayenne.map.ObjEntity)12 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)12 HashMap (java.util.HashMap)11 EJBQLQuery (org.apache.cayenne.query.EJBQLQuery)10 PropertyVisitor (org.apache.cayenne.reflect.PropertyVisitor)9 ToManyProperty (org.apache.cayenne.reflect.ToManyProperty)9 ToOneProperty (org.apache.cayenne.reflect.ToOneProperty)9 DataRow (org.apache.cayenne.DataRow)8 Collection (java.util.Collection)7 DbAttribute (org.apache.cayenne.map.DbAttribute)7 ObjectContext (org.apache.cayenne.ObjectContext)6 DbEntity (org.apache.cayenne.map.DbEntity)6