Search in sources :

Example 26 with TypeDto

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

the class EntityBuilderImpl method build.

private ClassData build(EntityDto entity, List<FieldDto> fields, EntityType type, Bundle bundle) {
    try {
        CtClass declaring = makeClass(entity, fields, type, bundle);
        switch(type) {
            case HISTORY:
                String className = type.getClassName(entity.getClassName());
                String simpleName = ClassName.getSimpleName(className);
                TypeDto idType = TypeDto.LONG;
                // add 4 extra fields to history class definition
                // this field is related with id field in entity
                addProperty(declaring, idType.getTypeClass(), simpleName + Constants.Util.CURRENT_VERSION, null);
                // this field contains information about the schema version of an entity
                addProperty(declaring, Long.class.getName(), simpleName + StringUtils.capitalize(Constants.Util.SCHEMA_VERSION_FIELD_NAME), null);
                break;
            case TRASH:
                // this field contains information about the schema version of an entity
                addProperty(declaring, Long.class.getName(), Constants.Util.SCHEMA_VERSION_FIELD_NAME, null);
                break;
            default:
        }
        return new ClassData(declaring.getName(), entity.getModule(), entity.getNamespace(), declaring.toBytecode(), type);
    } catch (ReflectiveOperationException | CannotCompileException | IOException | NotFoundException e) {
        throw new EntityCreationException("Unable to create entity " + entity.getName(), e);
    }
}
Also used : CtClass(javassist.CtClass) ClassData(org.motechproject.mds.domain.ClassData) NotFoundException(javassist.NotFoundException) TypeDto(org.motechproject.mds.dto.TypeDto) CannotCompileException(javassist.CannotCompileException) IOException(java.io.IOException) EntityCreationException(org.motechproject.mds.exception.entity.EntityCreationException)

Example 27 with TypeDto

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

the class LookupBuilder method body.

private String body() {
    StringBuilder body = new StringBuilder();
    body.append("java.util.List properties = new java.util.ArrayList();");
    Map<String, String> jdoQueryVariables = new HashMap<>();
    for (String lookupFieldName : lookup.getFieldsOrder()) {
        boolean appendJdoVariableName = false;
        FieldDto field = getLookupField(lookupFieldName);
        // don't use lookupFieldName for fetching fields, as it can contain dots, etc.
        LookupFieldDto lookupField = lookup.getLookupField(field.getBasic().getName());
        FieldDto relationField = null;
        if (lookupFieldName.contains(".")) {
            EntityDto relatedEntity = schemaHolder.getEntityByClassName(new RelationshipHolder(field).getRelatedClass());
            relationField = schemaHolder.getFieldByName(relatedEntity, LookupName.getRelatedFieldName(lookupFieldName));
        }
        TypeDto type = field.getType();
        String typeClassName = resolveClassName(type, relationField);
        body.append("properties.add(");
        if (type.isCombobox()) {
            ComboboxHolder holder = new ComboboxHolder(entity, field);
            typeClassName = holder.getUnderlyingType();
            body.append(resolvePropertyForCombobox(holder, type));
        } else if (relationField != null && relationField.getType().isCombobox()) {
            RelationshipHolder relationshipHolder = new RelationshipHolder(field);
            EntityDto relatedEntity = schemaHolder.getEntityByClassName(relationshipHolder.getRelatedClass());
            ComboboxHolder holder = new ComboboxHolder(relatedEntity, relationField);
            typeClassName = holder.getUnderlyingType();
            body.append(resolvePropertyForCombobox(holder, type));
            addJdoVariableName(jdoQueryVariables, lookupFieldName);
            appendJdoVariableName = needsJdoVariable(type);
        } else if (relationField != null && (type.getTypeClass().equals(TypeDto.MANY_TO_MANY_RELATIONSHIP.getTypeClass()) || type.getTypeClass().equals(TypeDto.ONE_TO_MANY_RELATIONSHIP.getTypeClass()))) {
            // related class collection
            body.append(PropertyBuilder.class.getName());
            body.append(".createRelationProperty");
            addJdoVariableName(jdoQueryVariables, lookupFieldName);
            appendJdoVariableName = true;
        } else {
            body.append(PropertyBuilder.class.getName());
            body.append(".create");
        }
        body.append(buildPropertyParameters(appendJdoVariableName, lookupFieldName, lookupField, typeClassName, jdoQueryVariables));
    }
    body.append(buildReturn());
    return body.toString();
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) RelationshipHolder(org.motechproject.mds.domain.RelationshipHolder) HashMap(java.util.HashMap) 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) PropertyBuilder(org.motechproject.mds.query.PropertyBuilder)

Example 28 with TypeDto

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

the class MDSConstructorImpl method buildEnum.

private void buildEnum(JavassistLoader loader, MdsJDOEnhancer enhancer, EntityDto entity, SchemaHolder schemaHolder) {
    for (FieldDto field : schemaHolder.getFields(entity.getClassName())) {
        TypeDto type = field.getType();
        if (!type.isCombobox()) {
            continue;
        }
        ComboboxHolder holder = new ComboboxHolder(entity, field);
        if (holder.isEnum() || holder.isEnumCollection()) {
            if (field.isReadOnly()) {
                String enumName = holder.getEnumName();
                Class<?> definition = loadClass(entity, enumName);
                if (null != definition) {
                    MotechClassPool.registerEnum(enumName);
                    CtClass ctClass = MotechClassPool.getDefault().getOrNull(enumName);
                    if (null != ctClass) {
                        try {
                            ctClass.defrost();
                            byte[] bytecode = ctClass.toBytecode();
                            ClassData data = new ClassData(enumName, bytecode);
                            // register with the classloader so that we avoid issues with the persistence manager
                            MDSClassLoader.getInstance().safeDefineClass(data.getClassName(), data.getBytecode());
                            addClassData(loader, enhancer, data);
                        } catch (IOException | CannotCompileException e) {
                            LOGGER.error("Could not load enum: {}", enumName);
                        }
                    }
                }
            } else {
                buildEnum(loader, enhancer, holder);
            }
        }
    }
}
Also used : CtClass(javassist.CtClass) ClassData(org.motechproject.mds.domain.ClassData) ComboboxHolder(org.motechproject.mds.domain.ComboboxHolder) TypeDto(org.motechproject.mds.dto.TypeDto) IOException(java.io.IOException) CannotCompileException(javassist.CannotCompileException) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 29 with TypeDto

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

the class FieldProcessor method createValidation.

private FieldValidationDto createValidation(AccessibleObject ac, TypeDto type) {
    FieldValidationDto validationDto = null;
    for (Annotation annotation : ac.getAnnotations()) {
        List<TypeValidationDto> validations = getSchemaHolder().findValidations(type.getTypeClass(), annotation.annotationType());
        for (TypeValidationDto validation : validations) {
            String displayName = validation.getDisplayName();
            TypeDto valueType = getSchemaHolder().getType(validation.getValueType());
            if (null == valueType) {
                throw new IllegalStateException("The valueType is not set in: " + validation);
            }
            String valueAsString = getValidationValue(displayName, annotation);
            if (InSet.class.isAssignableFrom(annotation.annotationType()) || NotInSet.class.isAssignableFrom(annotation.annotationType())) {
                valueAsString = valueAsString.replaceAll("(\\{|\\})", "");
            }
            Object value = TypeHelper.parse(valueAsString, valueType.getClassObjectForType());
            ValidationCriterionDto dto = new ValidationCriterionDto();
            dto.setDisplayName(displayName);
            dto.setType(valueType);
            dto.setEnabled(true);
            dto.setValue(value);
            if (null == validationDto) {
                validationDto = new FieldValidationDto();
            }
            validationDto.addCriterion(dto);
        }
    }
    return validationDto;
}
Also used : NotInSet(org.motechproject.mds.annotations.NotInSet) TypeValidationDto(org.motechproject.mds.dto.TypeValidationDto) ValidationCriterionDto(org.motechproject.mds.dto.ValidationCriterionDto) FieldValidationDto(org.motechproject.mds.dto.FieldValidationDto) TypeDto(org.motechproject.mds.dto.TypeDto) InSet(org.motechproject.mds.annotations.InSet) NotInSet(org.motechproject.mds.annotations.NotInSet) AccessibleObject(java.lang.reflect.AccessibleObject) Annotation(java.lang.annotation.Annotation)

Example 30 with TypeDto

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

the class FieldProcessor method process.

@Override
protected void process(AnnotatedElement element) {
    AccessibleObject ac = (AccessibleObject) element;
    Class<?> classType = MemberUtil.getCorrectType(ac);
    Class<?> genericType = MemberUtil.getGenericType(element);
    Class<?> declaringClass = MemberUtil.getDeclaringClass(ac);
    Class<?> valueType = null;
    if (null != classType) {
        if (Map.class.isAssignableFrom(classType)) {
            valueType = MemberUtil.getGenericType(element, 1);
        }
        String fieldName = MemberUtil.getFieldName(ac);
        FieldDto currentField = getFieldByName(declaringClass.getName(), fieldName);
        boolean isRelationship = ReflectionsUtil.hasAnnotationClassLoaderSafe(genericType, genericType, Entity.class);
        boolean isOwningSide = isRelationship && getMappedBy(ac) == null;
        boolean isCollection = Collection.class.isAssignableFrom(classType);
        Field annotation = getAnnotationClassLoaderSafe(ac, classType, Field.class);
        String relatedFieldName = getRelatedFieldName(ac, classType, genericType, declaringClass, isRelationship);
        java.lang.reflect.Field relatedField = (relatedFieldName != null) ? ReflectionUtils.findField(genericType, relatedFieldName) : null;
        boolean relatedFieldIsCollection = isRelatedFieldCollection(relatedField);
        boolean isTextArea = getAnnotationValue(annotation, TYPE, EMPTY).equalsIgnoreCase("text");
        TypeDto type = getCorrectType(classType, isCollection, isRelationship, relatedFieldIsCollection);
        FieldBasicDto basic = new FieldBasicDto();
        basic.setDisplayName(isUiChanged(currentField) ? currentField.getBasic().getDisplayName() : getAnnotationValue(annotation, DISPLAY_NAME, convertFromCamelCase(fieldName)));
        basic.setName(fieldName);
        basic.setDefaultValue(getDefaultValueForField(annotation, classType));
        basic.setRequired(isFieldRequired(annotation, classType));
        basic.setUnique(isFieldUnique(ac));
        FieldDto field = new FieldDto();
        if (null != annotation) {
            basic.setTooltip(isUiChanged(currentField) ? currentField.getBasic().getTooltip() : annotation.tooltip());
            basic.setPlaceholder(annotation.placeholder());
            String fn = getAnnotationValue(annotation, NAME, EMPTY);
            if (!fn.equals(EMPTY)) {
                field.addMetadata(new MetadataDto(DATABASE_COLUMN_NAME, fn));
            }
        }
        field.setEntityId(entity.getId());
        field.setType(type);
        field.setBasic(basic);
        field.setValidation(createValidation(ac, type));
        field.setReadOnly(true);
        field.setNonEditable(false);
        field.setNonDisplayable(false);
        field.setUiChanged(isUiChanged(currentField));
        field.setUiFilterable(isUiFilterable(currentField));
        setFieldSettings(ac, classType, isRelationship, isTextArea, field);
        setFieldMetadata(classType, genericType, valueType, isCollection, isRelationship, relatedFieldIsCollection, isOwningSide, field, relatedFieldName);
        add(field);
    } else {
        LOGGER.warn("Field type is unknown in: {}", ac);
    }
}
Also used : PredicateUtil.entityField(org.motechproject.mds.annotations.internal.PredicateUtil.entityField) Field(org.motechproject.mds.annotations.Field) FieldBasicDto(org.motechproject.mds.dto.FieldBasicDto) AccessibleObject(java.lang.reflect.AccessibleObject) TypeDto(org.motechproject.mds.dto.TypeDto) MetadataDto(org.motechproject.mds.dto.MetadataDto) FieldDto(org.motechproject.mds.dto.FieldDto)

Aggregations

TypeDto (org.motechproject.mds.dto.TypeDto)30 FieldDto (org.motechproject.mds.dto.FieldDto)17 FieldBasicDto (org.motechproject.mds.dto.FieldBasicDto)9 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)9 EntityDto (org.motechproject.mds.dto.EntityDto)7 MetadataDto (org.motechproject.mds.dto.MetadataDto)7 ComboboxHolder (org.motechproject.mds.domain.ComboboxHolder)6 CtClass (javassist.CtClass)3 FieldValidationDto (org.motechproject.mds.dto.FieldValidationDto)3 LookupDto (org.motechproject.mds.dto.LookupDto)3 IOException (java.io.IOException)2 AccessibleObject (java.lang.reflect.AccessibleObject)2 Method (java.lang.reflect.Method)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 CannotCompileException (javassist.CannotCompileException)2 Before (org.junit.Before)2 Test (org.junit.Test)2 ClassData (org.motechproject.mds.domain.ClassData)2 Field (org.motechproject.mds.domain.Field)2