Search in sources :

Example 1 with ObjectNotFoundException

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

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

the class InstanceServiceImpl method getRelatedFieldValue.

@Override
public Records<BasicEntityRecord> getRelatedFieldValue(Long entityId, Long instanceId, String fieldName, RelationshipsUpdate filter, QueryParams queryParams) {
    try {
        // first get the entity
        EntityDto entity = getEntity(entityId);
        validateCredentials(entity);
        List<FieldDto> fields = getEntityFields(entityId);
        String entityName = entity.getName();
        MotechDataService service = getServiceForEntity(entity);
        // then the related entity
        FieldDto relatedField = findFieldByName(fields, fieldName);
        if (relatedField == null) {
            throw new FieldNotFoundException(entity.getClassName(), fieldName);
        }
        String relatedClass = relatedField.getMetadataValue(Constants.MetadataKeys.RELATED_CLASS);
        if (StringUtils.isBlank(relatedClass)) {
            throw new IllegalArgumentException("Field " + fieldName + " in entity " + entity.getClassName() + " is not a related field");
        }
        // these will be used for building the records
        EntityDto relatedEntity = getEntity(relatedClass);
        List<FieldDto> relatedFields = getEntityFields(relatedEntity.getId());
        MotechDataService relatedDataService = getServiceForEntity(relatedEntity);
        Collection relatedAsColl = new ArrayList<>();
        // If the relationship already exists, fetch instances and use correct type
        if (instanceId != null) {
            // get the instance of the original entity
            Object instance = service.findById(instanceId);
            if (instance == null) {
                throw new ObjectNotFoundException(entityName, instanceId);
            }
            // the value of the related field
            relatedAsColl = TypeHelper.asCollection(PropertyUtil.getProperty(instance, fieldName));
        }
        relatedAsColl.addAll(relatedDataService.findByIds(filter.getAddedIds()));
        List<Long> updatedInstancesIds = new ArrayList<>();
        for (EntityRecord record : filter.getAddedNewRecords()) {
            Integer id = (Integer) record.getFieldByName(Constants.Util.ID_FIELD_NAME).getValue();
            if (id != null && id > 0) {
                updatedInstancesIds.add(id.longValue());
            }
        }
        relatedAsColl.removeIf(new Predicate() {

            @Override
            public boolean test(Object o) {
                Long objectId = (Long) PropertyUtil.safeGetProperty(o, Constants.Util.ID_FIELD_NAME);
                return filter.getRemovedIds().contains(objectId) || updatedInstancesIds.contains(objectId);
            }
        });
        for (EntityRecord record : filter.getAddedNewRecords()) {
            relatedAsColl.add(newInstanceFromEntityRecord(getEntityClass(relatedEntity), relatedFields, record.getFields(), relatedDataService));
        }
        // apply pagination ordering (currently in memory)
        List filtered = InMemoryQueryFilter.filter(relatedAsColl, queryParams);
        // convert the instance to a grid-friendly form
        List<BasicEntityRecord> entityRecords = instancesToBasicRecords(filtered, relatedEntity, relatedFields, relatedDataService, EntityType.STANDARD);
        // counts for the grid
        int recordCount = relatedAsColl.size();
        int rowCount = (int) Math.ceil(recordCount / (double) queryParams.getPageSize());
        // package as records
        return new Records<>(queryParams.getPage(), rowCount, recordCount, entityRecords);
    } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException | IllegalArgumentException | ClassNotFoundException | CannotCompileException | InstantiationException | NoSuchFieldException e) {
        throw new ObjectReadException(entityId, e);
    }
}
Also used : ArrayList(java.util.ArrayList) BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) CannotCompileException(javassist.CannotCompileException) MotechDataService(org.motechproject.mds.service.MotechDataService) Predicate(java.util.function.Predicate) EntityDto(org.motechproject.mds.dto.EntityDto) ArrayList(java.util.ArrayList) List(java.util.List) Records(org.motechproject.mds.web.domain.Records) FieldDto(org.motechproject.mds.dto.FieldDto) FieldNotFoundException(org.motechproject.mds.exception.field.FieldNotFoundException) ObjectReadException(org.motechproject.mds.exception.object.ObjectReadException) InvocationTargetException(java.lang.reflect.InvocationTargetException) EntityRecord(org.motechproject.mds.web.domain.EntityRecord) BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) ObjectNotFoundException(org.motechproject.mds.exception.object.ObjectNotFoundException) Collection(java.util.Collection)

Example 3 with ObjectNotFoundException

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

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

FieldDto (org.motechproject.mds.dto.FieldDto)4 ObjectNotFoundException (org.motechproject.mds.exception.object.ObjectNotFoundException)4 MotechDataService (org.motechproject.mds.service.MotechDataService)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 EntityDto (org.motechproject.mds.dto.EntityDto)3 ObjectReadException (org.motechproject.mds.exception.object.ObjectReadException)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 CannotCompileException (javassist.CannotCompileException)2 FieldNotFoundException (org.motechproject.mds.exception.field.FieldNotFoundException)2 Collection (java.util.Collection)1 Predicate (java.util.function.Predicate)1 InstanceNotFoundException (javax.management.InstanceNotFoundException)1 EntityInstancesNonEditableException (org.motechproject.mds.exception.entity.EntityInstancesNonEditableException)1 EntityNotFoundException (org.motechproject.mds.exception.entity.EntityNotFoundException)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