Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add likeIgnoreCase method, closes #10 #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ enum CriteriaOperation {
EQUAL_TO_MANY,
EQUAL_TO_ONE,
LIKE,
LIKE_IGNORE_CASE,
IS_NULL,
IS_NOT_NULL,
IN,
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/kodgemisi/specification/FilterCriteria.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ class FilterCriteria<T> {

private final ConditionType conditionType;

// TODO: it should not be constant value
private final boolean caseSensitive = false;

FilterCriteria(String key, T value, CriteriaOperation operation, Class<T> clazz, ConditionType conditionType) {
this.key = key;
this.value = value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,13 @@ public Predicate toPredicate(Root<E> 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<String> parameterExpression = criteriaBuilder.literal("%" + String.valueOf(filterCriteria.getValue()) + "%");
return criteriaBuilder.like(criteriaBuilder.upper(path.as(String.class)), criteriaBuilder.upper(parameterExpression));
}

case IN: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,39 @@ public GenericSpecificationBuilder<E> like(String key, Object value, RelationTyp
return addCriteria(key, value, CriteriaOperation.LIKE, relationType);
}

/**
* Adds a new ignore-case "like" criteria to the filterCriteriaList
* <blockquote><pre>
* GenericSpecificationBuilder.of(Person.class)
* .likeIgnoreCase("bio", keyword)
* .build();
* </pre></blockquote>
*
* @param key field name
* @param value
* @return
*/
public GenericSpecificationBuilder<E> 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
* <blockquote><pre>
* GenericSpecificationBuilder.of(Person.class)
* .likeIgnoreCase("department.name", departmentName, {@link com.kodgemisi.specification.RelationType} RelationType.TO_ONE)
* .build();
* </pre></blockquote>
*
* @param key field name
* @param value
* @return
*/
public GenericSpecificationBuilder<E> likeIgnoreCase(String key, Object value, RelationType relationType) {
return addCriteria(key, value, CriteriaOperation.LIKE_IGNORE_CASE, relationType);
}

public GenericSpecificationBuilder<E> isNull(String key) {
return addCriteria(key, CriteriaOperation.IS_NULL);
}
Expand Down