Search in sources :

Example 1 with LookupFieldType

use of org.motechproject.mds.dto.LookupFieldType 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)

Example 2 with LookupFieldType

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

the class Lookup method toDto.

public LookupDto toDto() {
    List<LookupFieldDto> lookupFields = new ArrayList<>();
    for (String lookupFieldName : getFieldsOrder()) {
        Field field = getLookupFieldByName(LookupName.getFieldName(lookupFieldName));
        LookupFieldType lookupFieldType = LookupFieldType.VALUE;
        if (isRangeParam(lookupFieldName)) {
            lookupFieldType = LookupFieldType.RANGE;
        } else if (isSetParam(lookupFieldName)) {
            lookupFieldType = LookupFieldType.SET;
        }
        String customOperator = getCustomOperators().get(lookupFieldName);
        boolean useGenericParam = toBoolean(getUseGenericParams().get(lookupFieldName));
        if (field != null) {
            LookupFieldDto lookupField = new LookupFieldDto(field.getId(), field.getName(), lookupFieldType, customOperator, useGenericParam, LookupName.getRelatedFieldName(lookupFieldName));
            lookupFields.add(lookupField);
        } else {
            throw new LookupWrongFieldNameException(String.format("Can't create LookupFieldDto. Field with given name %s does not exist in %s lookup", lookupFieldName, lookupName));
        }
    }
    return new LookupDto(id, lookupName, singleObjectReturn, exposedViaRest, lookupFields, readOnly, methodName, fieldsOrder, indexRequired);
}
Also used : LookupDto(org.motechproject.mds.dto.LookupDto) ArrayList(java.util.ArrayList) LookupFieldType(org.motechproject.mds.dto.LookupFieldType) LookupWrongFieldNameException(org.motechproject.mds.exception.lookup.LookupWrongFieldNameException) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto)

Example 3 with LookupFieldType

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

the class SwaggerGenerator method lookupParameters.

private List<Parameter> lookupParameters(Entity entity, Lookup lookup, Locale locale) {
    List<Parameter> parameters = new ArrayList<>();
    for (String lookupFieldName : lookup.getFieldsOrder()) {
        LookupFieldType lookupFieldType = lookup.getLookupFieldType(lookupFieldName);
        Field lookupField;
        if (lookupFieldName.contains(".")) {
            lookupField = getRelatedField(lookup.getLookupFieldByName(LookupName.getFieldName(lookupFieldName)).getMetadata(Constants.MetadataKeys.RELATED_CLASS).getValue(), LookupName.getRelatedFieldName(lookupFieldName));
        } else {
            lookupField = lookup.getLookupFieldByName(lookupFieldName);
        }
        String paramDesc = lookupParamDescription(lookupField, lookupFieldType, locale);
        Parameter parameter = SwaggerFieldConverter.lookupParameter(lookupFieldName, lookupField, lookupFieldType, paramDesc);
        parameters.add(parameter);
    }
    parameters.addAll(queryParamsParameters(entity.getFieldsExposedByRest(), locale));
    return parameters;
}
Also used : Field(org.motechproject.mds.domain.Field) ArrayList(java.util.ArrayList) Parameter(org.motechproject.mds.docs.swagger.model.Parameter) LookupFieldType(org.motechproject.mds.dto.LookupFieldType)

Aggregations

ArrayList (java.util.ArrayList)3 LookupFieldType (org.motechproject.mds.dto.LookupFieldType)3 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)2 Annotation (java.lang.annotation.Annotation)1 LookupField (org.motechproject.mds.annotations.LookupField)1 Parameter (org.motechproject.mds.docs.swagger.model.Parameter)1 Field (org.motechproject.mds.domain.Field)1 LookupDto (org.motechproject.mds.dto.LookupDto)1 LookupWrongFieldNameException (org.motechproject.mds.exception.lookup.LookupWrongFieldNameException)1