use of org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo in project spring-roo by spring-projects.
the class ThymeleafMetadata method getListDatatablesDetailMethod.
/**
* This method provides detail datatables list method using Thymeleaf
* response type
*
* @return MethodMetadata
*/
private MethodMetadata getListDatatablesDetailMethod() {
RelationInfo detailsInfo = controllerMetadata.getLastDetailsInfo();
final ServiceMetadata detailsServiceMetadata = controllerMetadata.getServiceMetadataForEntity(detailsInfo.childType);
final MethodMetadata findAllMethod = detailsServiceMetadata.getRefencedFieldFindAllDefinedMethod(detailsInfo.mappedBy);
final MethodMetadata countByDetailMethod = detailsServiceMetadata.getCountByReferenceFieldDefinedMethod(detailsInfo.mappedBy);
final FieldMetadata detailsServiceField = controllerMetadata.getDetailsServiceFields(detailsInfo.childType);
// Define methodName
final JavaSymbolName methodName = LIST_DATATABLES_DETAILS_METHOD_NAME;
List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
parameterTypes.add(new AnnotatedJavaType(findAllMethod.getParameterTypes().get(0).getJavaType(), ANN_METADATA_MODEL_ATTRIBUTE));
parameterTypes.add(DATATABLES_COLUMNS_PARAM);
parameterTypes.add(GLOBAL_SEARCH_PARAM);
parameterTypes.add(DATATABLES_PAGEABLE_PARAM);
AnnotationMetadataBuilder requestParamAnnotation = new AnnotationMetadataBuilder(REQUEST_PARAM);
requestParamAnnotation.addStringAttribute("value", "draw");
parameterTypes.add(new AnnotatedJavaType(JavaType.INT_OBJECT, requestParamAnnotation.build()));
MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
if (existingMethod != null) {
return existingMethod;
}
final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
final JavaSymbolName parentParamName = findAllMethod.getParameterNames().get(0);
parameterNames.add(parentParamName);
parameterNames.add(DATATABLES_COLUMNS_PARAM_NAME);
parameterNames.add(new JavaSymbolName("search"));
parameterNames.add(new JavaSymbolName("pageable"));
parameterNames.add(new JavaSymbolName("draw"));
// Adding annotations
final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
// Adding @GetMapping annotation
AnnotationMetadataBuilder getMappingAnnotation = new AnnotationMetadataBuilder(GET_MAPPING);
getMappingAnnotation.addStringAttribute("name", methodName.getSymbolName());
getMappingAnnotation.addEnumAttribute("produces", SPRINGLETS_DATATABLES, "MEDIA_TYPE");
getMappingAnnotation.addStringAttribute("value", "/dt");
annotations.add(getMappingAnnotation);
this.mvcMethodNames.put(methodName.getSymbolName(), methodName.getSymbolName());
// Adding @ResponseBody annotation
annotations.add(RESPONSE_BODY_ANNOTATION);
// Generating returnType
final JavaType serviceReturnType = findAllMethod.getReturnType();
final JavaType dataReturnType = JavaType.wrapperOf(SpringletsJavaType.SPRINGLETS_CONVERTED_DATATABLES_DATA, serviceReturnType.getParameters().get(0));
final JavaType returnType = JavaType.wrapperOf(RESPONSE_ENTITY, dataReturnType);
// TODO
// Add module dependency
// getTypeLocationService().addModuleDependency(this.controller.getType().getModule(),
// returnParameterTypes.get(i));
// Generate body
final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
final String itemsName = StringUtils.uncapitalize(detailsInfo.fieldName);
// Page<CustomerOrder> orders =
// customerOrderService.findByCustomer(customer, globalSearch,
// pageable);
bodyBuilder.newLine();
bodyBuilder.appendFormalLine("%s %s = %s().%s(%s, search, pageable);", getNameOfJavaType(serviceReturnType), itemsName, getAccessorMethod(detailsServiceField).getMethodName(), findAllMethod.getMethodName(), parentParamName);
final String totalVarName = "total" + StringUtils.capitalize(itemsName) + "Count";
// long totalOrdersCount =
// customerOrderService.countByCustomer(customer);
bodyBuilder.appendFormalLine("%s %s = %s().%s(%s);", getNameOfJavaType(countByDetailMethod.getReturnType()), totalVarName, getAccessorMethod(detailsServiceField).getMethodName(), countByDetailMethod.getMethodName(), parentParamName);
// ConvertedDatatablesData<CustomerOrder> data = new
// ConvertedDatatablesData<CustomerOrder>(orders,
// totalOrdersCount, draw, conversionService, columns);
bodyBuilder.appendFormalLine("%1$s data = new %1$s(%2$s, %3$s, draw, %4$s(), %5$s);", getNameOfJavaType(dataReturnType), itemsName, totalVarName, getAccessorMethod(this.conversionServiceField).getMethodName(), DATATABLES_COLUMNS_PARAM_NAME);
// return ResponseEntity.ok(data);
bodyBuilder.appendFormalLine("return %s.ok(data);", getNameOfJavaType(SpringJavaType.RESPONSE_ENTITY));
MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, returnType, parameterTypes, parameterNames, bodyBuilder);
methodBuilder.setAnnotations(annotations);
return methodBuilder.build();
}
Aggregations