Search in sources :

Example 6 with RelationshipQuery

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;
}
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 7 with RelationshipQuery

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;
}
Also used : RelationshipQuery(org.apache.cayenne.query.RelationshipQuery) ArrayList(java.util.ArrayList)

Aggregations

RelationshipQuery (org.apache.cayenne.query.RelationshipQuery)7 List (java.util.List)3 ObjectId (org.apache.cayenne.ObjectId)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 DbEntity (org.apache.cayenne.map.DbEntity)2 Artist (org.apache.cayenne.testdo.testmap.Artist)2 Painting (org.apache.cayenne.testdo.testmap.Painting)2 Map (java.util.Map)1 Set (java.util.Set)1 BaseContext (org.apache.cayenne.BaseContext)1 CayenneRuntimeException (org.apache.cayenne.CayenneRuntimeException)1 DataRow (org.apache.cayenne.DataRow)1 Persistent (org.apache.cayenne.Persistent)1 DbRelationship (org.apache.cayenne.map.DbRelationship)1 ObjRelationship (org.apache.cayenne.map.ObjRelationship)1 ObjectIdQuery (org.apache.cayenne.query.ObjectIdQuery)1 SelectQuery (org.apache.cayenne.query.SelectQuery)1 UpdateBatchQuery (org.apache.cayenne.query.UpdateBatchQuery)1 ArcProperty (org.apache.cayenne.reflect.ArcProperty)1