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);
}
}
use of org.apache.cayenne.Persistent in project cayenne by apache.
the class ChildDiffLoader method nodeCreated.
public void nodeCreated(Object nodeId) {
setExternalChange(Boolean.TRUE);
try {
ObjectId id = (ObjectId) nodeId;
if (id.getEntityName() == null) {
throw new NullPointerException("Null entity name in id " + id);
}
ObjEntity entity = context.getEntityResolver().getObjEntity(id.getEntityName());
if (entity == null) {
throw new IllegalArgumentException("Entity not mapped with Cayenne: " + id);
}
Persistent dataObject;
try {
dataObject = (Persistent) entity.getJavaClass().newInstance();
} catch (Exception ex) {
throw new CayenneRuntimeException("Error instantiating object.", ex);
}
dataObject.setObjectId(id);
context.registerNewObject(dataObject);
} finally {
setExternalChange(Boolean.FALSE);
}
}
use of org.apache.cayenne.Persistent in project cayenne by apache.
the class CacheInvalidationFilter method preCommit.
/**
* A callback method that records cache group to flush at the end of the commit.
*/
@PrePersist
@PreRemove
@PreUpdate
protected void preCommit(Object object) {
// TODO: for some reason we can't use Persistent as the argument type... (is it fixed in Cayenne 4.0.M4?)
Persistent p = (Persistent) object;
Function<Persistent, Collection<CacheGroupDescriptor>> invalidationFunction = mappedHandlers.get(p.getClass());
if (invalidationFunction == null) {
invalidationFunction = skipHandler;
for (InvalidationHandler handler : handlers) {
Function<Persistent, Collection<CacheGroupDescriptor>> function = handler.canHandle(p.getClass());
if (function != null) {
invalidationFunction = function;
break;
}
}
mappedHandlers.put(p.getClass(), invalidationFunction);
}
Collection<CacheGroupDescriptor> objectGroups = invalidationFunction.apply(p);
if (!objectGroups.isEmpty()) {
getOrCreateTxGroups().addAll(objectGroups);
}
}
use of org.apache.cayenne.Persistent in project cayenne by apache.
the class ObjectStoreTest method testRegisterNode.
@Test
public void testRegisterNode() {
ObjectId id = new ObjectId("E1", "ID", 500);
Persistent object = mock(Persistent.class);
objectStore.registerNode(id, object);
assertSame(object, objectStore.getNode(id));
}
use of org.apache.cayenne.Persistent in project cayenne by apache.
the class ObjectStoreTest method testUnregisterNode.
@Test
public void testUnregisterNode() {
ObjectId id = new ObjectId("E1", "ID", 500);
Persistent object = mock(Persistent.class);
objectStore.registerNode(id, object);
Object unregistered = objectStore.unregisterNode(id);
assertSame(object, unregistered);
verify(object, times(0)).setObjectId(null);
verify(object).setObjectContext(null);
verify(object).setPersistenceState(PersistenceState.TRANSIENT);
}
Aggregations