Search in sources :

Example 1 with FinderParameter

use of org.springframework.roo.addon.layers.repository.jpa.addon.finder.parser.FinderParameter in project spring-roo by spring-projects.

the class RepositoryJpaCustomImplMetadata method buildFormBeanFilterBody.

private void buildFormBeanFilterBody(InvocableMemberBodyBuilder bodyBuilder, JavaType formBeanType, String formBeanParameterName, String entityVariable, PartTree partTree) {
    // BooleanBuilder searchCondition = new BooleanBuilder();
    bodyBuilder.appendFormalLine("%1$s searchCondition = new %1$s();", getNameOfJavaType(QUERYDSL_BOOLEAN_BUILDER));
    // formBean is a DTO, filter only by finder params
    List<FinderParameter> finderParamsList = partTree.getParameters();
    for (FinderParameter finderParameter : finderParamsList) {
        // if (formSearch.getField() != null) {
        String accessorMethodName = BeanInfoUtils.getAccessorMethodName(finderParameter.getName(), finderParameter.getType()).getSymbolName();
        bodyBuilder.appendIndent();
        bodyBuilder.appendFormalLine(String.format("if (%s.%s() != null) {", formBeanParameterName, accessorMethodName));
        // Get path field name from field mappings
        String pathFieldName = getFinderParamPath(finderParameter, entityVariable);
        // query.where(myEntity.field.eq(formBean.getField()));
        bodyBuilder.appendIndent();
        bodyBuilder.appendIndent();
        bodyBuilder.appendFormalLine(String.format("searchCondition.and(%s.eq(%s.%s()));", pathFieldName, formBeanParameterName, accessorMethodName));
        // }
        bodyBuilder.appendIndent();
        bodyBuilder.appendFormalLine("}");
    }
    // if (searchCondition.hasValue()) {
    bodyBuilder.appendFormalLine("if (searchCondition.hasValue()) {");
    // query.where(searchCondition);
    bodyBuilder.indent();
    bodyBuilder.appendFormalLine("query.where(searchCondition);");
    // }
    bodyBuilder.indentRemove();
    bodyBuilder.appendFormalLine("}");
}
Also used : FinderParameter(org.springframework.roo.addon.layers.repository.jpa.addon.finder.parser.FinderParameter)

Example 2 with FinderParameter

use of org.springframework.roo.addon.layers.repository.jpa.addon.finder.parser.FinderParameter in project spring-roo by spring-projects.

the class RepositoryJpaMetadata method getFinderMethod.

/**
 * Method that generates finder method on current interface
 *
 * @param finderMethod
 * @return
 */
private MethodMetadataBuilder getFinderMethod(FinderMethod finderMethod) {
    // Define method parameter types and parameter names
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    for (FinderParameter param : finderMethod.getParameters()) {
        parameterTypes.add(AnnotatedJavaType.convertFromJavaType(param.getType()));
        parameterNames.add(param.getName());
    }
    // Add additional Pageable method
    parameterTypes.add(AnnotatedJavaType.convertFromJavaType(SpringJavaType.PAGEABLE));
    parameterNames.add(new JavaSymbolName("pageable"));
    // Use the MethodMetadataBuilder for easy creation of MethodMetadata
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC + Modifier.ABSTRACT, finderMethod.getMethodName(), finderMethod.getReturnType(), parameterTypes, parameterNames, null);
    // Build and return a MethodMetadata
    return methodBuilder;
// instance
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) FinderParameter(org.springframework.roo.addon.layers.repository.jpa.addon.finder.parser.FinderParameter) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList)

Example 3 with FinderParameter

use of org.springframework.roo.addon.layers.repository.jpa.addon.finder.parser.FinderParameter in project spring-roo by spring-projects.

the class RepositoryJpaMetadata method getCountMethod.

/**
 * Method that generates finder method on current interface
 *
 * @param finderMethod
 * @return
 */
private MethodMetadataBuilder getCountMethod(FinderMethod finderMethod) {
    // Define method parameter types and parameter names
    List<AnnotatedJavaType> parameterTypes = new ArrayList<AnnotatedJavaType>();
    List<JavaSymbolName> parameterNames = new ArrayList<JavaSymbolName>();
    for (FinderParameter param : finderMethod.getParameters()) {
        parameterTypes.add(AnnotatedJavaType.convertFromJavaType(param.getType()));
        parameterNames.add(param.getName());
    }
    // Create count method name
    String countName = finderMethod.getMethodName().getSymbolName();
    if (StringUtils.startsWith(countName, "find")) {
        countName = StringUtils.removeStart(countName, "find");
    } else if (StringUtils.startsWith(countName, "query")) {
        countName = StringUtils.removeStart(countName, "query");
    } else if (StringUtils.startsWith(countName, "read")) {
        countName = StringUtils.removeStart(countName, "read");
    }
    countName = "count".concat(countName);
    JavaSymbolName countMethodName = new JavaSymbolName(countName);
    // Use the MethodMetadataBuilder for easy creation of MethodMetadata
    MethodMetadataBuilder methodBuilder = new MethodMetadataBuilder(getId(), Modifier.PUBLIC + Modifier.ABSTRACT, countMethodName, JavaType.LONG_PRIMITIVE, parameterTypes, parameterNames, null);
    // Build and return a MethodMetadata
    return methodBuilder;
// instance
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) FinderParameter(org.springframework.roo.addon.layers.repository.jpa.addon.finder.parser.FinderParameter) MethodMetadataBuilder(org.springframework.roo.classpath.details.MethodMetadataBuilder) AnnotatedJavaType(org.springframework.roo.classpath.details.annotations.AnnotatedJavaType) ArrayList(java.util.ArrayList)

Example 4 with FinderParameter

use of org.springframework.roo.addon.layers.repository.jpa.addon.finder.parser.FinderParameter in project spring-roo by spring-projects.

the class RepositoryJpaMetadataProviderImpl method checkDtoFieldsForFinder.

/**
 * Validates that all dto fields matches with parameters on finder of a repository
 *
 * @param formBeanDetails
 * @param finder
 * @param repository
 */
private void checkDtoFieldsForFinder(ClassOrInterfaceTypeDetails formBeanDetails, PartTree finder, JavaType repository) {
    final JavaType dtoType = formBeanDetails.getName();
    final String finderName = finder.getOriginalQuery();
    FieldMetadata paramField, dtoField;
    JavaType paramType, dtoFieldType;
    for (FinderParameter param : finder.getParameters()) {
        paramField = param.getPath().peek();
        dtoField = formBeanDetails.getField(paramField.getFieldName());
        Validate.notNull(dtoField, "Field '%s' not found on DTO %s for finder '%s' of %s", paramField.getFieldName(), dtoType, finderName, repository);
        paramType = paramField.getFieldType().getBaseType();
        dtoFieldType = dtoField.getFieldType().getBaseType();
        Validate.isTrue(paramType.equals(dtoFieldType), "Type missmatch for field '%s' on DTO %s for finder '%s' of %s: excepted %s and got %s", dtoField.getFieldName(), dtoType, finderName, repository, paramType, dtoFieldType);
    }
}
Also used : RooJavaType(org.springframework.roo.model.RooJavaType) JavaType(org.springframework.roo.model.JavaType) FieldMetadata(org.springframework.roo.classpath.details.FieldMetadata) FinderParameter(org.springframework.roo.addon.layers.repository.jpa.addon.finder.parser.FinderParameter)

Example 5 with FinderParameter

use of org.springframework.roo.addon.layers.repository.jpa.addon.finder.parser.FinderParameter in project spring-roo by spring-projects.

the class PartTreeUnitTest method validateOneParameter.

@Test
public void validateOneParameter() throws Exception {
    List<FinderParameter> parameters = new ArrayList<FinderParameter>();
    parameters.add(new FinderParameter(JavaType.STRING, new JavaSymbolName("text")));
    assertEqualsParameters(parameters, new PartTree("countByTextContaining", memberDetails).getParameters());
    parameters.add(new FinderParameter(JavaType.INT_OBJECT, new JavaSymbolName("number")));
    assertEqualsParameters(parameters, new PartTree("findByTextContainingAndNumberIsLessThan", memberDetails).getParameters());
    assertEqualsParameters(parameters, new PartTree("findByTextContainingAndNumberIsLessThanOrIsNull", memberDetails).getParameters());
}
Also used : JavaSymbolName(org.springframework.roo.model.JavaSymbolName) FinderParameter(org.springframework.roo.addon.layers.repository.jpa.addon.finder.parser.FinderParameter) ArrayList(java.util.ArrayList) PartTree(org.springframework.roo.addon.layers.repository.jpa.addon.finder.parser.PartTree) Test(org.junit.Test)

Aggregations

FinderParameter (org.springframework.roo.addon.layers.repository.jpa.addon.finder.parser.FinderParameter)12 ArrayList (java.util.ArrayList)9 JavaSymbolName (org.springframework.roo.model.JavaSymbolName)8 PartTree (org.springframework.roo.addon.layers.repository.jpa.addon.finder.parser.PartTree)6 JavaType (org.springframework.roo.model.JavaType)5 Test (org.junit.Test)4 RooJavaType (org.springframework.roo.model.RooJavaType)4 FieldMetadata (org.springframework.roo.classpath.details.FieldMetadata)3 List (java.util.List)2 ClassOrInterfaceTypeDetails (org.springframework.roo.classpath.details.ClassOrInterfaceTypeDetails)2 MethodMetadataBuilder (org.springframework.roo.classpath.details.MethodMetadataBuilder)2 AnnotatedJavaType (org.springframework.roo.classpath.details.annotations.AnnotatedJavaType)2 MemberDetails (org.springframework.roo.classpath.scanner.MemberDetails)2 Date (java.util.Date)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 Pair (org.apache.commons.lang3.tuple.Pair)1 JpaEntityMetadata (org.springframework.roo.addon.jpa.addon.entity.JpaEntityMetadata)1 FinderMethod (org.springframework.roo.addon.layers.repository.jpa.addon.finder.parser.FinderMethod)1 RooFinder (org.springframework.roo.addon.layers.repository.jpa.annotations.finder.RooFinder)1