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);
}
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);
}
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);
}
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));
}
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;
}
Aggregations