Search in sources :

Example 1 with BaseContext

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;
}
Also used : BaseContext(org.apache.cayenne.BaseContext) ArcProperty(org.apache.cayenne.reflect.ArcProperty) Set(java.util.Set) ClassDescriptor(org.apache.cayenne.reflect.ClassDescriptor) RelationshipQuery(org.apache.cayenne.query.RelationshipQuery) ObjectId(org.apache.cayenne.ObjectId) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Persistent(org.apache.cayenne.Persistent) Map(java.util.Map)

Example 2 with BaseContext

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())));
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) BaseContext(org.apache.cayenne.BaseContext) ObjectContext(org.apache.cayenne.ObjectContext) Test(org.junit.Test)

Example 3 with BaseContext

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())));
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) BaseContext(org.apache.cayenne.BaseContext) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) Test(org.junit.Test)

Aggregations

BaseContext (org.apache.cayenne.BaseContext)3 SelectQuery (org.apache.cayenne.query.SelectQuery)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 ObjectContext (org.apache.cayenne.ObjectContext)1 ObjectId (org.apache.cayenne.ObjectId)1 Persistent (org.apache.cayenne.Persistent)1 RelationshipQuery (org.apache.cayenne.query.RelationshipQuery)1 ArcProperty (org.apache.cayenne.reflect.ArcProperty)1 ClassDescriptor (org.apache.cayenne.reflect.ClassDescriptor)1 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)1