Search in sources :

Example 31 with RelationInfo

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();
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) 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) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) RelationInfo(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) ServiceMetadata(org.springframework.roo.addon.layers.service.addon.ServiceMetadata) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Aggregations

RelationInfo (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo)31 ArrayList (java.util.ArrayList)27 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)19 JpaEntityMetadata (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata)18 JavaType (org.springframework.roo.model.JavaType)18 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)17 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)16 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)15 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)14 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)12 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)12 ServiceMetadata (org.springframework.roo.addon.layers.service.addon.ServiceMetadata)11 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)11 RooJavaType (org.springframework.roo.model.RooJavaType)11 Pair (org.apache.commons.lang3.tuple.Pair)8 JdkJavaType (org.springframework.roo.model.JdkJavaType)8 SpringJavaType (org.springframework.roo.model.SpringJavaType)8 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)5 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)5 HashMap (java.util.HashMap)4