Search in sources :

Example 26 with JavadocComment

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

the class EqualsMetadata method generateHashCodeMethod.

/**
 * Generate hashCode method
 *
 * @param metadataId
 * @param annotationValues
 * @param locatedFields
 * @param importRegistrationResolver
 * @return
 */
protected static MethodMetadataBuilder generateHashCodeMethod(String metadataId, EqualsAnnotationValues annotationValues, List<FieldMetadata> locatedFields, ImportRegistrationResolver importRegistrationResolver) {
    // Create the method
    final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    StringBuilder builder = null;
    if (annotationValues.isJpaEntity()) {
        builder = getJpaEntityHashCodeMethodReturnStatment();
    } else {
        builder = getDefaultHashCodeMethodReturnStatment(annotationValues, locatedFields, importRegistrationResolver);
    }
    bodyBuilder.appendFormalLine(builder.toString());
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(metadataId, Modifier.PUBLIC, HASH_CODE_METHOD_NAME, INT_PRIMITIVE, bodyBuilder);
    if (annotationValues.isJpaEntity()) {
        CommentStructure commentStructure = new CommentStructure();
        commentStructure.addComment(new JavadocComment("This `hashCode` implementation is specific for JPA entities and uses a fixed `int` value to be able ".concat(IOUtils.LINE_SEPARATOR).concat("to identify the entity in collections after a new id is assigned to the entity, following the article in ").concat(IOUtils.LINE_SEPARATOR).concat("https://vladmihalcea.com/2016/06/06/how-to-implement-equals-and-hashcode-using-the-jpa-entity-identifier/")), CommentLocation.BEGINNING);
        methodBuilder.setCommentStructure(commentStructure);
    }
    return methodBuilder;
}
Also used : JavadocComment(org.springframework.roo.classpath.details.comments.JavadocComment) ToStringBuilder(org.apache.commons.lang3.builder.ToStringBuilder) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) CommentStructure(org.springframework.roo.classpath.details.comments.CommentStructure)

Example 27 with JavadocComment

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

the class JsonControllerIntegrationTestMetadata 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 28 with JavadocComment

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

the class ThymeleafMetadata method getExportMethod.

/**
 * Generates a method to export data using DynamicJasper and arguments
 * received as different export methods will delegate in this one.
 *
 * @return MethodMetadata
 */
private MethodMetadata getExportMethod() {
    JavaSymbolName methodName = EXPORT_METHOD_NAME;
    // Including parameter types
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    parameterTypes.add(GLOBAL_SEARCH_PARAM);
    AnnotationMetadataBuilder pageableDefaultAnnotation = new AnnotationMetadataBuilder(SpringJavaType.PAGEABLE_DEFAULT);
    pageableDefaultAnnotation.addIntegerAttribute("size", Integer.MAX_VALUE);
    parameterTypes.add(new AnnotatedJavaType(SpringJavaType.PAGEABLE, pageableDefaultAnnotation.build()));
    parameterTypes.add(STRING_ARRAY_PARAM);
    parameterTypes.add(new AnnotatedJavaType(new JavaType("javax.servlet.http.HttpServletResponse")));
    parameterTypes.add(new AnnotatedJavaType(this.jasperReportsMap.get("JasperReportsExporter")));
    parameterTypes.add(STRING_PARAM);
    parameterTypes.add(LOCALE_PARAM);
    MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
    if (existingMethod != null) {
        return existingMethod;
    }
    final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    parameterNames.add(GLOBAL_SEARCH_PARAM_NAME);
    parameterNames.add(PAGEABLE_PARAM_NAME);
    parameterNames.add(DATATABLES_COLUMNS_PARAM_NAME);
    parameterNames.add(RESPONSE_PARAM_NAME);
    parameterNames.add(EXPORTER_PARAM_NAME);
    parameterNames.add(FILE_NAME_PARAM_NAME);
    parameterNames.add(LOCALE_PARAM_NAME);
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // Get findAll method
    MethodMetadata findAllMethod = this.serviceMetadata.getCurrentFindAllWithGlobalSearchMethod();
    // Getting the default return type
    JavaType defaultReturnType = findAllMethod.getReturnType();
    // Obtain the filtered and ordered elements
    // Page<Owner> owners = ownerService.findAll(search, pageable);
    bodyBuilder.appendFormalLine("// Obtain the filtered and ordered elements");
    bodyBuilder.appendFormalLine("%s %s = %s().%s(%s, %s);", getNameOfJavaType(defaultReturnType), this.entityPluralUncapitalized, getAccessorMethod(this.controllerMetadata.getServiceField()).getMethodName(), findAllMethod.getMethodName().getSymbolName(), GLOBAL_SEARCH_PARAM_NAME, PAGEABLE_PARAM_NAME);
    bodyBuilder.newLine();
    // // Prevent generation of reports with empty data
    bodyBuilder.appendFormalLine("// Prevent generation of reports with empty data");
    // if (owners == null || owners.getContent().isEmpty()) {
    bodyBuilder.appendFormalLine("if (%1$s == null || %s.getContent().isEmpty()) {", this.entityPluralUncapitalized);
    // return;
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine("return;");
    // }
    bodyBuilder.indentRemove();
    bodyBuilder.appendFormalLine("}");
    bodyBuilder.newLine();
    // // Creates a new ReportBuilder using DynamicJasper library
    bodyBuilder.appendFormalLine("// Creates a new ReportBuilder using DynamicJasper library");
    // FastReportBuilder builder = new FastReportBuilder();
    bodyBuilder.appendFormalLine("%1$s builder = new %1$s();", getNameOfJavaType(FAST_REPORT_BUILDER));
    bodyBuilder.newLine();
    // // IMPORTANT: By default, this application uses
    // "export_default.jrxml"
    bodyBuilder.appendFormalLine("// IMPORTANT: By default, this application uses \"export_default.jrxml\"");
    // // to generate all reports. If you want to customize this specific
    // report,
    bodyBuilder.appendFormalLine("// to generate all reports. If you want to customize this specific report,");
    // create a new ".jrxml" template and customize it. Take in account the
    bodyBuilder.appendFormalLine("// create a new \".jrxml\" template and customize it. (Take in account the ");
    // DynamicJasper restrictions:
    bodyBuilder.appendFormalLine("// DynamicJasper restrictions: ");
    // http://dynamicjasper.com/2010/10/06/how-to-use-custom-jrxml-templates/)
    bodyBuilder.appendFormalLine("// http://dynamicjasper.com/2010/10/06/how-to-use-custom-jrxml-templates/)");
    // builder.setTemplateFile("templates/reports/export_default.jrxml");
    bodyBuilder.appendFormalLine("builder.setTemplateFile(\"templates/reports/export_default.jrxml\");");
    bodyBuilder.newLine();
    // // The generated report will display the same columns as the
    // Datatables component.
    bodyBuilder.appendFormalLine("// The generated report will display the same columns as the Datatables component.");
    // // However, this is not mandatory. You could edit this code if you
    // want to ignore
    bodyBuilder.appendFormalLine("// However, this is not mandatory. You could edit this code if you want to ignore");
    // // the provided datatablesColumns
    bodyBuilder.appendFormalLine("// the provided datatablesColumns");
    // if (datatablesColumns != null) {
    bodyBuilder.appendFormalLine("if (%s != null) {", DATATABLES_COLUMNS_PARAM_NAME);
    bodyBuilder.indent();
    // for (String column : datatablesColumns) {
    bodyBuilder.appendFormalLine("for (String column : %s) {", DATATABLES_COLUMNS_PARAM_NAME);
    bodyBuilder.indent();
    // // Delegates in addColumnToReportBuilder to include each datatables
    // column
    bodyBuilder.appendFormalLine("// Delegates in %s to include each datatables column", ADD_COLUMN_TO_REPORT_BUILDER_METHOD_NAME);
    // // to the report builder
    bodyBuilder.appendFormalLine("// to the report builder");
    // addColumnToReportBuilder(column, builder, locale);
    bodyBuilder.appendFormalLine("addColumnToReportBuilder(column, builder, %s, %s);", LOCALE_PARAM_NAME, FILE_NAME_PARAM_NAME);
    bodyBuilder.indentRemove();
    // }
    bodyBuilder.appendFormalLine("}");
    bodyBuilder.indentRemove();
    // }
    bodyBuilder.appendFormalLine("}");
    bodyBuilder.newLine();
    // // This property resizes the columns to use full width page.
    bodyBuilder.appendFormalLine("// This property resizes the columns to use full width page.");
    // // Set false value if you want to use the specific width of each
    // column.
    bodyBuilder.appendFormalLine("// Set false value if you want to use the specific width of each column.");
    // builder.setUseFullPageWidth(true);
    bodyBuilder.appendFormalLine("builder.setUseFullPageWidth(true);");
    bodyBuilder.newLine();
    // // Creates a new Jasper Reports Datasource using the obtained
    // elements
    bodyBuilder.appendFormalLine("// Creates a new Jasper Reports Datasource using the obtained elements");
    // JRDataSource ds = new
    // JRBeanCollectionDataSource(owners.getContent());
    bodyBuilder.appendFormalLine("%s ds = new %s(%s.getContent());", getNameOfJavaType(JR_DATA_SOURCE), getNameOfJavaType(JR_BEAN_COLLECTION_DATA_SOURCE), this.entityPluralUncapitalized);
    bodyBuilder.newLine();
    // // Generates the JasperReport
    bodyBuilder.appendFormalLine("// Generates the JasperReport");
    // JasperPrint jp;
    bodyBuilder.appendFormalLine("%s jp;", getNameOfJavaType(JASPER_PRINT));
    // try {
    bodyBuilder.appendFormalLine("try {");
    bodyBuilder.indent();
    // jp = DynamicJasperHelper.generateJasperPrint(builder.build(), new
    // ClassicLayoutManager(), ds);
    bodyBuilder.appendFormalLine("jp = %s.generateJasperPrint(builder.build(), new %s(), ds);", getNameOfJavaType(DYNAMIC_JASPER_HELPER), getNameOfJavaType(CLASSIC_LAYOUT_MANAGER));
    bodyBuilder.indentRemove();
    // "}"
    bodyBuilder.appendFormalLine("}");
    // Build catch block
    buildExportCatchBlock(bodyBuilder, JR_EXCEPTION);
    bodyBuilder.newLine();
    // // Converts the JaspertReport element to a ByteArrayOutputStream and
    bodyBuilder.appendFormalLine("// Converts the JaspertReport element to a ByteArrayOutputStream and");
    // // write it into the response stream using the provided
    // JasperReportExporter
    bodyBuilder.appendFormalLine("// write it into the response stream using the provided JasperReportExporter");
    bodyBuilder.appendFormalLine("try {");
    bodyBuilder.indent();
    // exporter.export(jp, fileName, response);
    bodyBuilder.appendFormalLine("%s.export(jp, %s, %s);", EXPORTER_PARAM_NAME, FILE_NAME_PARAM_NAME, RESPONSE_PARAM_NAME);
    bodyBuilder.indentRemove();
    // "}"
    bodyBuilder.appendFormalLine("}");
    buildExportCatchBlock(bodyBuilder, JR_EXCEPTION);
    buildExportCatchBlock(bodyBuilder, IO_EXCEPTION);
    bodyBuilder.reset();
    // Build method
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, parameterTypes, parameterNames, bodyBuilder);
    // Add Javadoc to method
    CommentStructure commentStructure = new CommentStructure();
    String description = "Method that obtains the filtered and ordered records using the Datatables information and ".concat(IOUtils.LINE_SEPARATOR).concat("export them to a new report file. (It ignores the current pagination).").concat(IOUtils.LINE_SEPARATOR).concat(IOUtils.LINE_SEPARATOR).concat("To generate the report file it uses the `DynamicJasper` library").concat(IOUtils.LINE_SEPARATOR).concat("(http://dynamicjasper.com). This library allows developers to generate reports dynamically").concat(IOUtils.LINE_SEPARATOR).concat("without use an specific template to each entity.").concat(IOUtils.LINE_SEPARATOR).concat(IOUtils.LINE_SEPARATOR).concat("To customize the appearance of ALL generated reports, you could customize the ").concat(IOUtils.LINE_SEPARATOR).concat("\"export_default.jrxml\" template located in \"src/main/resources/templates/reports/\". However,").concat(IOUtils.LINE_SEPARATOR).concat("if you want to customize the appearance of this specific report, you could create a new").concat(IOUtils.LINE_SEPARATOR).concat("\".jrxml\" file and provide it to the library replacing the `builder.setTemplateFile();`").concat(IOUtils.LINE_SEPARATOR).concat("operation used in this implementation.");
    // Create params info
    List<String> paramsInfo = new ArrayList<String>();
    paramsInfo.add("search GlobalSearch that contains the filter provided by the Datatables component.");
    paramsInfo.add("pageable Pageable that contains the Sort info provided by the Datatabes component.");
    paramsInfo.add("datatablesColumns Columns displayed in the Datatables component.");
    paramsInfo.add("response The HttpServletResponse.");
    paramsInfo.add("exporter An specific JasperReportsExporter to be used during export process.");
    paramsInfo.add("fileName The final filename to use.");
    paramsInfo.add("locale The current Locale in the view context.");
    // Add JavadocComment to CommentStructure and method
    commentStructure.addComment(new JavadocComment(description, paramsInfo, null, null), CommentLocation.BEGINNING);
    methodBuilder.setCommentStructure(commentStructure);
    return methodBuilder.build();
}
Also used : AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) CommentStructure(org.springframework.roo.classpath.details.comments.CommentStructure) 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) 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)

Aggregations

CommentStructure (org.springframework.roo.classpath.details.comments.CommentStructure)28 JavadocComment (org.springframework.roo.classpath.details.comments.JavadocComment)28 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)23 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)16 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)14 ArrayList (java.util.ArrayList)13 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)12 JavaType (org.springframework.roo.model.JavaType)12 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)11 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)10 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)7 JdkJavaType (org.springframework.roo.model.JdkJavaType)7 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)6 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)6 FieldMetadataBuilder (org.springframework.roo.classpath.details.FieldMetadataBuilder)5 BodyDeclaration (com.github.antlrjavaparser.api.body.BodyDeclaration)4 TypeDeclaration (com.github.antlrjavaparser.api.body.TypeDeclaration)4 AnnotationExpr (com.github.antlrjavaparser.api.expr.AnnotationExpr)4 ClassOrInterfaceType (com.github.antlrjavaparser.api.type.ClassOrInterfaceType)4 ParseException (com.github.antlrjavaparser.ParseException)3