Search in sources :

Example 26 with QueryResponse

use of org.apache.cayenne.QueryResponse in project cayenne by apache.

the class ChildDiffLoader method findObject.

protected Persistent findObject(Object nodeId) {
    // first do a lookup in ObjectStore; if even a hollow object is found,
    // return it;
    // if not - fetch.
    Persistent object = (Persistent) context.getGraphManager().getNode(nodeId);
    if (object != null) {
        return object;
    }
    ObjectId id = (ObjectId) nodeId;
    // modified
    if (id.isTemporary()) {
        return null;
    }
    // skip context cache lookup, go directly to its channel
    Query query = new ObjectIdQuery((ObjectId) nodeId);
    QueryResponse response = context.getChannel().onQuery(context, query);
    List<?> objects = response.firstList();
    if (objects.size() == 0) {
        throw new CayenneRuntimeException("No object for ID exists: %s", nodeId);
    } else if (objects.size() > 1) {
        throw new CayenneRuntimeException("Expected zero or one object, instead query matched: %d", objects.size());
    }
    return (Persistent) objects.get(0);
}
Also used : ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) Query(org.apache.cayenne.query.Query) ObjectId(org.apache.cayenne.ObjectId) QueryResponse(org.apache.cayenne.QueryResponse) CayenneRuntimeException(org.apache.cayenne.CayenneRuntimeException) Persistent(org.apache.cayenne.Persistent) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery)

Example 27 with QueryResponse

use of org.apache.cayenne.QueryResponse in project cayenne by apache.

the class ServerRuntimeTest method testGetDataChannel_CustomModule.

@Test
public void testGetDataChannel_CustomModule() {
    final DataChannel channel = new DataChannel() {

        public EntityResolver getEntityResolver() {
            return null;
        }

        public EventManager getEventManager() {
            return null;
        }

        public QueryResponse onQuery(ObjectContext originatingContext, Query query) {
            return null;
        }

        public GraphDiff onSync(ObjectContext originatingContext, GraphDiff changes, int syncType) {
            return null;
        }
    };
    Module module = binder -> binder.bind(DataChannel.class).toInstance(channel);
    ServerRuntime runtime = new ServerRuntime(Collections.singleton(module));
    assertSame(channel, runtime.getChannel());
}
Also used : ObjectContext(org.apache.cayenne.ObjectContext) Arrays(java.util.Arrays) DataContext(org.apache.cayenne.access.DataContext) Collection(java.util.Collection) Constants(org.apache.cayenne.configuration.Constants) Module(org.apache.cayenne.di.Module) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) DataChannel(org.apache.cayenne.DataChannel) Query(org.apache.cayenne.query.Query) BaseTransaction(org.apache.cayenne.tx.BaseTransaction) List(java.util.List) ObjectContextFactory(org.apache.cayenne.configuration.ObjectContextFactory) EntityResolver(org.apache.cayenne.map.EntityResolver) EventManager(org.apache.cayenne.event.EventManager) Arrays.asList(java.util.Arrays.asList) GraphDiff(org.apache.cayenne.graph.GraphDiff) QueryResponse(org.apache.cayenne.QueryResponse) TransactionFactory(org.apache.cayenne.tx.TransactionFactory) TransactionalOperation(org.apache.cayenne.tx.TransactionalOperation) Assert(org.junit.Assert) Collections(java.util.Collections) Key(org.apache.cayenne.di.Key) Mockito.mock(org.mockito.Mockito.mock) DataChannel(org.apache.cayenne.DataChannel) Query(org.apache.cayenne.query.Query) GraphDiff(org.apache.cayenne.graph.GraphDiff) ObjectContext(org.apache.cayenne.ObjectContext) Module(org.apache.cayenne.di.Module) Test(org.junit.Test)

Example 28 with QueryResponse

use of org.apache.cayenne.QueryResponse in project cayenne by apache.

the class StringIdQueryTest method testPerformQuery_MultipleEntities.

@Test
public void testPerformQuery_MultipleEntities() throws Exception {
    e1Helper.deleteAll();
    e1Helper.insert(3).insert(4);
    e2Helper.deleteAll();
    e2Helper.insert(5).insert(6).insert(7);
    StringIdQuery query = new StringIdQuery("E1:3", "E1:4", "E2:6", "E1:5");
    QueryResponse response = runtime.newContext().performGenericQuery(query);
    assertEquals(2, response.size());
    Set<String> ids = new HashSet<>();
    while (response.next()) {
        @SuppressWarnings("unchecked") List<DataRow> list = (List<DataRow>) response.currentList();
        for (DataRow row : list) {
            ids.add(row.getEntityName() + ":" + row.get("ID"));
        }
    }
    assertEquals(3, ids.size());
    assertTrue(ids.contains("E1:3"));
    assertTrue(ids.contains("E1:4"));
    assertTrue(ids.contains("E2:6"));
}
Also used : QueryResponse(org.apache.cayenne.QueryResponse) List(java.util.List) DataRow(org.apache.cayenne.DataRow) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 29 with QueryResponse

use of org.apache.cayenne.QueryResponse in project cayenne by apache.

the class AshwoodEntitySorter method findReflexiveMaster.

protected Object findReflexiveMaster(Persistent object, ObjRelationship toOneRel, String targetEntityName) {
    DbRelationship finalRel = toOneRel.getDbRelationships().get(0);
    ObjectContext context = object.getObjectContext();
    // the method
    if (object.getObjectId().isTemporary()) {
        return null;
    }
    ObjectIdQuery query = new ObjectIdQuery(object.getObjectId(), true, ObjectIdQuery.CACHE);
    QueryResponse response = context.getChannel().onQuery(null, query);
    List<?> result = response.firstList();
    if (result == null || result.size() == 0) {
        return null;
    }
    DataRow snapshot = (DataRow) result.get(0);
    ObjectId id = snapshot.createTargetObjectId(targetEntityName, finalRel);
    // hollow objects
    return (id != null) ? context.getGraphManager().getNode(id) : null;
}
Also used : ObjectId(org.apache.cayenne.ObjectId) DbRelationship(org.apache.cayenne.map.DbRelationship) QueryResponse(org.apache.cayenne.QueryResponse) ObjectContext(org.apache.cayenne.ObjectContext) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery) DataRow(org.apache.cayenne.DataRow)

Example 30 with QueryResponse

use of org.apache.cayenne.QueryResponse in project cayenne by apache.

the class ClientServerChannelIT method testPerformQueryPropagationInheritance.

@Test
public void testPerformQueryPropagationInheritance() throws Exception {
    tMtTable1.insert(65, "sub1", "xyz");
    SelectQuery query = new SelectQuery(ClientMtTable1.class);
    QueryResponse response = clientServerChannel.onQuery(null, query);
    assertNotNull(response);
    List<?> results = response.firstList();
    assertNotNull(results);
    assertEquals(1, results.size());
    Object result = results.get(0);
    assertTrue("Result is of wrong type: " + result, result instanceof ClientMtTable1Subclass1);
    ClientMtTable1Subclass1 clientObject = (ClientMtTable1Subclass1) result;
    assertEquals("sub1", clientObject.getGlobalAttribute1());
}
Also used : SelectQuery(org.apache.cayenne.query.SelectQuery) ClientMtTable1Subclass1(org.apache.cayenne.testdo.mt.ClientMtTable1Subclass1) QueryResponse(org.apache.cayenne.QueryResponse) Test(org.junit.Test)

Aggregations

QueryResponse (org.apache.cayenne.QueryResponse)31 Test (org.junit.Test)24 EJBQLQuery (org.apache.cayenne.query.EJBQLQuery)12 SelectQuery (org.apache.cayenne.query.SelectQuery)9 ObjectId (org.apache.cayenne.ObjectId)7 Query (org.apache.cayenne.query.Query)6 DataRow (org.apache.cayenne.DataRow)5 ObjectContext (org.apache.cayenne.ObjectContext)5 EntityResolver (org.apache.cayenne.map.EntityResolver)5 List (java.util.List)3 CayenneContext (org.apache.cayenne.CayenneContext)3 MockPersistentObject (org.apache.cayenne.MockPersistentObject)3 MockEventManager (org.apache.cayenne.event.MockEventManager)3 DataMap (org.apache.cayenne.map.DataMap)3 ObjEntity (org.apache.cayenne.map.ObjEntity)3 ObjectIdQuery (org.apache.cayenne.query.ObjectIdQuery)3 HashSet (java.util.HashSet)2 DataChannelFilter (org.apache.cayenne.DataChannelFilter)2 DataChannelFilterChain (org.apache.cayenne.DataChannelFilterChain)2 MockDataChannelFilter (org.apache.cayenne.MockDataChannelFilter)2