Search in sources :

Example 1 with LookupFieldDto

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

the class MdsDummyDataGeneratorImpl method prepareDummyEntity.

private void prepareDummyEntity(int number, int fieldsPerEntity, int lookupsPerEntity) throws IOException {
    EntityDto entityDto = new EntityDto(Long.valueOf(number), entityPrefix.concat(String.valueOf(number)));
    entityDto = entityService.createEntity(entityDto);
    List<FieldDto> fields = new ArrayList<>();
    for (int i = 0; i < fieldsPerEntity; i++) {
        TypeDto type = pickRandomFieldType();
        fields.add(new FieldDto(null, entityDto.getId(), type, new FieldBasicDto(fieldPrefix.concat(String.valueOf(i)), fieldPrefix.concat(String.valueOf(i))), false, null, null, settingsFor(type), null));
    }
    entityService.addFields(entityDto, fields);
    List<LookupDto> lookups = new ArrayList<>();
    for (int i = 0; i < lookupsPerEntity; i++) {
        List<LookupFieldDto> lookupFields = new ArrayList<>();
        List<FieldDto> entityFields = entityService.getFields(entityDto.getId());
        int amountOfFields = RAND.nextInt(entityFields.size());
        for (int j = 0; j < amountOfFields; j++) {
            lookupFields.add(new LookupFieldDto(null, entityFields.get(j).getBasic().getName(), LookupFieldType.VALUE));
        }
        lookups.add(new LookupDto(lookupPrefix.concat(String.valueOf(i)), RAND.nextBoolean(), RAND.nextBoolean(), lookupFields, false));
    }
    entityService.addLookups(entityDto.getId(), lookups);
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) FieldBasicDto(org.motechproject.mds.dto.FieldBasicDto) LookupDto(org.motechproject.mds.dto.LookupDto) ArrayList(java.util.ArrayList) TypeDto(org.motechproject.mds.dto.TypeDto) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto)

Example 2 with LookupFieldDto

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

the class LookupTestHelper method lookupFieldsFromNames.

public static List<LookupFieldDto> lookupFieldsFromNames(Collection<String> names) {
    List<LookupFieldDto> lookupFieldDtos = new ArrayList<>();
    for (String name : names) {
        LookupFieldDto lookupFieldDto = new LookupFieldDto(null, LookupName.getFieldName(name), LookupFieldType.VALUE);
        lookupFieldDto.setRelatedName(LookupName.getRelatedFieldName(name));
        lookupFieldDtos.add(lookupFieldDto);
    }
    return lookupFieldDtos;
}
Also used : ArrayList(java.util.ArrayList) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto)

Example 3 with LookupFieldDto

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

the class LookupProcessor method process.

@Override
protected void process(AnnotatedElement annotatedElement) {
    Method method = (Method) annotatedElement;
    Class returnType = method.getReturnType();
    String returnClassName = returnType.getName();
    boolean singleObjectReturn = true;
    if (returnType.isArray() || Collection.class.isAssignableFrom(returnType)) {
        singleObjectReturn = false;
        returnClassName = determineGenericClass(method.getGenericReturnType().toString());
    }
    EntityDto entity = findEntityByClassName(returnClassName);
    if (entity == null) {
        LOGGER.error("There's no matching entity for the resolved return type of the lookup" + "method: {}; Resolved return type: {}", method.getName(), returnClassName);
        return;
    }
    LOGGER.debug("Found entity class by the return type of lookup method: {}", entity.getName());
    Lookup annotation = ReflectionsUtil.findAnnotation(method, Lookup.class);
    String lookupName = generateLookupName(annotation.name(), method.getName());
    List<LookupFieldDto> lookupFields = findLookupFields(method, entity);
    boolean restExposed = processRestExposed(method);
    boolean indexRequired = annotation.indexRequired();
    verifyLookupParameters(method, returnClassName, lookupName, lookupFields, method.getParameterTypes());
    LookupDto lookup = new LookupDto();
    lookup.setSingleObjectReturn(singleObjectReturn);
    lookup.setLookupName(lookupName);
    lookup.setLookupFields(lookupFields);
    lookup.setReadOnly(true);
    lookup.setMethodName(method.getName());
    lookup.setIndexRequired(indexRequired);
    if (!restOptionsModifiedByUser(entity)) {
        lookup.setExposedViaRest(restExposed);
    }
    if (!getElements().containsKey(returnClassName)) {
        put(returnClassName, new ArrayList<>());
    }
    getElement(returnClassName).add(lookup);
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) LookupDto(org.motechproject.mds.dto.LookupDto) Collection(java.util.Collection) Lookup(org.motechproject.mds.annotations.Lookup) Method(java.lang.reflect.Method) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto)

Example 4 with LookupFieldDto

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

the class LookupProcessor method verifyLookupParameters.

private void verifyLookupParameters(Method method, String entityClassName, String lookupName, List<LookupFieldDto> lookupFields, Class<?>[] parameterTypes) {
    List<String> parametersNames = findParametersNames(method);
    for (LookupFieldDto lookupFieldDto : lookupFields) {
        if (lookupFieldDto.getType() == LookupFieldType.VALUE) {
            FieldDto fieldDto = findEntityFieldByName(entityClassName, lookupFieldDto.getLookupFieldName());
            int position = parametersNames.indexOf(lookupFieldDto.getLookupFieldName());
            if (fieldDto != null && fieldDto.getType() != null) {
                TypeDto type = fieldDto.getType();
                // check if field is a Combobox or a TextArea
                if (type.isCombobox() || (type.isTextArea() && "java.lang.String".equals(parameterTypes[position].getName()))) {
                    continue;
                }
                if (!parameterTypes[position].getName().equals(type.getTypeClass())) {
                    StringBuilder sb = new StringBuilder("Wrong type of argument ");
                    sb.append(position).append(" \"").append(parametersNames.get(position));
                    sb.append("\" in lookup \"").append(lookupName);
                    sb.append("\" - should be ").append(type.getTypeClass());
                    sb.append(" but is ").append(parameterTypes[position].getName());
                    throw new LookupWrongParameterTypeException(sb.toString());
                }
            }
        }
    }
}
Also used : LookupWrongParameterTypeException(org.motechproject.mds.exception.lookup.LookupWrongParameterTypeException) TypeDto(org.motechproject.mds.dto.TypeDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto)

Example 5 with LookupFieldDto

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

the class LookupProcessor method findLookupFields.

private List<LookupFieldDto> findLookupFields(Method method, EntityDto entity) {
    Annotation[][] paramAnnotations = method.getParameterAnnotations();
    List<LookupFieldDto> lookupFields = new ArrayList<>();
    List<String> methodParameterNames = new ArrayList<>();
    List<Class<?>> methodParameterTypes = new ArrayList<>();
    methodParameterTypes.addAll(Arrays.asList(method.getParameterTypes()));
    try {
        methodParameterNames.addAll(Arrays.asList(paranamer.lookupParameterNames(method)));
    } catch (RuntimeException e) {
        logParanamerError(method.toString(), e);
    }
    for (int i = 0; i < paramAnnotations.length; i++) {
        for (Annotation annotation : paramAnnotations[i]) {
            if (annotation.annotationType().equals(LookupField.class)) {
                LookupField fieldAnnotation = (LookupField) annotation;
                Class<?> methodParameterType = methodParameterTypes.get(i);
                // no name defined in annotation - get lookup field name from parameter name
                // name defined in annotation - get lookup field name from annotation
                String name = isBlank(fieldAnnotation.name()) ? methodParameterNames.get(i) : fieldAnnotation.name();
                LookupFieldType type = determineLookupType(methodParameterType);
                LookupFieldDto lookupField = new LookupFieldDto(null, LookupName.getFieldName(name), type);
                lookupField.setRelatedName(LookupName.getRelatedFieldName(name));
                setCustomOperator(fieldAnnotation, lookupField);
                setUseGenericParam(entity, methodParameterType, lookupField);
                lookupFields.add(lookupField);
                break;
            }
        }
    }
    // No LookupFields annotation? Then add all the fields.
    if (lookupFields.isEmpty()) {
        for (int i = 0; i < methodParameterNames.size(); i++) {
            String name = methodParameterNames.get(i);
            Class<?> type = methodParameterTypes.get(i);
            lookupFields.add(new LookupFieldDto(null, name, determineLookupType(type)));
        }
    }
    return lookupFields;
}
Also used : ArrayList(java.util.ArrayList) LookupField(org.motechproject.mds.annotations.LookupField) Annotation(java.lang.annotation.Annotation) LookupFieldType(org.motechproject.mds.dto.LookupFieldType) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto)

Aggregations

LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)36 LookupDto (org.motechproject.mds.dto.LookupDto)25 FieldDto (org.motechproject.mds.dto.FieldDto)24 EntityDto (org.motechproject.mds.dto.EntityDto)18 ArrayList (java.util.ArrayList)16 Test (org.junit.Test)13 Method (java.lang.reflect.Method)5 TypeDto (org.motechproject.mds.dto.TypeDto)5 Arrays.asList (java.util.Arrays.asList)4 HashMap (java.util.HashMap)4 List (java.util.List)4 RelationshipHolder (org.motechproject.mds.domain.RelationshipHolder)4 FieldBasicDto (org.motechproject.mds.dto.FieldBasicDto)4 FieldTestHelper.lookupFieldDto (org.motechproject.mds.testutil.FieldTestHelper.lookupFieldDto)4 Before (org.junit.Before)3 ComboboxHolder (org.motechproject.mds.domain.ComboboxHolder)3 LinkedList (java.util.LinkedList)2 Field (org.motechproject.mds.domain.Field)2 AdvancedSettingsDto (org.motechproject.mds.dto.AdvancedSettingsDto)2 LookupFieldType (org.motechproject.mds.dto.LookupFieldType)2