use of org.apache.cayenne.query.RelationshipQuery in project cayenne by apache.
the class ObjectContextQueryAction method interceptRelationshipQuery.
protected boolean interceptRelationshipQuery() {
if (query instanceof RelationshipQuery) {
RelationshipQuery relationshipQuery = (RelationshipQuery) query;
if (!relationshipQuery.isRefreshing()) {
if (targetContext == null && relationshipQuery.getRelationship(actingContext.getEntityResolver()).isToMany()) {
return !DONE;
}
ObjectId id = relationshipQuery.getObjectId();
Object object = actingContext.getGraphManager().getNode(id);
if (object != null) {
ClassDescriptor descriptor = actingContext.getEntityResolver().getClassDescriptor(id.getEntityName());
if (!descriptor.isFault(object)) {
ArcProperty property = (ArcProperty) descriptor.getProperty(relationshipQuery.getRelationshipName());
if (!property.isFault(object)) {
Object related = property.readPropertyDirectly(object);
List result;
// null to-one
if (related == null) {
result = new ArrayList(1);
} else // to-many List
if (related instanceof List) {
result = (List) related;
} else // to-many Set
if (related instanceof Set) {
result = new ArrayList((Set) related);
} else // to-many Map
if (related instanceof Map) {
result = new ArrayList(((Map) related).values());
} else // non-null to-one
{
result = new ArrayList(1);
result.add(related);
}
this.response = new ListResponse(result);
return DONE;
}
/**
* Workaround for CAY-1183. If a Relationship query is being sent
* from child context, we assure that local object is not NEW and
* relationship - unresolved (this way exception will occur). This
* helps when faulting objects that were committed to parent
* context (this), but not to database. Checking type of context's
* channel is the only way to ensure that we are on the top level
* of context hierarchy (there might be more than one-level-deep
* nested contexts).
*/
if (((Persistent) object).getPersistenceState() == PersistenceState.NEW && !(actingContext.getChannel() instanceof BaseContext)) {
this.response = new ListResponse();
return DONE;
}
}
}
}
}
return !DONE;
}
use of org.apache.cayenne.query.RelationshipQuery in project cayenne by apache.
the class RelationshipFault method resolveFromDB.
/**
* Executes a query that returns related objects. Subclasses would invoke
* this method whenever they need to resolve a fault.
*/
protected List<E> resolveFromDB() {
// non-persistent objects shouldn't trigger a fetch
if (isTransientParent()) {
return new ArrayList<>();
}
List<E> resolved = relationshipOwner.getObjectContext().performQuery(new RelationshipQuery(relationshipOwner.getObjectId(), relationshipName, false));
/*
* Duplicating the list (see CAY-1194). Doing that only for
* RelationshipFault query results, so only for nested DataContexts
*/
if (resolved instanceof RelationshipFault) {
resolved = new ArrayList<>(resolved);
}
// merge local before updating reverse to ensure we update reverse rels
// of the right objects (see CAY-1757)
mergeLocalChanges(resolved);
if (resolved.isEmpty()) {
return resolved;
}
updateReverse(resolved);
return resolved;
}
Aggregations