Search in sources :

Example 16 with FieldItem

use of org.springframework.roo.addon.web.mvc.views.components.FieldItem in project spring-roo by spring-projects.

the class AbstractViewGenerationService method createFieldItem.

protected FieldItem createFieldItem(JpaEntityMetadata entityMetadata, FieldMetadata entityField, String entityName, String suffixId, ViewContext<T> ctx, String referencedFieldName) {
    // Getting current identifier field
    FieldMetadata identifierField = entityMetadata.getCurrentIndentifierField();
    FieldMetadata versionField = entityMetadata.getCurrentVersionField();
    // Exclude id and version fields
    if (entityField.getAnnotation(JpaJavaType.ID) != null || entityField.getAnnotation(JpaJavaType.VERSION) != null || identifierField.getFieldName().equals(entityField.getFieldName()) || (versionField != null && versionField.getFieldName().equals(entityField.getFieldName()))) {
        return null;
    }
    // Generating new FieldItem element
    FieldItem fieldItem = null;
    if (referencedFieldName != null) {
        fieldItem = new FieldItem(entityField.getFieldName().getSymbolName(), ctx.getEntityName(), referencedFieldName, entityName, suffixId);
    } else {
        fieldItem = new FieldItem(entityField.getFieldName().getSymbolName(), entityName, suffixId);
    }
    // Calculate fieldType
    JavaType type = entityField.getFieldType();
    ClassOrInterfaceTypeDetails typeDetails = getTypeLocationService().getTypeDetails(type);
    // ROO-3810: Getting @NotNull annotation to include required attr
    AnnotationMetadata notNullAnnotation = entityField.getAnnotation(Jsr303JavaType.NOT_NULL);
    if (notNullAnnotation != null) {
        fieldItem.addConfigurationElement("required", true);
    } else {
        fieldItem.addConfigurationElement("required", false);
    }
    // Check if is a referenced field
    if (typeDetails != null && typeDetails.getAnnotation(RooJavaType.ROO_JPA_ENTITY) != null) {
        // If is child part field of a composition relation, specify it if view context
        if (entityMetadata.getCompositionRelationField() != null && entityMetadata.getCompositionRelationField().getFieldName().equals(entityField.getFieldName())) {
            fieldItem.addConfigurationElement("isCompositionChildField", true);
        } else {
            fieldItem.addConfigurationElement("isCompositionChildField", false);
        }
        boolean shouldBeAdded = getReferenceField(fieldItem, typeDetails, ctx);
        if (!shouldBeAdded) {
            return null;
        }
    } else if (type.isBoolean()) {
        // Check if is a boolean field
        fieldItem.setType(FieldTypes.BOOLEAN.toString());
    } else if (typeDetails != null && typeDetails.getPhysicalTypeCategory().equals(PhysicalTypeCategory.ENUMERATION)) {
        // Saving enum and items to display. Same name as
        // populateForm method
        fieldItem.setType(FieldTypes.ENUM.toString());
        fieldItem.addConfigurationElement("items", fieldItem.getFieldName());
    } else if (type.getFullyQualifiedTypeName().equals(Date.class.getName()) || type.getFullyQualifiedTypeName().equals(Calendar.class.getName())) {
        // Check if is a date field
        fieldItem.setType(FieldTypes.DATE.toString());
        // Getting datetime format to use
        AnnotationMetadata dateTimeFormatAnnotation = entityField.getAnnotation(SpringJavaType.DATE_TIME_FORMAT);
        String format = "d/m/Y";
        if (dateTimeFormatAnnotation != null) {
            AnnotationAttributeValue<String> styleAttribute = dateTimeFormatAnnotation.getAttribute("style");
            if (styleAttribute != null) {
                String annotationFormat = styleAttribute.getValue();
                if (annotationFormat.equals("M-")) {
                    format = "d-M-Y";
                } else {
                    format = annotationFormat;
                }
            }
        }
        fieldItem.addConfigurationElement("format", format);
    } else if (type.getFullyQualifiedTypeName().equals("java.util.Set") || type.getFullyQualifiedTypeName().equals("java.util.List")) {
        // getDetailsFieldViewItems method
        return null;
    } else if (type.isNumber()) {
        // ROO-3810: Getting @Min and @Max annotations to add validations if
        // necessary
        AnnotationMetadata minAnnotation = entityField.getAnnotation(Jsr303JavaType.MIN);
        if (minAnnotation != null) {
            AnnotationAttributeValue<Object> min = minAnnotation.getAttribute("value");
            if (min != null) {
                fieldItem.addConfigurationElement("min", min.getValue().toString());
            } else {
                fieldItem.addConfigurationElement("min", "NULL");
            }
        } else {
            fieldItem.addConfigurationElement("min", "NULL");
        }
        AnnotationMetadata maxAnnotation = entityField.getAnnotation(Jsr303JavaType.MAX);
        if (maxAnnotation != null) {
            AnnotationAttributeValue<Object> max = maxAnnotation.getAttribute("value");
            if (max != null) {
                fieldItem.addConfigurationElement("max", max.getValue().toString());
            } else {
                fieldItem.addConfigurationElement("max", "NULL");
            }
        } else {
            fieldItem.addConfigurationElement("max", "NULL");
        }
        // ROO-3872: Support numeric input validation from client and server side
        // Add digits constraints
        AnnotationMetadata digitsAnnotation = entityField.getAnnotation(Jsr303JavaType.DIGITS);
        if (digitsAnnotation != null) {
            // Fraction digits
            AnnotationAttributeValue<Object> digitsFraction = digitsAnnotation.getAttribute("fraction");
            fieldItem.addConfigurationElement("digitsFraction", digitsFraction.getValue().toString());
            // Integer digits
            AnnotationAttributeValue<Object> digitsInteger = digitsAnnotation.getAttribute("integer");
            fieldItem.addConfigurationElement("digitsInteger", digitsInteger.getValue().toString());
        } else {
            // Add default values for decimals
            if (type.equals(JavaType.INT_OBJECT) || type.equals(JavaType.INT_PRIMITIVE) || type.equals(JdkJavaType.BIG_INTEGER)) {
                fieldItem.addConfigurationElement("digitsFraction", "0");
            } else {
                fieldItem.addConfigurationElement("digitsFraction", "2");
            }
            fieldItem.addConfigurationElement("digitsInteger", "NULL");
        }
        fieldItem.setType(FieldTypes.NUMBER.toString());
    } else {
        // ROO-3810: Getting @Size annotation
        AnnotationMetadata sizeAnnotation = entityField.getAnnotation(Jsr303JavaType.SIZE);
        if (sizeAnnotation != null) {
            AnnotationAttributeValue<Object> maxLength = sizeAnnotation.getAttribute("max");
            if (maxLength != null) {
                fieldItem.addConfigurationElement("maxLength", maxLength.getValue().toString());
            } else {
                fieldItem.addConfigurationElement("maxLength", "NULL");
            }
        } else {
            fieldItem.addConfigurationElement("maxLength", "NULL");
        }
        fieldItem.setType(FieldTypes.TEXT.toString());
    }
    return fieldItem;
}
Also used : Jsr303JavaType(org.springframework.roo.model.Jsr303JavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) SpringJavaType(org.springframework.roo.model.SpringJavaType) SpringletsJavaType(org.springframework.roo.model.SpringletsJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) AnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) Calendar(java.util.Calendar) FieldItem(org.springframework.roo.addon.web.mvc.views.components.FieldItem) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) Date(java.util.Date)

Aggregations

FieldItem (org.springframework.roo.addon.web.mvc.views.components.FieldItem)16 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)13 DetailEntityItem (org.springframework.roo.addon.web.mvc.views.components.DetailEntityItem)11 EntityItem (org.springframework.roo.addon.web.mvc.views.components.EntityItem)11 ArrayList (java.util.ArrayList)8 List (java.util.List)7 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)7 JavaType (org.springframework.roo.model.JavaType)7 RooJavaType (org.springframework.roo.model.RooJavaType)7 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)6 JdkJavaType (org.springframework.roo.model.JdkJavaType)6 JpaJavaType (org.springframework.roo.model.JpaJavaType)6 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)6 SpringJavaType (org.springframework.roo.model.SpringJavaType)6 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)6 RepositoryJpaMetadata (org.springframework.roo.addon.layers.repository.jpa.addon.RepositoryJpaMetadata)5 Document (org.jsoup.nodes.Document)3 Element (org.jsoup.nodes.Element)2 Configuration (freemarker.template.Configuration)1 Template (freemarker.template.Template)1