Search in sources :

Example 41 with MethodMetadata

use of org.springframework.roo.classpath.details.MethodMetadata 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 42 with MethodMetadata

use of org.springframework.roo.classpath.details.MethodMetadata 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)

Example 43 with MethodMetadata

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

the class JSONMetadata method getUpdateBatchMethod.

/**
 * Creates update batch method
 *
 * @return {@link MethodMetadata}
 */
private MethodMetadata getUpdateBatchMethod() {
    // Define methodName
    final JavaSymbolName methodName = new JavaSymbolName("updateBatch");
    // Adding parameter types
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    parameterTypes.add(new AnnotatedJavaType(JavaType.collectionOf(this.entity), ANN_METADATA_VALID, ANN_METADATA_REQUEST_BODY));
    parameterTypes.add(new AnnotatedJavaType(SpringJavaType.BINDING_RESULT));
    MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
    if (existingMethod != null) {
        return existingMethod;
    }
    // Adding parameter names
    final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    parameterNames.add(new JavaSymbolName(StringUtils.uncapitalize(this.entityPlural)));
    parameterNames.add(new JavaSymbolName("result"));
    // Adding annotations
    final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
    // Adding @PutMapping annotation
    AnnotationMetadataBuilder putMappingAnnotation = new AnnotationMetadataBuilder(PUT_MAPPING);
    putMappingAnnotation.addStringAttribute("value", "/batch");
    putMappingAnnotation.addStringAttribute("name", methodName.getSymbolName());
    annotations.add(putMappingAnnotation);
    // Generate body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // if (result.hasErrors()) {
    // return ResponseEntity.status(HttpStatus.CONFLICT).body(result);
    // }
    bodyBuilder.newLine();
    bodyBuilder.appendFormalLine("if (result.hasErrors()) {");
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine("return %s.status(%s.CONFLICT).body(result);", getNameOfJavaType(RESPONSE_ENTITY), getNameOfJavaType(SpringJavaType.HTTP_STATUS));
    bodyBuilder.indentRemove();
    bodyBuilder.appendFormalLine("}");
    // customerService.save(customers);
    bodyBuilder.newLine();
    bodyBuilder.appendFormalLine("%s().%s(%s);", getAccessorMethod(controllerMetadata.getServiceField()).getMethodName(), serviceMetadata.getCurrentSaveBatchMethod().getMethodName(), StringUtils.uncapitalize(this.entityPlural));
    // return ResponseEntity.ok().build();
    bodyBuilder.newLine();
    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 : 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 44 with MethodMetadata

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

the class JSONMetadata method getCreateMethod.

/**
 * This method provides the "create" method using JSON response type
 *
 * @param serviceSaveMethod
 *
 * @return MethodMetadata
 */
private MethodMetadata getCreateMethod() {
    // Define methodName
    final JavaSymbolName methodName = new JavaSymbolName("create");
    // Adding parameter types
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    parameterTypes.add(new AnnotatedJavaType(this.entity, ANN_METADATA_VALID, ANN_METADATA_REQUEST_BODY));
    parameterTypes.add(new AnnotatedJavaType(SpringJavaType.BINDING_RESULT));
    MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
    if (existingMethod != null) {
        return existingMethod;
    }
    // Adding parameter names
    final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    parameterNames.add(new JavaSymbolName(entityItemName));
    parameterNames.add(new JavaSymbolName("result"));
    // 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);
    // Generate body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // Concurrency control
    if (entityMetadata.getCurrentVersionField() != null) {
        // if (customer.getId() != null || customer.getVersion() != null
        bodyBuilder.newLine();
        bodyBuilder.appendIndent();
        bodyBuilder.append("if (%s || %s", createNullExpression(entityMetadata.getCurrentIndentifierField()), createNullExpression(entityMetadata.getCurrentVersionField()));
        if (compositionRelationOneToOne.isEmpty()) {
            bodyBuilder.append(") {");
            bodyBuilder.newLine();
        } else {
            bodyBuilder.indent();
            bodyBuilder.indent();
            for (Pair<RelationInfo, JpaEntityMetadata> item : compositionRelationOneToOne) {
                JavaSymbolName versionFieldName = item.getRight().getCurrentVersionField().getFieldName();
                JavaSymbolName idFieldName = item.getRight().getCurrentIndentifierField().getFieldName();
                JavaSymbolName relationFieldName = item.getKey().fieldMetadata.getFieldName();
                // || (customer.getAddress() != null && (customer.getAddress().getId() != null || customer.getAddress().getVersion() != null))
                bodyBuilder.newLine();
                bodyBuilder.appendIndent();
                bodyBuilder.append("|| ( ");
                bodyBuilder.append("%1$s.get%2$s() != null && (%1$s.get%2$s().get%3$s() != null || %1$s.get%2$s().get%4$s() != null)", entityItemName, relationFieldName.getSymbolNameCapitalisedFirstLetter(), idFieldName.getSymbolNameCapitalisedFirstLetter(), versionFieldName.getSymbolNameCapitalisedFirstLetter());
                bodyBuilder.append(")");
            }
            bodyBuilder.append(") {");
            bodyBuilder.newLine();
            bodyBuilder.indentRemove();
            bodyBuilder.indentRemove();
        }
        bodyBuilder.indent();
        // return ResponseEntity.status(HttpStatus.CONFLICT).build();
        bodyBuilder.appendFormalLine("return %s.status(%s.CONFLICT).build();", getNameOfJavaType(RESPONSE_ENTITY), getNameOfJavaType(SpringJavaType.HTTP_STATUS));
        bodyBuilder.indentRemove();
        bodyBuilder.appendFormalLine("}");
    } else {
        // if (customer.getId() != null)
        bodyBuilder.newLine();
        bodyBuilder.appendIndent();
        bodyBuilder.append("if (%s", createNullExpression(entityMetadata.getCurrentIndentifierField()));
        if (compositionRelationOneToOne.isEmpty()) {
            bodyBuilder.append(") {");
            bodyBuilder.newLine();
        } else {
            bodyBuilder.indent();
            bodyBuilder.indent();
            for (Pair<RelationInfo, JpaEntityMetadata> item : compositionRelationOneToOne) {
                JavaSymbolName idFieldName = item.getRight().getCurrentIndentifierField().getFieldName();
                JavaSymbolName relationFieldName = item.getKey().fieldMetadata.getFieldName();
                // || (customer.getAddress() != null && (customer.getAddress().getId() != null || customer.getAddress().getVersion() != null))
                bodyBuilder.newLine();
                bodyBuilder.appendIndent();
                bodyBuilder.append("|| ( ");
                bodyBuilder.append("%1$s.get%2$s() != null && %1$s.get%2$s().get%3$s() != null", entityItemName, relationFieldName.getSymbolNameCapitalisedFirstLetter(), idFieldName.getSymbolNameCapitalisedFirstLetter());
                bodyBuilder.append(")");
            }
            bodyBuilder.append(") {");
            bodyBuilder.newLine();
            bodyBuilder.indentRemove();
            bodyBuilder.indentRemove();
        }
        bodyBuilder.indent();
        // return ResponseEntity.status(HttpStatus.CONFLICT).build();
        bodyBuilder.appendFormalLine("return %s.status(%s.CONFLICT).build();", getNameOfJavaType(RESPONSE_ENTITY), getNameOfJavaType(SpringJavaType.HTTP_STATUS));
        bodyBuilder.indentRemove();
        bodyBuilder.appendFormalLine("}");
    }
    // if (result.hasErrors()) {
    // return ResponseEntity.status(HttpStatus.CONFLICT).body(result);
    // }
    bodyBuilder.newLine();
    bodyBuilder.appendFormalLine("if (result.hasErrors()) {");
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine("return %s.status(%s.CONFLICT).body(result);", getNameOfJavaType(RESPONSE_ENTITY), getNameOfJavaType(SpringJavaType.HTTP_STATUS));
    bodyBuilder.indentRemove();
    bodyBuilder.appendFormalLine("}");
    // Entity newEntity = entityService.saveMethodName(entity);
    bodyBuilder.newLine();
    bodyBuilder.appendFormalLine("%s new%s = %s().%s(%s);", getNameOfJavaType(entity), StringUtils.capitalize(entityItemName), getAccessorMethod(controllerMetadata.getServiceField()).getMethodName(), serviceMetadata.getCurrentSaveMethod().getMethodName(), entityItemName);
    // UriComponents showURI = CustomersItemJsonController.showURI(newCustomer);
    bodyBuilder.appendFormalLine("%s showURI = %s.%s(new%s);", getNameOfJavaType(SpringJavaType.URI_COMPONENTS), getNameOfJavaType(itemController), SHOW_URI_METHOD_NAME, StringUtils.capitalize(entityItemName));
    // return ResponseEntity.created(showURI.toUri()).build();
    bodyBuilder.newLine();
    bodyBuilder.appendFormalLine("return %s.created(showURI.toUri()).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 : 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)

Example 45 with MethodMetadata

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

the class JSONMetadata method getShowDetailMethod.

/**
 * This method provides the "show" method using JSON response type
 *
 * @return MethodMetadata
 */
private MethodMetadata getShowDetailMethod() {
    // Define methodName
    final JavaSymbolName methodName = new JavaSymbolName("show");
    // Define parameters
    final List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    final List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    JavaType parentEntity = controllerMetadata.getLastDetailsInfo().entityType;
    JavaType entity = controllerMetadata.getLastDetailEntity();
    String entityItemName = StringUtils.uncapitalize(entity.getSimpleTypeName());
    parameterTypes.add(new AnnotatedJavaType(parentEntity, ANN_MODEL_ATTRIBUTE));
    parameterNames.add(new JavaSymbolName(StringUtils.uncapitalize(parentEntity.getSimpleTypeName())));
    parameterTypes.add(new AnnotatedJavaType(entity, ANN_MODEL_ATTRIBUTE));
    parameterNames.add(new JavaSymbolName(entityItemName));
    parameterTypes.add(AnnotatedJavaType.convertFromJavaType(SpringJavaType.MODEL));
    parameterNames.add(new JavaSymbolName("model"));
    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) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) Jsr303JavaType(org.springframework.roo.model.Jsr303JavaType) 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)

Aggregations

MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)149 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)119 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)115 ArrayList (java.util.ArrayList)111 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)110 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)92 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)73 JavaType (org.springframework.roo.model.JavaType)72 SpringJavaType (org.springframework.roo.model.SpringJavaType)40 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)39 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)38 JdkJavaType (org.springframework.roo.model.JdkJavaType)28 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)27 RooJavaType (org.springframework.roo.model.RooJavaType)22 ServiceMetadata (org.springframework.roo.addon.layers.service.addon.ServiceMetadata)18 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)18 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)17 RelationInfo (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata.RelationInfo)16 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)16 JpaJavaType (org.springframework.roo.model.JpaJavaType)15