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