use of org.motechproject.mds.dto.FieldDto 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.FieldDto 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.FieldDto in project motech by motech.
the class EntityDefaultFieldsHelper method defaultFields.
public static List<FieldDto> defaultFields(SchemaHolder schemaHolder) {
TypeDto longType = schemaHolder.getType(Long.class);
TypeDto stringType = schemaHolder.getType(String.class);
TypeDto dateTimeType = schemaHolder.getType(DateTime.class);
FieldDto idField = new FieldDto(Constants.Util.ID_FIELD_NAME, Constants.Util.ID_DISPLAY_FIELD_NAME, longType, true, true);
idField.setReadOnly(true);
idField.setMetadata(Collections.singletonList(new MetadataDto(AUTO_GENERATED, TRUE)));
FieldDto creatorField = new FieldDto(Constants.Util.CREATOR_FIELD_NAME, Constants.Util.CREATOR_DISPLAY_FIELD_NAME, stringType, true, false);
creatorField.setReadOnly(true);
creatorField.addMetadata(new MetadataDto(AUTO_GENERATED, TRUE));
FieldDto ownerField = new FieldDto(Constants.Util.OWNER_FIELD_NAME, Constants.Util.OWNER_DISPLAY_FIELD_NAME, stringType, false, false);
ownerField.setReadOnly(true);
ownerField.addMetadata(new MetadataDto(AUTO_GENERATED_EDITABLE, TRUE));
FieldDto modifiedByField = new FieldDto(Constants.Util.MODIFIED_BY_FIELD_NAME, Constants.Util.MODIFIED_BY_DISPLAY_FIELD_NAME, stringType, true, false);
modifiedByField.setReadOnly(true);
modifiedByField.addMetadata(new MetadataDto(AUTO_GENERATED, TRUE));
FieldDto modificationDateField = new FieldDto(Constants.Util.MODIFICATION_DATE_FIELD_NAME, Constants.Util.MODIFICATION_DATE_DISPLAY_FIELD_NAME, dateTimeType, true, false);
modificationDateField.setReadOnly(true);
modificationDateField.addMetadata(new MetadataDto(AUTO_GENERATED, TRUE));
FieldDto creationDateField = new FieldDto(Constants.Util.CREATION_DATE_FIELD_NAME, Constants.Util.CREATION_DATE_DISPLAY_FIELD_NAME, dateTimeType, true, false);
creationDateField.setReadOnly(true);
creationDateField.addMetadata(new MetadataDto(AUTO_GENERATED, TRUE));
return Arrays.asList(idField, creatorField, ownerField, modifiedByField, modificationDateField, creationDateField);
}
use of org.motechproject.mds.dto.FieldDto 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.FieldDto 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();
}
Aggregations