Search in sources :

Example 76 with FieldMetadata

use of org.springframework.roo.classpath.details.FieldMetadata 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 77 with FieldMetadata

use of org.springframework.roo.classpath.details.FieldMetadata 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 78 with FieldMetadata

use of org.springframework.roo.classpath.details.FieldMetadata 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)

Example 79 with FieldMetadata

use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.

the class ControllerCommands method getDetailFieldsRelatedToEntity.

/**
 * Get a field list that can be selected to do a detail controller.
 *
 * @param entity
 *            Entity on which create the detail controller
 * @param parentFieldName
 *            The parent's field name used to construct the field name
 *            related with the original entity
 * @return the related field list
 */
private List<String> getDetailFieldsRelatedToEntity(ClassOrInterfaceTypeDetails entity, String parentFieldName) {
    List<String> results = new ArrayList<String>();
    MemberDetails entityDetails = getMemberDetailsScanner().getMemberDetails(entity.getType().getSimpleTypeName(), entity);
    List<FieldMetadata> fields = entityDetails.getFields();
    for (FieldMetadata field : fields) {
        AnnotationMetadata oneToManyAnnotation = field.getAnnotation(JpaJavaType.ONE_TO_MANY);
        AnnotationMetadata manyToManyAnnotation = field.getAnnotation(JpaJavaType.MANY_TO_MANY);
        if ((oneToManyAnnotation != null || manyToManyAnnotation != null) && (field.getFieldType().getFullyQualifiedTypeName().equals(JavaType.LIST.getFullyQualifiedTypeName()) || field.getFieldType().getFullyQualifiedTypeName().equals(JavaType.SET.getFullyQualifiedTypeName()))) {
            results.add(parentFieldName.concat(field.getFieldName().getSymbolName()));
        }
    }
    return results;
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ArrayList(java.util.ArrayList) MemberDetails(org.springframework.roo.classpath.scanner.MemberDetails) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Example 80 with FieldMetadata

use of org.springframework.roo.classpath.details.FieldMetadata in project spring-roo by spring-projects.

the class ExceptionsMetadataProviderImpl method getMetadata.

@Override
protected ItdTypeDetailsProvidingMetadataItem getMetadata(String metadataIdentificationString, JavaType aspectName, PhysicalTypeMetadata governorPhysicalTypeMetadata, String itdFilename) {
    AnnotationMetadata handlersAnnotation = governorPhysicalTypeMetadata.getMemberHoldingTypeDetails().getAnnotation(ROO_EXCEPTION_HANDLERS);
    // Prepare list to register @RooExceptionHandler annotations data
    List<ExceptionHandlerAnnotationValues> exceptionHandlerValues = new ArrayList<ExceptionHandlerAnnotationValues>();
    // Get nested annotations
    AnnotationAttributeValue<Object> handlers = handlersAnnotation.getAttribute("value");
    List<?> values = (List<?>) handlers.getValue();
    Iterator<?> valuesIt = values.iterator();
    // Iterate over nested annotations
    while (valuesIt.hasNext()) {
        NestedAnnotationAttributeValue handlerAnnotation = (NestedAnnotationAttributeValue) valuesIt.next();
        if (handlerAnnotation.getValue() != null) {
            // Get attribute values
            JavaType exception = getNestedAttributeValue(handlerAnnotation, "exception");
            ClassOrInterfaceTypeDetails exceptionDetails = getTypeLocationService().getTypeDetails(exception);
            Validate.notNull(exception, "'exception' attribute in @RooExceptionHandler must not be null");
            String errorView = getNestedAttributeValue(handlerAnnotation, "errorView");
            // Register attribute values
            exceptionHandlerValues.add(new ExceptionHandlerAnnotationValues(exceptionDetails, errorView));
        }
    }
    // Check if type is a controller
    AnnotationMetadata rooControllerAnnotation = governorPhysicalTypeMetadata.getMemberHoldingTypeDetails().getAnnotation(RooJavaType.ROO_CONTROLLER);
    boolean isController = rooControllerAnnotation != null;
    List<FieldMetadata> fieldsMetadata = getMemberDetailsScanner().getMemberDetails(this.getClass().getName(), governorPhysicalTypeMetadata.getMemberHoldingTypeDetails()).getFields();
    return new ExceptionsMetadata(metadataIdentificationString, exceptionHandlerValues, aspectName, governorPhysicalTypeMetadata, fieldsMetadata, isController);
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ArrayList(java.util.ArrayList) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) ArrayList(java.util.ArrayList) List(java.util.List) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) NestedAnnotationAttributeValue(org.springframework.roo.classpath.details.annotations.NestedAnnotationAttributeValue)

Aggregations

FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)141 JavaType (org.springframework.roo.model.JavaType)76 ArrayList (java.util.ArrayList)69 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)59 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)40 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)40 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)39 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)38 RooJavaType (org.springframework.roo.model.RooJavaType)38 JdkJavaType (org.springframework.roo.model.JdkJavaType)33 JpaJavaType (org.springframework.roo.model.JpaJavaType)32 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)30 SpringJavaType (org.springframework.roo.model.SpringJavaType)30 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)29 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)26 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)21 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)21 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)18 RelationInfo (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo)17 ServiceMetadata (org.springframework.roo.addon.layers.service.addon.ServiceMetadata)15