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