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