Search in sources :

Example 26 with MotechDataService

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);
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) ObjectNotFoundException(org.motechproject.mds.exception.object.ObjectNotFoundException) ArrayList(java.util.ArrayList) List(java.util.List) MotechDataService(org.motechproject.mds.service.MotechDataService) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 27 with MotechDataService

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);
}
Also used : Field(java.lang.reflect.Field) TypeDto(org.motechproject.mds.dto.TypeDto) Method(java.lang.reflect.Method) MotechDataService(org.motechproject.mds.service.MotechDataService)

Example 28 with MotechDataService

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);
    }
}
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 29 with MotechDataService

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);
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) MotechDataService(org.motechproject.mds.service.MotechDataService)

Example 30 with MotechDataService

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);
    }
}
Also used : ObjectNotFoundException(org.motechproject.mds.exception.object.ObjectNotFoundException) BasicFieldRecord(org.motechproject.mds.web.domain.BasicFieldRecord) FieldRecord(org.motechproject.mds.web.domain.FieldRecord) ObjectReadException(org.motechproject.mds.exception.object.ObjectReadException) MotechDataService(org.motechproject.mds.service.MotechDataService) InvocationTargetException(java.lang.reflect.InvocationTargetException) FieldDto(org.motechproject.mds.dto.FieldDto)

Aggregations

MotechDataService (org.motechproject.mds.service.MotechDataService)52 EntityDto (org.motechproject.mds.dto.EntityDto)26 FieldDto (org.motechproject.mds.dto.FieldDto)14 ArrayList (java.util.ArrayList)13 List (java.util.List)9 Test (org.junit.Test)8 Entity (org.motechproject.mds.domain.Entity)4 LookupDto (org.motechproject.mds.dto.LookupDto)4 ObjectNotFoundException (org.motechproject.mds.exception.object.ObjectNotFoundException)4 LookupExecutor (org.motechproject.mds.lookup.LookupExecutor)4 BasicEntityRecord (org.motechproject.mds.web.domain.BasicEntityRecord)4 Bundle (org.osgi.framework.Bundle)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Collection (java.util.Collection)3 LookupExecutionException (org.motechproject.mds.exception.lookup.LookupExecutionException)3 ObjectReadException (org.motechproject.mds.exception.object.ObjectReadException)3 DefaultMotechDataService (org.motechproject.mds.service.DefaultMotechDataService)3 EntityRecord (org.motechproject.mds.web.domain.EntityRecord)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2