Search in sources :

Example 1 with GenericResponse

use of org.apache.cayenne.util.GenericResponse in project cayenne by apache.

the class ClientServerChannelQueryAction method interceptObjectConversion.

private void interceptObjectConversion() {
    if (!serverMetadata.isFetchingDataRows()) {
        GenericResponse clientResponse = new GenericResponse();
        for (response.reset(); response.next(); ) {
            if (response.isList()) {
                List serverObjects = response.currentList();
                clientResponse.addResultList(toClientObjects(serverObjects));
            } else {
                clientResponse.addBatchUpdateCount(response.currentUpdateCount());
            }
        }
        this.response = clientResponse;
    }
}
Also used : GenericResponse(org.apache.cayenne.util.GenericResponse) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with GenericResponse

use of org.apache.cayenne.util.GenericResponse in project cayenne by apache.

the class DataDomainQueryAction method interceptRelationshipQuery.

private boolean interceptRelationshipQuery() {
    if (query instanceof RelationshipQuery) {
        RelationshipQuery relationshipQuery = (RelationshipQuery) query;
        if (relationshipQuery.isRefreshing()) {
            return !DONE;
        }
        ObjRelationship relationship = relationshipQuery.getRelationship(domain.getEntityResolver());
        // check if we can derive target PK from FK...
        if (relationship.isSourceIndependentFromTargetChange()) {
            return !DONE;
        }
        // we can assume that there is one and only one DbRelationship as
        // we previously checked that "!isSourceIndependentFromTargetChange"
        DbRelationship dbRelationship = relationship.getDbRelationships().get(0);
        // FK pointing to a unique field that is a 'fake' PK (CAY-1755)...
        // It is not sufficient to generate target ObjectId.
        DbEntity targetEntity = dbRelationship.getTargetEntity();
        if (dbRelationship.getJoins().size() < targetEntity.getPrimaryKeys().size()) {
            return !DONE;
        }
        if (cache == null) {
            return !DONE;
        }
        DataRow sourceRow = cache.getCachedSnapshot(relationshipQuery.getObjectId());
        if (sourceRow == null) {
            return !DONE;
        }
        ObjectId targetId = sourceRow.createTargetObjectId(relationship.getTargetEntityName(), dbRelationship);
        // null id means that FK is null...
        if (targetId == null) {
            this.response = new GenericResponse(Collections.EMPTY_LIST);
            return DONE;
        }
        // target id resolution (unlike source) should be polymorphic
        DataRow targetRow = polymorphicRowFromCache(targetId);
        if (targetRow != null) {
            this.response = new GenericResponse(Collections.singletonList(targetRow));
            return DONE;
        }
        // create a fault
        if (context != null && relationship.isSourceDefiningTargetPrecenseAndType(domain.getEntityResolver())) {
            // prevent passing partial snapshots to ObjectResolver per
            // CAY-724.
            // Create a hollow object right here and skip object conversion
            // downstream
            this.noObjectConversion = true;
            Object object = context.findOrCreateObject(targetId);
            this.response = new GenericResponse(Collections.singletonList(object));
            return DONE;
        }
    }
    return !DONE;
}
Also used : ObjRelationship(org.apache.cayenne.map.ObjRelationship) DbEntity(org.apache.cayenne.map.DbEntity) RelationshipQuery(org.apache.cayenne.query.RelationshipQuery) ObjectId(org.apache.cayenne.ObjectId) GenericResponse(org.apache.cayenne.util.GenericResponse) DbRelationship(org.apache.cayenne.map.DbRelationship) DataRow(org.apache.cayenne.DataRow)

Example 3 with GenericResponse

use of org.apache.cayenne.util.GenericResponse in project cayenne by apache.

the class DataDomainQueryAction method runQuery.

private void runQuery() {
    // reset
    this.fullResponse = new GenericResponse();
    this.response = this.fullResponse;
    this.queriesByNode = null;
    this.queriesByExecutedQueries = null;
    // whether this is null or not will driver further decisions on how to process prefetched rows
    this.prefetchResultsByPath = metadata.getPrefetchTree() != null && !metadata.isFetchingDataRows() ? new HashMap<String, List>() : null;
    // categorize queries by node and by "executable" query...
    query.route(this, domain.getEntityResolver(), null);
    // run categorized queries
    if (queriesByNode != null) {
        for (Map.Entry<QueryEngine, Collection<Query>> entry : queriesByNode.entrySet()) {
            QueryEngine nextNode = entry.getKey();
            Collection<Query> nodeQueries = entry.getValue();
            nextNode.performQueries(nodeQueries, this);
        }
    }
}
Also used : RelationshipQuery(org.apache.cayenne.query.RelationshipQuery) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Query(org.apache.cayenne.query.Query) PrefetchSelectQuery(org.apache.cayenne.query.PrefetchSelectQuery) RefreshQuery(org.apache.cayenne.query.RefreshQuery) GenericResponse(org.apache.cayenne.util.GenericResponse) HashMap(java.util.HashMap) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map) DataMap(org.apache.cayenne.map.DataMap)

Example 4 with GenericResponse

use of org.apache.cayenne.util.GenericResponse in project cayenne by apache.

the class DataDomainQueryAction method interceptRefreshQuery.

/**
 * @since 3.0
 */
private boolean interceptRefreshQuery() {
    if (query instanceof RefreshQuery) {
        RefreshQuery refreshQuery = (RefreshQuery) query;
        if (refreshQuery.isRefreshAll()) {
            // not sending any events - peer contexts will not get refreshed
            if (domain.getSharedSnapshotCache() != null) {
                domain.getSharedSnapshotCache().clear();
            } else {
                // remove snapshots from local ObjectStore only
                context.getObjectStore().getDataRowCache().clear();
            }
            context.getQueryCache().clear();
            GenericResponse response = new GenericResponse();
            response.addUpdateCount(1);
            this.response = response;
            return DONE;
        }
        Collection<Persistent> objects = (Collection<Persistent>) refreshQuery.getObjects();
        if (objects != null && !objects.isEmpty()) {
            Collection<ObjectId> ids = new ArrayList<>(objects.size());
            for (final Persistent object : objects) {
                ids.add(object.getObjectId());
            }
            if (domain.getSharedSnapshotCache() != null) {
                // send an event for removed snapshots
                domain.getSharedSnapshotCache().processSnapshotChanges(context.getObjectStore(), Collections.EMPTY_MAP, Collections.EMPTY_LIST, ids, Collections.EMPTY_LIST);
            } else {
                // remove snapshots from local ObjectStore only
                context.getObjectStore().getDataRowCache().processSnapshotChanges(context.getObjectStore(), Collections.EMPTY_MAP, Collections.EMPTY_LIST, ids, Collections.EMPTY_LIST);
            }
            GenericResponse response = new GenericResponse();
            response.addUpdateCount(1);
            this.response = response;
            return DONE;
        }
        // usually does a cascading refresh
        if (refreshQuery.getQuery() != null) {
            Query cachedQuery = refreshQuery.getQuery();
            String cacheKey = cachedQuery.getMetaData(context.getEntityResolver()).getCacheKey();
            context.getQueryCache().remove(cacheKey);
            this.response = domain.onQuery(context, cachedQuery);
            return DONE;
        }
        // 4. refresh groups...
        if (refreshQuery.getGroupKeys() != null && refreshQuery.getGroupKeys().length > 0) {
            String[] groups = refreshQuery.getGroupKeys();
            for (String group : groups) {
                domain.getQueryCache().removeGroup(group);
            }
            GenericResponse response = new GenericResponse();
            response.addUpdateCount(1);
            this.response = response;
            return DONE;
        }
    }
    return !DONE;
}
Also used : RelationshipQuery(org.apache.cayenne.query.RelationshipQuery) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Query(org.apache.cayenne.query.Query) PrefetchSelectQuery(org.apache.cayenne.query.PrefetchSelectQuery) RefreshQuery(org.apache.cayenne.query.RefreshQuery) GenericResponse(org.apache.cayenne.util.GenericResponse) ObjectId(org.apache.cayenne.ObjectId) RefreshQuery(org.apache.cayenne.query.RefreshQuery) ArrayList(java.util.ArrayList) Collection(java.util.Collection) Persistent(org.apache.cayenne.Persistent)

Example 5 with GenericResponse

use of org.apache.cayenne.util.GenericResponse in project cayenne by apache.

the class ClientChannelTest method testOnQuerySelectOverrideModifiedCached.

@Test
public void testOnQuerySelectOverrideModifiedCached() {
    ObjEntity entity = new ObjEntity("test_entity");
    entity.setClassName(MockPersistentObject.class.getName());
    DataMap dataMap = new DataMap("test");
    dataMap.addObjEntity(entity);
    Collection<DataMap> entities = Collections.singleton(dataMap);
    EntityResolver resolver = new EntityResolver(entities);
    CayenneContext context = new CayenneContext();
    context.setEntityResolver(resolver);
    ObjectId oid = new ObjectId("test_entity", "x", "y");
    MockPersistentObject o1 = new MockPersistentObject(oid);
    o1.setPersistenceState(PersistenceState.MODIFIED);
    context.getGraphManager().registerNode(oid, o1);
    assertSame(o1, context.getGraphManager().getNode(oid));
    // another object with the same GID ... we must merge it with cached and return
    // cached object instead of the one fetched
    MockPersistentObject o2 = new MockPersistentObject(oid);
    MockClientConnection connection = new MockClientConnection(new GenericResponse(Arrays.asList(o2)));
    ClientChannel channel = new ClientChannel(connection, false, new MockEventManager(), false);
    context.setChannel(channel);
    QueryResponse response = channel.onQuery(context, new SelectQuery("test_entity"));
    assertNotNull(response);
    assertEquals(1, response.size());
    List<?> list = response.firstList();
    assertNotNull(list);
    assertEquals(1, list.size());
    assertTrue("Expected cached object, got: " + list, list.contains(o1));
    assertSame(o1, context.getGraphManager().getNode(oid));
}
Also used : ObjectId(org.apache.cayenne.ObjectId) GenericResponse(org.apache.cayenne.util.GenericResponse) EntityResolver(org.apache.cayenne.map.EntityResolver) CayenneContext(org.apache.cayenne.CayenneContext) DataMap(org.apache.cayenne.map.DataMap) SelectQuery(org.apache.cayenne.query.SelectQuery) ObjEntity(org.apache.cayenne.map.ObjEntity) MockPersistentObject(org.apache.cayenne.MockPersistentObject) QueryResponse(org.apache.cayenne.QueryResponse) MockEventManager(org.apache.cayenne.event.MockEventManager) Test(org.junit.Test)

Aggregations

GenericResponse (org.apache.cayenne.util.GenericResponse)9 ObjectId (org.apache.cayenne.ObjectId)5 MockEventManager (org.apache.cayenne.event.MockEventManager)5 EntityResolver (org.apache.cayenne.map.EntityResolver)5 Test (org.junit.Test)5 DataMap (org.apache.cayenne.map.DataMap)4 CayenneContext (org.apache.cayenne.CayenneContext)3 MockPersistentObject (org.apache.cayenne.MockPersistentObject)3 QueryResponse (org.apache.cayenne.QueryResponse)3 ObjEntity (org.apache.cayenne.map.ObjEntity)3 Query (org.apache.cayenne.query.Query)3 RelationshipQuery (org.apache.cayenne.query.RelationshipQuery)3 SelectQuery (org.apache.cayenne.query.SelectQuery)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 ArrayList (java.util.ArrayList)2 Collection (java.util.Collection)2 Persistent (org.apache.cayenne.Persistent)2 ObjectIdQuery (org.apache.cayenne.query.ObjectIdQuery)2 PrefetchSelectQuery (org.apache.cayenne.query.PrefetchSelectQuery)2 RefreshQuery (org.apache.cayenne.query.RefreshQuery)2