Search in sources :

Example 16 with FieldDto

use of org.motechproject.mds.dto.FieldDto in project motech by motech.

the class InstanceServiceImpl method saveInstance.

@Override
@Transactional
public Object saveInstance(EntityRecord entityRecord, Long deleteValueFieldId) {
    EntityDto entity = getEntity(entityRecord.getEntitySchemaId());
    validateCredentials(entity);
    validateNonEditableProperty(entity);
    List<FieldDto> entityFields = getEntityFields(entityRecord.getEntitySchemaId());
    try {
        MotechDataService service = getServiceForEntity(entity);
        Class<?> entityClass = getEntityClass(entity);
        boolean newObject = entityRecord.getId() == null;
        Object instance;
        if (newObject) {
            instance = newInstanceFromEntityRecord(entityClass, entityFields, entityRecord.getFields(), service);
            return service.create(instance);
        } else {
            instance = service.retrieve(ID_FIELD_NAME, entityRecord.getId());
            if (instance == null) {
                throw new ObjectNotFoundException(entity.getName(), entityRecord.getId());
            }
            updateFields(instance, entityRecord.getFields(), service, deleteValueFieldId, true);
            return service.update(instance);
        }
    } catch (Exception e) {
        if (entityRecord.getId() == null) {
            throw new ObjectCreateException(entity.getName(), e);
        } else {
            throw new ObjectUpdateException(entity.getName(), entityRecord.getId(), e);
        }
    }
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) ObjectNotFoundException(org.motechproject.mds.exception.object.ObjectNotFoundException) MotechDataService(org.motechproject.mds.service.MotechDataService) ObjectReadException(org.motechproject.mds.exception.object.ObjectReadException) FieldReadOnlyException(org.motechproject.mds.exception.field.FieldReadOnlyException) InvocationTargetException(java.lang.reflect.InvocationTargetException) LookupNotFoundException(org.motechproject.mds.exception.lookup.LookupNotFoundException) ObjectUpdateException(org.motechproject.mds.exception.object.ObjectUpdateException) LookupExecutorException(org.motechproject.mds.exception.lookup.LookupExecutorException) SecurityException(org.motechproject.mds.exception.object.SecurityException) EntityNotFoundException(org.motechproject.mds.exception.entity.EntityNotFoundException) LookupExecutionException(org.motechproject.mds.exception.lookup.LookupExecutionException) ObjectCreateException(org.motechproject.mds.exception.object.ObjectCreateException) FieldNotFoundException(org.motechproject.mds.exception.field.FieldNotFoundException) ObjectNotFoundException(org.motechproject.mds.exception.object.ObjectNotFoundException) InstanceNotFoundException(javax.management.InstanceNotFoundException) EntityInstancesNonEditableException(org.motechproject.mds.exception.entity.EntityInstancesNonEditableException) CannotCompileException(javassist.CannotCompileException) ObjectCreateException(org.motechproject.mds.exception.object.ObjectCreateException) ObjectUpdateException(org.motechproject.mds.exception.object.ObjectUpdateException) FieldDto(org.motechproject.mds.dto.FieldDto) Transactional(org.springframework.transaction.annotation.Transactional)

Example 17 with FieldDto

use of org.motechproject.mds.dto.FieldDto in project motech by motech.

the class InstanceServiceImpl method getInstanceFields.

@Override
public List<FieldInstanceDto> getInstanceFields(Long entityId, Long instanceId) {
    EntityDto entity = entityService.getEntity(entityId);
    validateCredentialsForReading(entity);
    List<FieldDto> fields = entityService.getEntityFieldsForUI(entityId);
    List<FieldInstanceDto> result = new ArrayList<>();
    for (FieldDto field : fields) {
        FieldInstanceDto fieldInstanceDto = new FieldInstanceDto(field.getId(), instanceId, field.getBasic());
        result.add(fieldInstanceDto);
    }
    return result;
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) ArrayList(java.util.ArrayList) FieldInstanceDto(org.motechproject.mds.dto.FieldInstanceDto) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 18 with FieldDto

use of org.motechproject.mds.dto.FieldDto in project motech by motech.

the class InstanceServiceImpl method buildRelatedInstances.

private Object buildRelatedInstances(MotechDataService service, Class<?> parameterType, Class<?> argumentType, Object fieldValue, Object relatedObject) throws IllegalAccessException, InstantiationException, ClassNotFoundException, NoSuchMethodException, CannotCompileException, NoSuchFieldException {
    Object parsedValue;
    RelationshipsUpdate relationshipsUpdate = mapper.convertValue(fieldValue, RelationshipsUpdate.class);
    EntityDto relatedEntity = getEntity(argumentType.getName());
    List<FieldDto> entityFields = getEntityFields(relatedEntity.getId());
    if (Collection.class.isAssignableFrom(parameterType)) {
        parsedValue = parseRelationshipCollection(service, (Class<? extends Collection>) parameterType, argumentType, (Collection) relatedObject, relationshipsUpdate, entityFields);
    } else {
        parsedValue = parseRelationshipValue(service, argumentType, relationshipsUpdate, entityFields);
    }
    return parsedValue;
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) RelationshipsUpdate(org.motechproject.mds.web.domain.RelationshipsUpdate) Collection(java.util.Collection) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 19 with FieldDto

use of org.motechproject.mds.dto.FieldDto in project motech by motech.

the class InstanceServiceImpl method newInstanceFromEntityRecord.

private Object newInstanceFromEntityRecord(Class<?> entityClass, List<FieldDto> entityFields, List<FieldRecord> fields, MotechDataService service) throws IllegalAccessException, InstantiationException, ClassNotFoundException, NoSuchMethodException, CannotCompileException, NoSuchFieldException {
    Object instance = entityClass.newInstance();
    for (FieldDto entityField : entityFields) {
        if (entityField.getType().isMap() && entityField.getBasic().getDefaultValue() != null) {
            setInstanceFieldMap(instance, entityField);
        }
    }
    updateFields(instance, fields, service, null, false);
    return instance;
}
Also used : FieldDto(org.motechproject.mds.dto.FieldDto)

Example 20 with FieldDto

use of org.motechproject.mds.dto.FieldDto in project motech by motech.

the class InstanceServiceImpl method newInstance.

@Override
public EntityRecord newInstance(Long entityId) {
    validateCredentials(getEntity(entityId));
    List<FieldDto> fields = entityService.getEntityFieldsForUI(entityId);
    List<FieldRecord> fieldRecords = new ArrayList<>();
    for (FieldDto field : fields) {
        FieldRecord fieldRecord = new FieldRecord(field);
        fieldRecords.add(fieldRecord);
    }
    populateDefaultFields(fieldRecords);
    return new EntityRecord(null, entityId, fieldRecords);
}
Also used : EntityRecord(org.motechproject.mds.web.domain.EntityRecord) BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) ArrayList(java.util.ArrayList) BasicFieldRecord(org.motechproject.mds.web.domain.BasicFieldRecord) FieldRecord(org.motechproject.mds.web.domain.FieldRecord) FieldDto(org.motechproject.mds.dto.FieldDto)

Aggregations

FieldDto (org.motechproject.mds.dto.FieldDto)158 Test (org.junit.Test)61 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)60 EntityDto (org.motechproject.mds.dto.EntityDto)53 ArrayList (java.util.ArrayList)47 LookupDto (org.motechproject.mds.dto.LookupDto)29 List (java.util.List)23 MetadataDto (org.motechproject.mds.dto.MetadataDto)22 FieldBasicDto (org.motechproject.mds.dto.FieldBasicDto)19 TypeDto (org.motechproject.mds.dto.TypeDto)17 Method (java.lang.reflect.Method)14 MotechDataService (org.motechproject.mds.service.MotechDataService)14 HashMap (java.util.HashMap)12 Field (org.motechproject.mds.domain.Field)12 FieldTestHelper.lookupFieldDto (org.motechproject.mds.testutil.FieldTestHelper.lookupFieldDto)12 Arrays.asList (java.util.Arrays.asList)11 SettingDto (org.motechproject.mds.dto.SettingDto)11 Before (org.junit.Before)7 AdvancedSettingsDto (org.motechproject.mds.dto.AdvancedSettingsDto)7 BasicEntityRecord (org.motechproject.mds.web.domain.BasicEntityRecord)7