use of org.apache.cayenne.reflect.ClassDescriptor in project cayenne by apache.
the class PrefetchProcessorTreeBuilder method addNode.
boolean addNode(PrefetchProcessorNode node) {
List<DataRow> rows;
ArcProperty arc;
ClassDescriptor descriptor;
PrefetchProcessorNode currentNode = getParent();
if (currentNode != null) {
@SuppressWarnings("unchecked") List<DataRow> dataRows = (List<DataRow>) extraResultsByPath.get(node.getPath());
rows = dataRows;
arc = (ArcProperty) currentNode.getResolver().getDescriptor().getProperty(node.getName());
if (arc == null) {
throw new CayenneRuntimeException("No relationship with name '%s' found in entity '%s'", node.getName(), currentNode.getResolver().getEntity().getName());
}
descriptor = arc.getTargetDescriptor();
} else {
arc = null;
if (this.descriptor != null) {
descriptor = this.descriptor;
} else {
descriptor = queryMetadata.getClassDescriptor();
}
rows = mainResultRows;
}
node.setDataRows(rows);
node.setIncoming(arc);
if (node.getParent() != null && !node.isJointPrefetch()) {
node.setResolver(new HierarchicalObjectResolverNode(node, context, descriptor, queryMetadata.isRefreshingObjects(), seen));
} else {
node.setResolver(new PrefetchObjectResolver(context, descriptor, queryMetadata.isRefreshingObjects(), seen));
}
if (node.getParent() == null || node.getParent().isPhantom()) {
node.setParentAttachmentStrategy(new NoopParentAttachmentStrategy());
} else if (node.isJointPrefetch()) {
node.setParentAttachmentStrategy(new StackLookupParentAttachmentStrategy(node));
} else if (node.getIncoming().getRelationship().isSourceIndependentFromTargetChange()) {
node.setParentAttachmentStrategy(new JoinedIdParentAttachementStrategy(context.getGraphManager(), node));
} else {
node.setParentAttachmentStrategy(new ResultScanParentAttachmentStrategy(node));
}
if (currentNode != null) {
currentNode.addChild(node);
}
node.afterInit();
// push node on stack
if (nodeStack.isEmpty()) {
root = node;
}
nodeStack.addLast(node);
return true;
}
use of org.apache.cayenne.reflect.ClassDescriptor in project cayenne by apache.
the class HierarchicalObjectResolverNode method objectsFromDataRows.
@Override
List<Persistent> objectsFromDataRows(List<? extends DataRow> rows) {
if (rows == null || rows.size() == 0) {
return new ArrayList<>(1);
}
List<Persistent> results = new ArrayList<>(rows.size());
for (DataRow row : rows) {
// determine entity to use
ClassDescriptor classDescriptor = descriptorResolutionStrategy.descriptorForRow(row);
// not using DataRow.createObjectId for performance reasons -
// ObjectResolver
// has all needed metadata already cached.
ObjectId anId = createObjectId(row, classDescriptor.getEntity(), null);
Persistent object = objectFromDataRow(row, anId, classDescriptor);
if (object == null) {
throw new CayenneRuntimeException("Can't build Object from row: %s", row);
}
// keep the dupe objects (and data rows) around, as there maybe an
// attached
// joint prefetch...
results.add(object);
node.getParentAttachmentStrategy().linkToParent(row, object);
}
// now deal with snapshots
// TODO: refactoring: dupes will clutter the lists and cause extra
// processing...
// removal of dupes happens only downstream, as we need the objects
// matching
// fetched rows for joint prefetch resolving... maybe pushback unique
// and
// non-unique lists to the "node", instead of returning a single list
// from this
// method
cache.snapshotsUpdatedForObjects(results, rows, refreshObjects);
return results;
}
use of org.apache.cayenne.reflect.ClassDescriptor in project cayenne by apache.
the class RelationshipQuery method updateMetadata.
void updateMetadata(EntityResolver resolver) {
// caching metadata as it may be accessed multiple times (at a DC and DD level)
if (metadataResolver != resolver) {
if (objectId == null) {
throw new CayenneRuntimeException("Can't resolve query - objectID is null.");
}
ClassDescriptor descriptor = resolver.getClassDescriptor(objectId.getEntityName());
this.arc = (ArcProperty) descriptor.getProperty(relationshipName);
if (arc == null) {
throw new CayenneRuntimeException("No relationship named %s found in entity %s; object id: %s", relationshipName, objectId.getEntityName(), objectId);
}
this.metadata = new DefaultQueryMetadata() {
@Override
public boolean isRefreshingObjects() {
return refreshing;
}
@Override
public ObjEntity getObjEntity() {
return arc.getTargetDescriptor().getEntity();
}
@Override
public ClassDescriptor getClassDescriptor() {
return arc.getTargetDescriptor();
}
@Override
public int getStatementFetchSize() {
return statementFetchSize;
}
};
this.metadataResolver = resolver;
}
}
use of org.apache.cayenne.reflect.ClassDescriptor in project cayenne by apache.
the class DataRowUtilsIT method testMerge.
@Test
public void testMerge() throws Exception {
createOneArtist();
String n1 = "changed";
String n2 = "changed again";
Artist a1 = ObjectSelect.query(Artist.class).select(context).get(0);
a1.setArtistName(n1);
DataRow s2 = new DataRow(2);
s2.put("ARTIST_NAME", n2);
s2.put("DATE_OF_BIRTH", new java.util.Date());
ClassDescriptor d = context.getEntityResolver().getClassDescriptor("Artist");
DataRowUtils.mergeObjectWithSnapshot(context, d, a1, s2);
// name was modified, so it should not change during merge
assertEquals(n1, a1.getArtistName());
// date of birth came from database, it should be updated during merge
assertEquals(s2.get("DATE_OF_BIRTH"), a1.getDateOfBirth());
}
use of org.apache.cayenne.reflect.ClassDescriptor in project cayenne by apache.
the class CayenneContext method newObject.
/**
* Creates and registers a new Persistent object instance.
*/
@Override
public <T> T newObject(Class<T> persistentClass) {
if (persistentClass == null) {
throw new NullPointerException("Persistent class can't be null.");
}
ObjEntity entity = getEntityResolver().getObjEntity(persistentClass);
if (entity == null) {
throw new CayenneRuntimeException("No entity mapped for class: %s", persistentClass);
}
ClassDescriptor descriptor = getEntityResolver().getClassDescriptor(entity.getName());
@SuppressWarnings("unchecked") T object = (T) descriptor.createObject();
descriptor.injectValueHolders(object);
registerNewObject((Persistent) object, entity.getName(), descriptor);
return object;
}
Aggregations