use of org.apache.cayenne.lifecycle.id.EntityIdCoder 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