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);
}
}
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);
}
}
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);
}
}
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));
}
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));
}
Aggregations