use of org.apache.cayenne.BaseContext 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.BaseContext in project cayenne by apache.
the class NestedDataContextLocalCacheIT method testLocalCacheStaysLocal.
@Test
public void testLocalCacheStaysLocal() {
SelectQuery query = new SelectQuery(Artist.class);
query.setCacheStrategy(QueryCacheStrategy.LOCAL_CACHE);
ObjectContext child1 = runtime.newContext(context);
assertNull(((BaseContext) child1).getQueryCache().get(query.getMetaData(child1.getEntityResolver())));
assertNull(context.getQueryCache().get(query.getMetaData(context.getEntityResolver())));
List<?> results = child1.performQuery(query);
assertSame(results, ((BaseContext) child1).getQueryCache().get(query.getMetaData(child1.getEntityResolver())));
assertNull(context.getQueryCache().get(query.getMetaData(context.getEntityResolver())));
}
use of org.apache.cayenne.BaseContext in project cayenne by apache.
the class NestedObjectContextLocalIT method testLocalCacheStaysLocal.
@Test
public void testLocalCacheStaysLocal() {
SelectQuery<ClientMtTable1> query = new SelectQuery<ClientMtTable1>(ClientMtTable1.class);
query.setCacheStrategy(QueryCacheStrategy.LOCAL_CACHE);
BaseContext child1 = (BaseContext) runtime.newContext(clientContext);
assertNull(child1.getQueryCache().get(query.getMetaData(child1.getEntityResolver())));
assertNull(clientContext.getQueryCache().get(query.getMetaData(clientContext.getEntityResolver())));
List<?> results = child1.performQuery(query);
assertSame(results, child1.getQueryCache().get(query.getMetaData(child1.getEntityResolver())));
assertNull(clientContext.getQueryCache().get(query.getMetaData(clientContext.getEntityResolver())));
}
Aggregations