diff --git a/README.md b/README.md index 43fe78f..9311af9 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,7 @@ It's enough to add this dependency to use this library. .greaterThanOrEqualTo("birthDate", minBirthDate) .lessThanOrEqualTo("birthDate", maxBirthDate) .like("about", keyword) + .likeIgnoreCase("about", keyword) .in("city", cities) .custom(customSpecification) .build(); diff --git a/src/main/java/com/kodgemisi/specification/CriteriaOperation.java b/src/main/java/com/kodgemisi/specification/CriteriaOperation.java index e2cc687..2fc1615 100644 --- a/src/main/java/com/kodgemisi/specification/CriteriaOperation.java +++ b/src/main/java/com/kodgemisi/specification/CriteriaOperation.java @@ -14,6 +14,7 @@ enum CriteriaOperation { EQUAL_TO_MANY, EQUAL_TO_ONE, LIKE, + LIKE_IGNORE_CASE, IS_NULL, IS_NOT_NULL, IN, diff --git a/src/main/java/com/kodgemisi/specification/FilterCriteria.java b/src/main/java/com/kodgemisi/specification/FilterCriteria.java index acc1fd3..4537cc7 100644 --- a/src/main/java/com/kodgemisi/specification/FilterCriteria.java +++ b/src/main/java/com/kodgemisi/specification/FilterCriteria.java @@ -38,9 +38,6 @@ class FilterCriteria { private final ConditionType conditionType; - // TODO: it should not be constant value - private final boolean caseSensitive = false; - FilterCriteria(String key, T value, CriteriaOperation operation, Class clazz, ConditionType conditionType) { this.key = key; this.value = value; diff --git a/src/main/java/com/kodgemisi/specification/GenericSpecification.java b/src/main/java/com/kodgemisi/specification/GenericSpecification.java index 0a6d352..8949371 100644 --- a/src/main/java/com/kodgemisi/specification/GenericSpecification.java +++ b/src/main/java/com/kodgemisi/specification/GenericSpecification.java @@ -70,13 +70,13 @@ public Predicate toPredicate(Root root, CriteriaQuery query, CriteriaBuild } case LIKE: { final Path path = resolvePath(root, filterCriteria.getKey(), filterCriteria.getRelationType()); - if (filterCriteria.isCaseSensitive()) { - criteriaBuilder.like(path.as(String.class), "%" + filterCriteria.getValue() + "%"); - } - else { - return criteriaBuilder.like(criteriaBuilder.lower(path.as(String.class)), - "%" + String.valueOf(filterCriteria.getValue()).toLowerCase() + "%"); - } + return criteriaBuilder.like(path.as(String.class), "%" + filterCriteria.getValue() + "%"); + } + + case LIKE_IGNORE_CASE: { + final Path path = resolvePath(root, filterCriteria.getKey(), filterCriteria.getRelationType()); + final Expression parameterExpression = criteriaBuilder.literal("%" + String.valueOf(filterCriteria.getValue()) + "%"); + return criteriaBuilder.like(criteriaBuilder.upper(path.as(String.class)), criteriaBuilder.upper(parameterExpression)); } case IN: { diff --git a/src/main/java/com/kodgemisi/specification/GenericSpecificationBuilder.java b/src/main/java/com/kodgemisi/specification/GenericSpecificationBuilder.java index 16e59cb..0f7a76b 100644 --- a/src/main/java/com/kodgemisi/specification/GenericSpecificationBuilder.java +++ b/src/main/java/com/kodgemisi/specification/GenericSpecificationBuilder.java @@ -210,6 +210,39 @@ public GenericSpecificationBuilder like(String key, Object value, RelationTyp return addCriteria(key, value, CriteriaOperation.LIKE, relationType); } + /** + * Adds a new ignore-case "like" criteria to the filterCriteriaList + *
+	 *     GenericSpecificationBuilder.of(Person.class)
+	 *     	.likeIgnoreCase("bio", keyword)
+	 *     	.build();
+	 * 
+ * + * @param key field name + * @param value + * @return + */ + public GenericSpecificationBuilder likeIgnoreCase(String key, Object value) { + return addCriteria(key, value, CriteriaOperation.LIKE_IGNORE_CASE, RelationType.NO_RELATION); + } + + /** + * Adds a new ignore-case "like" criteria to the filterCriteriaList by joining to given relation + * In order to define a relation, you must use "." delimiter after relation name + *
+	 *     GenericSpecificationBuilder.of(Person.class)
+	 *     	.likeIgnoreCase("department.name", departmentName, {@link com.kodgemisi.specification.RelationType} RelationType.TO_ONE)
+	 *     	.build();
+	 * 
+ * + * @param key field name + * @param value + * @return + */ + public GenericSpecificationBuilder likeIgnoreCase(String key, Object value, RelationType relationType) { + return addCriteria(key, value, CriteriaOperation.LIKE_IGNORE_CASE, relationType); + } + public GenericSpecificationBuilder isNull(String key) { return addCriteria(key, CriteriaOperation.IS_NULL); }