Search in sources :

Example 6 with LookupDto

use of org.motechproject.mds.dto.LookupDto 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);
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) LookupDto(org.motechproject.mds.dto.LookupDto) Collection(java.util.Collection) Lookup(org.motechproject.mds.annotations.Lookup) Method(java.lang.reflect.Method) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto)

Example 7 with LookupDto

use of org.motechproject.mds.dto.LookupDto in project motech by motech.

the class EntityMetadataBuilderImpl method processField.

public void processField(ClassMetadata cmd, ClassData classData, EntityDto entity, EntityType entityType, Class<?> definition, String fieldName, FieldDto field, SchemaHolder schemaHolder) {
    // Metadata for ID field has been added earlier in addIdField() method
    if (!fieldName.equals(ID_FIELD_NAME)) {
        FieldMetadata fmd = null;
        if (isFieldNotInherited(fieldName, entity, schemaHolder)) {
            fmd = setFieldMetadata(cmd, classData, entity, entityType, field, definition);
        }
        // when field is in Lookup, we set field metadata indexed to retrieve instance faster
        if (!field.getLookups().isEmpty() && entityType.equals(EntityType.STANDARD)) {
            if (fmd == null) {
                String inheritedFieldName = ClassName.getSimpleName(entity.getSuperClass()) + "." + fieldName;
                fmd = cmd.newFieldMetadata(inheritedFieldName);
            }
            if (!isBlobOrClob(field)) {
                IndexMetadata imd = getOrCreateIndexMetadata(fmd);
                imd.setName(KeyNames.lookupIndexKeyName(entity.getName(), entity.getId(), fieldName, entityType));
            }
            fmd.setIndexed(false);
            for (LookupDto lookupDto : field.getLookups()) {
                if (lookupDto.isIndexRequired()) {
                    fmd.setIndexed(true);
                    break;
                }
            }
        }
        if (fmd != null) {
            customizeFieldMd(fmd, entity, field, entityType, definition);
        }
    }
}
Also used : FieldMetadata(javax.jdo.metadata.FieldMetadata) LookupDto(org.motechproject.mds.dto.LookupDto) IndexMetadata(javax.jdo.metadata.IndexMetadata)

Example 8 with LookupDto

use of org.motechproject.mds.dto.LookupDto in project motech by motech.

the class EntityInfrastructureBuilderImpl method getServiceCode.

private byte[] getServiceCode(String serviceClassName, String interfaceClassName, String className, EntityDto entity, SchemaHolder schemaHolder) {
    try {
        CtClass superClass = classPool.getCtClass(TransactionalMotechDataService.class.getName());
        superClass.setGenericSignature(getGenericSignature(className));
        CtClass serviceInterface = classPool.getCtClass(interfaceClassName);
        CtClass serviceClass = createOrRetrieveClass(serviceClassName, superClass);
        // add the interface if its not already there
        if (!JavassistUtil.hasInterface(serviceClass, serviceInterface)) {
            serviceClass.addInterface(serviceInterface);
        }
        List<CtMethod> methods = new ArrayList<>();
        // a count method for the lookup
        if (null != entity) {
            List<LookupDto> lookups = schemaHolder.getLookups(entity);
            for (LookupDto lookup : lookups) {
                for (LookupType lookupType : LookupType.values()) {
                    LookupBuilder lookupBuilder = new LookupBuilder(entity, lookup, serviceClass, lookupType, schemaHolder);
                    methods.add(lookupBuilder.buildMethod());
                }
            }
        }
        // clear lookup methods before adding the new ones
        removeExistingMethods(serviceClass);
        for (CtMethod method : methods) {
            serviceClass.addMethod(method);
        }
        return serviceClass.toBytecode();
    } catch (NotFoundException | IOException | CannotCompileException e) {
        throw new EntityInfrastructureException(serviceClassName, e);
    }
}
Also used : EntityInfrastructureException(org.motechproject.mds.exception.entity.EntityInfrastructureException) LookupDto(org.motechproject.mds.dto.LookupDto) ArrayList(java.util.ArrayList) NotFoundException(javassist.NotFoundException) IOException(java.io.IOException) CannotCompileException(javassist.CannotCompileException) CtClass(javassist.CtClass) CtMethod(javassist.CtMethod) TransactionalMotechDataService(org.motechproject.mds.service.TransactionalMotechDataService)

Example 9 with LookupDto

use of org.motechproject.mds.dto.LookupDto 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 LookupDto

use of org.motechproject.mds.dto.LookupDto in project motech by motech.

the class Field method toDto.

public FieldDto toDto() {
    FieldBasicDto basic = new FieldBasicDto(displayName, name, required, unique, parseDefaultValue(), tooltip, placeholder);
    TypeDto typeDto = null;
    List<MetadataDto> metaDto = new ArrayList<>();
    for (FieldMetadata meta : metadata) {
        metaDto.add(meta.toDto());
    }
    FieldValidationDto validationDto = null;
    if (CollectionUtils.isNotEmpty(validations)) {
        validationDto = new FieldValidationDto();
        for (FieldValidation validation : validations) {
            validationDto.addCriterion(validation.toDto());
        }
    }
    List<SettingDto> settingsDto = new ArrayList<>();
    for (FieldSetting setting : settings) {
        settingsDto.add(setting.toDto());
    }
    List<LookupDto> lookupDtos = new ArrayList<>();
    for (Lookup lookup : getLookups()) {
        lookupDtos.add(lookup.toDto());
    }
    if (typeDto == null && type != null) {
        typeDto = type.toDto();
    }
    return new FieldDto(id, entity == null ? null : entity.getId(), typeDto, basic, readOnly, nonEditable, nonDisplayable, uiFilterable, uiChanged, metaDto, validationDto, settingsDto, lookupDtos);
}
Also used : LookupDto(org.motechproject.mds.dto.LookupDto) ArrayList(java.util.ArrayList) MetadataDto(org.motechproject.mds.dto.MetadataDto) FieldBasicDto(org.motechproject.mds.dto.FieldBasicDto) FieldValidationDto(org.motechproject.mds.dto.FieldValidationDto) TypeDto(org.motechproject.mds.dto.TypeDto) SettingDto(org.motechproject.mds.dto.SettingDto) FieldDto(org.motechproject.mds.dto.FieldDto)

Aggregations

LookupDto (org.motechproject.mds.dto.LookupDto)54 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)32 FieldDto (org.motechproject.mds.dto.FieldDto)29 ArrayList (java.util.ArrayList)26 EntityDto (org.motechproject.mds.dto.EntityDto)24 Test (org.junit.Test)23 List (java.util.List)10 Method (java.lang.reflect.Method)9 Arrays.asList (java.util.Arrays.asList)8 FieldTestHelper.lookupFieldDto (org.motechproject.mds.testutil.FieldTestHelper.lookupFieldDto)7 HashMap (java.util.HashMap)6 Field (org.motechproject.mds.domain.Field)5 Lookup (org.motechproject.mds.domain.Lookup)5 AdvancedSettingsDto (org.motechproject.mds.dto.AdvancedSettingsDto)5 FieldBasicDto (org.motechproject.mds.dto.FieldBasicDto)5 Before (org.junit.Before)4 Matchers.anyString (org.mockito.Matchers.anyString)4 RestOptionsDto (org.motechproject.mds.dto.RestOptionsDto)4 LookupExecutor (org.motechproject.mds.lookup.LookupExecutor)4 MetadataDto (org.motechproject.mds.dto.MetadataDto)3