Search in sources :

Example 6 with LookupExecutor

use of org.motechproject.mds.lookup.LookupExecutor in project motech by motech.

the class MdsLookupServiceImpl method findMany.

@Override
public <T> List<T> findMany(String entityClassName, String lookupName, Map<String, ?> lookupParams, QueryParams queryParams) {
    LookupExecutor lookupExecutor = buildLookupExecutor(entityClassName, lookupName);
    Object result = lookupExecutor.execute(lookupParams, queryParams);
    return returnListResult(result);
}
Also used : LookupExecutor(org.motechproject.mds.lookup.LookupExecutor)

Example 7 with LookupExecutor

use of org.motechproject.mds.lookup.LookupExecutor in project motech by motech.

the class MDSDataProvider method findUsingLookup.

private Object findUsingLookup(String type, String lookupName, Map<String, String> lookupMap) {
    Object obj = null;
    LookupDto lookup = null;
    EntityDto entity = entityService.getEntityByClassName(type);
    if (entity != null) {
        lookup = entityService.getLookupByName(entity.getId(), lookupName);
    }
    if (entity != null && lookup != null) {
        String serviceName = MotechClassPool.getInterfaceName(type);
        MotechDataService service = OSGiServiceUtils.findService(bundleContext, serviceName);
        if (service != null) {
            Map<String, FieldDto> fieldsByName = entityService.getLookupFieldsMapping(entity.getId(), lookupName);
            LookupExecutor executor = new LookupExecutor(service, lookup, fieldsByName);
            obj = executor.execute(lookupMap);
        } else {
            getLogger().error("Service %s not found", serviceName);
        }
    }
    // we allow executing lookups that return multiple objects
    // if such a lookup returns more then 1 object we throw an exception
    Object result = null;
    if (obj instanceof Collection) {
        Collection collection = (Collection) obj;
        if (collection.size() == 1) {
            result = collection.iterator().next();
        } else if (collection.size() > 1) {
            throw new IllegalArgumentException(String.format("Data provided lookup for %s returned more then 1 object, number of objects found: %d", type, collection.size()));
        }
    } else {
        result = obj;
    }
    return result;
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) LookupDto(org.motechproject.mds.dto.LookupDto) Collection(java.util.Collection) LookupExecutor(org.motechproject.mds.lookup.LookupExecutor) MotechDataService(org.motechproject.mds.service.MotechDataService) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 8 with LookupExecutor

use of org.motechproject.mds.lookup.LookupExecutor in project motech by motech.

the class MdsRestFacadeImpl method readLookups.

private void readLookups(EntityInfo entity) {
    for (LookupDto lookup : entity.getLookups()) {
        Map<String, FieldDto> fieldMap = getLookupFieldsMapping(entity, lookup);
        String lookuMethodpName = lookup.getMethodName();
        if (lookup.isExposedViaRest()) {
            // we create executors for exposed lookups
            LookupExecutor executor = new LookupExecutor(dataService, lookup, fieldMap);
            lookupExecutors.put(lookuMethodpName, executor);
        } else {
            // we keep a list of forbidden lookups in order to print the appropriate error
            forbiddenLookupMethodNames.add(lookuMethodpName);
        }
    }
}
Also used : LookupDto(org.motechproject.mds.dto.LookupDto) LookupExecutor(org.motechproject.mds.lookup.LookupExecutor) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto)

Aggregations

LookupExecutor (org.motechproject.mds.lookup.LookupExecutor)8 LookupDto (org.motechproject.mds.dto.LookupDto)5 EntityDto (org.motechproject.mds.dto.EntityDto)4 FieldDto (org.motechproject.mds.dto.FieldDto)4 MotechDataService (org.motechproject.mds.service.MotechDataService)4 Collection (java.util.Collection)2 LookupExecutionException (org.motechproject.mds.exception.lookup.LookupExecutionException)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)1 LookupExecutorException (org.motechproject.mds.exception.lookup.LookupExecutorException)1 RestLookupExecutionForbiddenException (org.motechproject.mds.exception.rest.RestLookupExecutionForbiddenException)1 RestLookupNotFoundException (org.motechproject.mds.exception.rest.RestLookupNotFoundException)1 RestNoLookupResultException (org.motechproject.mds.exception.rest.RestNoLookupResultException)1 QueryParams (org.motechproject.mds.query.QueryParams)1 BasicEntityRecord (org.motechproject.mds.web.domain.BasicEntityRecord)1 Transactional (org.springframework.transaction.annotation.Transactional)1