Search in sources :

Example 11 with ArcProperty

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

the class Util method setReverse.

static void setReverse(final Persistent sourceObject, String propertyName, final Persistent targetObject) {
    ArcProperty property = (ArcProperty) Cayenne.getClassDescriptor(sourceObject).getProperty(propertyName);
    ArcProperty reverseArc = property.getComplimentaryReverseArc();
    if (reverseArc != null) {
        reverseArc.visit(new PropertyVisitor() {

            public boolean visitToMany(ToManyProperty property) {
                property.addTargetDirectly(targetObject, sourceObject);
                return false;
            }

            public boolean visitToOne(ToOneProperty property) {
                property.setTarget(targetObject, sourceObject, false);
                return false;
            }

            public boolean visitAttribute(AttributeProperty property) {
                return false;
            }
        });
        sourceObject.getObjectContext().getGraphManager().arcCreated(targetObject.getObjectId(), sourceObject.getObjectId(), reverseArc.getName());
        markAsDirty(targetObject);
    }
}
Also used : ArcProperty(org.apache.cayenne.reflect.ArcProperty) 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 12 with ArcProperty

use of org.apache.cayenne.reflect.ArcProperty 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 13 with ArcProperty

use of org.apache.cayenne.reflect.ArcProperty 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 14 with ArcProperty

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

the class DataRowUtilsIT method testIsToOneTargetModified.

@Test
public void testIsToOneTargetModified() throws Exception {
    createOneArtist();
    ClassDescriptor d = context.getEntityResolver().getClassDescriptor("Painting");
    ArcProperty toArtist = (ArcProperty) d.getProperty("toArtist");
    Artist artist2 = (Artist) context.performQuery(new SelectQuery(Artist.class)).get(0);
    Painting painting = context.newObject(Painting.class);
    painting.setPaintingTitle("PX");
    painting.setToArtist(artist2);
    context.commitChanges();
    tArtist.insert(119, "artist3");
    SelectQuery query = new SelectQuery(Artist.class, Artist.ARTIST_NAME.eq("artist3"));
    Artist artist3 = (Artist) context.performQuery(query).get(0);
    assertNotSame(artist3, painting.getToArtist());
    ObjectDiff diff = context.getObjectStore().registerDiff(painting.getObjectId(), null);
    assertFalse(DataRowUtils.isToOneTargetModified(toArtist, painting, diff));
    painting.setToArtist(artist3);
    assertTrue(DataRowUtils.isToOneTargetModified(toArtist, painting, diff));
}
Also used : Artist(org.apache.cayenne.testdo.testmap.Artist) SelectQuery(org.apache.cayenne.query.SelectQuery) ArcProperty(org.apache.cayenne.reflect.ArcProperty) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

Example 15 with ArcProperty

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

the class DataRowUtilsIT method testIsToOneTargetModifiedWithNewTarget.

@Test
public void testIsToOneTargetModifiedWithNewTarget() throws Exception {
    createOneArtistAndOnePainting();
    // add NEW gallery to painting
    List<Painting> paintings = context.performQuery(new SelectQuery(Painting.class));
    assertEquals(1, paintings.size());
    Painting p1 = paintings.get(0);
    ClassDescriptor d = context.getEntityResolver().getClassDescriptor("Painting");
    ArcProperty toGallery = (ArcProperty) d.getProperty("toGallery");
    ObjectDiff diff = context.getObjectStore().registerDiff(p1.getObjectId(), null);
    assertFalse(DataRowUtils.isToOneTargetModified(toGallery, p1, diff));
    Gallery g1 = (Gallery) context.newObject("Gallery");
    g1.addToPaintingArray(p1);
    assertTrue(DataRowUtils.isToOneTargetModified(toGallery, p1, diff));
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) ArcProperty(org.apache.cayenne.reflect.ArcProperty) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) Gallery(org.apache.cayenne.testdo.testmap.Gallery) Painting(org.apache.cayenne.testdo.testmap.Painting) Test(org.junit.Test)

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