Search in sources :

Example 46 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 47 with Persistent

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);
    }
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) ObjectId(org.apache.cayenne.ObjectId) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) Persistent(org.apache.cayenne.Persistent) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException)

Example 48 with Persistent

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);
    }
}
Also used : Collection(java.util.Collection) Persistent(org.apache.cayenne.Persistent) PrePersist(org.apache.cayenne.annotation.PrePersist) PreRemove(org.apache.cayenne.annotation.PreRemove) PreUpdate(org.apache.cayenne.annotation.PreUpdate)

Example 49 with Persistent

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));
}
Also used : ObjectId(org.apache.cayenne.ObjectId) Persistent(org.apache.cayenne.Persistent) Test(org.junit.Test)

Example 50 with Persistent

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);
}
Also used : ObjectId(org.apache.cayenne.ObjectId) Persistent(org.apache.cayenne.Persistent) Test(org.junit.Test)

Aggregations

Persistent (org.apache.cayenne.Persistent)88 ObjectId (org.apache.cayenne.ObjectId)38 Test (org.junit.Test)32 ArrayList (java.util.ArrayList)17 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)17 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)17 DataObject (org.apache.cayenne.DataObject)13 Map (java.util.Map)12 ObjEntity (org.apache.cayenne.map.ObjEntity)10 EJBQLQuery (org.apache.cayenne.query.EJBQLQuery)10 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)10 DataRow (org.apache.cayenne.DataRow)8 PropertyVisitor (org.apache.cayenne.reflect.PropertyVisitor)8 ToManyProperty (org.apache.cayenne.reflect.ToManyProperty)8 ToOneProperty (org.apache.cayenne.reflect.ToOneProperty)8 Collection (java.util.Collection)7 HashMap (java.util.HashMap)7 ObjectContext (org.apache.cayenne.ObjectContext)6 DbEntity (org.apache.cayenne.map.DbEntity)6 List (java.util.List)5