Search in sources :

Example 11 with XmlElementBuilder

use of org.springframework.roo.support.util.XmlElementBuilder in project spring-roo by spring-projects.

the class CreatorOperationsImpl method createWrapperAddon.

public void createWrapperAddon(final JavaPackage topLevelPackage, final String groupId, final String artifactId, final String version, final String vendorName, final String lincenseUrl, final String docUrl, final String osgiImports, final String description, String projectName) {
    Validate.notNull(topLevelPackage, "Top Level Package required");
    if (StringUtils.isBlank(projectName)) {
        projectName = topLevelPackage.getFullyQualifiedPackageName().replace(".", "-");
    }
    final String wrapperGroupId = topLevelPackage.getFullyQualifiedPackageName();
    final InputStream templateInputStream = FileUtils.getInputStream(getClass(), "wrapper/roo-addon-wrapper-template.xml");
    final Document pom = XmlUtils.readXml(templateInputStream);
    final Element root = pom.getDocumentElement();
    XmlUtils.findRequiredElement("/project/name", root).setTextContent(projectName);
    XmlUtils.findRequiredElement("/project/groupId", root).setTextContent(wrapperGroupId);
    XmlUtils.findRequiredElement("/project/artifactId", root).setTextContent(wrapperGroupId + "." + artifactId);
    XmlUtils.findRequiredElement("/project/version", root).setTextContent(version + ".0001");
    XmlUtils.findRequiredElement("/project/dependencies/dependency/groupId", root).setTextContent(groupId);
    XmlUtils.findRequiredElement("/project/dependencies/dependency/artifactId", root).setTextContent(artifactId);
    XmlUtils.findRequiredElement("/project/dependencies/dependency/version", root).setTextContent(version);
    XmlUtils.findRequiredElement("/project/properties/pkgArtifactId", root).setTextContent(artifactId);
    XmlUtils.findRequiredElement("/project/properties/pkgVersion", root).setTextContent(version);
    XmlUtils.findRequiredElement("/project/properties/pkgVendor", root).setTextContent(vendorName);
    XmlUtils.findRequiredElement("/project/properties/pkgLicense", root).setTextContent(lincenseUrl);
    if (docUrl != null && docUrl.length() > 0) {
        XmlUtils.findRequiredElement("/project/properties/pkgDocUrl", root).setTextContent(docUrl);
    }
    if (osgiImports != null && osgiImports.length() > 0) {
        final Element config = XmlUtils.findRequiredElement("/project/build/plugins/plugin[artifactId = 'maven-bundle-plugin']/configuration/instructions", root);
        config.appendChild(new XmlElementBuilder("Import-Package", pom).setText(osgiImports).build());
    }
    if (description != null && description.length() > 0) {
        final Element descriptionE = XmlUtils.findRequiredElement("/project/description", root);
        descriptionE.setTextContent(description + " " + descriptionE.getTextContent());
    }
    writePomFile(pom, null);
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) XmlElementBuilder(org.springframework.roo.support.util.XmlElementBuilder)

Example 12 with XmlElementBuilder

use of org.springframework.roo.support.util.XmlElementBuilder 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)

Example 13 with XmlElementBuilder

use of org.springframework.roo.support.util.XmlElementBuilder in project spring-roo by spring-projects.

the class JspViewManager method getCreateDocument.

public Document getCreateDocument() {
    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("xmlns:c", "http://java.sun.com/jsp/jstl/core").addAttribute("xmlns:spring", "http://www.springframework.org/tags").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());
    // Add form create element
    final Element formCreate = new XmlElementBuilder("form:create", document).addAttribute("id", XmlUtils.convertId("fc:" + formBackingType.getFullyQualifiedTypeName())).addAttribute("modelAttribute", entityName).addAttribute("path", controllerPath).addAttribute("render", "${empty dependencies}").build();
    if (!controllerPath.equalsIgnoreCase(formBackingType.getSimpleTypeName())) {
        formCreate.setAttribute("path", controllerPath);
    }
    final List<FieldMetadata> formFields = new ArrayList<FieldMetadata>();
    final List<FieldMetadata> fieldCopy = new ArrayList<FieldMetadata>(fields);
    // Handle Roo identifiers
    if (!formBackingTypePersistenceMetadata.getRooIdentifierFields().isEmpty()) {
        final String identifierFieldName = formBackingTypePersistenceMetadata.getIdentifierField().getFieldName().getSymbolName();
        formCreate.setAttribute("compositePkField", identifierFieldName);
        for (final FieldMetadata embeddedField : formBackingTypePersistenceMetadata.getRooIdentifierFields()) {
            final FieldMetadataBuilder fieldBuilder = new FieldMetadataBuilder(embeddedField);
            fieldBuilder.setFieldName(new JavaSymbolName(identifierFieldName + "." + embeddedField.getFieldName().getSymbolName()));
            for (int i = 0; i < fieldCopy.size(); i++) {
                // Make sure form fields are not presented twice.
                if (fieldCopy.get(i).getFieldName().equals(embeddedField.getFieldName())) {
                    fieldCopy.remove(i);
                    break;
                }
            }
            formFields.add(fieldBuilder.build());
        }
    }
    formFields.addAll(fieldCopy);
    // If identifier manually assigned, show it in creation
    if (formBackingTypePersistenceMetadata.getIdentifierField().getAnnotation(JpaJavaType.GENERATED_VALUE) == null) {
        formFields.add(formBackingTypePersistenceMetadata.getIdentifierField());
    }
    createFieldsForCreateAndUpdate(formFields, document, formCreate, true);
    formCreate.setAttribute("z", XmlRoundTripUtils.calculateUniqueKeyFor(formCreate));
    final Element dependency = new XmlElementBuilder("form:dependency", document).addAttribute("id", XmlUtils.convertId("d:" + formBackingType.getFullyQualifiedTypeName())).addAttribute("render", "${not empty dependencies}").addAttribute("dependencies", "${dependencies}").build();
    dependency.setAttribute("z", XmlRoundTripUtils.calculateUniqueKeyFor(dependency));
    div.appendChild(formCreate);
    div.appendChild(dependency);
    return document;
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) XmlElementBuilder(org.springframework.roo.support.util.XmlElementBuilder) FieldMetadataBuilder(org.springframework.roo.classpath.details.FieldMetadataBuilder)

Example 14 with XmlElementBuilder

use of org.springframework.roo.support.util.XmlElementBuilder in project spring-roo by spring-projects.

the class JspViewManager method getShowDocument.

public Document getShowDocument() {
    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:page", "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 pageShow = new XmlElementBuilder("page:show", document).addAttribute("id", XmlUtils.convertId("ps:" + formBackingType.getFullyQualifiedTypeName())).addAttribute("object", "${" + entityName.toLowerCase() + "}").addAttribute("path", controllerPath).build();
    if (!webScaffoldAnnotationValues.isCreate()) {
        pageShow.setAttribute("create", "false");
    }
    if (!webScaffoldAnnotationValues.isUpdate()) {
        pageShow.setAttribute("update", "false");
    }
    if (!webScaffoldAnnotationValues.isDelete()) {
        pageShow.setAttribute("delete", "false");
    }
    pageShow.setAttribute("z", XmlRoundTripUtils.calculateUniqueKeyFor(pageShow));
    // Add field:display elements for each field
    for (final FieldMetadata field : fields) {
        // Ignoring java.util.Map field types (see ROO-194)
        if (field.getFieldType().equals(new JavaType(Map.class.getName()))) {
            continue;
        }
        final String fieldName = uncapitalize(field.getFieldName().getSymbolName());
        final Element fieldDisplay = new XmlElementBuilder("field:display", document).addAttribute("id", XmlUtils.convertId("s:" + formBackingType.getFullyQualifiedTypeName() + "." + field.getFieldName().getSymbolName())).addAttribute("object", "${" + entityName.toLowerCase() + "}").addAttribute("field", fieldName).build();
        if (field.getFieldType().equals(DATE)) {
            if (fieldName.equals(CREATED)) {
                continue;
            }
            fieldDisplay.setAttribute("date", "true");
            fieldDisplay.setAttribute("dateTimePattern", "${" + entityName + "_" + fieldName.toLowerCase() + "_date_format}");
        } else if (field.getFieldType().equals(CALENDAR)) {
            fieldDisplay.setAttribute("calendar", "true");
            fieldDisplay.setAttribute("dateTimePattern", "${" + entityName + "_" + fieldName.toLowerCase() + "_date_format}");
        } else if (field.getFieldType().isCommonCollectionType() && field.getCustomData().get(CustomDataKeys.ONE_TO_MANY_FIELD) != null) {
            continue;
        }
        fieldDisplay.setAttribute("z", XmlRoundTripUtils.calculateUniqueKeyFor(fieldDisplay));
        pageShow.appendChild(fieldDisplay);
    }
    div.appendChild(pageShow);
    return document;
}
Also used : JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) XmlElementBuilder(org.springframework.roo.support.util.XmlElementBuilder)

Example 15 with XmlElementBuilder

use of org.springframework.roo.support.util.XmlElementBuilder in project spring-roo by spring-projects.

the class JspViewManager method getUpdateDocument.

public Document getUpdateDocument() {
    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());
    // Add form update element
    final Element formUpdate = new XmlElementBuilder("form:update", document).addAttribute("id", XmlUtils.convertId("fu:" + formBackingType.getFullyQualifiedTypeName())).addAttribute("modelAttribute", entityName).build();
    if (!controllerPath.equalsIgnoreCase(formBackingType.getSimpleTypeName())) {
        formUpdate.setAttribute("path", controllerPath);
    }
    if (!"id".equals(formBackingTypePersistenceMetadata.getIdentifierField().getFieldName().getSymbolName())) {
        formUpdate.setAttribute("idField", formBackingTypePersistenceMetadata.getIdentifierField().getFieldName().getSymbolName());
    }
    final MethodMetadata versionAccessorMethod = formBackingTypePersistenceMetadata.getVersionAccessorMethod();
    if (versionAccessorMethod == null) {
        formUpdate.setAttribute("versionField", "none");
    } else {
        final String methodName = versionAccessorMethod.getMethodName().getSymbolName();
        formUpdate.setAttribute("versionField", methodName.substring("get".length()));
    }
    // Filter out embedded ID fields as they represent the composite PK
    // which is not to be updated.
    final List<FieldMetadata> fieldCopy = new ArrayList<FieldMetadata>(fields);
    for (final FieldMetadata embeddedField : formBackingTypePersistenceMetadata.getRooIdentifierFields()) {
        for (int i = 0; i < fieldCopy.size(); i++) {
            // Make sure form fields are not presented twice.
            if (fieldCopy.get(i).getFieldName().equals(embeddedField.getFieldName())) {
                fieldCopy.remove(i);
            }
        }
    }
    createFieldsForCreateAndUpdate(fieldCopy, document, formUpdate, false);
    formUpdate.setAttribute("z", XmlRoundTripUtils.calculateUniqueKeyFor(formUpdate));
    div.appendChild(formUpdate);
    return document;
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) DocumentBuilder(javax.xml.parsers.DocumentBuilder) Element(org.w3c.dom.Element) ArrayList(java.util.ArrayList) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) Document(org.w3c.dom.Document) XmlElementBuilder(org.springframework.roo.support.util.XmlElementBuilder)

Aggregations

XmlElementBuilder (org.springframework.roo.support.util.XmlElementBuilder)19 Element (org.w3c.dom.Element)19 Document (org.w3c.dom.Document)11 DocumentBuilder (javax.xml.parsers.DocumentBuilder)6 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)6 ArrayList (java.util.ArrayList)3 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)3 JavaType (org.springframework.roo.model.JavaType)3 JpaJavaType (org.springframework.roo.model.JpaJavaType)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 JavaTypeMetadataDetails (org.springframework.roo.addon.web.mvc.controller.addon.details.JavaTypeMetadataDetails)2 JavaTypePersistenceMetadataDetails (org.springframework.roo.addon.web.mvc.controller.addon.details.JavaTypePersistenceMetadataDetails)2 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileFilter (java.io.FileFilter)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1