Search in sources :

Example 1 with ObjectSelect

use of org.apache.cayenne.query.ObjectSelect in project cayenne by apache.

the class DataDomainFiltersIT method testOnQuery_Blocking.

@Test
public void testOnQuery_Blocking() {
    DataDomain domain = runtime.getDataDomain();
    QueryResponse r1 = new ListResponse();
    QueryResponse r2 = new ListResponse();
    DataChannelQueryFilter f1 = (originatingContext, query, filterChain) -> r1;
    DataChannelQueryFilter f2 = (originatingContext, query, filterChain) -> r2;
    domain.queryFilters.add(f1);
    domain.queryFilters.add(f2);
    ObjectSelect<Artist> query = ObjectSelect.query(Artist.class);
    QueryResponse response = domain.onQuery(context, query);
    assertSame(r2, response);
}
Also used : ObjectContext(org.apache.cayenne.ObjectContext) DataChannelSyncFilter(org.apache.cayenne.DataChannelSyncFilter) DataChannelSyncFilterChain(org.apache.cayenne.DataChannelSyncFilterChain) ServerCase(org.apache.cayenne.unit.di.server.ServerCase) Assert.assertNotNull(org.junit.Assert.assertNotNull) Test(org.junit.Test) CayenneProjects(org.apache.cayenne.unit.di.server.CayenneProjects) ArrayList(java.util.ArrayList) Query(org.apache.cayenne.query.Query) Artist(org.apache.cayenne.testdo.testmap.Artist) Assert.assertSame(org.junit.Assert.assertSame) DataChannelQueryFilterChain(org.apache.cayenne.DataChannelQueryFilterChain) List(java.util.List) PostPersist(org.apache.cayenne.annotation.PostPersist) Inject(org.apache.cayenne.di.Inject) GraphDiff(org.apache.cayenne.graph.GraphDiff) QueryResponse(org.apache.cayenne.QueryResponse) DataChannelQueryFilter(org.apache.cayenne.DataChannelQueryFilter) ObjectSelect(org.apache.cayenne.query.ObjectSelect) ServerRuntime(org.apache.cayenne.configuration.server.ServerRuntime) UseServerRuntime(org.apache.cayenne.unit.di.server.UseServerRuntime) Assert.assertEquals(org.junit.Assert.assertEquals) ListResponse(org.apache.cayenne.util.ListResponse) Artist(org.apache.cayenne.testdo.testmap.Artist) ListResponse(org.apache.cayenne.util.ListResponse) QueryResponse(org.apache.cayenne.QueryResponse) DataChannelQueryFilter(org.apache.cayenne.DataChannelQueryFilter) Test(org.junit.Test)

Example 2 with ObjectSelect

use of org.apache.cayenne.query.ObjectSelect in project cayenne by apache.

the class ClientServerChannelQueryIT method testSelectQueryEntityNameRoot.

@Test
public void testSelectQueryEntityNameRoot() throws Exception {
    createTwoMtTable1sAnd2sDataSet();
    ObjectSelect q = ObjectSelect.query(MtTable1.class);
    List<?> results = context.performQuery(q);
    assertEquals(2, results.size());
    assertTrue(results.get(0) instanceof ClientMtTable1);
}
Also used : ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ObjectSelect(org.apache.cayenne.query.ObjectSelect) Test(org.junit.Test)

Example 3 with ObjectSelect

use of org.apache.cayenne.query.ObjectSelect in project cayenne by apache.

the class ClientServerChannelQueryIT method testSelectQueryOrdering.

@Test
public void testSelectQueryOrdering() throws Exception {
    createTwoMtTable1sAnd2sDataSet();
    ObjectSelect q = ObjectSelect.query(MtTable1.class).orderBy(ClientMtTable1.GLOBAL_ATTRIBUTE1.asc());
    List<?> results = context.performQuery(q);
    assertEquals(2, results.size());
    ClientMtTable1 o1 = (ClientMtTable1) results.get(0);
    ClientMtTable1 o2 = (ClientMtTable1) results.get(1);
    assertTrue(o1.getGlobalAttribute1().compareTo(o2.getGlobalAttribute1()) < 0);
    // now run the same query with reverse ordering to check that the first ordering
    // result wasn't coincidental.
    q.getOrderings().clear();
    q.orderBy(ClientMtTable1.GLOBAL_ATTRIBUTE1.desc());
    List<?> results1 = context.performQuery(q);
    assertEquals(2, results1.size());
    ClientMtTable1 o3 = (ClientMtTable1) results1.get(0);
    ClientMtTable1 o4 = (ClientMtTable1) results1.get(1);
    assertTrue(o3.getGlobalAttribute1().compareTo(o4.getGlobalAttribute1()) > 0);
}
Also used : ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) MtTable1(org.apache.cayenne.testdo.mt.MtTable1) ClientMtTable1(org.apache.cayenne.testdo.mt.ClientMtTable1) ObjectSelect(org.apache.cayenne.query.ObjectSelect) Test(org.junit.Test)

Example 4 with ObjectSelect

use of org.apache.cayenne.query.ObjectSelect in project cayenne by apache.

the class DataDomainFiltersIT method testOnQuery_FilterOrdering.

@Test
public void testOnQuery_FilterOrdering() {
    DataDomain domain = runtime.getDataDomain();
    List<String> results = new ArrayList<>();
    DataChannelQueryFilter f1 = (originatingContext, query, filterChain) -> {
        results.add("f1start");
        QueryResponse response = filterChain.onQuery(originatingContext, query);
        results.add("f1end");
        return response;
    };
    DataChannelQueryFilter f2 = (originatingContext, query, filterChain) -> {
        results.add("f2start");
        QueryResponse response = filterChain.onQuery(originatingContext, query);
        results.add("f2end");
        return response;
    };
    domain.queryFilters.add(f1);
    domain.queryFilters.add(f2);
    ObjectSelect<Artist> query = ObjectSelect.query(Artist.class);
    QueryResponse response = domain.onQuery(context, query);
    assertNotNull(response);
    assertEquals(4, results.size());
    assertEquals("f2start", results.get(0));
    assertEquals("f1start", results.get(1));
    assertEquals("f1end", results.get(2));
    assertEquals("f2end", results.get(3));
}
Also used : ObjectContext(org.apache.cayenne.ObjectContext) DataChannelSyncFilter(org.apache.cayenne.DataChannelSyncFilter) DataChannelSyncFilterChain(org.apache.cayenne.DataChannelSyncFilterChain) ServerCase(org.apache.cayenne.unit.di.server.ServerCase) Assert.assertNotNull(org.junit.Assert.assertNotNull) Test(org.junit.Test) CayenneProjects(org.apache.cayenne.unit.di.server.CayenneProjects) ArrayList(java.util.ArrayList) Query(org.apache.cayenne.query.Query) Artist(org.apache.cayenne.testdo.testmap.Artist) Assert.assertSame(org.junit.Assert.assertSame) DataChannelQueryFilterChain(org.apache.cayenne.DataChannelQueryFilterChain) List(java.util.List) PostPersist(org.apache.cayenne.annotation.PostPersist) Inject(org.apache.cayenne.di.Inject) GraphDiff(org.apache.cayenne.graph.GraphDiff) QueryResponse(org.apache.cayenne.QueryResponse) DataChannelQueryFilter(org.apache.cayenne.DataChannelQueryFilter) ObjectSelect(org.apache.cayenne.query.ObjectSelect) ServerRuntime(org.apache.cayenne.configuration.server.ServerRuntime) UseServerRuntime(org.apache.cayenne.unit.di.server.UseServerRuntime) Assert.assertEquals(org.junit.Assert.assertEquals) ListResponse(org.apache.cayenne.util.ListResponse) Artist(org.apache.cayenne.testdo.testmap.Artist) QueryResponse(org.apache.cayenne.QueryResponse) ArrayList(java.util.ArrayList) DataChannelQueryFilter(org.apache.cayenne.DataChannelQueryFilter) Test(org.junit.Test)

Example 5 with ObjectSelect

use of org.apache.cayenne.query.ObjectSelect in project cayenne by apache.

the class ObjectIdBatchFault method fetchObjects.

private Map<String, Object> fetchObjects() {
    if (sources == null) {
        return Collections.emptyMap();
    }
    EntityResolver resolver = context.getEntityResolver();
    // reasons
    if (sources.size() == 1) {
        String uuid = sources.get(0).getId();
        String entityName = EntityIdCoder.getEntityName(uuid);
        ObjEntity entity = resolver.getObjEntity(entityName);
        ObjectId id = new EntityIdCoder(entity).toObjectId(uuid);
        Object object = Cayenne.objectForQuery(context, new ObjectIdQuery(id));
        if (object == null) {
            return Collections.emptyMap();
        } else {
            return Collections.singletonMap(uuid, object);
        }
    }
    Map<String, ObjectSelect<DataObject>> queriesByEntity = new HashMap<>();
    Map<String, EntityIdCoder> codersByEntity = new HashMap<>();
    for (ObjectIdBatchSourceItem source : sources) {
        String uuid = source.getId();
        String entityName = EntityIdCoder.getEntityName(uuid);
        EntityIdCoder coder = codersByEntity.get(entityName);
        ObjectSelect<DataObject> query;
        if (coder == null) {
            coder = new EntityIdCoder(resolver.getObjEntity(entityName));
            codersByEntity.put(entityName, coder);
            query = ObjectSelect.query(DataObject.class, entityName);
            queriesByEntity.put(entityName, query);
        } else {
            query = queriesByEntity.get(entityName);
        }
        ObjectId id = coder.toObjectId(uuid);
        Expression idExp = ExpressionFactory.matchAllDbExp(id.getIdSnapshot(), Expression.EQUAL_TO);
        query.or(idExp);
    }
    int capacity = (int) Math.ceil(sources.size() / 0.75d);
    Map<String, Object> results = new HashMap<>(capacity);
    for (ObjectSelect<DataObject> query : queriesByEntity.values()) {
        EntityIdCoder coder = codersByEntity.get(query.getEntityName());
        List<DataObject> objects = query.select(context);
        for (DataObject object : objects) {
            String uuid = coder.toStringId(object.getObjectId());
            results.put(uuid, object);
        }
    }
    return results;
}
Also used : EntityIdCoder(org.apache.cayenne.lifecycle.id.EntityIdCoder) ObjectId(org.apache.cayenne.ObjectId) HashMap(java.util.HashMap) ObjectSelect(org.apache.cayenne.query.ObjectSelect) EntityResolver(org.apache.cayenne.map.EntityResolver) ObjEntity(org.apache.cayenne.map.ObjEntity) DataObject(org.apache.cayenne.DataObject) Expression(org.apache.cayenne.exp.Expression) DataObject(org.apache.cayenne.DataObject) ObjectIdQuery(org.apache.cayenne.query.ObjectIdQuery)

Aggregations

ObjectSelect (org.apache.cayenne.query.ObjectSelect)8 Test (org.junit.Test)6 Expression (org.apache.cayenne.exp.Expression)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 List (java.util.List)2 DataChannelQueryFilter (org.apache.cayenne.DataChannelQueryFilter)2 DataChannelQueryFilterChain (org.apache.cayenne.DataChannelQueryFilterChain)2 DataChannelSyncFilter (org.apache.cayenne.DataChannelSyncFilter)2 DataChannelSyncFilterChain (org.apache.cayenne.DataChannelSyncFilterChain)2 DataObject (org.apache.cayenne.DataObject)2 ObjectContext (org.apache.cayenne.ObjectContext)2 QueryResponse (org.apache.cayenne.QueryResponse)2 PostPersist (org.apache.cayenne.annotation.PostPersist)2 ServerRuntime (org.apache.cayenne.configuration.server.ServerRuntime)2 Inject (org.apache.cayenne.di.Inject)2 GraphDiff (org.apache.cayenne.graph.GraphDiff)2 ObjEntity (org.apache.cayenne.map.ObjEntity)2 Query (org.apache.cayenne.query.Query)2 ClientMtTable1 (org.apache.cayenne.testdo.mt.ClientMtTable1)2