Search in sources :

Example 6 with LookupFieldDto

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;
}
Also used : ArrayList(java.util.ArrayList) FieldNotFoundException(org.motechproject.mds.exception.field.FieldNotFoundException) MDSClassLoader(org.motechproject.mds.util.MDSClassLoader) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto)

Example 7 with LookupFieldDto

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;
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) RelationshipHolder(org.motechproject.mds.domain.RelationshipHolder) ArrayList(java.util.ArrayList) QueryParams(org.motechproject.mds.query.QueryParams) CtMethod(javassist.CtMethod) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto)

Example 8 with LookupFieldDto

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();
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) RelationshipHolder(org.motechproject.mds.domain.RelationshipHolder) ComboboxHolder(org.motechproject.mds.domain.ComboboxHolder) TypeDto(org.motechproject.mds.dto.TypeDto) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto)

Example 9 with LookupFieldDto

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);
}
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 10 with LookupFieldDto

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);
        }
    }
}
Also used : LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto)

Aggregations

LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)36 LookupDto (org.motechproject.mds.dto.LookupDto)25 FieldDto (org.motechproject.mds.dto.FieldDto)24 EntityDto (org.motechproject.mds.dto.EntityDto)18 ArrayList (java.util.ArrayList)16 Test (org.junit.Test)13 Method (java.lang.reflect.Method)5 TypeDto (org.motechproject.mds.dto.TypeDto)5 Arrays.asList (java.util.Arrays.asList)4 HashMap (java.util.HashMap)4 List (java.util.List)4 RelationshipHolder (org.motechproject.mds.domain.RelationshipHolder)4 FieldBasicDto (org.motechproject.mds.dto.FieldBasicDto)4 FieldTestHelper.lookupFieldDto (org.motechproject.mds.testutil.FieldTestHelper.lookupFieldDto)4 Before (org.junit.Before)3 ComboboxHolder (org.motechproject.mds.domain.ComboboxHolder)3 LinkedList (java.util.LinkedList)2 Field (org.motechproject.mds.domain.Field)2 AdvancedSettingsDto (org.motechproject.mds.dto.AdvancedSettingsDto)2 LookupFieldType (org.motechproject.mds.dto.LookupFieldType)2