Search in sources :

Example 21 with ArcProperty

use of org.apache.cayenne.reflect.ArcProperty in project cayenne by apache.

the class CayenneContextChildDiffLoader method arcCreated.

@Override
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());
    property.visit(new PropertyVisitor() {

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

        public boolean visitToMany(ToManyProperty property) {
            property.addTargetDirectly(source, target);
            return false;
        }

        public boolean visitToOne(ToOneProperty property) {
            property.setTarget(source, target, false);
            return false;
        }
    });
    context.propertyChanged(source, (String) arcId, null, target);
}
Also used : ArcProperty(org.apache.cayenne.reflect.ArcProperty) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ToManyProperty(org.apache.cayenne.reflect.ToManyProperty) AttributeProperty(org.apache.cayenne.reflect.AttributeProperty) PropertyVisitor(org.apache.cayenne.reflect.PropertyVisitor) ToOneProperty(org.apache.cayenne.reflect.ToOneProperty)

Example 22 with ArcProperty

use of org.apache.cayenne.reflect.ArcProperty in project cayenne by apache.

the class CayenneContextGraphManager method remapTargets.

/**
 * Remaps keys in to-many map relationships that contain dirty objects with
 * potentially modified properties.
 */
private void remapTargets() {
    Iterator<Object> it = stateLog.dirtyIds().iterator();
    EntityResolver resolver = context.getEntityResolver();
    while (it.hasNext()) {
        ObjectId id = (ObjectId) it.next();
        ClassDescriptor descriptor = resolver.getClassDescriptor(id.getEntityName());
        Collection<ArcProperty> mapArcProperties = descriptor.getMapArcProperties();
        if (!mapArcProperties.isEmpty()) {
            Object object = getNode(id);
            for (ArcProperty arc : mapArcProperties) {
                ToManyMapProperty reverseArc = (ToManyMapProperty) arc.getComplimentaryReverseArc();
                Object source = arc.readPropertyDirectly(object);
                if (source != null && !reverseArc.isFault(source)) {
                    remapTarget(reverseArc, source, object);
                }
            }
        }
    }
}
Also used : ArcProperty(org.apache.cayenne.reflect.ArcProperty) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ToManyMapProperty(org.apache.cayenne.reflect.ToManyMapProperty) EntityResolver(org.apache.cayenne.map.EntityResolver)

Example 23 with ArcProperty

use of org.apache.cayenne.reflect.ArcProperty in project cayenne by apache.

the class CayenneContextMergeHandler method arcDeleted.

public void arcDeleted(Object nodeId, Object targetNodeId, Object arcId) {
    // null source or target likely means the object is not faulted yet... Faults
    // shouldn't get disturbed by adding/removing arcs
    Object source = context.internalGraphManager().getNode(nodeId);
    if (source == null) {
        // no need to disconnect non-existent object
        return;
    }
    // (see "TODO" in 'arcCreated')
    ArcProperty p = (ArcProperty) propertyForId(nodeId, arcId.toString());
    if (p.isFault(source)) {
        return;
    }
    Object target = context.internalGraphManager().getNode(targetNodeId);
    if (target == null) {
        target = context.createFault((ObjectId) targetNodeId);
    }
    if (p instanceof ToManyProperty) {
        ((ToManyProperty) p).removeTargetDirectly(source, target);
    } else {
        p.writePropertyDirectly(source, target, null);
    }
}
Also used : ArcProperty(org.apache.cayenne.reflect.ArcProperty) ToManyProperty(org.apache.cayenne.reflect.ToManyProperty)

Aggregations

ArcProperty (org.apache.cayenne.reflect.ArcProperty)23 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)18 ToManyProperty (org.apache.cayenne.reflect.ToManyProperty)13 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)11 ToOneProperty (org.apache.cayenne.reflect.ToOneProperty)11 PropertyVisitor (org.apache.cayenne.reflect.PropertyVisitor)10 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)6 Persistent (org.apache.cayenne.Persistent)5 ObjRelationship (org.apache.cayenne.map.ObjRelationship)5 PropertyDescriptor (org.apache.cayenne.reflect.PropertyDescriptor)5 HashMap (java.util.HashMap)4 PrefetchTreeNode (org.apache.cayenne.query.PrefetchTreeNode)4 Test (org.junit.Test)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 Map (java.util.Map)3 ObjectId (org.apache.cayenne.ObjectId)3 DbAttribute (org.apache.cayenne.map.DbAttribute)3 DbJoin (org.apache.cayenne.map.DbJoin)3 DbRelationship (org.apache.cayenne.map.DbRelationship)3