use of org.motechproject.mds.service.MotechDataService in project motech by motech.
the class InstanceServiceImpl method getEntityInstance.
@Override
public EntityRecord getEntityInstance(Long entityId, Long instanceId) {
EntityDto entity = getEntity(entityId);
validateCredentialsForReading(entity);
MotechDataService service = getServiceForEntity(entity);
Object instance = service.retrieve(ID_FIELD_NAME, instanceId);
if (instance == null) {
throw new ObjectNotFoundException(entity.getName(), instanceId);
}
List<FieldDto> fields = entityService.getEntityFieldsForUI(entityId);
Map<String, List<FieldDto>> relatedEntitiesFields = getRelatedEntitiesFields(fields);
return instanceToRecord(instance, entity, fields, service, EntityType.STANDARD, relatedEntitiesFields);
}
use of org.motechproject.mds.service.MotechDataService in project motech by motech.
the class InstanceServiceImpl method setRelationProperty.
private void setRelationProperty(Object instance, FieldRecord fieldRecord) throws NoSuchMethodException, ClassNotFoundException, NoSuchFieldException, IllegalAccessException, InstantiationException, CannotCompileException {
String fieldName = fieldRecord.getName();
String methodName = MemberUtil.getSetterName(fieldName);
Class<?> clazz = instance.getClass().getClassLoader().loadClass(instance.getClass().getName());
Field field = FieldUtils.getField(clazz, fieldName, true);
Class<?> parameterType = field.getType();
Object value = null;
MotechDataService serviceForRelatedClass = null;
TypeDto type = getType(fieldRecord);
if (StringUtils.isNotEmpty(ObjectUtils.toString(fieldRecord.getValue()))) {
Class<?> argumentType = null;
if (type.equals(TypeDto.ONE_TO_MANY_RELATIONSHIP) || type.equals(TypeDto.MANY_TO_MANY_RELATIONSHIP)) {
argumentType = (Class<?>) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0];
} else if (type.equals(TypeDto.MANY_TO_ONE_RELATIONSHIP) || type.equals(TypeDto.ONE_TO_ONE_RELATIONSHIP)) {
argumentType = parameterType;
}
serviceForRelatedClass = DataServiceHelper.getDataService(bundleContext, argumentType.getName());
Object related = PropertyUtil.safeGetProperty(instance, fieldName);
value = buildRelatedInstances(serviceForRelatedClass, parameterType, argumentType, fieldRecord.getValue(), related);
}
Method method = MethodUtils.getAccessibleMethod(instance.getClass(), methodName, parameterType);
invokeMethod(method, instance, value, methodName, fieldName);
}
use of org.motechproject.mds.service.MotechDataService 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.service.MotechDataService in project motech by motech.
the class InstanceServiceImpl method deleteInstance.
@Override
public void deleteInstance(Long entityId, Long instanceId) {
EntityDto entity = getEntity(entityId);
validateCredentials(entity);
validateNonEditableProperty(entity);
MotechDataService service = getServiceForEntity(entity);
service.delete(ID_FIELD_NAME, instanceId);
}
use of org.motechproject.mds.service.MotechDataService in project motech by motech.
the class InstanceServiceImpl method getInstanceValueAsRelatedField.
@Override
public FieldRecord getInstanceValueAsRelatedField(Long entityId, Long fieldId, Long instanceId) {
validateCredentialsForReading(getEntity(entityId));
try {
FieldRecord fieldRecord;
FieldDto field = entityService.getEntityFieldById(entityId, fieldId);
MotechDataService service = DataServiceHelper.getDataService(bundleContext, field.getMetadata(RELATED_CLASS).getValue());
Object instance = service.findById(instanceId);
if (instance == null) {
throw new ObjectNotFoundException(service.getClassType().getName(), instanceId);
}
List<FieldDto> relatedEntityFields = getEntityFieldsByClassName(field.getMetadata(RELATED_CLASS).getValue());
fieldRecord = new FieldRecord(field);
fieldRecord.setValue(parseValueForDisplay(instance, relatedEntityFields));
fieldRecord.setDisplayValue(instance.toString());
return fieldRecord;
} catch (IllegalAccessException | NoSuchMethodException | InvocationTargetException e) {
throw new ObjectReadException(entityId, e);
}
}
Aggregations