Search in sources :

Example 1 with LookupExecutorException

use of org.motechproject.mds.exception.lookup.LookupExecutorException in project motech by motech.

the class LookupExecutor method executeCount.

public long executeCount(Map<String, ?> lookupMap) {
    List<Object> args = getLookupArgs(lookupMap);
    List<Class> argTypes = buildArgTypes();
    String countMethodName = LookupName.lookupCountMethod(lookup.getMethodName());
    try {
        return (long) MethodUtils.invokeMethod(dataService, countMethodName, args.toArray(new Object[args.size()]), argTypes.toArray(new Class[argTypes.size()]));
    } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
        throw new LookupExecutorException("Unable to execute count lookup " + lookup.getLookupName() + ".", e, null);
    }
}
Also used : InvocationTargetException(java.lang.reflect.InvocationTargetException) LookupExecutorException(org.motechproject.mds.exception.lookup.LookupExecutorException)

Example 2 with LookupExecutorException

use of org.motechproject.mds.exception.lookup.LookupExecutorException 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)

Example 3 with LookupExecutorException

use of org.motechproject.mds.exception.lookup.LookupExecutorException in project motech by motech.

the class LookupExecutor method execute.

public Object execute(Map<String, ?> lookupMap, QueryParams queryParams) {
    List<Object> args = getLookupArgs(lookupMap);
    List<Class> argTypes = buildArgTypes();
    String lookupExceptionMessage = "Unable to execute lookup ";
    String lookupExceptionMessageKey = "mds.error.lookupExecError";
    if (queryParams != null) {
        args.add(queryParams);
        argTypes.add(QueryParams.class);
    }
    try {
        return MethodUtils.invokeMethod(dataService, lookup.getMethodName(), args.toArray(new Object[args.size()]), argTypes.toArray(new Class[argTypes.size()]));
    } catch (NoSuchMethodException | IllegalAccessException e) {
        throw new LookupExecutorException(lookupExceptionMessage + lookup.getLookupName() + ".", e, null);
    } catch (InvocationTargetException e) {
        if (e.getTargetException() instanceof JDOUserException) {
            JDOUserException userException = (JDOUserException) e.getTargetException();
            lookupExceptionMessageKey = "mds.error.lookupExecUserError";
            for (Throwable exception : userException.getNestedExceptions()) {
                if (exception instanceof QueryNotUniqueException) {
                    lookupExceptionMessageKey = "mds.error.lookupExecNotUniqueError";
                }
            }
        }
        throw new LookupExecutorException(lookupExceptionMessage + lookup.getLookupName() + ".", e, lookupExceptionMessageKey);
    }
}
Also used : JDOUserException(javax.jdo.JDOUserException) InvocationTargetException(java.lang.reflect.InvocationTargetException) QueryNotUniqueException(org.datanucleus.store.query.QueryNotUniqueException) LookupExecutorException(org.motechproject.mds.exception.lookup.LookupExecutorException)

Aggregations

LookupExecutorException (org.motechproject.mds.exception.lookup.LookupExecutorException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 JDOUserException (javax.jdo.JDOUserException)1 QueryNotUniqueException (org.datanucleus.store.query.QueryNotUniqueException)1 EntityDto (org.motechproject.mds.dto.EntityDto)1 FieldDto (org.motechproject.mds.dto.FieldDto)1 LookupDto (org.motechproject.mds.dto.LookupDto)1 LookupExecutionException (org.motechproject.mds.exception.lookup.LookupExecutionException)1 LookupExecutor (org.motechproject.mds.lookup.LookupExecutor)1 MotechDataService (org.motechproject.mds.service.MotechDataService)1 BasicEntityRecord (org.motechproject.mds.web.domain.BasicEntityRecord)1