Search in sources :

Example 16 with AnnotatedJavaType

use of org.springframework.roo.classpath.details.annotations.AnnotatedJavaType in project spring-roo by spring-projects.

the class ThymeleafMetadata method getDeleteDetailMethod.

private MethodMetadata getDeleteDetailMethod() {
    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);
    JavaSymbolName methodName = DELETE_METHOD_NAME;
    JavaSymbolName itemsName = detailsInfo.fieldMetadata.getFieldName();
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    parameterTypes.add(new AnnotatedJavaType(removeFromMethod.getParameterTypes().get(0).getJavaType(), ANN_METADATA_MODEL_ATTRIBUTE));
    parameterTypes.add(new AnnotatedJavaType(detailsInfo.childType, ANN_METADATA_MODEL_ATTRIBUTE));
    MethodMetadata existingMethod = getGovernorMethod(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.getSymbolName());
    annotations.add(postMappingAnnotation);
    this.mvcMethodNames.put(methodName.getSymbolName(), methodName.getSymbolName());
    annotations.add(new AnnotationMetadataBuilder(SpringJavaType.RESPONSE_BODY));
    final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    parameterNames.add(removeFromMethod.getParameterNames().get(0));
    parameterNames.add(itemsName);
    // Generate body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // customerService.addToOrders(customer, order.getId());
    bodyBuilder.appendFormalLine("%s().%s(%s,%s.singleton(%s.%s()));", getAccessorMethod(detailsServiceField).getMethodName(), removeFromMethod.getMethodName(), removeFromMethod.getParameterNames().get(0), getNameOfJavaType(JavaType.COLLECTIONS), itemsName, detailsInfo.childEntityMetadata.getCurrentIdentifierAccessor().getMethodName());
    // return ResponseEntity.ok().build();
    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();
}
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) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) RelationInfoExtended(org.springframework.roo.addon.web.mvc.controller.addon.RelationInfoExtended) 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)

Example 17 with AnnotatedJavaType

use of org.springframework.roo.classpath.details.annotations.AnnotatedJavaType in project spring-roo by spring-projects.

the class ThymeleafMetadata method getListDatatablesByIdsInDetailMethod.

/**
 * This method provides detail datatablesByIdsIn list method using Thymeleaf
 * response type
 *
 * @return MethodMetadata
 */
private MethodMetadata getListDatatablesByIdsInDetailMethod() {
    RelationInfo detailsInfo = controllerMetadata.getLastDetailsInfo();
    final ServiceMetadata detailsServiceMetadata = controllerMetadata.getServiceMetadataForEntity(detailsInfo.childType);
    final MethodMetadata findAllByIdsInMethod = detailsServiceMetadata.getCurrentFindAllByIdsInWithGlobalSearchMethod();
    if (findAllByIdsInMethod == null) {
        return null;
    }
    final FieldMetadata detailsServiceField = controllerMetadata.getDetailsServiceFields(detailsInfo.childType);
    // Define methodName
    final JavaSymbolName methodName = LIST_DATATABLES_BY_IDS_IN_DETAILS_METHOD_NAME;
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    AnnotationMetadataBuilder requestParamIdsAnnotation = new AnnotationMetadataBuilder(REQUEST_PARAM);
    requestParamIdsAnnotation.addStringAttribute("value", "ids");
    parameterTypes.add(new AnnotatedJavaType(JavaType.wrapperOf(JavaType.LIST, findAllByIdsInMethod.getParameterTypes().get(0).getJavaType().getBaseType()), requestParamIdsAnnotation.build()));
    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(new JavaSymbolName("ids"));
    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", "/dtByIdsIn");
    annotations.add(getMappingAnnotation);
    this.mvcMethodNames.put(methodName.getSymbolName(), methodName.getSymbolName());
    // Adding @ResponseBody annotation
    annotations.add(RESPONSE_BODY_ANNOTATION);
    // Generating returnType
    final JavaType serviceReturnType = findAllByIdsInMethod.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(ids, search, pageable);", getNameOfJavaType(serviceReturnType), itemsName, getAccessorMethod(detailsServiceField).getMethodName(), findAllByIdsInMethod.getMethodName());
    final String totalVarName = "total" + StringUtils.capitalize(itemsName) + "Count";
    // long totalOrdersCount =
    // customerOrderService.countByCustomer(customer);
    bodyBuilder.appendFormalLine("long %s = %s.getTotalElements();", totalVarName, itemsName);
    // 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)

Example 18 with AnnotatedJavaType

use of org.springframework.roo.classpath.details.annotations.AnnotatedJavaType in project spring-roo by spring-projects.

the class ThymeleafMetadata method getCreateDetailsMethod.

/**
 * This method provides the "create" method for details association
 * relationship using Thymeleaf view response type
 *
 * @return MethodMetadata
 */
private MethodMetadata getCreateDetailsMethod() {
    RelationInfo detailsInfo = controllerMetadata.getLastDetailsInfo();
    final ServiceMetadata detailsServiceMetadata = controllerMetadata.getServiceMetadataForEntity(detailsInfo.entityType);
    final MethodMetadata setMethod = detailsServiceMetadata.getSetRelationMethods().get(detailsInfo);
    final FieldMetadata detailsServiceField = controllerMetadata.getDetailsServiceFields(detailsInfo.entityType);
    // Define methodName
    final JavaSymbolName methodName = CREATE_METHOD_NAME;
    JavaSymbolName itemsName = setMethod.getParameterNames().get(1);
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    parameterTypes.add(new AnnotatedJavaType(setMethod.getParameterTypes().get(0).getJavaType(), ANN_METADATA_MODEL_ATTRIBUTE));
    AnnotationMetadataBuilder requestParamAnnotation = new AnnotationMetadataBuilder(REQUEST_PARAM);
    requestParamAnnotation.addStringAttribute("value", itemsName.getSymbolName().concat("Ids"));
    requestParamAnnotation.addBooleanAttribute("required", false);
    JavaType identifierType = setMethod.getParameterTypes().get(1).getJavaType().getParameters().get(0);
    parameterTypes.add(new AnnotatedJavaType(JavaType.wrapperOf(JavaType.LIST, identifierType), requestParamAnnotation.build()));
    // Version parameter
    if (this.entityMetadata.getCurrentVersionField() != null) {
        AnnotationMetadataBuilder versionRequestParamAnnotation = new AnnotationMetadataBuilder(REQUEST_PARAM);
        versionRequestParamAnnotation.addStringAttribute("value", "parentVersion");
        parameterTypes.add(new AnnotatedJavaType(this.entityMetadata.getCurrentVersionField().getFieldType(), versionRequestParamAnnotation.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(AnnotatedJavaType.convertFromJavaType(SpringJavaType.MODEL));
    MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
    if (existingMethod != null) {
        return existingMethod;
    }
    // Adding annotations
    final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
    // Adding @PostMapping annotation
    AnnotationMetadataBuilder postMappingAnnotation = new AnnotationMetadataBuilder(SpringJavaType.POST_MAPPING);
    postMappingAnnotation.addStringAttribute("name", methodName.getSymbolName());
    annotations.add(postMappingAnnotation);
    this.mvcMethodNames.put(methodName.getSymbolName(), methodName.getSymbolName());
    final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    parameterNames.addAll(setMethod.getParameterNames());
    parameterNames.add(VERSION_PARAM_NAME);
    parameterNames.add(new JavaSymbolName("concurrencyControl"));
    parameterNames.add(MODEL_PARAM_NAME);
    // Generate body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    /*
     * // Remove empty values for (Iterator<Long> iterator =
     * products.iterator(); iterator.hasNext();) { if (iterator.next() ==
     * null) { iterator.remove(); } }
     */
    bodyBuilder.appendFormalLine("// Remove empty values");
    // if(books != null){
    bodyBuilder.appendFormalLine("if (%s != null) {", itemsName);
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine("for (%s<%s> iterator = %s.iterator(); iterator.hasNext();) {", getNameOfJavaType(JavaType.ITERATOR), getNameOfJavaType(identifierType), itemsName);
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine("if (iterator.next() == null) {");
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine("iterator.remove();");
    bodyBuilder.indentRemove();
    bodyBuilder.appendFormalLine("}");
    bodyBuilder.indentRemove();
    bodyBuilder.appendFormalLine("}");
    bodyBuilder.indentRemove();
    bodyBuilder.appendFormalLine("}");
    if (this.entityMetadata.getCurrentVersionField() != null) {
        // // Concurrency control
        bodyBuilder.appendFormalLine("// Concurrency control");
        JavaSymbolName parentName = setMethod.getParameterNames().get(0);
        // if(version != pet.getVersion() && StringUtils.isEmpty(concurrencyControl)){
        bodyBuilder.appendFormalLine("if(!%s.equals(%s, %s.%s()) && %s.isEmpty(concurrencyControl)){", getNameOfJavaType(JavaType.OBJECTS), VERSION_PARAM_NAME, parentName, getAccessorMethod(this.entityMetadata.getCurrentVersionField()).getMethodName(), getNameOfJavaType(new JavaType("org.apache.commons.lang3.StringUtils")));
        bodyBuilder.indent();
        // populateForm(model);
        bodyBuilder.appendFormalLine("populateForm(model);");
        // // Obtain the selected books and include them in the author that will be
        bodyBuilder.appendFormalLine("// Obtain the selected books and include them in the author that will be ");
        // // included in the view
        bodyBuilder.appendFormalLine("// included in the view");
        // if(books != null){
        bodyBuilder.appendFormalLine("if (%s != null) {", itemsName);
        bodyBuilder.indent();
        // author.setBooks(new HashSet<Book>(getBookService().findAll(books)));
        bodyBuilder.appendFormalLine("%s.%s(new %s<%s>(%s().findAll(%s)));", parentName, setMethod.getMethodName(), getNameOfJavaType(JavaType.HASH_SET), getNameOfJavaType(detailsInfo.childType), getAccessorMethod(controllerMetadata.getDetailsServiceFields(detailsInfo.childType)).getMethodName(), itemsName);
        bodyBuilder.indentRemove();
        bodyBuilder.appendFormalLine("}else{");
        bodyBuilder.indent();
        // author.setBooks(new HashSet<Book>());
        bodyBuilder.appendFormalLine("%s.%s(new %s<%s>());", parentName, setMethod.getMethodName(), getNameOfJavaType(JavaType.HASH_SET), getNameOfJavaType(detailsInfo.childType));
        bodyBuilder.indentRemove();
        bodyBuilder.appendFormalLine("}");
        // // Reset the version to prevent update
        bodyBuilder.appendFormalLine("// Reset the version to prevent update");
        // author.setVersion(version);
        bodyBuilder.appendFormalLine(" %s.setVersion(%s);", parentName, VERSION_PARAM_NAME);
        // // Include the updated author in the model
        bodyBuilder.appendFormalLine("// Include the updated element in the model");
        // model.addAttribute("author", author);
        bodyBuilder.appendFormalLine("model.addAttribute(\"%s\", %s);", parentName, parentName);
        // model.addAttribute("concurrency", true);
        bodyBuilder.appendFormalLine("model.addAttribute(\"concurrency\", true);");
        // return new ModelAndView("owners/pets/create");
        bodyBuilder.appendFormalLine("return new %s(\"%s/%s/create\");", getNameOfJavaType(SpringJavaType.MODEL_AND_VIEW), viewsPath, itemsName);
        bodyBuilder.indentRemove();
        // }else if(version != pet.getVersion() && "discard".equals(concurrencyControl)){
        bodyBuilder.appendFormalLine("}else if(!%s.equals(%s, %s.%s()) && \"discard\".equals(concurrencyControl)){", getNameOfJavaType(JavaType.OBJECTS), VERSION_PARAM_NAME, parentName, getAccessorMethod(this.entityMetadata.getCurrentVersionField()).getMethodName());
        bodyBuilder.indent();
        // populateForm(model);
        bodyBuilder.appendFormalLine("populateForm(model);");
        // // Provide the original author from the Database
        bodyBuilder.appendFormalLine("// Provide the original element from the Database");
        // model.addAttribute("author", author);
        bodyBuilder.appendFormalLine("model.addAttribute(\"%s\", %s);", parentName, parentName);
        // model.addAttribute("concurrency", false);
        bodyBuilder.appendFormalLine("model.addAttribute(\"concurrency\", false);");
        // return new ModelAndView("owners/pets/create");
        bodyBuilder.appendFormalLine("return new %s(\"%s/%s/create\");", getNameOfJavaType(SpringJavaType.MODEL_AND_VIEW), viewsPath, itemsName);
        bodyBuilder.indentRemove();
        bodyBuilder.appendFormalLine("}");
    }
    // categoryService.setProducts(category, products);
    bodyBuilder.appendFormalLine("%s().%s(%s,%s);", getAccessorMethod(detailsServiceField).getMethodName(), setMethod.getMethodName(), setMethod.getParameterNames().get(0), itemsName);
    // return new ModelAndView("redirect:" +
    // collectionLink.to("list").toUriString());
    bodyBuilder.appendFormalLine("return new %s(\"redirect:\" + %s().to(%s.LIST).toUriString());", getNameOfJavaType(SpringJavaType.MODEL_AND_VIEW), getAccessorMethod(this.collectionMethodLinkBuilderFactoryField).getMethodName(), getNameOfJavaType(relatedCollectionLinkFactory));
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, SpringJavaType.MODEL_AND_VIEW, 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)

Example 19 with AnnotatedJavaType

use of org.springframework.roo.classpath.details.annotations.AnnotatedJavaType 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();
}
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) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) RelationInfoExtended(org.springframework.roo.addon.web.mvc.controller.addon.RelationInfoExtended) 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)

Example 20 with AnnotatedJavaType

use of org.springframework.roo.classpath.details.annotations.AnnotatedJavaType 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();
}
Also used : AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) 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) JpaEntityMetadata(org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Aggregations

AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)130 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)117 ArrayList (java.util.ArrayList)116 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)108 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)99 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)81 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)71 JavaType (org.springframework.roo.model.JavaType)63 SpringJavaType (org.springframework.roo.model.SpringJavaType)46 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)41 JdkJavaType (org.springframework.roo.model.JdkJavaType)28 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)25 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)23 ServiceMetadata (org.springframework.roo.addon.layers.service.addon.ServiceMetadata)18 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)16 RelationInfo (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo)15 RelationInfoExtended (org.springframework.roo.addon.web.mvc.controller.addon.RelationInfoExtended)12 JpaEntityMetadata (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata)10 Pair (org.apache.commons.lang3.tuple.Pair)7 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)7