Search in sources :

Example 91 with Persistent

use of org.apache.cayenne.Persistent in project cayenne by apache.

the class BaseQueryMetadata method resolve.

boolean resolve(Object root, EntityResolver resolver) {
    if (lastRoot != root || lastEntityResolver != resolver) {
        this.classDescriptor = null;
        this.dbEntity = null;
        this.dataMap = null;
        ObjEntity entity = null;
        if (root != null) {
            if (root instanceof Class<?>) {
                entity = resolver.getObjEntity((Class<?>) root, true);
                if (entity != null) {
                    this.dbEntity = entity.getDbEntity();
                    this.dataMap = entity.getDataMap();
                }
            } else if (root instanceof ObjEntity) {
                entity = (ObjEntity) root;
                this.dbEntity = entity.getDbEntity();
                this.dataMap = entity.getDataMap();
            } else if (root instanceof String) {
                entity = resolver.getObjEntity((String) root);
                if (entity != null) {
                    this.dbEntity = entity.getDbEntity();
                    this.dataMap = entity.getDataMap();
                }
            } else if (root instanceof DbEntity) {
                this.dbEntity = (DbEntity) root;
                this.dataMap = dbEntity.getDataMap();
            } else if (root instanceof DataMap) {
                this.dataMap = (DataMap) root;
            } else if (root instanceof Persistent) {
                entity = resolver.getObjEntity((Persistent) root);
                if (entity != null) {
                    this.dbEntity = entity.getDbEntity();
                    this.dataMap = entity.getDataMap();
                }
            }
        }
        if (entity != null) {
            this.classDescriptor = resolver.getClassDescriptor(entity.getName());
        }
        this.lastRoot = root;
        this.lastEntityResolver = resolver;
        return true;
    }
    return false;
}
Also used : ObjEntity(org.apache.cayenne.map.ObjEntity) DbEntity(org.apache.cayenne.map.DbEntity) Persistent(org.apache.cayenne.Persistent) DataMap(org.apache.cayenne.map.DataMap)

Example 92 with Persistent

use of org.apache.cayenne.Persistent in project cayenne by apache.

the class ToCacheKeyTraversalHandler method objectNode.

@Override
public void objectNode(Object leaf, Expression parentNode) {
    if (leaf == null) {
        out.append("null");
        return;
    }
    if (leaf instanceof ASTScalar) {
        leaf = ((ASTScalar) leaf).getValue();
    } else if (leaf instanceof Object[]) {
        for (Object value : (Object[]) leaf) {
            objectNode(value, parentNode);
            out.append(',');
        }
        return;
    }
    if (leaf instanceof Persistent) {
        ObjectId id = ((Persistent) leaf).getObjectId();
        Object encode = (id != null) ? id : leaf;
        out.append(encode);
    } else if (leaf instanceof Enum<?>) {
        Enum<?> e = (Enum<?>) leaf;
        out.append("e:").append(leaf.getClass().getName()).append(':').append(e.ordinal());
    } else {
        ValueObjectType<Object, ?> valueObjectType;
        if (registry == null || (valueObjectType = registry.getValueType(leaf.getClass())) == null) {
            // Registry will be null in cayenne-client context.
            // Maybe we shouldn't create cache key at all in that case...
            out.append(leaf);
        } else {
            out.append(valueObjectType.toCacheKey(leaf));
        }
    }
}
Also used : ValueObjectType(org.apache.cayenne.access.types.ValueObjectType) ObjectId(org.apache.cayenne.ObjectId) Persistent(org.apache.cayenne.Persistent) ASTScalar(org.apache.cayenne.exp.parser.ASTScalar)

Example 93 with Persistent

use of org.apache.cayenne.Persistent in project cayenne by apache.

the class ObjectStoreTest method testUnregisterNode.

@Test
public void testUnregisterNode() {
    ObjectId id = ObjectId.of("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)

Example 94 with Persistent

use of org.apache.cayenne.Persistent in project cayenne by apache.

the class ObjectStoreTest method testRegisterNode.

@Test
public void testRegisterNode() {
    ObjectId id = ObjectId.of("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 95 with Persistent

use of org.apache.cayenne.Persistent in project cayenne by apache.

the class ArcValuesCreationHandlerTest method setup.

@SuppressWarnings("unchecked")
@Before
public void setup() {
    factory = mock(DbRowOpFactory.class);
    handler = new ArcValuesCreationHandler(factory, DbRowOpType.INSERT);
    dbRowOp = mock(InsertDbRowOp.class);
    values = new Values(dbRowOp, false);
    ObjectDiff diff = mock(ObjectDiff.class);
    ClassDescriptor descriptor = mock(ClassDescriptor.class);
    ObjEntity entity = mock(ObjEntity.class);
    ObjRelationship relationship = mock(ObjRelationship.class);
    DbRelationship dbRelationship = mock(DbRelationship.class);
    ObjectStore store = mock(ObjectStore.class);
    Persistent object = mock(Persistent.class);
    when(relationship.getDbRelationships()).thenReturn(Collections.singletonList(dbRelationship));
    when(entity.getRelationship(anyString())).thenReturn(relationship);
    when(descriptor.getEntity()).thenReturn(entity);
    when(dbRowOp.accept(any(DbRowOpVisitor.class))).thenCallRealMethod();
    when(dbRowOp.getValues()).thenReturn(values);
    when(factory.getDiff()).thenReturn(diff);
    when(factory.getDescriptor()).thenReturn(descriptor);
    when(factory.getStore()).thenReturn(store);
    when(factory.getObject()).thenReturn(object);
    when(factory.getOrCreate(isNull(), any(ObjectId.class), any(DbRowOpType.class))).thenReturn(dbRowOp);
}
Also used : InsertDbRowOp(org.apache.cayenne.access.flush.operation.InsertDbRowOp) ObjRelationship(org.apache.cayenne.map.ObjRelationship) ObjectStore(org.apache.cayenne.access.ObjectStore) DbRowOpVisitor(org.apache.cayenne.access.flush.operation.DbRowOpVisitor) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) ObjectDiff(org.apache.cayenne.access.ObjectDiff) ObjectId(org.apache.cayenne.ObjectId) DbRowOpType(org.apache.cayenne.access.flush.operation.DbRowOpType) Values(org.apache.cayenne.access.flush.operation.Values) Persistent(org.apache.cayenne.Persistent) ObjEntity(org.apache.cayenne.map.ObjEntity) DbRelationship(org.apache.cayenne.map.DbRelationship) Before(org.junit.Before)

Aggregations

Persistent (org.apache.cayenne.Persistent)111 ObjectId (org.apache.cayenne.ObjectId)48 Test (org.junit.Test)40 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)20 ArrayList (java.util.ArrayList)18 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)17 Map (java.util.Map)13 DataObject (org.apache.cayenne.DataObject)13 ObjEntity (org.apache.cayenne.map.ObjEntity)12 AttributeProperty (org.apache.cayenne.reflect.AttributeProperty)12 HashMap (java.util.HashMap)11 EJBQLQuery (org.apache.cayenne.query.EJBQLQuery)10 PropertyVisitor (org.apache.cayenne.reflect.PropertyVisitor)9 ToManyProperty (org.apache.cayenne.reflect.ToManyProperty)9 ToOneProperty (org.apache.cayenne.reflect.ToOneProperty)9 DataRow (org.apache.cayenne.DataRow)8 Collection (java.util.Collection)7 DbAttribute (org.apache.cayenne.map.DbAttribute)7 ObjectContext (org.apache.cayenne.ObjectContext)6 DbEntity (org.apache.cayenne.map.DbEntity)6