Search in sources :

Example 96 with JavaSymbolName

use of org.springframework.roo.model.JavaSymbolName 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)

Example 97 with JavaSymbolName

use of org.springframework.roo.model.JavaSymbolName in project spring-roo by spring-projects.

the class RepositoryJpaCustomImplMetadata method getCustomCountImpl.

/**
 * Method that generates implementation methods for each count method, associated
 * to each custom finder.
 *
 * @param methodInfo
 * @param validFields
 * @return
 */
private MethodMetadata getCustomCountImpl(Pair<MethodMetadata, PartTree> methodInfo) {
    final MethodMetadata method = methodInfo.getLeft();
    // Define method name
    JavaSymbolName methodName = method.getMethodName();
    // Define method parameter types
    List<AnnotatedJavaType> parameterTypes = method.getParameterTypes();
    // Define method parameter names
    List<JavaSymbolName> parameterNames = method.getParameterNames();
    MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
    if (existingMethod != null) {
        return existingMethod;
    }
    // Generate body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // Getting variable names to use in the code
    String entity = this.entity.getSimpleTypeName();
    String entityVariable = StringUtils.uncapitalize(entity);
    JavaType finderParamType = parameterTypes.get(0).getJavaType();
    String finderParamName = parameterNames.get(0).getSymbolName();
    // Types to import
    bodyBuilder.newLine();
    // QEntity qEntity = QEntity.entity;
    bodyBuilder.appendFormalLine(String.format("%1$s %2$s = %1$s.%2$s;", getNameOfJavaType(entityQtype), entityVariable));
    bodyBuilder.newLine();
    // JPQLQuery query = from(qEntity);
    bodyBuilder.appendFormalLine(String.format("%s query = from(%s);", getNameOfJavaType(getJPQLQueryFor(this.entity)), entityVariable));
    bodyBuilder.newLine();
    buildFormBeanFilterBody(bodyBuilder, finderParamType, finderParamName, entityVariable, methodInfo.getRight());
    // return query.fetchCount();
    bodyBuilder.appendFormalLine("return query.fetchCount();");
    // Use provided finder method to generate its implementation
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, method.getReturnType(), parameterTypes, parameterNames, bodyBuilder);
    return methodBuilder.build();
}
Also used : 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) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) MethodMetadata(org.springframework.roo.classpath.details.MethodMetadata) InvocableMemberBodyBuilder(org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)

Example 98 with JavaSymbolName

use of org.springframework.roo.model.JavaSymbolName in project spring-roo by spring-projects.

the class RepositoryJpaCustomImplMetadata method getCustomFindersImpl.

/**
 * Method that generates implementation methods for each finder which return type
 * is a projection or argument is a DTO.
 *
 * @param methodInfo
 * @return
 */
private MethodMetadata getCustomFindersImpl(Pair<MethodMetadata, PartTree> methodInfo, List<FieldMetadata> fields) {
    MethodMetadata method = methodInfo.getLeft();
    // Define method name
    JavaSymbolName methodName = method.getMethodName();
    // Define method parameter types
    List<AnnotatedJavaType> parameterTypes = method.getParameterTypes();
    // Define method parameter names
    List<JavaSymbolName> parameterNames = method.getParameterNames();
    MethodMetadata existingMethod = getGovernorMethod(methodName, AnnotatedJavaType.convertFromAnnotatedJavaTypes(parameterTypes));
    if (existingMethod != null) {
        return existingMethod;
    }
    // Generate body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // Getting variable name to use in the code
    JavaSymbolName globalSearch = getParameterNameFor(method, SpringletsJavaType.SPRINGLETS_GLOBAL_SEARCH);
    JavaSymbolName pageable = getParameterNameFor(method, SpringJavaType.PAGEABLE);
    String entity = this.entity.getSimpleTypeName();
    String entityVariable = StringUtils.uncapitalize(entity);
    JavaType finderParamType = parameterTypes.get(0).getJavaType();
    String finderParamName = parameterNames.get(0).getSymbolName();
    // Types to import
    JavaType returnType = getDomainTypeOfFinderMethod(method);
    bodyBuilder.newLine();
    // QEntity qEntity = QEntity.entity;
    bodyBuilder.appendFormalLine(String.format("%1$s %2$s = %1$s.%2$s;", getNameOfJavaType(entityQtype), entityVariable));
    bodyBuilder.newLine();
    // Construct query
    buildQuery(bodyBuilder, entityVariable, globalSearch, null, null, null, finderParamType, finderParamName, returnType, method.getMethodName(), methodInfo.getRight());
    bodyBuilder.newLine();
    // AttributeMappingBuilder mapping = buildMapper()
    StringBuffer mappingBuilderLine = new StringBuffer();
    mappingBuilderLine.append(String.format("%s mapping = buildMapper()", getNameOfJavaType(SpringletsJavaType.SPRINGLETS_QUERYDSL_REPOSITORY_SUPPORT_ATTRIBUTE_BUILDER)));
    // .map(entiyVarName, varName) ...
    if (!this.typesAreProjections.get(returnType)) {
        // 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(returnType);
        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, QUERYDSL_PROJECTIONS, returnType);
    // Use provided finder method to generate its implementation
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC, methodName, methodInfo.getLeft().getReturnType(), parameterTypes, parameterNames, bodyBuilder);
    return methodBuilder.build();
}
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)

Example 99 with JavaSymbolName

use of org.springframework.roo.model.JavaSymbolName in project spring-roo by spring-projects.

the class RepositoryJpaCustomImplMetadata method getFindAllImpl.

/**
 * Method that generates the findAll implementation method
 * @param findAllGlobalSearchMethod
 * @param ids the entity id fields
 * @param fields the entity fields to search for
 *
 * @return
 */
private MethodMetadata getFindAllImpl(MethodMetadata findAllGlobalSearchMethod, FieldMetadata idField, List<FieldMetadata> fields) {
    // Define method name
    JavaSymbolName methodName = findAllGlobalSearchMethod.getMethodName();
    // Define method parameter types
    List<AnnotatedJavaType> parameterTypes = findAllGlobalSearchMethod.getParameterTypes();
    // Define method parameter names
    List<JavaSymbolName> parameterNames = findAllGlobalSearchMethod.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, findAllGlobalSearchMethod.getMethodName(), findAllGlobalSearchMethod.getReturnType(), findAllGlobalSearchMethod.getParameterTypes(), findAllGlobalSearchMethod.getParameterNames(), null);
    // Generate body
    InvocableMemberBodyBuilder bodyBuilder = new InvocableMemberBodyBuilder();
    // Getting variable name to use in the code
    JavaSymbolName globalSearch = parameterNames.get(0);
    JavaSymbolName pageable = parameterNames.get(1);
    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();
    // 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)

Example 100 with JavaSymbolName

use of org.springframework.roo.model.JavaSymbolName in project spring-roo by spring-projects.

the class RepositoryJpaCustomImplMetadataProviderImpl method activate.

/**
 * This service is being activated so setup it:
 * <ul>
 * <li>Create and open the {@link MetadataDependencyRegistryTracker}.</li>
 * <li>Create and open the {@link CustomDataKeyDecoratorTracker}.</li>
 * <li>Registers {@link RooJavaType#ROO_REPOSITORY_JPA_CUSTOM_IMPL} as additional
 * JavaType that will trigger metadata registration.</li>
 * <li>Set ensure the governor type details represent a class.</li>
 * </ul>
 */
@Override
@SuppressWarnings("unchecked")
protected void activate(final ComponentContext cContext) {
    super.activate(cContext);
    BundleContext localContext = cContext.getBundleContext();
    this.registryTracker = new MetadataDependencyRegistryTracker(localContext, this, PhysicalTypeIdentifier.getMetadataIdentiferType(), getProvidesType());
    this.registryTracker.open();
    addMetadataTrigger(ROO_REPOSITORY_JPA_CUSTOM_IMPL);
    this.keyDecoratorTracker = new CustomDataKeyDecoratorTracker(localContext, getClass(), new LayerTypeMatcher(ROO_REPOSITORY_JPA_CUSTOM_IMPL, new JavaSymbolName(RooJpaRepositoryCustomImpl.REPOSITORY_ATTRIBUTE)));
    this.keyDecoratorTracker.open();
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) LayerTypeMatcher(org.springframework.roo.classpath.layers.LayerTypeMatcher) MetadataDependencyRegistryTracker(org.springframework.roo.metadata.internal.MetadataDependencyRegistryTracker) CustomDataKeyDecoratorTracker(org.springframework.roo.classpath.customdata.taggers.CustomDataKeyDecoratorTracker) BundleContext(org.osgi.framework.BundleContext)

Aggregations

JavaSymbolName (org.springframework.roo.model.JavaSymbolName)317 ArrayList (java.util.ArrayList)186 AnnotationMetadataBuilder (org.springframework.roo.classpath.details.annotations.AnnotationMetadataBuilder)155 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)143 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)142 JavaType (org.springframework.roo.model.JavaType)133 InvocableMemberBodyBuilder (org.springframework.roo.classpath.itd.InvocableMemberBodyBuilder)121 MethodMetadata (org.springframework.roo.classpath.details.MethodMetadata)119 SpringJavaType (org.springframework.roo.model.SpringJavaType)61 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)59 JdkJavaType (org.springframework.roo.model.JdkJavaType)59 FieldMetadataBuilder (org.springframework.roo.classpath.details.FieldMetadataBuilder)48 AnnotationMetadata (org.springframework.roo.classpath.details.annotations.AnnotationMetadata)47 SpringletsJavaType (org.springframework.roo.model.SpringletsJavaType)45 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)39 AnnotationAttributeValue (org.springframework.roo.classpath.details.annotations.AnnotationAttributeValue)36 ClassOrInterfaceTypeDetailsBuilder (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetailsBuilder)29 Jsr303JavaType (org.springframework.roo.model.Jsr303JavaType)29 StringAttributeValue (org.springframework.roo.classpath.details.annotations.StringAttributeValue)27 EnumDetails (org.springframework.roo.model.EnumDetails)25