Search in sources :

Example 1 with EntityIdCoder

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

HashMap (java.util.HashMap)1 DataObject (org.apache.cayenne.DataObject)1 ObjectId (org.apache.cayenne.ObjectId)1 Expression (org.apache.cayenne.exp.Expression)1 EntityIdCoder (org.apache.cayenne.lifecycle.id.EntityIdCoder)1 EntityResolver (org.apache.cayenne.map.EntityResolver)1 ObjEntity (org.apache.cayenne.map.ObjEntity)1 ObjectIdQuery (org.apache.cayenne.query.ObjectIdQuery)1 ObjectSelect (org.apache.cayenne.query.ObjectSelect)1