All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.cayenne.util.ObjectContextQueryAction Maven / Gradle / Ivy

The newest version!
/*****************************************************************
 *   Licensed to the Apache Software Foundation (ASF) under one
 *  or more contributor license agreements.  See the NOTICE file
 *  distributed with this work for additional information
 *  regarding copyright ownership.  The ASF licenses this file
 *  to you under the Apache License, Version 2.0 (the
 *  "License"); you may not use this file except in compliance
 *  with the License.  You may obtain a copy of the License at
 *
 *    https://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing,
 *  software distributed under the License is distributed on an
 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 *  KIND, either express or implied.  See the License for the
 *  specific language governing permissions and limitations
 *  under the License.
 ****************************************************************/

package org.apache.cayenne.util;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.cayenne.ObjectContext;
import org.apache.cayenne.ObjectId;
import org.apache.cayenne.PersistenceState;
import org.apache.cayenne.Persistent;
import org.apache.cayenne.QueryResponse;
import org.apache.cayenne.access.DataContext;
import org.apache.cayenne.cache.QueryCache;
import org.apache.cayenne.cache.QueryCacheEntryFactory;
import org.apache.cayenne.map.EntityInheritanceTree;
import org.apache.cayenne.query.*;
import org.apache.cayenne.reflect.ArcProperty;
import org.apache.cayenne.reflect.ClassDescriptor;

/**
 * A helper class that implements
 * {@link org.apache.cayenne.DataChannel#onQuery(ObjectContext, Query)} logic on behalf of
 * an ObjectContext.
 * 

* Intended for internal use only. *

* * @since 1.2 */ public abstract class ObjectContextQueryAction { protected static final boolean DONE = true; protected ObjectContext targetContext; protected ObjectContext actingContext; protected Query query; protected QueryMetadata metadata; protected boolean queryOriginator; protected transient QueryResponse response; public ObjectContextQueryAction(ObjectContext actingContext, ObjectContext targetContext, Query query) { this.actingContext = actingContext; this.query = query; // this means that a caller must pass self as both acting context and target // context to indicate that a query originated here... null (ROP) or differing // context indicates that the query was originated elsewhere, which has // consequences in LOCAL_CACHE handling this.queryOriginator = targetContext != null && targetContext == actingContext; // no special target context and same target context as acting context mean the // same thing. "normalize" the internal state to avoid confusion this.targetContext = targetContext != actingContext ? targetContext : null; this.metadata = query.getMetaData(actingContext.getEntityResolver()); } /** * Worker method that performs internal query. */ public QueryResponse execute() { if (interceptIteratedQuery() != DONE) { if (interceptOIDQuery() != DONE) { if (interceptRelationshipQuery() != DONE) { if (interceptRefreshQuery() != DONE) { if (interceptLocalCache() != DONE) { executePostCache(); } } } } } interceptObjectConversion(); return response; } private boolean interceptIteratedQuery() { if (query instanceof IteratedQueryDecorator) { runQuery(); return DONE; } return !DONE; } private void executePostCache() { if (interceptInternalQuery() != DONE) { if (interceptPaginatedQuery() != DONE) { runQuery(); } } } /** * Transfers fetched objects into the target context if it is different from "acting" * context. Note that when this method is invoked, result objects are already * registered with acting context by the parent channel. */ protected void interceptObjectConversion() { if (targetContext != null && !metadata.isFetchingDataRows()) { // rewrite response to contain objects from the query context GenericResponse childResponse = new GenericResponse(); ShallowMergeOperation merger = null; for (response.reset(); response.next();) { if (response.isList()) { List objects = response.currentList(); if (objects.isEmpty()) { childResponse.addResultList(objects); } else { // minor optimization, skip Object[] if there are no persistent objects boolean haveObjects = metadata.getResultSetMapping() == null; if(!haveObjects) { for (Object next : metadata.getResultSetMapping()) { if(next instanceof EntityResultSegment) { haveObjects = true; break; } } } if (merger == null) { merger = new ShallowMergeOperation(targetContext); } // TODO: Andrus 1/31/2006 - IncrementalFaultList is not properly // transferred between contexts.... List childObjects = new ArrayList<>(objects.size()); for (Object object1 : objects) { if(object1 instanceof Persistent) { Persistent object = (Persistent) object1; childObjects.add(merger.merge(object)); } else if(haveObjects && object1 instanceof Object[]) { // merge objects inside Object[] Object[] parentData = (Object[]) object1; Object[] childData = new Object[parentData.length]; System.arraycopy(parentData, 0, childData, 0, parentData.length); for(int i=0; i