Search in sources :

Example 46 with MethodMetadata

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

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

Example 48 with MethodMetadata

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

the class ThymeleafControllerIntegrationTestMetadata method getTestExampleMethod.

/**
 * Builds and return a test example method.
 *
 * @return {@link MethodMetadata}
 */
private MethodMetadata getTestExampleMethod() {
    JavaSymbolName methodName = new JavaSymbolName("testMethodExample");
    // Check if method exists in governor
    MethodMetadata method = getGovernorMethod(methodName);
    if (method != null) {
        return method;
    }
    // Build method body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // Setup
    bodyBuilder.appendFormalLine("// Setup");
    bodyBuilder.appendFormalLine("// Previous tasks");
    bodyBuilder.newLine();
    // Exercise
    bodyBuilder.appendFormalLine("// Exercise");
    bodyBuilder.appendFormalLine("// Execute method to test");
    bodyBuilder.newLine();
    // Verify
    bodyBuilder.appendFormalLine("// Verify");
    bodyBuilder.appendFormalLine("// Check results with assertions");
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(this.getId(), Modifier.PUBLIC, methodName, JavaType.VOID_PRIMITIVE, bodyBuilder);
    // Add @Test
    methodBuilder.addAnnotation(new AnnotationMetadataBuilder(TEST));
    CommentStructure commentStructure = new CommentStructure();
    commentStructure.addComment(new JavadocComment("Test method example. To be implemented by developer."), CommentLocation.BEGINNING);
    methodBuilder.setCommentStructure(commentStructure);
    return methodBuilder.build();
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) JavadocComment(org.springframework.roo.classpath.details.comments.JavadocComment) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder) CommentStructure(org.springframework.roo.classpath.details.comments.CommentStructure)

Example 49 with MethodMetadata

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

the class ThymeleafMetadata method getFinderFormMethodForFinderInService.

/**
 * Generates a finder method which delegates on entity service to get result
 *
 * @param finderName
 * @param serviceFinderMethod
 * @return
 */
private MethodMetadata getFinderFormMethodForFinderInService(String finderName, MethodMetadata serviceFinderMethod) {
    // Define methodName
    String pathName = finderName;
    if (pathName.startsWith("findBy")) {
        pathName = pathName.replace("findBy", "by");
    }
    final JavaSymbolName methodName = new JavaSymbolName(pathName.concat("Form"));
    // Form Bean is always the first parameter of finder
    final JavaType formBean = serviceFinderMethod.getParameterTypes().get(0).getJavaType();
    List<AnnotationMetadata> formBeanAnnotations = new ArrayList<AnnotationMetadata>();
    AnnotationMetadataBuilder formBeanAnnotation = new AnnotationMetadataBuilder(SpringJavaType.MODEL_ATTRIBUTE);
    formBeanAnnotation.addStringAttribute("value", FORM_BEAN_PARAM_NAME.getSymbolName());
    formBeanAnnotations.add(formBeanAnnotation.build());
    AnnotatedJavaType annotatedFormBean = new AnnotatedJavaType(formBean, formBeanAnnotations);
    // Including annotated formBean parameter and Model parameter
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    parameterTypes.add(annotatedFormBean);
    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(FORM_BEAN_PARAM_NAME);
    parameterNames.add(MODEL_PARAM_NAME);
    // Adding annotations
    final List<AnnotationMetadataBuilder> annotations = new ArrayList<AnnotationMetadataBuilder>();
    // Adding @GetMapping annotation
    AnnotationMetadataBuilder getMappingAnnotation = new AnnotationMetadataBuilder(GET_MAPPING);
    getMappingAnnotation.addStringAttribute("name", methodName.getSymbolName());
    // TODO Delegates on ControllerOperations to obtain the URL for this
    // finder
    getMappingAnnotation.addStringAttribute("value", "/" + pathName + "/search-form");
    annotations.add(getMappingAnnotation);
    this.mvcMethodNames.put(methodName.getSymbolName(), methodName.getSymbolName());
    // Generate body
    final InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    buildPopulateFormatBody(bodyBuilder, formBeansDateTimeFields.get(formBean));
    final List<FieldMetadata> enumFileds = formBeansEnumFields.get(formBean);
    if (enumFileds != null && !enumFileds.isEmpty()) {
        buildPopulateEnumsBody(bodyBuilder, formBeansEnumFields.get(formBean));
    }
    // return new ModelAndView("customers/findByFirstNameLastNameForm");
    bodyBuilder.appendFormalLine("return new %s(\"%s/%s\");", getNameOfJavaType(SpringJavaType.MODEL_AND_VIEW), viewsPath, finderName.concat("Form"));
    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) AnnotationMetadata(org.springframework.roo.classpath.details.annotations.AnnotationMetadata) 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) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) AnnotationMetadataBuilder(org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)

Example 50 with MethodMetadata

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

the class RepositoryJpaCustomImplMetadata method getFindAllByIdsInImpl.

/**
 * Method that generates the findAllByIdsIn implementation method
 * @param findAllByIdsInGlobalSearchMethod
 * @param ids the entity id fields
 * @param fields the entity fields to search for
 *
 * @return
 */
private MethodMetadata getFindAllByIdsInImpl(MethodMetadata findAllByIdsInGlobalSearchMethod, FieldMetadata idField, List<FieldMetadata> fields) {
    // Define method name
    JavaSymbolName methodName = findAllByIdsInGlobalSearchMethod.getMethodName();
    // Define method parameter types
    List<AnnotatedJavaType> parameterTypes = findAllByIdsInGlobalSearchMethod.getParameterTypes();
    // Define method parameter names
    List<JavaSymbolName> parameterNames = findAllByIdsInGlobalSearchMethod.getParameterNames();
    MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
    if (existingMethod != null) {
        return existingMethod;
    }
    // Use provided findAll method to generate its implementation
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, findAllByIdsInGlobalSearchMethod.getMethodName(), findAllByIdsInGlobalSearchMethod.getReturnType(), findAllByIdsInGlobalSearchMethod.getParameterTypes(), findAllByIdsInGlobalSearchMethod.getParameterNames(), null);
    // Generate body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // Getting variable name to use in the code
    JavaSymbolName globalSearch = parameterNames.get(1);
    JavaSymbolName pageable = parameterNames.get(2);
    String entity = this.entity.getSimpleTypeName();
    String entityVariable = StringUtils.uncapitalize(entity);
    // Types to import
    JavaType projection = QUERYDSL_PROJECTIONS;
    bodyBuilder.newLine();
    // QEntity qEntity = QEntity.entity;
    bodyBuilder.appendFormalLine(String.format("%1$s %2$s = %1$s.%2$s;", entityQtype.getNameIncludingTypeParameters(false, importResolver), entityVariable));
    bodyBuilder.newLine();
    // Construct query
    buildQuery(bodyBuilder, entityVariable, globalSearch, null, null, null, null, null, this.defaultReturnType, null, null);
    bodyBuilder.newLine();
    bodyBuilder.appendFormalLine("// Also, filter by the provided ids");
    bodyBuilder.appendFormalLine("query.where(%s.%s.in(ids));", entityVariable, idField.getFieldName());
    bodyBuilder.newLine();
    // AttributeMappingBuilder mapping = buildMapper()
    StringBuffer mappingBuilderLine = new StringBuffer();
    mappingBuilderLine.append(String.format("%s mapping = buildMapper()", getNameOfJavaType(SpringletsJavaType.SPRINGLETS_QUERYDSL_REPOSITORY_SUPPORT_ATTRIBUTE_BUILDER)));
    if (!this.typesAreProjections.get(this.defaultReturnType)) {
        // Return type is the same entity
        Iterator<FieldMetadata> iterator = fields.iterator();
        while (iterator.hasNext()) {
            FieldMetadata field = iterator.next();
            String fieldName = field.getFieldName().getSymbolName();
            mappingBuilderLine.append(String.format("\n\t\t\t.map(%s, %s.%s)", getConstantForField(fieldName).getFieldName(), entityVariable, fieldName));
        }
    } else {
        // Return type is a projection
        List<Pair<String, String>> projectionFields = this.typesFieldMaps.get(this.defaultReturnType);
        Iterator<Pair<String, String>> iterator = projectionFields.iterator();
        while (iterator.hasNext()) {
            Entry<String, String> entry = iterator.next();
            mappingBuilderLine.append(String.format("\n\t\t\t.map(%s, %s)", getConstantForField(entry.getKey()).getFieldName(), entry.getValue()));
        }
    }
    mappingBuilderLine.append(";");
    bodyBuilder.appendFormalLine(mappingBuilderLine.toString());
    bodyBuilder.newLine();
    // applyPagination(pageable, query, mapping);
    bodyBuilder.appendFormalLine(String.format("applyPagination(%s, query, mapping);", pageable));
    // applyOrderById(query);
    bodyBuilder.appendFormalLine("applyOrderById(query);");
    bodyBuilder.newLine();
    buildQueryResult(bodyBuilder, pageable, entityVariable, projection, this.defaultReturnType);
    // Sets body to generated method
    methodBuilder.setBodyBuilder(bodyBuilder);
    // Build and return a MethodMetadata
    return methodBuilder.build();
// instance
}
Also used : FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) JavaSymbolName(org.springframework.roo.model.JavaSymbolName) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) SpringJavaType(org.springframework.roo.model.SpringJavaType) SpringletsJavaType(org.springframework.roo.model.SpringletsJavaType) JpaJavaType(org.springframework.roo.model.JpaJavaType) JavaType(org.springframework.roo.model.JavaType) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder) Pair(org.apache.commons.lang3.tuple.Pair)

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