use of org.apache.cayenne.reflect.ClassDescriptor in project cayenne by apache.
the class PostprocessVisitor method processObjectChange.
private void processObjectChange(DbRowOp dbRow) {
if (dbRow.getChangeId().getEntityName().startsWith(ASTDbPath.DB_PREFIX)) {
return;
}
DataRow dataRow = context.currentSnapshot(dbRow.getObject());
if (dbRow.getObject() instanceof DataObject) {
DataObject dataObject = (DataObject) dbRow.getObject();
dataRow.setReplacesVersion(dataObject.getSnapshotVersion());
dataObject.setSnapshotVersion(dataRow.getVersion());
}
if (updatedSnapshots == null) {
updatedSnapshots = new HashMap<>();
}
updatedSnapshots.put(dbRow.getObject().getObjectId(), dataRow);
// update Map reverse relationships
ClassDescriptor descriptor = context.getEntityResolver().getClassDescriptor(dbRow.getChangeId().getEntityName());
for (ArcProperty arc : descriptor.getMapArcProperties()) {
ToManyMapProperty reverseArc = (ToManyMapProperty) arc.getComplimentaryReverseArc();
// must resolve faults... hopefully for to-one this will not cause extra fetches...
Object source = arc.readProperty(dbRow.getObject());
if (source != null && !reverseArc.isFault(source)) {
remapTarget(reverseArc, source, dbRow.getObject());
}
}
}
use of org.apache.cayenne.reflect.ClassDescriptor in project cayenne by apache.
the class EntityResolverClassDescriptorIT method testArcProperties.
@Test
public void testArcProperties() {
EntityResolver resolver = runtime.getDataDomain().getEntityResolver();
resolver.getClassDescriptorMap().clearDescriptors();
ClassDescriptor descriptor = resolver.getClassDescriptor("MtTable1");
assertNotNull(descriptor);
PropertyDescriptor p = descriptor.getProperty(MtTable1.TABLE2ARRAY.getName());
assertTrue(p instanceof ArcProperty);
ClassDescriptor target = ((ArcProperty) p).getTargetDescriptor();
assertNotNull(target);
assertSame(resolver.getClassDescriptor("MtTable2"), target);
assertNotNull(((ArcProperty) p).getComplimentaryReverseArc());
assertEquals(MtTable2.TABLE1.getName(), ((ArcProperty) p).getComplimentaryReverseArc().getName());
}
use of org.apache.cayenne.reflect.ClassDescriptor in project cayenne by apache.
the class EntityResolverClassDescriptorIT method testServerDescriptorFactory.
@Test
public void testServerDescriptorFactory() {
EntityResolver resolver = runtime.getDataDomain().getEntityResolver();
resolver.getClassDescriptorMap().clearDescriptors();
ClassDescriptor descriptor = mock(ClassDescriptor.class);
ClassDescriptorFactory factory = mock(ClassDescriptorFactory.class);
when(factory.getDescriptor("MtTable1")).thenReturn(descriptor);
resolver.getClassDescriptorMap().addFactory(factory);
try {
ClassDescriptor resolved = resolver.getClassDescriptor("MtTable1");
assertNotNull(resolved);
resolved = ((LazyClassDescriptorDecorator) resolved).getDescriptor();
assertSame(descriptor, resolved);
} finally {
resolver.getClassDescriptorMap().removeFactory(factory);
}
}
use of org.apache.cayenne.reflect.ClassDescriptor in project cayenne by apache.
the class DataContext method objectFromDataRow.
/**
* Creates a DataObject from DataRow.
*
* @see DataRow
* @since 3.1
*/
public <T extends Persistent> T objectFromDataRow(Class<T> objectClass, DataRow dataRow) {
ObjEntity entity = this.getEntityResolver().getObjEntity(objectClass);
if (entity == null) {
throw new CayenneRuntimeException("Unmapped Java class: %s", objectClass);
}
ClassDescriptor descriptor = getEntityResolver().getClassDescriptor(entity.getName());
List<T> list = objectsFromDataRows(descriptor, Collections.singletonList(dataRow));
return list.get(0);
}
use of org.apache.cayenne.reflect.ClassDescriptor in project cayenne by apache.
the class DataContext method findOrCreateObject.
/**
* An internal version of {@link #localObject(Persistent)} that operates on
* ObjectId instead of Persistent, and wouldn't attempt to look up an object
* in the parent channel.
*
* @since 3.1
*/
Persistent findOrCreateObject(ObjectId id) {
if (id == null) {
throw new IllegalArgumentException("Null ObjectId");
}
// behavior.
synchronized (getGraphManager()) {
Persistent cachedObject = (Persistent) getGraphManager().getNode(id);
// return an existing object
if (cachedObject != null) {
int state = cachedObject.getPersistenceState();
// objects...
if (state != PersistenceState.MODIFIED && state != PersistenceState.DELETED) {
ClassDescriptor descriptor = getEntityResolver().getClassDescriptor(id.getEntityName());
descriptor.injectValueHolders(cachedObject);
}
return cachedObject;
}
// create and register a hollow object
ClassDescriptor descriptor = getEntityResolver().getClassDescriptor(id.getEntityName());
Persistent localObject = (Persistent) descriptor.createObject();
localObject.setObjectContext(this);
localObject.setObjectId(id);
getGraphManager().registerNode(id, localObject);
localObject.setPersistenceState(PersistenceState.HOLLOW);
return localObject;
}
}
Aggregations