Search in sources :

Example 1 with LookupExecutor

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

the class MdsLookupServiceImpl method findOne.

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

Example 2 with LookupExecutor

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

the class MdsLookupServiceImpl method buildLookupExecutor.

private LookupExecutor buildLookupExecutor(String entityClassName, String lookupName) {
    String fullyQualifiedEntityClassName;
    if (entityClassName.contains(".")) {
        fullyQualifiedEntityClassName = entityClassName;
    } else {
        fullyQualifiedEntityClassName = Constants.PackagesGenerated.ENTITY + "." + entityClassName;
    }
    MotechDataService dataService = OSGiServiceUtils.findService(bundleContext, MotechClassPool.getInterfaceName(fullyQualifiedEntityClassName));
    EntityDto entity = entityService.getEntityByClassName(fullyQualifiedEntityClassName);
    LookupDto lookup = entityService.getLookupByName(entity.getId(), lookupName);
    return new LookupExecutor(dataService, lookup, entityService.getLookupFieldsMapping(entity.getId(), lookupName));
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) LookupDto(org.motechproject.mds.dto.LookupDto) LookupExecutor(org.motechproject.mds.lookup.LookupExecutor) MotechDataService(org.motechproject.mds.service.MotechDataService)

Example 3 with LookupExecutor

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

the class MdsRestFacadeImpl method executeLookup.

@Override
@Transactional
public Object executeLookup(String lookupName, Map<String, String> lookupMap, QueryParams queryParams, boolean includeBlob) {
    if (lookupExecutors.containsKey(lookupName)) {
        LookupExecutor executor = lookupExecutors.get(lookupName);
        Object result = executor.execute(lookupMap, queryParams);
        if (result instanceof Collection) {
            if (includeBlob) {
                for (T value : ((Collection<T>) result)) {
                    getBlobs(value);
                }
            }
            return new RestResponse(entityName, entityClass.getName(), moduleName, namespace, executor.executeCount(lookupMap), queryParams, RestProjection.createProjectionCollection((Collection) result, restFields, blobFields));
        } else {
            if (result == null) {
                throw new RestNoLookupResultException("No result for lookup:" + lookupName);
            }
            if (includeBlob) {
                getBlobs((T) result);
            }
            return new RestResponse(entityName, entityClass.getName(), moduleName, namespace, 1l, new QueryParams(1, 1), RestProjection.createProjection(result, restFields, blobFields));
        }
    } else if (forbiddenLookupMethodNames.contains(lookupName)) {
        throw new RestLookupExecutionForbiddenException(lookupName);
    } else {
        throw new RestLookupNotFoundException(lookupName);
    }
}
Also used : RestNoLookupResultException(org.motechproject.mds.exception.rest.RestNoLookupResultException) RestLookupExecutionForbiddenException(org.motechproject.mds.exception.rest.RestLookupExecutionForbiddenException) RestLookupNotFoundException(org.motechproject.mds.exception.rest.RestLookupNotFoundException) Collection(java.util.Collection) QueryParams(org.motechproject.mds.query.QueryParams) LookupExecutor(org.motechproject.mds.lookup.LookupExecutor) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with LookupExecutor

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

the class InstanceServiceImpl method countRecordsByLookup.

@Override
public long countRecordsByLookup(Long entityId, String lookupName, Map<String, Object> lookupMap) {
    EntityDto entity = getEntity(entityId);
    validateCredentialsForReading(entity);
    LookupDto lookup = getLookupByName(entityId, lookupName);
    Map<String, FieldDto> fieldMap = entityService.getLookupFieldsMapping(entityId, lookupName);
    MotechDataService service = getServiceForEntity(entity);
    try {
        LookupExecutor lookupExecutor = new LookupExecutor(service, lookup, fieldMap);
        return lookupExecutor.executeCount(lookupMap);
    } catch (RuntimeException e) {
        throw new LookupExecutionException(e, LOOKUP_EXCEPTION_MESSAGE_KEY);
    }
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) LookupDto(org.motechproject.mds.dto.LookupDto) LookupExecutor(org.motechproject.mds.lookup.LookupExecutor) MotechDataService(org.motechproject.mds.service.MotechDataService) LookupExecutionException(org.motechproject.mds.exception.lookup.LookupExecutionException) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 5 with LookupExecutor

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

the class InstanceServiceImpl method getEntityRecordsFromLookup.

@Override
public List<BasicEntityRecord> getEntityRecordsFromLookup(Long entityId, String lookupName, Map<String, Object> lookupMap, QueryParams queryParams) {
    EntityDto entity = getEntity(entityId);
    validateCredentialsForReading(entity);
    LookupDto lookup = getLookupByName(entityId, lookupName);
    List<FieldDto> fields = entityService.getEntityFieldsForUI(entityId);
    Map<String, FieldDto> fieldMap = entityService.getLookupFieldsMapping(entityId, lookupName);
    MotechDataService service = getServiceForEntity(entity);
    try {
        LookupExecutor lookupExecutor = new LookupExecutor(service, lookup, fieldMap);
        Object result = lookupExecutor.execute(lookupMap, queryParams);
        if (lookup.isSingleObjectReturn()) {
            BasicEntityRecord record = instanceToBasicRecord(result, entity, fields, service, EntityType.STANDARD, getRelatedEntitiesFields(fields));
            return (record == null) ? new ArrayList<BasicEntityRecord>() : Collections.singletonList(record);
        } else {
            List instances = (List) result;
            return instancesToBasicRecords(instances, entity, fields, service, EntityType.STANDARD);
        }
    } catch (LookupExecutorException e) {
        if (e.getMessageKey() != null) {
            throw new LookupExecutionException(e, e.getMessageKey());
        } else {
            throw new LookupExecutionException(e, LOOKUP_EXCEPTION_MESSAGE_KEY);
        }
    } catch (RuntimeException e) {
        throw new LookupExecutionException(e, LOOKUP_EXCEPTION_MESSAGE_KEY);
    }
}
Also used : LookupDto(org.motechproject.mds.dto.LookupDto) LookupExecutor(org.motechproject.mds.lookup.LookupExecutor) BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) MotechDataService(org.motechproject.mds.service.MotechDataService) EntityDto(org.motechproject.mds.dto.EntityDto) ArrayList(java.util.ArrayList) List(java.util.List) LookupExecutionException(org.motechproject.mds.exception.lookup.LookupExecutionException) FieldDto(org.motechproject.mds.dto.FieldDto) LookupExecutorException(org.motechproject.mds.exception.lookup.LookupExecutorException)

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