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