Search in sources :

Example 6 with ServiceMetadata

use of org.springframework.roo.addon.layers.service.addon.ServiceMetadata 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 7 with ServiceMetadata

use of org.springframework.roo.addon.layers.service.addon.ServiceMetadata 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 8 with ServiceMetadata

use of org.springframework.roo.addon.layers.service.addon.ServiceMetadata in project spring-roo by spring-projects.

the class ThymeleafMetadata method getUpdateDetailMethod.

/**
 * This method provides the "update" method using Thymeleaf view response
 * type
 *
 * @return MethodMetadata
 */
private MethodMetadata getUpdateDetailMethod() {
    // Define methodName
    final JavaSymbolName methodName = UPDATE_METHOD_NAME;
    final RelationInfoExtended info = controllerMetadata.getLastDetailsInfo();
    final JavaType parentEntity = info.entityType;
    final JavaType entity = info.childType;
    final String entityItemName = StringUtils.uncapitalize(entity.getSimpleTypeName());
    final ServiceMetadata serviceMetadata = controllerMetadata.getServiceMetadataForEntity(entity);
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    parameterTypes.add(new AnnotatedJavaType(parentEntity, ANN_METADATA_MODEL_ATTRIBUTE));
    parameterTypes.add(new AnnotatedJavaType(entity, ANN_METADATA_VALID, ANN_METADATA_MODEL_ATTRIBUTE));
    // 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(new AnnotatedJavaType(SpringJavaType.BINDING_RESULT));
    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(StringUtils.uncapitalize(parentEntity.getSimpleTypeName())));
    parameterNames.add(new JavaSymbolName(entityItemName));
    if (this.entityMetadata.getCurrentVersionField() != null) {
        parameterNames.add(this.entityMetadata.getCurrentVersionField().getFieldName());
        parameterNames.add(new JavaSymbolName("concurrencyControl"));
    }
    parameterNames.add(new JavaSymbolName("result"));
    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();
    // if (result.hasErrors()) {
    bodyBuilder.appendFormalLine("if (result.hasErrors()) {");
    bodyBuilder.indent();
    // populateFormats(model);
    bodyBuilder.appendFormalLine("%s(model);", populateFormMethod.getMethodName());
    bodyBuilder.newLine();
    // return new ModelAndView("customerorders/details/edit");
    bodyBuilder.appendFormalLine("return new %s(\"%s/%s/edit\");", getNameOfJavaType(SpringJavaType.MODEL_AND_VIEW), viewsPath, controllerMetadata.getDetailsPathAsString("/"));
    ;
    // }
    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.getLastDetailServiceField()).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("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 {
        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("}");
    }
    // customerService.save(customer);
    bodyBuilder.appendFormalLine("%s().%s(%s);", getAccessorMethod(controllerMetadata.getLastDetailServiceField()).getMethodName(), serviceMetadata.getCurrentSaveMethod().getMethodName(), entityItemName);
    // 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 : 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) 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 9 with ServiceMetadata

use of org.springframework.roo.addon.layers.service.addon.ServiceMetadata in project spring-roo by spring-projects.

the class JSONMetadata method getAddToDetailsMethod.

private MethodMetadata getAddToDetailsMethod() {
    RelationInfo detailsInfo = controllerMetadata.getLastDetailsInfo();
    final ServiceMetadata detailsServiceMetadata = controllerMetadata.getServiceMetadataForEntity(detailsInfo.entityType);
    final MethodMetadata addToMethod = detailsServiceMetadata.getAddToRelationMethods().get(detailsInfo);
    final FieldMetadata detailsServiceField = controllerMetadata.getDetailsServiceFields(detailsInfo.entityType);
    // Define methodName
    final JavaSymbolName methodName = addToMethod.getMethodName();
    JavaSymbolName itemsName = addToMethod.getParameterNames().get(1);
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    parameterTypes.add(new AnnotatedJavaType(addToMethod.getParameterTypes().get(0).getJavaType(), ANN_MODEL_ATTRIBUTE));
    AnnotationMetadataBuilder pathVariableAnnotation = new AnnotationMetadataBuilder(SpringJavaType.PATH_VARIABLE);
    pathVariableAnnotation.addStringAttribute("value", itemsName.getSymbolName());
    parameterTypes.add(new AnnotatedJavaType(addToMethod.getParameterTypes().get(1).getJavaType().getParameters().get(0), pathVariableAnnotation.build()));
    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(POST_MAPPING);
    postMappingAnnotation.addStringAttribute("name", methodName.getSymbolName());
    annotations.add(postMappingAnnotation);
    final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    parameterNames.addAll(addToMethod.getParameterNames());
    // Generate body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // customerService.addToOrders(customer, Collections.singleton(order));
    bodyBuilder.appendFormalLine("%s().%s(%s,%s.singleton(%s));", getAccessorMethod(detailsServiceField).getMethodName(), addToMethod.getMethodName(), addToMethod.getParameterNames().get(0), getNameOfJavaType(JavaType.COLLECTIONS), itemsName);
    // 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) 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 10 with ServiceMetadata

use of org.springframework.roo.addon.layers.service.addon.ServiceMetadata in project spring-roo by spring-projects.

the class ControllerOperationsImpl method getAllMethodsToPublish.

/**
 * Get all the methods that can be published from the service or the
 * controller established by parameter
 *
 * @param currentService
 *            Service from which obtain methods
 * @param currentController
 *            Controller from which obtain methods
 * @return methods names list
 */
public List<String> getAllMethodsToPublish(String currentService, String currentController) {
    // Generating all possible values
    List<String> serviceMethodsToPublish = new ArrayList<String>();
    List<ClassOrInterfaceTypeDetails> servicesToPublish = new ArrayList<ClassOrInterfaceTypeDetails>();
    if (StringUtils.isEmpty(currentService)) {
        // Get controllers
        Collection<ClassOrInterfaceTypeDetails> controllers = getTypeLocationService().findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_CONTROLLER);
        for (ClassOrInterfaceTypeDetails controller : controllers) {
            String name = getClasspathOperations().replaceTopLevelPackageString(controller, currentController);
            if (currentController.equals(name)) {
                // Get the entity associated
                AnnotationMetadata controllerAnnotation = controller.getAnnotation(RooJavaType.ROO_CONTROLLER);
                JavaType entity = (JavaType) controllerAnnotation.getAttribute("entity").getValue();
                // Search the service related with the entity
                Set<ClassOrInterfaceTypeDetails> services = getTypeLocationService().findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_SERVICE);
                Iterator<ClassOrInterfaceTypeDetails> itServices = services.iterator();
                while (itServices.hasNext()) {
                    ClassOrInterfaceTypeDetails existingService = itServices.next();
                    AnnotationAttributeValue<Object> entityAttr = existingService.getAnnotation(RooJavaType.ROO_SERVICE).getAttribute("entity");
                    if (entityAttr != null && entityAttr.getValue().equals(entity)) {
                        servicesToPublish.add(existingService);
                    }
                }
                break;
            }
        }
    } else {
        // Get the services
        Set<ClassOrInterfaceTypeDetails> services = getTypeLocationService().findClassesOrInterfaceDetailsWithAnnotation(RooJavaType.ROO_SERVICE);
        for (ClassOrInterfaceTypeDetails service : services) {
            String name = getClasspathOperations().replaceTopLevelPackageString(service, currentService);
            if (currentService.equals(name)) {
                servicesToPublish.add(service);
                break;
            }
        }
    }
    for (ClassOrInterfaceTypeDetails serviceToPublish : servicesToPublish) {
        // Getting service metadata
        final ServiceMetadata serviceMetadata = getMetadataService().get(ServiceMetadata.createIdentifier(serviceToPublish));
        // Get all methods generated by Roo
        List<MethodMetadata> methodsImplementedByRoo = new ArrayList<MethodMetadata>();
        methodsImplementedByRoo.addAll(serviceMetadata.getNotTransactionalDefinedMethods());
        methodsImplementedByRoo.addAll(serviceMetadata.getTransactionalDefinedMethods());
        // Get all methods and compare them with the generated by Roo
        List<MethodMetadata> methods = serviceToPublish.getMethods();
        boolean notGeneratedByRoo;
        for (MethodMetadata method : methods) {
            notGeneratedByRoo = true;
            Iterator<MethodMetadata> iterMethodsImplRoo = methodsImplementedByRoo.iterator();
            while (iterMethodsImplRoo.hasNext() && notGeneratedByRoo) {
                MethodMetadata methodImplementedByRoo = iterMethodsImplRoo.next();
                // If name is equals check the parameters
                if (method.getMethodName().equals(methodImplementedByRoo.getMethodName())) {
                    // order
                    if (method.getParameterTypes().size() == methodImplementedByRoo.getParameterTypes().size()) {
                        Iterator<AnnotatedJavaType> iterParameterTypesMethodRoo = methodImplementedByRoo.getParameterTypes().iterator();
                        boolean allParametersAreEquals = true;
                        for (AnnotatedJavaType parameterType : method.getParameterTypes()) {
                            AnnotatedJavaType parameterTypeMethodRoo = iterParameterTypesMethodRoo.next();
                            if (!parameterType.getJavaType().equals(parameterTypeMethodRoo.getJavaType())) {
                                allParametersAreEquals = false;
                                break;
                            }
                        }
                        if (allParametersAreEquals) {
                            notGeneratedByRoo = false;
                        }
                    }
                }
            }
            // If is not generated by Roo add to list of the elements
            if (notGeneratedByRoo) {
                StringBuffer methodNameBuffer = new StringBuffer("");
                if (StringUtils.isEmpty(currentService)) {
                    methodNameBuffer.append(getClasspathOperations().replaceTopLevelPackage(serviceToPublish)).append(".");
                }
                methodNameBuffer.append(method.getMethodName().getSymbolName());
                List<AnnotatedJavaType> parameterTypes = method.getParameterTypes();
                methodNameBuffer = methodNameBuffer.append("(");
                for (int i = 0; i < parameterTypes.size(); i++) {
                    String paramType = parameterTypes.get(i).getJavaType().getSimpleTypeName();
                    methodNameBuffer = methodNameBuffer.append(paramType).append(",");
                }
                String methodName;
                if (!parameterTypes.isEmpty()) {
                    methodName = methodNameBuffer.toString().substring(0, methodNameBuffer.toString().length() - 1).concat(")");
                } else {
                    methodName = methodNameBuffer.append(")").toString();
                }
                serviceMethodsToPublish.add(methodName);
            }
        }
    }
    return serviceMethodsToPublish;
}
Also used : AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) RooJavaType(org.springframework.roo.model.RooJavaType) SpringJavaType(org.springframework.roo.model.SpringJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) ClassOrInterfaceTypeDetails(org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails) ServiceMetadata(org.springframework.roo.addon.layers.service.addon.ServiceMetadata)

Aggregations

ServiceMetadata (org.springframework.roo.addon.layers.service.addon.ServiceMetadata)20 ArrayList (java.util.ArrayList)18 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)18 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)18 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)15 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)15 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)15 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)15 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)15 JavaType (org.springframework.roo.model.JavaType)12 RelationInfo (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo)11 RelationInfoExtended (org.springframework.roo.addon.web.mvc.controller.addon.RelationInfoExtended)8 SpringJavaType (org.springframework.roo.model.SpringJavaType)8 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)7 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)7 JdkJavaType (org.springframework.roo.model.JdkJavaType)6 JpaEntityMetadata (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata)5 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)5 RooJavaType (org.springframework.roo.model.RooJavaType)4 HashMap (java.util.HashMap)3