Search in sources :

Example 1 with ObjectUpdateException

use of org.motechproject.mds.exception.object.ObjectUpdateException 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 2 with ObjectUpdateException

use of org.motechproject.mds.exception.object.ObjectUpdateException in project motech by motech.

the class DefaultMotechDataService method revertFromTrash.

@Override
@Transactional
public T revertFromTrash(Long trashId) {
    validateCredentials();
    Object trashRecord = trashService.findTrashById(trashId, getClassType().getName());
    if (trashRecord == null) {
        throw new TrashInstanceNotFoundException(getClassType().getName(), trashId);
    }
    verifySchemaVersion(trashRecord, trashId, true);
    try {
        T newInstance = getClassType().newInstance();
        copyValuesFromRecord(newInstance, trashRecord);
        newInstance = create(newInstance);
        trashService.removeFromTrash(trashRecord);
        return newInstance;
    } catch (InstantiationException | IllegalAccessException e) {
        throw new ObjectUpdateException(trashRecord.getClass().getName(), trashId, e);
    }
}
Also used : TrashInstanceNotFoundException(org.motechproject.mds.exception.audit.TrashInstanceNotFoundException) ObjectUpdateException(org.motechproject.mds.exception.object.ObjectUpdateException) Transactional(org.springframework.transaction.annotation.Transactional)

Aggregations

ObjectUpdateException (org.motechproject.mds.exception.object.ObjectUpdateException)2 Transactional (org.springframework.transaction.annotation.Transactional)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 CannotCompileException (javassist.CannotCompileException)1 InstanceNotFoundException (javax.management.InstanceNotFoundException)1 EntityDto (org.motechproject.mds.dto.EntityDto)1 FieldDto (org.motechproject.mds.dto.FieldDto)1 TrashInstanceNotFoundException (org.motechproject.mds.exception.audit.TrashInstanceNotFoundException)1 EntityInstancesNonEditableException (org.motechproject.mds.exception.entity.EntityInstancesNonEditableException)1 EntityNotFoundException (org.motechproject.mds.exception.entity.EntityNotFoundException)1 FieldNotFoundException (org.motechproject.mds.exception.field.FieldNotFoundException)1 FieldReadOnlyException (org.motechproject.mds.exception.field.FieldReadOnlyException)1 LookupExecutionException (org.motechproject.mds.exception.lookup.LookupExecutionException)1 LookupExecutorException (org.motechproject.mds.exception.lookup.LookupExecutorException)1 LookupNotFoundException (org.motechproject.mds.exception.lookup.LookupNotFoundException)1 ObjectCreateException (org.motechproject.mds.exception.object.ObjectCreateException)1 ObjectNotFoundException (org.motechproject.mds.exception.object.ObjectNotFoundException)1 ObjectReadException (org.motechproject.mds.exception.object.ObjectReadException)1 SecurityException (org.motechproject.mds.exception.object.SecurityException)1 MotechDataService (org.motechproject.mds.service.MotechDataService)1