Search in sources :

Example 46 with InvocableMemberBodyBuilder

use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.

the class ThymeleafMetadata method getListDatatablesMethod.

/**
 * This method provides the "list" Datatables JSON method using JSON
 * response type and returns Datatables element
 *
 * @return MethodMetadata
 */
private MethodMetadata getListDatatablesMethod() {
    // Define methodName
    final JavaSymbolName methodName = LIST_DATATABLES_METHOD_NAME;
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    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>();
    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
    final AnnotationMetadataBuilder getMappingAnnotation = new AnnotationMetadataBuilder(GET_MAPPING);
    getMappingAnnotation.addEnumAttribute("produces", SPRINGLETS_DATATABLES, "MEDIA_TYPE");
    getMappingAnnotation.addStringAttribute("name", methodName.getSymbolName());
    getMappingAnnotation.addStringAttribute("value", "/dt");
    annotations.add(getMappingAnnotation);
    this.mvcMethodNames.put(methodName.getSymbolName(), methodName.getSymbolName());
    // Adding @ResponseBody annotation
    annotations.add(RESPONSE_BODY_ANNOTATION);
    // Generate body
    final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    final String itemNames = StringUtils.uncapitalize(this.entityPlural);
    // Getting the findAllMethod
    MethodMetadata findAllMethod = this.serviceMetadata.getCurrentFindAllWithGlobalSearchMethod();
    // Getting the findAll return type
    JavaType defaultReturnType = findAllMethod.getReturnType().getParameters().get(0);
    // Page<Customer> customers = customerService.findAll(search, pageable);
    bodyBuilder.appendFormalLine("%s<%s> %s = %s().%s(search, pageable);", getNameOfJavaType(SpringJavaType.PAGE), getNameOfJavaType(defaultReturnType), itemNames, getAccessorMethod(this.controllerMetadata.getServiceField()).getMethodName(), findAllMethod.getMethodName());
    final String totalVarName = "total" + StringUtils.capitalize(this.entityPlural) + "Count";
    // long totalCustomersCount = customers.getTotalElements();
    bodyBuilder.appendFormalLine("long %s = %s.getTotalElements();", totalVarName, itemNames);
    // if (search != null && StringUtils.hasText(search.getText())) {
    // totalCustomersCount = customerService.count();
    // }
    bodyBuilder.appendFormalLine("if (search != null && %s.isNotBlank(search.getText())) {", getNameOfJavaType(STRING_UTILS_APACHE));
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine("%s = %s().%s();", totalVarName, getAccessorMethod(this.controllerMetadata.getServiceField()).getMethodName(), serviceMetadata.getCurrentCountMethod().getMethodName());
    bodyBuilder.indentRemove();
    bodyBuilder.appendFormalLine("}");
    // ConvertedDatatablesData<Owner> datatablesData = new
    // ConvertedDatatablesData<Owner>(owners,
    // totalOwnersCount, draw, conversionService, columns);
    bodyBuilder.appendFormalLine("%1$s<%2$s> datatablesData = new %1$s<%2$s>(%3$s, %4$s, draw, %5$s(), %6$s);", getNameOfJavaType(SpringletsJavaType.SPRINGLETS_CONVERTED_DATATABLES_DATA), getNameOfJavaType(defaultReturnType), itemNames, totalVarName, getAccessorMethod(this.conversionServiceField).getMethodName(), DATATABLES_COLUMNS_PARAM_NAME);
    // return ResponseEntity.ok(datatablesData);
    bodyBuilder.appendFormalLine("return %s.ok(datatablesData);", getNameOfJavaType(RESPONSE_ENTITY));
    // Generating returnType
    JavaType returnType = JavaType.wrapperOf(RESPONSE_ENTITY, JavaType.wrapperOf(SpringletsJavaType.SPRINGLETS_CONVERTED_DATATABLES_DATA, defaultReturnType));
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, returnType, parameterTypes, parameterNames, bodyBuilder);
    methodBuilder.setAnnotations(annotations);
    return methodBuilder.build();
}
Also used : 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) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 47 with InvocableMemberBodyBuilder

use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder in project spring-roo by spring-projects.

the class ThymeleafMetadata method getCreateFormMethod.

/**
 * This method provides the "create" form method using Thymeleaf view
 * response type
 *
 * @return MethodMetadata
 */
private MethodMetadata getCreateFormMethod() {
    // Define methodName
    final JavaSymbolName methodName = CREATE_FORM_METHOD_NAME;
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    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(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());
    bodyBuilder.newLine();
    // model.addAttribute("entity", new Entity());
    bodyBuilder.appendFormalLine(String.format("model.addAttribute(\"%s\", new %s());", StringUtils.uncapitalize(getNameOfJavaType(this.entity)), getNameOfJavaType(this.entity)));
    // return "path/create";
    bodyBuilder.appendFormalLine("return new %s(\"%s/create\");", getNameOfJavaType(SpringJavaType.MODEL_AND_VIEW), viewsPath);
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, SpringJavaType.MODEL_AND_VIEW, parameterTypes, parameterNames, bodyBuilder);
    methodBuilder.setAnnotations(annotations);
    return methodBuilder.build();
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 48 with InvocableMemberBodyBuilder

use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder 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();
}
Also used : AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) CommentStructure(org.springframework.roo.classpath.details.comments.CommentStructure) 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) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) JavadocComment(org.springframework.roo.classpath.details.comments.JavadocComment) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 49 with InvocableMemberBodyBuilder

use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder 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();
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 50 with InvocableMemberBodyBuilder

use of org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder 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();
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)

Aggregations

InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)152 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)121 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)117 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)105 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)92 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)90 ArrayList (java.util.ArrayList)85 JavaType (org.springframework.roo.model.JavaType)70 SpringJavaType (org.springframework.roo.model.SpringJavaType)46 JdkJavaType (org.springframework.roo.model.JdkJavaType)44 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)40 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)34 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)27 CommentStructure (org.springframework.roo.classpath.details.comments.CommentStructure)16 JavadocComment (org.springframework.roo.classpath.details.comments.JavadocComment)16 ServiceMetadata (org.springframework.roo.addon.layers.service.addon.ServiceMetadata)15 ConstructorMetadataBuilder (org.springframework.roo.classpath.details.ConstructorMetadataBuilder)15 RelationInfo (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo)14 RelationInfoExtended (org.springframework.roo.addon.web.mvc.controller.addon.RelationInfoExtended)11 JpaJavaType (org.springframework.roo.model.JpaJavaType)9