use of org.springframework.roo.classpath.details.MethodMetadataBuilder in project spring-roo by spring-projects.
the class ThymeleafMetadata method buildExportTypeMethod.
/**
* Builds export (TYPE) method, which is similar in all export target types.
*
* @param exporterClassInstantiation
* the String with the instantiation of the JasperReports support
* class.
* @param fileName
* the String with the output file name.
* @param methodName
* the JavaSymbolName with the method name.
* @return MethodMetadata
*/
private MethodMetadata buildExportTypeMethod(final String exporterClassInstantiation, final String fileName, final JavaSymbolName methodName, final String getMappingAnnotatinName, final String getMappingAnnotationValue, final String fileType) {
// 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()));
AnnotationMetadataBuilder requestParamAnnotation = new AnnotationMetadataBuilder(SpringJavaType.REQUEST_PARAM);
requestParamAnnotation.addStringAttribute("value", DATATABLES_COLUMNS_PARAM_NAME.getSymbolName());
parameterTypes.add(new AnnotatedJavaType(JavaType.STRING_ARRAY, requestParamAnnotation.build()));
parameterTypes.add(new AnnotatedJavaType(new JavaType("javax.servlet.http.HttpServletResponse")));
parameterTypes.add(LOCALE_PARAM);
MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
if (existingMethod != null) {
return existingMethod;
}
// Including parameter names
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(LOCALE_PARAM_NAME);
// Adding annotations
final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
AnnotationMetadataBuilder getMappingBuilder = new AnnotationMetadataBuilder(GET_MAPPING);
getMappingBuilder.addStringAttribute("name", getMappingAnnotatinName);
getMappingBuilder.addStringAttribute("value", getMappingAnnotationValue);
annotations.add(getMappingBuilder);
annotations.add(RESPONSE_BODY_ANNOTATION);
// Add throws types
final List<JavaType> throwTypes = new ArrayList<JavaType>();
throwTypes.add(JR_EXCEPTION);
throwTypes.add(IO_EXCEPTION);
throwTypes.add(COLUMN_BUILDER_EXCEPTION);
throwTypes.add(CLASS_NOT_FOUND_EXCEPTION);
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// export(search, pageable, datatablesColumns, response, new
// JasperReportsCsvExporter(), "ENTITY-ITEM_report.csv");
bodyBuilder.appendFormalLine("export(%s, %s, %s, %s, %s, \"%s\", %s);", GLOBAL_SEARCH_PARAM_NAME, PAGEABLE_PARAM_NAME, DATATABLES_COLUMNS_PARAM_NAME, RESPONSE_PARAM_NAME, exporterClassInstantiation, fileName, LOCALE_PARAM_NAME.getSymbolName());
// return ResponseEntity.ok().build();
bodyBuilder.appendFormalLine("return %s.ok().build();", getNameOfJavaType(RESPONSE_ENTITY));
// Build method
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.wrapperWilcard(RESPONSE_ENTITY), parameterTypes, parameterNames, bodyBuilder);
methodBuilder.setAnnotations(annotations);
// Add JavaDoc
CommentStructure commentStructure = new CommentStructure();
String description = "It delegates in the `export` method providing the necessary information".concat(IOUtils.LINE_SEPARATOR).concat(String.format("to generate a %s report.", fileType));
// Add params info to commment block
List<String> paramsInfo = new ArrayList<String>();
paramsInfo.add("search The GlobalSearch that contains the filter provided by the Datatables component");
paramsInfo.add("pageable The Pageable that contains the Sort info provided by the Datatabes component");
paramsInfo.add("datatablesColumns The Columns displayed in the Datatables component");
paramsInfo.add("response The HttpServletResponse");
// Add JavadocComment to CommentStructure and to method
commentStructure.addComment(new JavadocComment(description, paramsInfo, null, null), CommentLocation.BEGINNING);
methodBuilder.setCommentStructure(commentStructure);
return methodBuilder.build();
}
use of org.springframework.roo.classpath.details.MethodMetadataBuilder in project spring-roo by spring-projects.
the class JSONMetadata method getShowMethod.
/**
* This method provides the "show" method using JSON response type
*
* @return MethodMetadata
*/
private MethodMetadata getShowMethod() {
// Define methodName
final JavaSymbolName methodName = new JavaSymbolName("show");
// Define parameters
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
parameterTypes.add(new AnnotatedJavaType(entity, ANN_MODEL_ATTRIBUTE));
final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
parameterNames.add(new JavaSymbolName(entityItemName));
MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
if (existingMethod != null) {
return existingMethod;
}
// Adding annotations
final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
// Adding @GetMapping annotation
AnnotationMetadataBuilder getMappingAnnotation = new AnnotationMetadataBuilder(GET_MAPPING);
getMappingAnnotation.addStringAttribute("name", methodName.getSymbolName());
annotations.add(getMappingAnnotation);
// Generate body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// return ResponseEntity.ok(customer);
bodyBuilder.appendFormalLine("return %s.ok(%s);", getNameOfJavaType(RESPONSE_ENTITY), entityItemName);
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.wrapperWilcard(RESPONSE_ENTITY), parameterTypes, parameterNames, bodyBuilder);
methodBuilder.setAnnotations(annotations);
return methodBuilder.build();
}
use of org.springframework.roo.classpath.details.MethodMetadataBuilder in project spring-roo by spring-projects.
the class JSONMetadata method getShowURIMethod.
/**
* Creates showURI method
*
* @return
*/
private MethodMetadata getShowURIMethod() {
// Define method parameter types and parameter names
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
parameterTypes.add(AnnotatedJavaType.convertFromJavaType(entity));
List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
parameterNames.add(new JavaSymbolName(entityItemName));
MethodMetadata existingMethod = getGovernorMethod(SHOW_URI_METHOD_NAME, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
if (existingMethod != null) {
return existingMethod;
}
// return MvcUriComponentsBuilder
// .fromMethodCall(
// MvcUriComponentsBuilder.on(CustomersItemJsonController.class).show(customer))
// .buildAndExpand(customer.getId()).encode();
InvocableMemberBodyBuilder body = new InvocableMemberBodyBuilder();
body.appendFormalLine("return %s", getNameOfJavaType(SpringJavaType.MVC_URI_COMPONENTS_BUILDER));
body.indent();
body.appendFormalLine(".fromMethodCall(");
body.indent();
body.appendFormalLine("%s.on(%s.class).%s(%s))", getNameOfJavaType(SpringJavaType.MVC_URI_COMPONENTS_BUILDER), getNameOfJavaType(getDestination()), this.showMethod.getMethodName(), entityItemName);
body.indentRemove();
body.appendFormalLine(".buildAndExpand(%s.get%s()).encode();", entityItemName, StringUtils.capitalize(entityIdentifier));
body.indentRemove();
// Use the MethodMetadataBuilder for easy creation of MethodMetadata
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC + Modifier.STATIC, SHOW_URI_METHOD_NAME, SpringJavaType.URI_COMPONENTS, parameterTypes, parameterNames, body);
return methodBuilder.build();
}
use of org.springframework.roo.classpath.details.MethodMetadataBuilder in project spring-roo by spring-projects.
the class JSONMetadata method getUpdateBatchMethod.
/**
* Creates update batch method
*
* @return {@link MethodMetadata}
*/
private MethodMetadata getUpdateBatchMethod() {
// Define methodName
final JavaSymbolName methodName = new JavaSymbolName("updateBatch");
// Adding parameter types
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
parameterTypes.add(new AnnotatedJavaType(JavaType.collectionOf(this.entity), ANN_METADATA_VALID, ANN_METADATA_REQUEST_BODY));
parameterTypes.add(new AnnotatedJavaType(SpringJavaType.BINDING_RESULT));
MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
if (existingMethod != null) {
return existingMethod;
}
// Adding parameter names
final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
parameterNames.add(new JavaSymbolName(StringUtils.uncapitalize(this.entityPlural)));
parameterNames.add(new JavaSymbolName("result"));
// Adding annotations
final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
// Adding @PutMapping annotation
AnnotationMetadataBuilder putMappingAnnotation = new AnnotationMetadataBuilder(PUT_MAPPING);
putMappingAnnotation.addStringAttribute("value", "/batch");
putMappingAnnotation.addStringAttribute("name", methodName.getSymbolName());
annotations.add(putMappingAnnotation);
// Generate body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// if (result.hasErrors()) {
// return ResponseEntity.status(HttpStatus.CONFLICT).body(result);
// }
bodyBuilder.newLine();
bodyBuilder.appendFormalLine("if (result.hasErrors()) {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine("return %s.status(%s.CONFLICT).body(result);", getNameOfJavaType(RESPONSE_ENTITY), getNameOfJavaType(SpringJavaType.HTTP_STATUS));
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
// customerService.save(customers);
bodyBuilder.newLine();
bodyBuilder.appendFormalLine("%s().%s(%s);", getAccessorMethod(controllerMetadata.getServiceField()).getMethodName(), serviceMetadata.getCurrentSaveBatchMethod().getMethodName(), StringUtils.uncapitalize(this.entityPlural));
// return ResponseEntity.ok().build();
bodyBuilder.newLine();
bodyBuilder.appendFormalLine("return %s.ok().build();", getNameOfJavaType(RESPONSE_ENTITY));
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.wrapperWilcard(RESPONSE_ENTITY), parameterTypes, parameterNames, bodyBuilder);
methodBuilder.setAnnotations(annotations);
return methodBuilder.build();
}
use of org.springframework.roo.classpath.details.MethodMetadataBuilder in project spring-roo by spring-projects.
the class JSONMetadata method getCreateMethod.
/**
* This method provides the "create" method using JSON response type
*
* @param serviceSaveMethod
*
* @return MethodMetadata
*/
private MethodMetadata getCreateMethod() {
// Define methodName
final JavaSymbolName methodName = new JavaSymbolName("create");
// Adding parameter types
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
parameterTypes.add(new AnnotatedJavaType(this.entity, ANN_METADATA_VALID, ANN_METADATA_REQUEST_BODY));
parameterTypes.add(new AnnotatedJavaType(SpringJavaType.BINDING_RESULT));
MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
if (existingMethod != null) {
return existingMethod;
}
// Adding parameter names
final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
parameterNames.add(new JavaSymbolName(entityItemName));
parameterNames.add(new JavaSymbolName("result"));
// Adding annotations
final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
// Adding @PostMapping annotation
AnnotationMetadataBuilder postMappingAnnotation = new AnnotationMetadataBuilder(POST_MAPPING);
postMappingAnnotation.addStringAttribute("name", methodName.getSymbolName());
annotations.add(postMappingAnnotation);
// Generate body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// Concurrency control
if (entityMetadata.getCurrentVersionField() != null) {
// if (customer.getId() != null || customer.getVersion() != null
bodyBuilder.newLine();
bodyBuilder.appendIndent();
bodyBuilder.append("if (%s || %s", createNullExpression(entityMetadata.getCurrentIndentifierField()), createNullExpression(entityMetadata.getCurrentVersionField()));
if (compositionRelationOneToOne.isEmpty()) {
bodyBuilder.append(") {");
bodyBuilder.newLine();
} else {
bodyBuilder.indent();
bodyBuilder.indent();
for (Pair<RelationInfo, JpaEntityMetadata> item : compositionRelationOneToOne) {
JavaSymbolName versionFieldName = item.getRight().getCurrentVersionField().getFieldName();
JavaSymbolName idFieldName = item.getRight().getCurrentIndentifierField().getFieldName();
JavaSymbolName relationFieldName = item.getKey().fieldMetadata.getFieldName();
// || (customer.getAddress() != null && (customer.getAddress().getId() != null || customer.getAddress().getVersion() != null))
bodyBuilder.newLine();
bodyBuilder.appendIndent();
bodyBuilder.append("|| ( ");
bodyBuilder.append("%1$s.get%2$s() != null && (%1$s.get%2$s().get%3$s() != null || %1$s.get%2$s().get%4$s() != null)", entityItemName, relationFieldName.getSymbolNameCapitalisedFirstLetter(), idFieldName.getSymbolNameCapitalisedFirstLetter(), versionFieldName.getSymbolNameCapitalisedFirstLetter());
bodyBuilder.append(")");
}
bodyBuilder.append(") {");
bodyBuilder.newLine();
bodyBuilder.indentRemove();
bodyBuilder.indentRemove();
}
bodyBuilder.indent();
// return ResponseEntity.status(HttpStatus.CONFLICT).build();
bodyBuilder.appendFormalLine("return %s.status(%s.CONFLICT).build();", getNameOfJavaType(RESPONSE_ENTITY), getNameOfJavaType(SpringJavaType.HTTP_STATUS));
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
} else {
// if (customer.getId() != null)
bodyBuilder.newLine();
bodyBuilder.appendIndent();
bodyBuilder.append("if (%s", createNullExpression(entityMetadata.getCurrentIndentifierField()));
if (compositionRelationOneToOne.isEmpty()) {
bodyBuilder.append(") {");
bodyBuilder.newLine();
} else {
bodyBuilder.indent();
bodyBuilder.indent();
for (Pair<RelationInfo, JpaEntityMetadata> item : compositionRelationOneToOne) {
JavaSymbolName idFieldName = item.getRight().getCurrentIndentifierField().getFieldName();
JavaSymbolName relationFieldName = item.getKey().fieldMetadata.getFieldName();
// || (customer.getAddress() != null && (customer.getAddress().getId() != null || customer.getAddress().getVersion() != null))
bodyBuilder.newLine();
bodyBuilder.appendIndent();
bodyBuilder.append("|| ( ");
bodyBuilder.append("%1$s.get%2$s() != null && %1$s.get%2$s().get%3$s() != null", entityItemName, relationFieldName.getSymbolNameCapitalisedFirstLetter(), idFieldName.getSymbolNameCapitalisedFirstLetter());
bodyBuilder.append(")");
}
bodyBuilder.append(") {");
bodyBuilder.newLine();
bodyBuilder.indentRemove();
bodyBuilder.indentRemove();
}
bodyBuilder.indent();
// return ResponseEntity.status(HttpStatus.CONFLICT).build();
bodyBuilder.appendFormalLine("return %s.status(%s.CONFLICT).build();", getNameOfJavaType(RESPONSE_ENTITY), getNameOfJavaType(SpringJavaType.HTTP_STATUS));
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
}
// if (result.hasErrors()) {
// return ResponseEntity.status(HttpStatus.CONFLICT).body(result);
// }
bodyBuilder.newLine();
bodyBuilder.appendFormalLine("if (result.hasErrors()) {");
bodyBuilder.indent();
bodyBuilder.appendFormalLine("return %s.status(%s.CONFLICT).body(result);", getNameOfJavaType(RESPONSE_ENTITY), getNameOfJavaType(SpringJavaType.HTTP_STATUS));
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
// Entity newEntity = entityService.saveMethodName(entity);
bodyBuilder.newLine();
bodyBuilder.appendFormalLine("%s new%s = %s().%s(%s);", getNameOfJavaType(entity), StringUtils.capitalize(entityItemName), getAccessorMethod(controllerMetadata.getServiceField()).getMethodName(), serviceMetadata.getCurrentSaveMethod().getMethodName(), entityItemName);
// UriComponents showURI = CustomersItemJsonController.showURI(newCustomer);
bodyBuilder.appendFormalLine("%s showURI = %s.%s(new%s);", getNameOfJavaType(SpringJavaType.URI_COMPONENTS), getNameOfJavaType(itemController), SHOW_URI_METHOD_NAME, StringUtils.capitalize(entityItemName));
// return ResponseEntity.created(showURI.toUri()).build();
bodyBuilder.newLine();
bodyBuilder.appendFormalLine("return %s.created(showURI.toUri()).build();", getNameOfJavaType(RESPONSE_ENTITY));
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.wrapperWilcard(RESPONSE_ENTITY), parameterTypes, parameterNames, bodyBuilder);
methodBuilder.setAnnotations(annotations);
return methodBuilder.build();
}
Aggregations