Search in sources :

Example 71 with AnnotationMetadataBuilder

use of org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder 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 72 with AnnotationMetadataBuilder

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

the class JSONMetadata method getConstructor.

private ConstructorMetadata getConstructor(boolean autowired) {
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // Generating constructor
    ConstructorMetadataBuilder constructor = new ConstructorMetadataBuilder(getId());
    constructor.setModifier(Modifier.PUBLIC);
    if (autowired) {
        constructor.addAnnotation(new AnnotationMetadataBuilder(SpringJavaType.AUTOWIRED));
    }
    // Getting serviceFieldName
    String serviceFieldName = controllerMetadata.getServiceField().getFieldName().getSymbolName();
    constructor.addParameter(serviceFieldName, controllerMetadata.getService());
    // Generating body
    bodyBuilder.appendFormalLine(String.format("this.%s = %s;", serviceFieldName, serviceFieldName));
    if (this.type == ControllerType.DETAIL || this.type == ControllerType.DETAIL_ITEM) {
        for (FieldMetadata serviceField : controllerMetadata.getDetailsServiceFields().values()) {
            // Getting parentServiceFieldName
            String childServiceFieldName = serviceField.getFieldName().getSymbolName();
            // Adding parameters
            constructor.addParameter(childServiceFieldName, serviceField.getFieldType());
            // Generating body
            bodyBuilder.appendFormalLine(String.format("this.%s = %s;", childServiceFieldName, childServiceFieldName));
        }
    }
    // Adding body
    constructor.setBodyBuilder(bodyBuilder);
    return constructor.build();
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) ConstructorMetadataBuilder(org.springframework.roo.classpath.details.ConstructorMetadataBuilder) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 73 with AnnotationMetadataBuilder

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

Example 74 with AnnotationMetadataBuilder

use of org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder 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 75 with AnnotationMetadataBuilder

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

the class JSONMetadata method getDeleteBatchMethod.

/**
 * Creates delete batch method
 *
 * @return {@link MethodMetadata}
 */
private MethodMetadata getDeleteBatchMethod() {
    // Define methodName
    final JavaSymbolName methodName = new JavaSymbolName("deleteBatch");
    // Adding parameter types
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    AnnotationMetadataBuilder pathVariable = new AnnotationMetadataBuilder(SpringJavaType.PATH_VARIABLE);
    pathVariable.addStringAttribute("value", entityIdentifierPlural);
    parameterTypes.add(new AnnotatedJavaType(JavaType.collectionOf(entityMetadata.getCurrentIndentifierField().getFieldType()), pathVariable.build()));
    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(entityIdentifierPlural));
    // Adding annotations
    final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
    // Adding @DeleteMapping annotation
    AnnotationMetadataBuilder deleteMappingAnnotation = new AnnotationMetadataBuilder(DELETE_MAPPING);
    deleteMappingAnnotation.addStringAttribute("value", "/batch/{" + entityIdentifierPlural + "}");
    deleteMappingAnnotation.addStringAttribute("name", methodName.getSymbolName());
    annotations.add(deleteMappingAnnotation);
    // Generate body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // serviceField.SERVICE_DELETE_METHOD(ids);
    bodyBuilder.newLine();
    bodyBuilder.appendFormalLine("%s().%s(%s);", getAccessorMethod(controllerMetadata.getServiceField()).getMethodName(), serviceMetadata.getCurrentDeleteBatchMethod().getMethodName(), entityIdentifierPlural);
    // 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)

Aggregations

AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)232 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)155 ArrayList (java.util.ArrayList)129 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)90 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)87 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)81 JavaType (org.springframework.roo.model.JavaType)78 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)73 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)54 SpringJavaType (org.springframework.roo.model.SpringJavaType)47 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)42 FieldMetadataBuilder (org.springframework.roo.classpath.details.FieldMetadataBuilder)40 JdkJavaType (org.springframework.roo.model.JdkJavaType)39 AnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue)31 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)30 RooJavaType (org.springframework.roo.model.RooJavaType)28 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)28 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)26 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)25 StringAttributeValue (org.springframework.roo.classpath.details.annotations.StringAttributeValue)23