Search in sources :

Example 1 with LookupField

use of org.motechproject.mds.annotations.LookupField in project motech by motech.

the class LookupProcessor method findParametersNames.

private List<String> findParametersNames(Method method) {
    Annotation[][] paramAnnotations = method.getParameterAnnotations();
    List<String> methodParameterNames = new ArrayList<>();
    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;
                String name = isBlank(fieldAnnotation.name()) ? methodParameterNames.get(i) : fieldAnnotation.name();
                if (i >= methodParameterNames.size()) {
                    methodParameterNames.add(name);
                } else {
                    methodParameterNames.set(i, name);
                }
            }
        }
    }
    return methodParameterNames;
}
Also used : ArrayList(java.util.ArrayList) LookupField(org.motechproject.mds.annotations.LookupField) Annotation(java.lang.annotation.Annotation)

Example 2 with LookupField

use of org.motechproject.mds.annotations.LookupField 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

Annotation (java.lang.annotation.Annotation)2 ArrayList (java.util.ArrayList)2 LookupField (org.motechproject.mds.annotations.LookupField)2 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)1 LookupFieldType (org.motechproject.mds.dto.LookupFieldType)1