Search in sources :

Example 16 with EntityDto

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

the class InstanceServiceImpl method verifyEntityAccess.

@Override
public void verifyEntityAccess(Long entityId) {
    EntityDto entity = getEntity(entityId);
    validateCredentialsForReading(entity);
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto)

Example 17 with EntityDto

use of org.motechproject.mds.dto.EntityDto 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 18 with EntityDto

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

the class InstanceServiceImpl method revertPreviousVersion.

@Override
public void revertPreviousVersion(Long entityId, Long instanceId, Long historyId) {
    validateNonEditableProperty(entityId);
    EntityDto entity = getEntity(entityId);
    MotechDataService service = getServiceForEntity(entity);
    service.revertToHistoricalRevision(instanceId, historyId);
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) MotechDataService(org.motechproject.mds.service.MotechDataService)

Example 19 with EntityDto

use of org.motechproject.mds.dto.EntityDto 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 20 with EntityDto

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

the class InstanceServiceImpl method getHistoryRecord.

@Override
public HistoryRecord getHistoryRecord(Long entityId, Long instanceId, Long historyId) {
    EntityDto entity = getEntity(entityId);
    validateCredentialsForReading(entity);
    MotechDataService service = getServiceForEntity(entity);
    Object instance = service.retrieve(ID_FIELD_NAME, instanceId);
    Object historyInstance = historyService.getSingleHistoryInstance(instance, historyId);
    return convertToHistoryRecord(historyInstance, entity, instanceId, service);
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) MotechDataService(org.motechproject.mds.service.MotechDataService)

Aggregations

EntityDto (org.motechproject.mds.dto.EntityDto)136 Test (org.junit.Test)61 FieldDto (org.motechproject.mds.dto.FieldDto)53 ArrayList (java.util.ArrayList)34 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)32 MotechDataService (org.motechproject.mds.service.MotechDataService)26 LookupDto (org.motechproject.mds.dto.LookupDto)24 List (java.util.List)19 BasicEntityRecord (org.motechproject.mds.web.domain.BasicEntityRecord)12 Method (java.lang.reflect.Method)11 FieldTestHelper.lookupFieldDto (org.motechproject.mds.testutil.FieldTestHelper.lookupFieldDto)11 Arrays.asList (java.util.Arrays.asList)9 FieldBasicDto (org.motechproject.mds.dto.FieldBasicDto)9 EntityRecord (org.motechproject.mds.web.domain.EntityRecord)9 AdvancedSettingsDto (org.motechproject.mds.dto.AdvancedSettingsDto)7 TypeDto (org.motechproject.mds.dto.TypeDto)7 HashMap (java.util.HashMap)6 SchemaHolder (org.motechproject.mds.dto.SchemaHolder)6 BasicFieldRecord (org.motechproject.mds.web.domain.BasicFieldRecord)6 FieldRecord (org.motechproject.mds.web.domain.FieldRecord)6