use of org.springframework.roo.classpath.details.comments.CommentStructure in project spring-roo by spring-projects.
the class ItdSourceFileComposer method appendFields.
private void appendFields() {
final List<? extends FieldMetadata> fields = itdTypeDetails.getDeclaredFields();
if (fields == null || fields.isEmpty()) {
return;
}
content = true;
for (final FieldMetadata field : fields) {
// ROO-3447: Append comments if exists
CommentStructure commentStructure = field.getCommentStructure();
if (commentStructure != null && commentStructure.getBeginComments() != null) {
List<AbstractComment> comments = commentStructure.getBeginComments();
String commentString = "";
for (AbstractComment comment : comments) {
// Join all JavadocComment's
if (comment instanceof JavadocComment) {
commentString = commentString.concat(comment.getComment()).concat(IOUtils.LINE_SEPARATOR);
} else {
// Not JavadocComment, write comment as it is, unchanged
appendFormalLine(comment.getComment());
}
}
// Write JavadocComment's to ITD, in a single JavadocComment instance
String[] commentLines = commentString.split(IOUtils.LINE_SEPARATOR);
for (String commentLine : commentLines) {
appendFormalLine(commentLine);
}
} else {
// ROO-3834: Append default Javadoc if not exists a comment structure
JavadocComment javadocComment = new JavadocComment("TODO Auto-generated attribute documentation");
// Now lines should be formatted, so write them
String[] comment = javadocComment.getComment().split(IOUtils.LINE_SEPARATOR);
for (String line : comment) {
appendFormalLine(line);
}
}
// Append annotations
for (final AnnotationMetadata annotation : field.getAnnotations()) {
appendIndent();
outputAnnotation(annotation);
this.newLine(false);
}
// Append "<modifier> <fieldType> <fieldName>" portion
appendIndent();
if (field.getModifier() != 0) {
append(Modifier.toString(field.getModifier()));
append(" ");
}
append(field.getFieldType().getNameIncludingTypeParameters(false, resolver));
append(" ");
append(introductionTo.getSimpleTypeName());
append(".");
append(field.getFieldName().getSymbolName());
// Append initializer, if present
if (field.getFieldInitializer() != null) {
append(" = ");
append(field.getFieldInitializer());
}
// Complete the field declaration
append(";");
this.newLine(false);
this.newLine();
}
}
use of org.springframework.roo.classpath.details.comments.CommentStructure 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;
}
use of org.springframework.roo.classpath.details.comments.CommentStructure 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();
}
use of org.springframework.roo.classpath.details.comments.CommentStructure 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();
}
Aggregations