use of org.motechproject.mds.dto.LookupFieldDto in project motech by motech.
the class LookupExecutor method buildArgTypes.
private List<Class> buildArgTypes() {
List<Class> argTypes = new ArrayList<>();
for (LookupFieldDto lookupField : lookup.getLookupFields()) {
switch(lookupField.getType()) {
case RANGE:
argTypes.add(Range.class);
break;
case SET:
argTypes.add(Set.class);
break;
default:
FieldDto field = fieldsByName.get(lookupField.getLookupFieldName());
if (field == null) {
throw new FieldNotFoundException(entityClass.getName(), lookupField.getName());
}
String typeClassName = getTypeClassName(lookupField, field);
try {
ClassLoader safeClassLoader = null == classLoader ? MDSClassLoader.getInstance() : classLoader;
argTypes.add(safeClassLoader.loadClass(typeClassName));
} catch (ClassNotFoundException e) {
throw new IllegalStateException("Type not found " + typeClassName, e);
}
}
}
return argTypes;
}
use of org.motechproject.mds.dto.LookupFieldDto in project motech by motech.
the class LookupBuilder method build.
private CtMethod build(boolean body) throws CannotCompileException, NotFoundException {
Collection<String> paramCollection = new ArrayList<>();
List<String> fieldOrder = lookup.getFieldsOrder();
for (int i = 0; i < fieldOrder.size(); i++) {
String fieldName = fieldOrder.get(i);
FieldDto field = getLookupField(fieldName);
// don't use fieldName for fetching fields, as it can contain dots, etc.
LookupFieldDto lookupField = lookup.getLookupField(field.getBasic().getName());
FieldDto relationField = null;
EntityDto relatedEntity = null;
if (fieldOrder.get(i).contains(".")) {
relatedEntity = schemaHolder.getEntityByClassName(new RelationshipHolder(field).getRelatedClass());
relationField = schemaHolder.getFieldByName(relatedEntity, LookupName.getRelatedFieldName(fieldOrder.get(i)));
}
String type = getTypeForParam(i, resolveEntity(entity, relatedEntity), resolveField(field, relationField), lookupField);
String param = String.format("%s %s", type, fieldOrder.get(i).replace(".", ""));
paramCollection.add(param);
}
// query params at the end for ordering/paging
if (WITH_QUERY_PARAMS == lookupType) {
String queryParam = String.format("%s queryParams", QueryParams.class.getName());
paramCollection.add(queryParam);
}
String params = StringUtils.join(paramCollection, ", ");
String signature = String.format("public %s %s(%s)", returnType(), lookupName, params);
String methodAsString = body ? String.format("%s{%s}", signature, body()) : String.format("%s;", signature);
String generic = buildGenericSignature();
CtMethod method = CtNewMethod.make(methodAsString, definition);
method.setGenericSignature(generic);
return method;
}
use of org.motechproject.mds.dto.LookupFieldDto in project motech by motech.
the class LookupBuilder method buildGenericSignature.
private String buildGenericSignature() throws NotFoundException {
// we must build generic signatures for lookup methods
// an example signature for the method signature
// List<org.motechproject.mds.Test> method(String p1, Integer p2)
// is
// cmt -- (Ljava/lang/String;Ljava/lang/Integer;)Ljava/util/List<Lorg/motechproject/mds/Test;>;
StringBuilder sb = new StringBuilder();
sb.append('(');
List<String> fieldsOrder = lookup.getFieldsOrder();
for (int i = 0; i < fieldsOrder.size(); ++i) {
String fieldName = fieldsOrder.get(i);
FieldDto field = getLookupField(fieldName);
// don't use fieldName for fetching fields, as it can contain dots, etc.
LookupFieldDto lookupField = lookup.getLookupField(field.getBasic().getName());
FieldDto relationField = null;
EntityDto relatedEntity = null;
if (fieldsOrder.get(i).contains(".")) {
relatedEntity = schemaHolder.getEntityByClassName(new RelationshipHolder(field).getRelatedClass());
relationField = schemaHolder.getFieldByName(relatedEntity, LookupName.getRelatedFieldName(fieldsOrder.get(i)));
}
String paramType = getTypeForParam(i, resolveEntity(entity, relatedEntity), resolveField(field, relationField), lookupField);
String genericType;
TypeDto type = resolveField(field, relationField).getType();
if (type.isCombobox()) {
ComboboxHolder holder = new ComboboxHolder(resolveEntity(entity, relatedEntity), resolveField(field, relationField));
genericType = holder.getUnderlyingType();
} else {
genericType = type.getTypeClass();
}
if (StringUtils.equals(paramType, genericType) || TypeHelper.isPrimitive(paramType)) {
// simple parameter
sb.append(JavassistUtil.toGenericParam(paramType));
} else {
// we wrap in a range/set or a different wrapper
sb.append(JavassistUtil.genericSignature(paramType, genericType));
}
}
sb.append(')');
if (lookup.isSingleObjectReturn()) {
sb.append(JavassistUtil.toGenericParam(className));
} else {
sb.append(JavassistUtil.genericSignature(List.class.getName(), className));
}
return sb.toString();
}
use of org.motechproject.mds.dto.LookupFieldDto 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.LookupFieldDto in project motech by motech.
the class Lookup method updateLookupFields.
private void updateLookupFields(LookupDto lookupDto) {
getRangeLookupFields().clear();
getSetLookupFields().clear();
for (LookupFieldDto lookupFieldDto : lookupDto.getLookupFields()) {
String name = lookupFieldDto.getLookupFieldName();
if (lookupFieldDto.getType() == LookupFieldType.RANGE) {
getRangeLookupFields().add(name);
} else if (lookupFieldDto.getType() == LookupFieldType.SET) {
getSetLookupFields().add(name);
}
}
}
Aggregations