use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.
the class ThymeleafMetadata method getRemoveFromDetailsBatchMethod.
private MethodMetadata getRemoveFromDetailsBatchMethod() {
RelationInfoExtended detailsInfo = controllerMetadata.getLastDetailsInfo();
final ServiceMetadata detailsServiceMetadata = controllerMetadata.getServiceMetadataForEntity(detailsInfo.entityType);
final MethodMetadata removeFromMethod = detailsServiceMetadata.getRemoveFromRelationMethods().get(detailsInfo);
final FieldMetadata detailsServiceField = controllerMetadata.getDetailsServiceFields(detailsInfo.entityType);
// Define methodName
final String methodName = removeFromMethod.getMethodName().getSymbolName().concat("Batch");
JavaSymbolName itemsName = removeFromMethod.getParameterNames().get(1);
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
parameterTypes.add(new AnnotatedJavaType(removeFromMethod.getParameterTypes().get(0).getJavaType(), AnnotationMetadataBuilder.getInstance(SpringJavaType.MODEL_ATTRIBUTE)));
AnnotationMetadataBuilder pathVariableAnnotation = new AnnotationMetadataBuilder(SpringJavaType.PATH_VARIABLE);
pathVariableAnnotation.addStringAttribute("value", itemsName.getSymbolName());
parameterTypes.add(new AnnotatedJavaType(JavaType.collectionOf(removeFromMethod.getParameterTypes().get(1).getJavaType().getParameters().get(0)), pathVariableAnnotation.build()));
MethodMetadata existingMethod = getGovernorMethod(new JavaSymbolName(methodName), AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
if (existingMethod != null) {
return existingMethod;
}
// Adding annotations
final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
// Adding @DeleteMapping annotation
AnnotationMetadataBuilder postMappingAnnotation = new AnnotationMetadataBuilder(SpringJavaType.DELETE_MAPPING);
postMappingAnnotation.addStringAttribute("name", methodName);
postMappingAnnotation.addStringAttribute("value", "/batch/{" + itemsName.getSymbolName() + "}");
annotations.add(postMappingAnnotation);
this.mvcMethodNames.put(methodName, methodName);
annotations.add(new AnnotationMetadataBuilder(SpringJavaType.RESPONSE_BODY));
final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
parameterNames.addAll(removeFromMethod.getParameterNames());
// Generate body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// customerService.removeFromOrders(customer, ordersToRemove);
bodyBuilder.appendFormalLine("%s().%s(%s, %s);", getAccessorMethod(detailsServiceField).getMethodName(), removeFromMethod.getMethodName(), removeFromMethod.getParameterNames().get(0), itemsName);
// return ResponseEntity.ok().build();
bodyBuilder.appendFormalLine("return %s.ok().build();", getNameOfJavaType(RESPONSE_ENTITY));
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, new JavaSymbolName(methodName), JavaType.wrapperWilcard(RESPONSE_ENTITY), parameterTypes, parameterNames, bodyBuilder);
methodBuilder.setAnnotations(annotations);
return methodBuilder.build();
}
use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.
the class ThymeleafMetadata method getInitBinderMethod.
/**
* Creates getInitBinderMethod method
*
* @return
*/
private MethodMetadata getInitBinderMethod(JavaType entity) {
// Define methodName
final JavaSymbolName methodName = new JavaSymbolName("init" + entity.getSimpleTypeName() + "Binder");
// Define method parameter types and parameter names
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
parameterTypes.add(AnnotatedJavaType.convertFromJavaType(SpringJavaType.WEB_DATA_BINDER));
List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
parameterNames.add(new JavaSymbolName("dataBinder"));
MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
if (existingMethod != null) {
return existingMethod;
}
InvocableMemberBodyBuilder body = new InvocableMemberBodyBuilder();
// dataBinder.setDisallowedFields("id");
body.appendFormalLine("dataBinder.setDisallowedFields(\"%s\");", entityIdentifier);
for (Pair<RelationInfo, JpaEntityMetadata> item : compositionRelationOneToOne) {
body.appendFormalLine("dataBinder.setDisallowedFields(\"%s.%s\");", item.getKey().fieldName, item.getValue().getCurrentIndentifierField().getFieldName());
}
// Use the MethodMetadataBuilder for easy creation of MethodMetadata
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, parameterTypes, parameterNames, body);
// Adding annotation
final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
// Adding @InitBinder annotation
AnnotationMetadataBuilder getInitBinderAnnotation = new AnnotationMetadataBuilder(INIT_BINDER);
getInitBinderAnnotation.addStringAttribute("value", StringUtils.uncapitalize(entity.getSimpleTypeName()));
annotations.add(getInitBinderAnnotation);
methodBuilder.setAnnotations(annotations);
return methodBuilder.build();
}
use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.
the class ThymeleafMetadata method getUpdateMethod.
/**
* This method provides the "update" method using Thymeleaf view response
* type
*
* @return MethodMetadata
*/
private MethodMetadata getUpdateMethod() {
// Define methodName
final JavaSymbolName methodName = UPDATE_METHOD_NAME;
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
parameterTypes.add(new AnnotatedJavaType(this.entity, ANN_METADATA_VALID, ANN_METADATA_MODEL_ATTRIBUTE));
parameterTypes.add(new AnnotatedJavaType(SpringJavaType.BINDING_RESULT));
// Version parameter
if (this.entityMetadata.getCurrentVersionField() != null) {
AnnotationMetadataBuilder requestParamAnnotation = new AnnotationMetadataBuilder(REQUEST_PARAM);
requestParamAnnotation.addStringAttribute("value", VERSION_PARAM_NAME.getSymbolName());
parameterTypes.add(new AnnotatedJavaType(this.entityMetadata.getCurrentVersionField().getFieldType(), requestParamAnnotation.build()));
// Concurrency control parameter
AnnotationMetadataBuilder concurrencyControlRequestParam = new AnnotationMetadataBuilder(REQUEST_PARAM);
concurrencyControlRequestParam.addStringAttribute("value", "concurrency");
concurrencyControlRequestParam.addBooleanAttribute("required", false);
concurrencyControlRequestParam.addStringAttribute("defaultValue", "");
parameterTypes.add(new AnnotatedJavaType(JavaType.STRING, concurrencyControlRequestParam.build()));
}
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(new JavaSymbolName(entityItemName));
parameterNames.add(new JavaSymbolName("result"));
if (this.entityMetadata.getCurrentVersionField() != null) {
parameterNames.add(this.entityMetadata.getCurrentVersionField().getFieldName());
parameterNames.add(new JavaSymbolName("concurrencyControl"));
}
parameterNames.add(MODEL_PARAM_NAME);
// Adding annotations
final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
// Adding @PutMapping annotation
AnnotationMetadataBuilder putMappingAnnotation = new AnnotationMetadataBuilder(SpringJavaType.PUT_MAPPING);
putMappingAnnotation.addStringAttribute("name", methodName.getSymbolName());
annotations.add(putMappingAnnotation);
this.mvcMethodNames.put(methodName.getSymbolName(), methodName.getSymbolName());
// Generate body
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// // Check if provided form contain errors
bodyBuilder.appendFormalLine("// Check if provided form contain errors");
// if (result.hasErrors()) {
bodyBuilder.appendFormalLine("if (result.hasErrors()) {");
bodyBuilder.indent();
// populateFormats(model);
bodyBuilder.appendFormalLine("%s(model);", populateFormMethod.getMethodName());
bodyBuilder.newLine();
// return "path/create";
bodyBuilder.appendFormalLine("return new %s(\"%s/edit\");", getNameOfJavaType(SpringJavaType.MODEL_AND_VIEW), viewsPath);
bodyBuilder.indentRemove();
// }
bodyBuilder.appendFormalLine("}");
// // Concurrency control
if (this.entityMetadata.getCurrentVersionField() != null) {
bodyBuilder.appendFormalLine("// Concurrency control");
// Pet existingPet = getPetService().findOne(pet.getId());
String existingVarName = "existing".concat(entity.getSimpleTypeName());
bodyBuilder.appendFormalLine("%s %s = %s().%s(%s.%s());", getNameOfJavaType(entity), existingVarName, getAccessorMethod(controllerMetadata.getServiceField()).getMethodName(), serviceMetadata.getCurrentFindOneMethod().getMethodName(), entityItemName, getAccessorMethod(this.entityMetadata.getCurrentIndentifierField()).getMethodName());
// if (!Objects.equals(pet.getVersion() , existingPet.getVersion()) {
bodyBuilder.appendFormalLine("if (!%s.equals(%s.%s(), %s.%s())) {", getNameOfJavaType(JavaType.OBJECTS), entityItemName, getAccessorMethod(this.entityMetadata.getCurrentVersionField()).getMethodName(), existingVarName, getAccessorMethod(this.entityMetadata.getCurrentVersionField()).getMethodName());
bodyBuilder.indent();
// if (concurrencyProblem && StringUtils.isEmpty(concurrencyControl)){
bodyBuilder.appendFormalLine("if (%s.isEmpty(concurrencyControl)){", getNameOfJavaType(STRING_UTILS_APACHE));
bodyBuilder.indent();
// populateForm(model);
bodyBuilder.appendFormalLine("populateForm(model);");
// model.addAttribute("entity", entityParam)
bodyBuilder.appendFormalLine("model.addAttribute(\"%s\", %s);", entityItemName, entityItemName);
// model.addAttribute("concurrency", true);
bodyBuilder.appendFormalLine("model.addAttribute(\"concurrency\", true);");
// return new ModelAndView("pets/edit");
bodyBuilder.appendFormalLine("return new %s(\"%s/edit\");", getNameOfJavaType(SpringJavaType.MODEL_AND_VIEW), viewsPath);
bodyBuilder.indentRemove();
// } else if(concurrencyProblem && "discard".equals(concurrencyControl)){
bodyBuilder.appendFormalLine("} else if(\"discard\".equals(concurrencyControl)){");
bodyBuilder.indent();
// populateForm(model);
bodyBuilder.appendFormalLine("populateForm(model);");
// model.addAttribute("pet", existingPet);
bodyBuilder.appendFormalLine("model.addAttribute(\"%s\", %s);", entityItemName, existingVarName);
// model.addAttribute("concurrency", false);
bodyBuilder.appendFormalLine("model.addAttribute(\"concurrency\", false);");
// return new ModelAndView("pets/edit");
bodyBuilder.appendFormalLine("return new %s(\"%s/edit\");", getNameOfJavaType(SpringJavaType.MODEL_AND_VIEW), viewsPath);
bodyBuilder.indentRemove();
// } else if(concurrencyProblem && "apply".equals(concurrencyControl)){
bodyBuilder.appendFormalLine("} else if(\"apply\".equals(concurrencyControl)){");
bodyBuilder.indent();
// // Update the version field to be able to override the existing values
bodyBuilder.appendFormalLine("// Update the version field to be able to override the existing values");
// pet.setVersion(existingPet.getVersion());
bodyBuilder.appendFormalLine("%s.%s(%s.%s());", entityItemName, getMutatorMethod(this.entityMetadata.getCurrentVersionField()).getMethodName(), existingVarName, getAccessorMethod(this.entityMetadata.getCurrentVersionField()).getMethodName());
bodyBuilder.indentRemove();
// } else if(concurrencyProblem && "apply".equals(concurrencyControl)){
bodyBuilder.appendFormalLine("} else {");
bodyBuilder.indent();
// // Invalid concurrencyControlValue
bodyBuilder.appendFormalLine("// Invalid concurrencyControlValue");
// throw new IllegalArgumentException("Invalid concurrencyControlValue");
bodyBuilder.appendFormalLine("throw new IllegalArgumentException(\"Invalid concurrencyControlValue\");");
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
bodyBuilder.indentRemove();
bodyBuilder.appendFormalLine("}");
}
String savedVarName = "saved" + entity.getSimpleTypeName();
// Customer savedCustomer = customerService.save(customer);;
bodyBuilder.appendFormalLine("%s %s = %s().%s(%s);", getNameOfJavaType(entity), savedVarName, getAccessorMethod(controllerMetadata.getServiceField()).getMethodName(), serviceMetadata.getCurrentSaveMethod().getMethodName(), entityItemName);
// UriComponents showURI =
// itemLink.to(CategoryItemThymeleafLinkFactory.SHOW).with("category",
// savedCategory.getId()).toUri();
bodyBuilder.appendFormalLine("%s showURI = %s().to(%s.SHOW).with(\"%s\", %s.%s()).toUri();", getNameOfJavaType(SpringJavaType.URI_COMPONENTS), getAccessorMethod(this.itemMethodLinkBuilderFactoryField).getMethodName(), getNameOfJavaType(relatedItemLinkFactory), this.entityItemName, savedVarName, getAccessorMethod(this.entityMetadata.getCurrentIndentifierField()).getMethodName());
// return new ModelAndView("redirect:" + showURI.toUriString());
bodyBuilder.appendFormalLine("return new %s(\"redirect:\" + showURI.toUriString());", getNameOfJavaType(SpringJavaType.MODEL_AND_VIEW));
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, SpringJavaType.MODEL_AND_VIEW, parameterTypes, parameterNames, bodyBuilder);
methodBuilder.setAnnotations(annotations);
return methodBuilder.build();
}
use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.
the class ThymeleafMetadata method getPopulateFormMethod.
/**
* This method provides the "populateForm" method
*
* @return MethodMetadata
*/
private MethodMetadata getPopulateFormMethod() {
// Define methodName
final JavaSymbolName methodName = new JavaSymbolName("populateForm");
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
parameterTypes.add(AnnotatedJavaType.convertFromJavaType(SpringJavaType.MODEL));
MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
if (existingMethod != null) {
return existingMethod;
}
final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
parameterNames.add(MODEL_PARAM_NAME);
// Adding annotations
final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
// Generate body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// populateFormats(model);
bodyBuilder.appendFormalLine("%s(model);", populateFormatsMethod.getMethodName());
buildPopulateEnumsBody(bodyBuilder, enumFields);
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, parameterTypes, parameterNames, bodyBuilder);
methodBuilder.setAnnotations(annotations);
return methodBuilder.build();
}
use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.
the class ThymeleafMetadata method getCreateFormDetailsMethod.
/**
* This method provides the "create" form method for details using Thymeleaf
* view response type
*
* @return MethodMetadata
*/
private MethodMetadata getCreateFormDetailsMethod() {
// Define methodName
final JavaSymbolName methodName = CREATE_FORM_METHOD_NAME;
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
final RelationInfoExtended info = controllerMetadata.getLastDetailsInfo();
final JavaType parentEntity = info.entityType;
final JavaType entity = info.childType;
parameterTypes.add(new AnnotatedJavaType(parentEntity, ANN_METADATA_MODEL_ATTRIBUTE));
parameterTypes.add(MODEL_PARAM);
MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
if (existingMethod != null) {
return existingMethod;
}
final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
String parentEntityParamName = StringUtils.uncapitalize(parentEntity.getSimpleTypeName());
parameterNames.add(new JavaSymbolName(parentEntityParamName));
parameterNames.add(MODEL_PARAM_NAME);
// Adding annotations
final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
// Adding @GetMapping annotation
AnnotationMetadataBuilder getMapping = new AnnotationMetadataBuilder(GET_MAPPING);
getMapping.addStringAttribute("value", "/create-form");
getMapping.addStringAttribute("name", methodName.getSymbolName());
annotations.add(getMapping);
this.mvcMethodNames.put(methodName.getSymbolName(), methodName.getSymbolName());
// Generate body
InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
// populateForm(model);
bodyBuilder.appendFormalLine("%s(model);", populateFormMethod.getMethodName());
// model.addAttribute("entity", new Entity());
bodyBuilder.appendFormalLine(String.format("model.addAttribute(\"%s\", new %s());", StringUtils.uncapitalize(getNameOfJavaType(entity)), getNameOfJavaType(entity)));
// return new ModelAndView("path/create");
bodyBuilder.appendFormalLine("return new %s(\"%s/%s/create\");", getNameOfJavaType(SpringJavaType.MODEL_AND_VIEW), viewsPath, controllerMetadata.getDetailsPathAsString("/"));
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, SpringJavaType.MODEL_AND_VIEW, parameterTypes, parameterNames, bodyBuilder);
methodBuilder.setAnnotations(annotations);
return methodBuilder.build();
}
Aggregations