Search in sources :

Example 1 with FieldNotFoundException

use of org.motechproject.mds.exception.field.FieldNotFoundException in project motech by motech.

the class LookupExecutor method buildArgTypes.

private List<Class> buildArgTypes() {
    List<Class> argTypes = new ArrayList<>();
    for (LookupFieldDto lookupField : lookup.getLookupFields()) {
        switch(lookupField.getType()) {
            case RANGE:
                argTypes.add(Range.class);
                break;
            case SET:
                argTypes.add(Set.class);
                break;
            default:
                FieldDto field = fieldsByName.get(lookupField.getLookupFieldName());
                if (field == null) {
                    throw new FieldNotFoundException(entityClass.getName(), lookupField.getName());
                }
                String typeClassName = getTypeClassName(lookupField, field);
                try {
                    ClassLoader safeClassLoader = null == classLoader ? MDSClassLoader.getInstance() : classLoader;
                    argTypes.add(safeClassLoader.loadClass(typeClassName));
                } catch (ClassNotFoundException e) {
                    throw new IllegalStateException("Type not found " + typeClassName, e);
                }
        }
    }
    return argTypes;
}
Also used : ArrayList(java.util.ArrayList) FieldNotFoundException(org.motechproject.mds.exception.field.FieldNotFoundException) MDSClassLoader(org.motechproject.mds.util.MDSClassLoader) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto)

Example 2 with FieldNotFoundException

use of org.motechproject.mds.exception.field.FieldNotFoundException in project motech by motech.

the class EntityServiceImpl method findFieldByName.

@Override
@Transactional
public FieldDto findFieldByName(Long entityId, String name) {
    Entity entity = getEntityDraft(entityId);
    Field field = entity.getField(name);
    if (field == null) {
        throw new FieldNotFoundException(entity.getClassName(), name);
    }
    return field.toDto();
}
Also used : MdsEntity(org.motechproject.mds.domain.MdsEntity) Entity(org.motechproject.mds.domain.Entity) MdsVersionedEntity(org.motechproject.mds.domain.MdsVersionedEntity) Field(org.motechproject.mds.domain.Field) FieldNotFoundException(org.motechproject.mds.exception.field.FieldNotFoundException) Transactional(org.springframework.transaction.annotation.Transactional)

Example 3 with FieldNotFoundException

use of org.motechproject.mds.exception.field.FieldNotFoundException in project motech by motech.

the class ComboboxValueServiceImpl method getAllValuesForCombobox.

@Override
@Transactional
public List<String> getAllValuesForCombobox(String entityClassName, String fieldName) {
    EntityInfo entityInfo = entityInfoReader.getEntityInfo(entityClassName);
    if (entityInfo == null) {
        throw new EntityNotFoundException(entityClassName);
    }
    FieldDto field = entityInfo.getField(fieldName).getField();
    if (field == null) {
        throw new FieldNotFoundException(entityClassName, fieldName);
    } else if (!field.getType().isCombobox()) {
        throw new IllegalArgumentException("Field " + fieldName + "in entity " + entityClassName + " is not a combobx field");
    }
    return getAllValuesForCombobox(entityInfo.getEntity(), field);
}
Also used : EntityInfo(org.motechproject.mds.entityinfo.EntityInfo) FieldNotFoundException(org.motechproject.mds.exception.field.FieldNotFoundException) EntityNotFoundException(org.motechproject.mds.exception.entity.EntityNotFoundException) FieldDto(org.motechproject.mds.dto.FieldDto) Transactional(org.springframework.transaction.annotation.Transactional)

Example 4 with FieldNotFoundException

use of org.motechproject.mds.exception.field.FieldNotFoundException 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 5 with FieldNotFoundException

use of org.motechproject.mds.exception.field.FieldNotFoundException in project motech by motech.

the class LookupExecutor method getLookupArgs.

private List<Object> getLookupArgs(Map<String, ?> paramMap) {
    List<Object> args = new ArrayList<>();
    for (LookupFieldDto lookupField : lookup.getLookupFields()) {
        FieldDto field = fieldsByName.get(lookupField.getLookupFieldName());
        if (field == null) {
            throw new FieldNotFoundException(entityClass.getName(), lookupField.getName());
        }
        Object val = paramMap.get(lookupField.getLookupFieldName());
        String typeClass = getTypeClass(field);
        String genericType = getGenericTypeClass(field);
        Object arg;
        if (lookupField.getType() == LookupFieldType.RANGE) {
            arg = TypeHelper.toRange(val, typeClass);
        } else if (lookupField.getType() == LookupFieldType.SET) {
            arg = TypeHelper.toSet(val, typeClass, classLoader);
        } else {
            arg = TypeHelper.parse(val, lookupField.isUseGenericParam() ? genericType : typeClass, classLoader);
        }
        args.add(arg);
    }
    return args;
}
Also used : ArrayList(java.util.ArrayList) FieldNotFoundException(org.motechproject.mds.exception.field.FieldNotFoundException) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto)

Aggregations

FieldNotFoundException (org.motechproject.mds.exception.field.FieldNotFoundException)8 ArrayList (java.util.ArrayList)4 Field (org.motechproject.mds.domain.Field)4 FieldDto (org.motechproject.mds.dto.FieldDto)4 Transactional (org.springframework.transaction.annotation.Transactional)4 Entity (org.motechproject.mds.domain.Entity)3 MdsEntity (org.motechproject.mds.domain.MdsEntity)3 MdsVersionedEntity (org.motechproject.mds.domain.MdsVersionedEntity)3 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Collection (java.util.Collection)1 List (java.util.List)1 Predicate (java.util.function.Predicate)1 CannotCompileException (javassist.CannotCompileException)1 EntityDto (org.motechproject.mds.dto.EntityDto)1 EntityInfo (org.motechproject.mds.entityinfo.EntityInfo)1 EntityNotFoundException (org.motechproject.mds.exception.entity.EntityNotFoundException)1 ObjectNotFoundException (org.motechproject.mds.exception.object.ObjectNotFoundException)1 ObjectReadException (org.motechproject.mds.exception.object.ObjectReadException)1 MotechDataService (org.motechproject.mds.service.MotechDataService)1