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;
}
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);
}
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;
}
Aggregations