Search in sources :

Example 1 with JavaTypePersistenceMetadataDetails

use of org.springframework.roo.addon.web.mvc.controller.addon.details.JavaTypePersistenceMetadataDetails in project spring-roo by spring-projects.

the class JspMetadataListener method get.

public MetadataItem get(final String jspMetadataId) {
    // Work out the MIDs of the other metadata we depend on
    // NB: The JavaType and Path are to the corresponding web scaffold
    // controller class
    final String webScaffoldMetadataKey = WebScaffoldMetadata.createIdentifier(JspMetadata.getJavaType(jspMetadataId), JspMetadata.getPath(jspMetadataId));
    final WebScaffoldMetadata webScaffoldMetadata = (WebScaffoldMetadata) getMetadataService().get(webScaffoldMetadataKey);
    if (webScaffoldMetadata == null || !webScaffoldMetadata.isValid()) {
        // to manage any JSPs at this time
        return null;
    }
    final JavaType formBackingType = webScaffoldMetadata.getAnnotationValues().getFormBackingObject();
    final MemberDetails memberDetails = getWebMetadataService().getMemberDetails(formBackingType);
    final JavaTypeMetadataDetails formBackingTypeMetadataDetails = getWebMetadataService().getJavaTypeMetadataDetails(formBackingType, memberDetails, jspMetadataId);
    Validate.notNull(formBackingTypeMetadataDetails, "Unable to obtain metadata for type %s", formBackingType.getFullyQualifiedTypeName());
    formBackingObjectTypesToLocalMids.put(formBackingType, jspMetadataId);
    final SortedMap<JavaType, JavaTypeMetadataDetails> relatedTypeMd = getWebMetadataService().getRelatedApplicationTypeMetadata(formBackingType, memberDetails, jspMetadataId);
    final JavaTypeMetadataDetails formbackingTypeMetadata = relatedTypeMd.get(formBackingType);
    Validate.notNull(formbackingTypeMetadata, "Form backing type metadata required");
    final JavaTypePersistenceMetadataDetails formBackingTypePersistenceMetadata = formbackingTypeMetadata.getPersistenceDetails();
    if (formBackingTypePersistenceMetadata == null) {
        return null;
    }
    final ClassOrInterfaceTypeDetails formBackingTypeDetails = getTypeLocationService().getTypeDetails(formBackingType);
    final LogicalPath formBackingTypePath = PhysicalTypeIdentifier.getPath(formBackingTypeDetails.getDeclaredByMetadataId());
    getMetadataDependencyRegistry().registerDependency(PhysicalTypeIdentifier.createIdentifier(formBackingType, formBackingTypePath), JspMetadata.createIdentifier(formBackingType, formBackingTypePath));
    final LogicalPath path = JspMetadata.getPath(jspMetadataId);
    // Install web artifacts only if Spring MVC config is missing
    // TODO: Remove this call when 'controller' commands are gone
    final PathResolver pathResolver = getProjectOperations().getPathResolver();
    final LogicalPath webappPath = LogicalPath.getInstance(Path.SRC_MAIN_WEBAPP, path.getModule());
    if (!getFileManager().exists(pathResolver.getIdentifier(webappPath, WEB_INF_VIEWS))) {
        getJspOperations().installCommonViewArtefacts(path.getModule());
    }
    installImage(webappPath, "images/show.png");
    if (webScaffoldMetadata.getAnnotationValues().isUpdate()) {
        installImage(webappPath, "images/update.png");
    }
    if (webScaffoldMetadata.getAnnotationValues().isDelete()) {
        installImage(webappPath, "images/delete.png");
    }
    final List<FieldMetadata> eligibleFields = getWebMetadataService().getScaffoldEligibleFieldMetadata(formBackingType, memberDetails, jspMetadataId);
    if (eligibleFields.isEmpty() && formBackingTypePersistenceMetadata.getRooIdentifierFields().isEmpty()) {
        return null;
    }
    final JspViewManager viewManager = new JspViewManager(eligibleFields, webScaffoldMetadata.getAnnotationValues(), relatedTypeMd);
    String controllerPath = webScaffoldMetadata.getAnnotationValues().getPath();
    if (controllerPath.startsWith("/")) {
        controllerPath = controllerPath.substring(1);
    }
    // Make the holding directory for this controller
    final String destinationDirectory = pathResolver.getIdentifier(webappPath, WEB_INF_VIEWS + controllerPath);
    if (!getFileManager().exists(destinationDirectory)) {
        getFileManager().createDirectory(destinationDirectory);
    } else {
        final File file = new File(destinationDirectory);
        Validate.isTrue(file.isDirectory(), "%s is a file, when a directory was expected", destinationDirectory);
    }
    // By now we have a directory to put the JSPs inside
    final String listPath1 = destinationDirectory + "/list.jspx";
    getXmlRoundTripFileManager().writeToDiskIfNecessary(listPath1, viewManager.getListDocument());
    getTilesOperations().addViewDefinition(controllerPath, webappPath, controllerPath + "/" + "list", TilesOperations.DEFAULT_TEMPLATE, WEB_INF_VIEWS + controllerPath + "/list.jspx");
    final String showPath = destinationDirectory + "/show.jspx";
    getXmlRoundTripFileManager().writeToDiskIfNecessary(showPath, viewManager.getShowDocument());
    getTilesOperations().addViewDefinition(controllerPath, webappPath, controllerPath + "/" + "show", TilesOperations.DEFAULT_TEMPLATE, WEB_INF_VIEWS + controllerPath + "/show.jspx");
    final Map<String, String> properties = new LinkedHashMap<String, String>();
    final JavaSymbolName categoryName = new JavaSymbolName(formBackingType.getSimpleTypeName());
    properties.put("menu_category_" + categoryName.getSymbolName().toLowerCase() + "_label", categoryName.getReadableSymbolName());
    final JavaSymbolName newMenuItemId = new JavaSymbolName("new");
    properties.put("menu_item_" + categoryName.getSymbolName().toLowerCase() + "_" + newMenuItemId.getSymbolName().toLowerCase() + "_label", new JavaSymbolName(formBackingType.getSimpleTypeName()).getReadableSymbolName());
    if (webScaffoldMetadata.getAnnotationValues().isCreate()) {
        final String listPath = destinationDirectory + "/create.jspx";
        getXmlRoundTripFileManager().writeToDiskIfNecessary(listPath, viewManager.getCreateDocument());
        // Add 'create new' menu item
        getMenuOperations().addMenuItem(categoryName, newMenuItemId, "global_menu_new", "/" + controllerPath + "?form", MenuOperations.DEFAULT_MENU_ITEM_PREFIX, webappPath);
        getTilesOperations().addViewDefinition(controllerPath, webappPath, controllerPath + "/" + "create", TilesOperations.DEFAULT_TEMPLATE, WEB_INF_VIEWS + controllerPath + "/create.jspx");
    } else {
        getMenuOperations().cleanUpMenuItem(categoryName, new JavaSymbolName("new"), MenuOperations.DEFAULT_MENU_ITEM_PREFIX, webappPath);
        getTilesOperations().removeViewDefinition(controllerPath + "/" + "create", controllerPath, webappPath);
    }
    if (webScaffoldMetadata.getAnnotationValues().isUpdate()) {
        final String listPath = destinationDirectory + "/update.jspx";
        getXmlRoundTripFileManager().writeToDiskIfNecessary(listPath, viewManager.getUpdateDocument());
        getTilesOperations().addViewDefinition(controllerPath, webappPath, controllerPath + "/" + "update", TilesOperations.DEFAULT_TEMPLATE, WEB_INF_VIEWS + controllerPath + "/update.jspx");
    } else {
        getTilesOperations().removeViewDefinition(controllerPath + "/" + "update", controllerPath, webappPath);
    }
    // Setup labels for i18n support
    final String resourceId = XmlUtils.convertId("label." + formBackingType.getFullyQualifiedTypeName().toLowerCase());
    properties.put(resourceId, new JavaSymbolName(formBackingType.getSimpleTypeName()).getReadableSymbolName());
    final String pluralResourceId = XmlUtils.convertId(resourceId + ".plural");
    final String plural = formBackingTypeMetadataDetails.getPlural();
    properties.put(pluralResourceId, new JavaSymbolName(plural).getReadableSymbolName());
    final JavaTypePersistenceMetadataDetails javaTypePersistenceMetadataDetails = formBackingTypeMetadataDetails.getPersistenceDetails();
    Validate.notNull(javaTypePersistenceMetadataDetails, "Unable to determine persistence metadata for type %s", formBackingType.getFullyQualifiedTypeName());
    if (!javaTypePersistenceMetadataDetails.getRooIdentifierFields().isEmpty()) {
        for (final FieldMetadata idField : javaTypePersistenceMetadataDetails.getRooIdentifierFields()) {
            properties.put(XmlUtils.convertId(resourceId + "." + javaTypePersistenceMetadataDetails.getIdentifierField().getFieldName().getSymbolName() + "." + idField.getFieldName().getSymbolName().toLowerCase()), idField.getFieldName().getReadableSymbolName());
        }
    }
    // If no auto generated value for id, put name in i18n
    if (javaTypePersistenceMetadataDetails.getIdentifierField().getAnnotation(JpaJavaType.GENERATED_VALUE) == null) {
        properties.put(XmlUtils.convertId(resourceId + "." + javaTypePersistenceMetadataDetails.getIdentifierField().getFieldName().getSymbolName().toLowerCase()), javaTypePersistenceMetadataDetails.getIdentifierField().getFieldName().getReadableSymbolName());
    }
    for (final MethodMetadata method : memberDetails.getMethods()) {
        if (!BeanInfoUtils.isAccessorMethod(method)) {
            continue;
        }
        final FieldMetadata field = BeanInfoUtils.getFieldForJavaBeanMethod(memberDetails, method);
        if (field == null) {
            continue;
        }
        final JavaSymbolName fieldName = field.getFieldName();
        final String fieldResourceId = XmlUtils.convertId(resourceId + "." + fieldName.getSymbolName().toLowerCase());
        if (getTypeLocationService().isInProject(method.getReturnType()) && getWebMetadataService().isRooIdentifier(method.getReturnType(), getWebMetadataService().getMemberDetails(method.getReturnType()))) {
            final JavaTypePersistenceMetadataDetails typePersistenceMetadataDetails = getWebMetadataService().getJavaTypePersistenceMetadataDetails(method.getReturnType(), getWebMetadataService().getMemberDetails(method.getReturnType()), jspMetadataId);
            if (typePersistenceMetadataDetails != null) {
                for (final FieldMetadata f : typePersistenceMetadataDetails.getRooIdentifierFields()) {
                    final String sb = f.getFieldName().getReadableSymbolName();
                    properties.put(XmlUtils.convertId(resourceId + "." + javaTypePersistenceMetadataDetails.getIdentifierField().getFieldName().getSymbolName() + "." + f.getFieldName().getSymbolName().toLowerCase()), StringUtils.isNotBlank(sb) ? sb : fieldName.getSymbolName());
                }
            }
        } else if (!method.getMethodName().equals(javaTypePersistenceMetadataDetails.getIdentifierAccessorMethod().getMethodName()) || javaTypePersistenceMetadataDetails.getVersionAccessorMethod() != null && !method.getMethodName().equals(javaTypePersistenceMetadataDetails.getVersionAccessorMethod().getMethodName())) {
            final String sb = fieldName.getReadableSymbolName();
            properties.put(fieldResourceId, StringUtils.isNotBlank(sb) ? sb : fieldName.getSymbolName());
        }
    }
    if (javaTypePersistenceMetadataDetails.getFindAllMethod() != null) {
        // Add 'list all' menu item
        final JavaSymbolName listMenuItemId = new JavaSymbolName("list");
        getMenuOperations().addMenuItem(categoryName, listMenuItemId, "global_menu_list", "/" + controllerPath + "?page=1&size=${empty param.size ? 10 : param.size}", MenuOperations.DEFAULT_MENU_ITEM_PREFIX, webappPath);
        properties.put("menu_item_" + categoryName.getSymbolName().toLowerCase() + "_" + listMenuItemId.getSymbolName().toLowerCase() + "_label", new JavaSymbolName(plural).getReadableSymbolName());
    } else {
        getMenuOperations().cleanUpMenuItem(categoryName, new JavaSymbolName("list"), MenuOperations.DEFAULT_MENU_ITEM_PREFIX, webappPath);
    }
    final String controllerPhysicalTypeId = PhysicalTypeIdentifier.createIdentifier(JspMetadata.getJavaType(jspMetadataId), JspMetadata.getPath(jspMetadataId));
    final PhysicalTypeMetadata controllerPhysicalTypeMd = (PhysicalTypeMetadata) getMetadataService().get(controllerPhysicalTypeId);
    if (controllerPhysicalTypeMd == null) {
        return null;
    }
    final MemberHoldingTypeDetails mhtd = controllerPhysicalTypeMd.getMemberHoldingTypeDetails();
    if (mhtd == null) {
        return null;
    }
    final List<String> allowedMenuItems = new ArrayList<String>();
    if (MemberFindingUtils.getAnnotationOfType(mhtd.getAnnotations(), RooJavaType.ROO_WEB_FINDER) != null) {
        // This controller is annotated with @RooWebFinder
        final Set<FinderMetadataDetails> finderMethodsDetails = getWebMetadataService().getDynamicFinderMethodsAndFields(formBackingType, memberDetails, jspMetadataId);
        if (finderMethodsDetails == null) {
            return null;
        }
        for (final FinderMetadataDetails finderDetails : finderMethodsDetails) {
            final String finderName = finderDetails.getFinderMethodMetadata().getMethodName().getSymbolName();
            final String listPath = destinationDirectory + "/" + finderName + ".jspx";
            // long (see ROO-1027)
            if (listPath.length() > 244) {
                continue;
            }
            getXmlRoundTripFileManager().writeToDiskIfNecessary(listPath, viewManager.getFinderDocument(finderDetails));
            final JavaSymbolName finderLabel = new JavaSymbolName(finderName.replace("find" + plural + "By", ""));
            // Add 'Find by' menu item
            getMenuOperations().addMenuItem(categoryName, finderLabel, "global_menu_find", "/" + controllerPath + "?find=" + finderName.replace("find" + plural, "") + "&form" + "&page=1&size=${empty param.size ? 10 : param.size}", MenuOperations.FINDER_MENU_ITEM_PREFIX, webappPath);
            properties.put("menu_item_" + categoryName.getSymbolName().toLowerCase() + "_" + finderLabel.getSymbolName().toLowerCase() + "_label", finderLabel.getReadableSymbolName());
            allowedMenuItems.add(MenuOperations.FINDER_MENU_ITEM_PREFIX + categoryName.getSymbolName().toLowerCase() + "_" + finderLabel.getSymbolName().toLowerCase());
            for (final JavaSymbolName paramName : finderDetails.getFinderMethodMetadata().getParameterNames()) {
                properties.put(XmlUtils.convertId(resourceId + "." + paramName.getSymbolName().toLowerCase()), paramName.getReadableSymbolName());
            }
            getTilesOperations().addViewDefinition(controllerPath, webappPath, controllerPath + "/" + finderName, TilesOperations.DEFAULT_TEMPLATE, WEB_INF_VIEWS + controllerPath + "/" + finderName + ".jspx");
        }
    }
    getMenuOperations().cleanUpFinderMenuItems(categoryName, allowedMenuItems, webappPath);
    return new JspMetadata(jspMetadataId, webScaffoldMetadata);
}
Also used : JavaTypePersistenceMetadataDetails(org.springframework.roo.addon.web.mvc.controller.addon.details.JavaTypePersistenceMetadataDetails) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) WebScaffoldMetadata(org.springframework.roo.addon.web.mvc.controller.addon.scaffold.WebScaffoldMetadata) JavaTypeMetadataDetails(org.springframework.roo.addon.web.mvc.controller.addon.details.JavaTypeMetadataDetails) LogicalPath(org.springframework.roo.project.LogicalPath) PathResolver(org.springframework.roo.project.PathResolver) FinderMetadataDetails(org.springframework.roo.addon.web.mvc.controller.addon.details.FinderMetadataDetails) RooJavaType(org.springframework.roo.model.RooJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) PhysicalTypeMetadata(org.springframework.roo.classpath.PhysicalTypeMetadata) MemberHoldingTypeDetails(org.springframework.roo.classpath.details.MemberHoldingTypeDetails) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) File(java.io.File)

Example 2 with JavaTypePersistenceMetadataDetails

use of org.springframework.roo.addon.web.mvc.controller.addon.details.JavaTypePersistenceMetadataDetails in project spring-roo by spring-projects.

the class JspViewManager method getFinderDocument.

public Document getFinderDocument(final FinderMetadataDetails finderMetadataDetails) {
    final DocumentBuilder builder = XmlUtils.getDocumentBuilder();
    final Document document = builder.newDocument();
    // Add document namespaces
    final Element div = (Element) document.appendChild(new XmlElementBuilder("div", document).addAttribute("xmlns:form", "urn:jsptagdir:/WEB-INF/tags/form").addAttribute("xmlns:field", "urn:jsptagdir:/WEB-INF/tags/form/fields").addAttribute("xmlns:jsp", "http://java.sun.com/JSP/Page").addAttribute("version", "2.0").addChild(new XmlElementBuilder("jsp:directive.page", document).addAttribute("contentType", "text/html;charset=UTF-8").build()).addChild(new XmlElementBuilder("jsp:output", document).addAttribute("omit-xml-declaration", "yes").build()).build());
    final Element formFind = new XmlElementBuilder("form:find", document).addAttribute("id", XmlUtils.convertId("ff:" + formBackingType.getFullyQualifiedTypeName())).addAttribute("path", controllerPath).addAttribute("finderName", finderMetadataDetails.getFinderMethodMetadata().getMethodName().getSymbolName().replace("find" + formBackingTypeMetadata.getPlural(), "")).build();
    formFind.setAttribute("z", XmlRoundTripUtils.calculateUniqueKeyFor(formFind));
    div.appendChild(formFind);
    for (final FieldMetadata field : finderMetadataDetails.getFinderMethodParamFields()) {
        final JavaType type = field.getFieldType();
        final JavaSymbolName paramName = field.getFieldName();
        JavaSymbolName fieldName = null;
        if (paramName.getSymbolName().startsWith("max") || paramName.getSymbolName().startsWith("min")) {
            fieldName = new JavaSymbolName(Introspector.decapitalize(StringUtils.capitalize(paramName.getSymbolName().substring(3))));
        } else {
            fieldName = paramName;
        }
        // Ignoring java.util.Map field types (see ROO-194)
        if (type.equals(new JavaType(Map.class.getName()))) {
            continue;
        }
        Validate.notNull(paramName, "Could not find field '%s' in '%s'", paramName, type.getFullyQualifiedTypeName());
        Element fieldElement = null;
        final JavaTypeMetadataDetails typeMetadataHolder = relatedDomainTypes.get(getJavaTypeForField(field));
        if (type.isCommonCollectionType() && relatedDomainTypes.containsKey(getJavaTypeForField(field))) {
            final JavaTypeMetadataDetails collectionTypeMetadataHolder = relatedDomainTypes.get(getJavaTypeForField(field));
            final JavaTypePersistenceMetadataDetails typePersistenceMetadataHolder = collectionTypeMetadataHolder.getPersistenceDetails();
            if (typePersistenceMetadataHolder != null) {
                fieldElement = new XmlElementBuilder("field:select", document).addAttribute("required", "true").addAttribute("items", "${" + collectionTypeMetadataHolder.getPlural().toLowerCase() + "}").addAttribute("itemValue", typePersistenceMetadataHolder.getIdentifierField().getFieldName().getSymbolName()).addAttribute("path", "/" + getPathForType(getJavaTypeForField(field))).build();
                if (field.getCustomData().keySet().contains(CustomDataKeys.MANY_TO_MANY_FIELD)) {
                    fieldElement.setAttribute("multiple", "true");
                }
            }
        } else if (typeMetadataHolder != null && typeMetadataHolder.isEnumType() && field.getCustomData().keySet().contains(CustomDataKeys.ENUMERATED_FIELD)) {
            fieldElement = new XmlElementBuilder("field:select", document).addAttribute("required", "true").addAttribute("items", "${" + typeMetadataHolder.getPlural().toLowerCase() + "}").addAttribute("path", "/" + getPathForType(type)).build();
        } else if (type.equals(BOOLEAN_OBJECT) || type.equals(BOOLEAN_PRIMITIVE)) {
            fieldElement = document.createElement("field:checkbox");
        } else if (typeMetadataHolder != null && typeMetadataHolder.isApplicationType()) {
            final JavaTypePersistenceMetadataDetails typePersistenceMetadataHolder = typeMetadataHolder.getPersistenceDetails();
            if (typePersistenceMetadataHolder != null) {
                fieldElement = new XmlElementBuilder("field:select", document).addAttribute("required", "true").addAttribute("items", "${" + typeMetadataHolder.getPlural().toLowerCase() + "}").addAttribute("itemValue", typePersistenceMetadataHolder.getIdentifierField().getFieldName().getSymbolName()).addAttribute("path", "/" + getPathForType(type)).build();
            }
        } else if (type.equals(DATE) || type.equals(CALENDAR)) {
            fieldElement = new XmlElementBuilder("field:datetime", document).addAttribute("required", "true").addAttribute("dateTimePattern", "${" + entityName + "_" + fieldName.getSymbolName().toLowerCase() + "_date_format}").build();
        }
        if (fieldElement == null) {
            fieldElement = new XmlElementBuilder("field:input", document).addAttribute("required", "true").build();
        }
        addCommonAttributes(field, fieldElement);
        fieldElement.setAttribute("disableFormBinding", "true");
        fieldElement.setAttribute("field", paramName.getSymbolName());
        fieldElement.setAttribute("id", XmlUtils.convertId("f:" + formBackingType.getFullyQualifiedTypeName() + "." + paramName));
        fieldElement.setAttribute("z", XmlRoundTripUtils.calculateUniqueKeyFor(fieldElement));
        formFind.appendChild(fieldElement);
    }
    DomUtils.removeTextNodes(document);
    return document;
}
Also used : JavaTypePersistenceMetadataDetails(org.springframework.roo.addon.web.mvc.controller.addon.details.JavaTypePersistenceMetadataDetails) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) JavaTypeMetadataDetails(org.springframework.roo.addon.web.mvc.controller.addon.details.JavaTypeMetadataDetails) Document(org.w3c.dom.Document) XmlElementBuilder(org.springframework.roo.support.util.XmlElementBuilder)

Example 3 with JavaTypePersistenceMetadataDetails

use of org.springframework.roo.addon.web.mvc.controller.addon.details.JavaTypePersistenceMetadataDetails in project spring-roo by spring-projects.

the class JspViewManager method createFieldsForCreateAndUpdate.

private void createFieldsForCreateAndUpdate(final List<FieldMetadata> formFields, final Document document, final Element root, final boolean isCreate) {
    for (final FieldMetadata field : formFields) {
        final String fieldName = field.getFieldName().getSymbolName();
        JavaType fieldType = field.getFieldType();
        AnnotationMetadata annotationMetadata;
        // Ignoring java.util.Map field types (see ROO-194)
        if (fieldType.equals(new JavaType(Map.class.getName()))) {
            continue;
        }
        // separately to the field list
        if (field.getCustomData().keySet().contains(CustomDataKeys.EMBEDDED_ID_FIELD)) {
            continue;
        }
        fieldType = getJavaTypeForField(field);
        final JavaTypeMetadataDetails typeMetadataHolder = relatedDomainTypes.get(fieldType);
        JavaTypePersistenceMetadataDetails typePersistenceMetadataHolder = null;
        if (typeMetadataHolder != null) {
            typePersistenceMetadataHolder = typeMetadataHolder.getPersistenceDetails();
        }
        Element fieldElement = null;
        if (fieldType.getFullyQualifiedTypeName().equals(Boolean.class.getName()) || fieldType.getFullyQualifiedTypeName().equals(boolean.class.getName())) {
            fieldElement = document.createElement("field:checkbox");
        // Handle enum fields
        } else if (typeMetadataHolder != null && typeMetadataHolder.isEnumType()) {
            fieldElement = new XmlElementBuilder("field:select", document).addAttribute("items", "${" + typeMetadataHolder.getPlural().toLowerCase() + "}").addAttribute("path", getPathForType(fieldType)).build();
        } else if (field.getCustomData().keySet().contains(CustomDataKeys.ONE_TO_MANY_FIELD)) {
            // accordingly
            if (typePersistenceMetadataHolder != null) {
                fieldElement = new XmlElementBuilder("field:simple", document).addAttribute("messageCode", "entity_reference_not_managed").addAttribute("messageCodeAttribute", new JavaSymbolName(fieldType.getSimpleTypeName()).getReadableSymbolName()).build();
            } else {
                continue;
            }
        } else if (field.getCustomData().keySet().contains(CustomDataKeys.MANY_TO_ONE_FIELD) || field.getCustomData().keySet().contains(CustomDataKeys.MANY_TO_MANY_FIELD) || field.getCustomData().keySet().contains(CustomDataKeys.ONE_TO_ONE_FIELD)) {
            final JavaType referenceType = getJavaTypeForField(field);
            final JavaTypeMetadataDetails referenceTypeMetadata = relatedDomainTypes.get(referenceType);
            if (referenceType != null && referenceTypeMetadata != null && referenceTypeMetadata.isApplicationType() && typePersistenceMetadataHolder != null) {
                fieldElement = new XmlElementBuilder("field:select", document).addAttribute("items", "${" + referenceTypeMetadata.getPlural().toLowerCase() + "}").addAttribute("itemValue", typePersistenceMetadataHolder.getIdentifierField().getFieldName().getSymbolName()).addAttribute("path", "/" + getPathForType(getJavaTypeForField(field))).build();
                if (field.getCustomData().keySet().contains(CustomDataKeys.MANY_TO_MANY_FIELD)) {
                    fieldElement.setAttribute("multiple", "true");
                }
            }
        } else if (fieldType.equals(DATE) || fieldType.equals(CALENDAR)) {
            if (fieldName.equals(CREATED)) {
                continue;
            }
            // Only include the date picker for styles supported by Dojo
            // (SMALL & MEDIUM)
            fieldElement = new XmlElementBuilder("field:datetime", document).addAttribute("dateTimePattern", "${" + entityName + "_" + fieldName.toLowerCase() + "_date_format}").build();
            if (null != MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), FUTURE)) {
                fieldElement.setAttribute("future", "true");
            } else if (null != MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), PAST)) {
                fieldElement.setAttribute("past", "true");
            }
        } else if (field.getCustomData().keySet().contains(CustomDataKeys.LOB_FIELD)) {
            fieldElement = new XmlElementBuilder("field:textarea", document).build();
        }
        if ((annotationMetadata = MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), SIZE)) != null) {
            final AnnotationAttributeValue<?> max = annotationMetadata.getAttribute(new JavaSymbolName("max"));
            if (max != null) {
                final int maxValue = (Integer) max.getValue();
                if (fieldElement == null && maxValue > 30) {
                    fieldElement = new XmlElementBuilder("field:textarea", document).build();
                }
            }
        }
        // Use a default input field if no other criteria apply
        if (fieldElement == null) {
            fieldElement = document.createElement("field:input");
        }
        addCommonAttributes(field, fieldElement);
        fieldElement.setAttribute("field", fieldName);
        fieldElement.setAttribute("id", XmlUtils.convertId("c:" + formBackingType.getFullyQualifiedTypeName() + "." + field.getFieldName().getSymbolName()));
        // If identifier manually assigned, then add 'required=true'
        if (formBackingTypePersistenceMetadata.getIdentifierField().getFieldName().equals(field.getFieldName()) && field.getAnnotation(JpaJavaType.GENERATED_VALUE) == null) {
            fieldElement.setAttribute("required", "true");
        }
        fieldElement.setAttribute("z", XmlRoundTripUtils.calculateUniqueKeyFor(fieldElement));
        root.appendChild(fieldElement);
    }
}
Also used : JavaTypePersistenceMetadataDetails(org.springframework.roo.addon.web.mvc.controller.addon.details.JavaTypePersistenceMetadataDetails) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) Element(org.w3c.dom.Element) JavaTypeMetadataDetails(org.springframework.roo.addon.web.mvc.controller.addon.details.JavaTypeMetadataDetails) XmlElementBuilder(org.springframework.roo.support.util.XmlElementBuilder) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Aggregations

JavaTypeMetadataDetails (org.springframework.roo.addon.web.mvc.controller.addon.details.JavaTypeMetadataDetails)3 JavaTypePersistenceMetadataDetails (org.springframework.roo.addon.web.mvc.controller.addon.details.JavaTypePersistenceMetadataDetails)3 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)3 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)3 JavaType (org.springframework.roo.model.JavaType)3 JpaJavaType (org.springframework.roo.model.JpaJavaType)3 XmlElementBuilder (org.springframework.roo.support.util.XmlElementBuilder)2 Element (org.w3c.dom.Element)2 File (java.io.File)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 FinderMetadataDetails (org.springframework.roo.addon.web.mvc.controller.addon.details.FinderMetadataDetails)1 WebScaffoldMetadata (org.springframework.roo.addon.web.mvc.controller.addon.scaffold.WebScaffoldMetadata)1 PhysicalTypeMetadata (org.springframework.roo.classpath.PhysicalTypeMetadata)1 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)1 MemberHoldingTypeDetails (org.springframework.roo.classpath.details.MemberHoldingTypeDetails)1 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)1 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)1 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)1