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("}");
}
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
}
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
}
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);
}
}
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());
}
Aggregations