Search in sources :

Example 26 with FieldDto

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

Example 27 with FieldDto

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;
}
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 28 with FieldDto

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

Example 29 with FieldDto

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;
}
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 30 with FieldDto

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

Aggregations

FieldDto (org.motechproject.mds.dto.FieldDto)158 Test (org.junit.Test)61 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)60 EntityDto (org.motechproject.mds.dto.EntityDto)53 ArrayList (java.util.ArrayList)47 LookupDto (org.motechproject.mds.dto.LookupDto)29 List (java.util.List)23 MetadataDto (org.motechproject.mds.dto.MetadataDto)22 FieldBasicDto (org.motechproject.mds.dto.FieldBasicDto)19 TypeDto (org.motechproject.mds.dto.TypeDto)17 Method (java.lang.reflect.Method)14 MotechDataService (org.motechproject.mds.service.MotechDataService)14 HashMap (java.util.HashMap)12 Field (org.motechproject.mds.domain.Field)12 FieldTestHelper.lookupFieldDto (org.motechproject.mds.testutil.FieldTestHelper.lookupFieldDto)12 Arrays.asList (java.util.Arrays.asList)11 SettingDto (org.motechproject.mds.dto.SettingDto)11 Before (org.junit.Before)7 AdvancedSettingsDto (org.motechproject.mds.dto.AdvancedSettingsDto)7 BasicEntityRecord (org.motechproject.mds.web.domain.BasicEntityRecord)7