Search in sources :

Example 91 with JavaSymbolName

use of org.springframework.roo.model.JavaSymbolName in project spring-roo by spring-projects.

the class ThymeleafControllerIntegrationTestMetadata method getTestExampleMethod.

/**
 * Builds and return a test example method.
 *
 * @return {@link MethodMetadata}
 */
private MethodMetadata getTestExampleMethod() {
    JavaSymbolName methodName = new JavaSymbolName("testMethodExample");
    // Check if method exists in governor
    MethodMetadata method = getGovernorMethod(methodName);
    if (method != null) {
        return method;
    }
    // Build method body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // Setup
    bodyBuilder.appendFormalLine("// Setup");
    bodyBuilder.appendFormalLine("// Previous tasks");
    bodyBuilder.newLine();
    // Exercise
    bodyBuilder.appendFormalLine("// Exercise");
    bodyBuilder.appendFormalLine("// Execute method to test");
    bodyBuilder.newLine();
    // Verify
    bodyBuilder.appendFormalLine("// Verify");
    bodyBuilder.appendFormalLine("// Check results with assertions");
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(this.getId(), Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, bodyBuilder);
    // Add @Test
    methodBuilder.addAnnotation(new AnnotationMetadataBuilder(TEST));
    CommentStructure commentStructure = new CommentStructure();
    commentStructure.addComment(new JavadocComment("Test method example. To be implemented by developer."), CommentLocation.BEGINNING);
    methodBuilder.setCommentStructure(commentStructure);
    return methodBuilder.build();
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) JavadocComment(org.springframework.roo.classpath.details.comments.JavadocComment) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) CommentStructure(org.springframework.roo.classpath.details.comments.CommentStructure)

Example 92 with JavaSymbolName

use of org.springframework.roo.model.JavaSymbolName in project spring-roo by spring-projects.

the class ThymeleafMetadata method getFinderFormMethodForFinderInService.

/**
 * Generates a finder method which delegates on entity service to get result
 *
 * @param finderName
 * @param serviceFinderMethod
 * @return
 */
private MethodMetadata getFinderFormMethodForFinderInService(String finderName, MethodMetadata serviceFinderMethod) {
    // Define methodName
    String pathName = finderName;
    if (pathName.startsWith("findBy")) {
        pathName = pathName.replace("findBy", "by");
    }
    final JavaSymbolName methodName = new JavaSymbolName(pathName.concat("Form"));
    // Form Bean is always the first parameter of finder
    final JavaType formBean = serviceFinderMethod.getParameterTypes().get(0).getJavaType();
    List<AnnotationMetadata> formBeanAnnotations = new ArrayList<AnnotationMetadata>();
    AnnotationMetadataBuilder formBeanAnnotation = new AnnotationMetadataBuilder(SpringJavaType.MODEL_ATTRIBUTE);
    formBeanAnnotation.addStringAttribute("value", FORM_BEAN_PARAM_NAME.getSymbolName());
    formBeanAnnotations.add(formBeanAnnotation.build());
    AnnotatedJavaType annotatedFormBean = new AnnotatedJavaType(formBean, formBeanAnnotations);
    // Including annotated formBean parameter and Model parameter
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    parameterTypes.add(annotatedFormBean);
    parameterTypes.add(MODEL_PARAM);
    MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
    if (existingMethod != null) {
        return existingMethod;
    }
    final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    parameterNames.add(FORM_BEAN_PARAM_NAME);
    parameterNames.add(MODEL_PARAM_NAME);
    // Adding annotations
    final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
    // Adding @GetMapping annotation
    AnnotationMetadataBuilder getMappingAnnotation = new AnnotationMetadataBuilder(GET_MAPPING);
    getMappingAnnotation.addStringAttribute("name", methodName.getSymbolName());
    // TODO Delegates on ControllerOperations to obtain the URL for this
    // finder
    getMappingAnnotation.addStringAttribute("value", "/" + pathName + "/search-form");
    annotations.add(getMappingAnnotation);
    this.mvcMethodNames.put(methodName.getSymbolName(), methodName.getSymbolName());
    // Generate body
    final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    buildPopulateFormatBody(bodyBuilder, formBeansDateTimeFields.get(formBean));
    final List<FieldMetadata> enumFileds = formBeansEnumFields.get(formBean);
    if (enumFileds != null && !enumFileds.isEmpty()) {
        buildPopulateEnumsBody(bodyBuilder, formBeansEnumFields.get(formBean));
    }
    // return new ModelAndView("customers/findByFirstNameLastNameForm");
    bodyBuilder.appendFormalLine("return new %s(\"%s/%s\");", getNameOfJavaType(SpringJavaType.MODEL_AND_VIEW), viewsPath, finderName.concat("Form"));
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, SpringJavaType.MODEL_AND_VIEW, parameterTypes, parameterNames, bodyBuilder);
    methodBuilder.setAnnotations(annotations);
    return methodBuilder.build();
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) Jsr303JavaType(org.springframework.roo.model.Jsr303JavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) SpringJavaType(org.springframework.roo.model.SpringJavaType) SpringletsJavaType(org.springframework.roo.model.SpringletsJavaType) JavaType(org.springframework.roo.model.JavaType) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 93 with JavaSymbolName

use of org.springframework.roo.model.JavaSymbolName in project spring-roo by spring-projects.

the class WebMvcConfigurationMetadata method getAddInterceptors.

/**
 * Method that generates "addInterceptors" method.
 *
 * @return MethodMetadata
 */
private MethodMetadata getAddInterceptors() {
    // Define method name
    JavaSymbolName methodName = new JavaSymbolName("addInterceptors");
    // Define method parameter types
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    parameterTypes.add(AnnotatedJavaType.convertFromJavaType(new JavaType("org.springframework.web.servlet.config.annotation.InterceptorRegistry")));
    // Define method parameter names
    List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    parameterNames.add(new JavaSymbolName("registry"));
    if (governorHasMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes))) {
        return getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
    }
    // Generate body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    bodyBuilder.appendFormalLine("registry.addInterceptor(localeChangeInterceptor());");
    // Add TracEE interceptor
    bodyBuilder.appendFormalLine("registry.addInterceptor(new %s());", getNameOfJavaType(TRACEE_INTERCEPTOR_JAVATYPE));
    // Use the MethodMetadataBuilder for easy creation of MethodMetadata
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, parameterTypes, parameterNames, bodyBuilder);
    // Add Bean annotation
    methodBuilder.addAnnotation(new AnnotationMetadataBuilder(JavaType.OVERRIDE));
    // Build and return a MethodMetadata
    return methodBuilder.build();
// instance
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) SpringJavaType(org.springframework.roo.model.SpringJavaType) JavaType(org.springframework.roo.model.JavaType) JdkJavaType(org.springframework.roo.model.JdkJavaType) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 94 with JavaSymbolName

use of org.springframework.roo.model.JavaSymbolName 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 95 with JavaSymbolName

use of org.springframework.roo.model.JavaSymbolName in project spring-roo by spring-projects.

the class JspViewManager method addCommonAttributes.

private void addCommonAttributes(final FieldMetadata field, final Element fieldElement) {
    AnnotationMetadata annotationMetadata;
    if (field.getFieldType().equals(INT_OBJECT) || field.getFieldType().getFullyQualifiedTypeName().equals(int.class.getName()) || field.getFieldType().equals(SHORT_OBJECT) || field.getFieldType().getFullyQualifiedTypeName().equals(short.class.getName()) || field.getFieldType().equals(LONG_OBJECT) || field.getFieldType().getFullyQualifiedTypeName().equals(long.class.getName()) || field.getFieldType().equals(BIG_INTEGER)) {
        fieldElement.setAttribute("validationMessageCode", "field_invalid_integer");
    } else if (isEmailField(field)) {
        fieldElement.setAttribute("validationMessageCode", "field_invalid_email");
    } else if (field.getFieldType().equals(DOUBLE_OBJECT) || field.getFieldType().getFullyQualifiedTypeName().equals(double.class.getName()) || field.getFieldType().equals(FLOAT_OBJECT) || field.getFieldType().getFullyQualifiedTypeName().equals(float.class.getName()) || field.getFieldType().equals(BIG_DECIMAL)) {
        fieldElement.setAttribute("validationMessageCode", "field_invalid_number");
    }
    if ("field:input".equals(fieldElement.getTagName()) && null != (annotationMetadata = MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), MIN))) {
        final AnnotationAttributeValue<?> min = annotationMetadata.getAttribute(VALUE);
        if (min != null) {
            fieldElement.setAttribute("min", min.getValue().toString());
            fieldElement.setAttribute("required", "true");
        }
    }
    if ("field:input".equals(fieldElement.getTagName()) && null != (annotationMetadata = MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), MAX)) && !"field:textarea".equals(fieldElement.getTagName())) {
        final AnnotationAttributeValue<?> maxA = annotationMetadata.getAttribute(VALUE);
        if (maxA != null) {
            fieldElement.setAttribute("max", maxA.getValue().toString());
        }
    }
    if ("field:input".equals(fieldElement.getTagName()) && null != (annotationMetadata = MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), DECIMAL_MIN)) && !"field:textarea".equals(fieldElement.getTagName())) {
        final AnnotationAttributeValue<?> decimalMin = annotationMetadata.getAttribute(VALUE);
        if (decimalMin != null) {
            fieldElement.setAttribute("decimalMin", decimalMin.getValue().toString());
            fieldElement.setAttribute("required", "true");
        }
    }
    if ("field:input".equals(fieldElement.getTagName()) && null != (annotationMetadata = MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), DECIMAL_MAX))) {
        final AnnotationAttributeValue<?> decimalMax = annotationMetadata.getAttribute(VALUE);
        if (decimalMax != null) {
            fieldElement.setAttribute("decimalMax", decimalMax.getValue().toString());
        }
    }
    if (null != (annotationMetadata = MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), PATTERN))) {
        final AnnotationAttributeValue<?> regexp = annotationMetadata.getAttribute(new JavaSymbolName("regexp"));
        if (regexp != null) {
            fieldElement.setAttribute("validationRegex", regexp.getValue().toString());
        }
    }
    if ("field:input".equals(fieldElement.getTagName()) && null != (annotationMetadata = MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), SIZE))) {
        final AnnotationAttributeValue<?> max = annotationMetadata.getAttribute(new JavaSymbolName("max"));
        if (max != null) {
            fieldElement.setAttribute("max", max.getValue().toString());
        }
        final AnnotationAttributeValue<?> min = annotationMetadata.getAttribute(new JavaSymbolName("min"));
        if (min != null) {
            fieldElement.setAttribute("min", min.getValue().toString());
            fieldElement.setAttribute("required", "true");
        }
    }
    if (null != (annotationMetadata = MemberFindingUtils.getAnnotationOfType(field.getAnnotations(), NOT_NULL))) {
        final String tagName = fieldElement.getTagName();
        if (tagName.endsWith("textarea") || tagName.endsWith("input") || tagName.endsWith("datetime") || tagName.endsWith("textarea") || tagName.endsWith("select") || tagName.endsWith("reference")) {
            fieldElement.setAttribute("required", "true");
        }
    }
    if (field.getCustomData().keySet().contains(CustomDataKeys.COLUMN_FIELD)) {
        @SuppressWarnings("unchecked") final Map<String, Object> values = (Map<String, Object>) field.getCustomData().get(CustomDataKeys.COLUMN_FIELD);
        if (values.keySet().contains("nullable") && (Boolean) values.get("nullable") == false) {
            fieldElement.setAttribute("required", "true");
        }
    }
    // Disable form binding for nested fields (mainly PKs)
    if (field.getFieldName().getSymbolName().contains(".")) {
        fieldElement.setAttribute("disableFormBinding", "true");
    }
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) Map(java.util.Map) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata)

Aggregations

JavaSymbolName (org.springframework.roo.model.JavaSymbolName)317 ArrayList (java.util.ArrayList)186 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)155 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)143 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)142 JavaType (org.springframework.roo.model.JavaType)133 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)121 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)119 SpringJavaType (org.springframework.roo.model.SpringJavaType)61 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)59 JdkJavaType (org.springframework.roo.model.JdkJavaType)59 FieldMetadataBuilder (org.springframework.roo.classpath.details.FieldMetadataBuilder)48 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)47 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)45 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)39 AnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue)36 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)29 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)29 StringAttributeValue (org.springframework.roo.classpath.details.annotations.StringAttributeValue)27 EnumDetails (org.springframework.roo.model.EnumDetails)25