|
@@ -3,11 +3,13 @@ package com.izouma.nineth.utils;
|
|
|
import com.izouma.nineth.annotations.Searchable;
|
|
import com.izouma.nineth.annotations.Searchable;
|
|
|
import com.izouma.nineth.annotations.SearchableOne;
|
|
import com.izouma.nineth.annotations.SearchableOne;
|
|
|
import com.izouma.nineth.dto.PageQuery;
|
|
import com.izouma.nineth.dto.PageQuery;
|
|
|
|
|
+import com.izouma.nineth.enums.SearchMode;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
import org.springframework.data.domain.PageRequest;
|
|
import org.springframework.data.domain.PageRequest;
|
|
|
import org.springframework.data.domain.Sort;
|
|
import org.springframework.data.domain.Sort;
|
|
|
import org.springframework.data.jpa.domain.Specification;
|
|
import org.springframework.data.jpa.domain.Specification;
|
|
|
|
|
+import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
import javax.persistence.criteria.CriteriaBuilder;
|
|
import javax.persistence.criteria.CriteriaBuilder;
|
|
|
import javax.persistence.criteria.CriteriaQuery;
|
|
import javax.persistence.criteria.CriteriaQuery;
|
|
@@ -23,7 +25,15 @@ import java.util.regex.Pattern;
|
|
|
|
|
|
|
|
@Slf4j
|
|
@Slf4j
|
|
|
@SuppressWarnings("ALL")
|
|
@SuppressWarnings("ALL")
|
|
|
|
|
+@Component
|
|
|
public class JpaUtils {
|
|
public class JpaUtils {
|
|
|
|
|
+
|
|
|
|
|
+ private static SearchMode defaultSearchMode = SearchMode.PREFIX;
|
|
|
|
|
+
|
|
|
|
|
+ public static void setDefaultSearchMode(SearchMode defaultSearchMode) {
|
|
|
|
|
+ JpaUtils.defaultSearchMode = defaultSearchMode;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
public static PageRequest toPageRequest(PageQuery pageQuery) {
|
|
public static PageRequest toPageRequest(PageQuery pageQuery) {
|
|
|
PageRequest pageRequest;
|
|
PageRequest pageRequest;
|
|
|
if (StringUtils.isNotEmpty(pageQuery.getSort())) {
|
|
if (StringUtils.isNotEmpty(pageQuery.getSort())) {
|
|
@@ -145,7 +155,7 @@ public class JpaUtils {
|
|
|
} else if (String.class == fieldType) {
|
|
} else if (String.class == fieldType) {
|
|
|
SearchableOne annotation = field.getAnnotation(SearchableOne.class);
|
|
SearchableOne annotation = field.getAnnotation(SearchableOne.class);
|
|
|
if (annotation != null && annotation.value()) {
|
|
if (annotation != null && annotation.value()) {
|
|
|
- and.add(criteriaBuilder.like(root.get(field.getName()), "%" + value + "%"));
|
|
|
|
|
|
|
+ and.add(criteriaBuilder.like(root.get(field.getName()), value + "%"));
|
|
|
} else {
|
|
} else {
|
|
|
and.add(criteriaBuilder.and(criteriaBuilder.equal(root.get(property), value)));
|
|
and.add(criteriaBuilder.and(criteriaBuilder.equal(root.get(property), value)));
|
|
|
}
|
|
}
|
|
@@ -171,8 +181,25 @@ public class JpaUtils {
|
|
|
continue;
|
|
continue;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+ SearchMode searchMode = annotation.mode();
|
|
|
|
|
+ if (searchMode == SearchMode.CONFIG) {
|
|
|
|
|
+ searchMode = defaultSearchMode;
|
|
|
|
|
+ }
|
|
|
if (field.getType() == String.class) {
|
|
if (field.getType() == String.class) {
|
|
|
- or.add(criteriaBuilder.like(root.get(field.getName()), "%" + pageQuery.getSearch() + "%"));
|
|
|
|
|
|
|
+ switch (searchMode) {
|
|
|
|
|
+ case PREFIX:
|
|
|
|
|
+ or.add(criteriaBuilder.like(root.get(field.getName()), pageQuery.getSearch() + "%"));
|
|
|
|
|
+ break;
|
|
|
|
|
+ case SUFFIX:
|
|
|
|
|
+ or.add(criteriaBuilder.like(root.get(field.getName()), "%" + pageQuery.getSearch()));
|
|
|
|
|
+ break;
|
|
|
|
|
+ case FULL:
|
|
|
|
|
+ or.add(criteriaBuilder.like(root.get(field.getName()), "%" + pageQuery.getSearch() + "%"));
|
|
|
|
|
+ break;
|
|
|
|
|
+ case EXACT:
|
|
|
|
|
+ or.add(criteriaBuilder.equal(root.get(field.getName()), pageQuery.getSearch()));
|
|
|
|
|
+ break;
|
|
|
|
|
+ }
|
|
|
} else if (field.getType() == Long.class || field.getType() == long.class) {
|
|
} else if (field.getType() == Long.class || field.getType() == long.class) {
|
|
|
try {
|
|
try {
|
|
|
or.add(criteriaBuilder.equal(root.get(field.getName()), Long.parseLong(pageQuery.getSearch())));
|
|
or.add(criteriaBuilder.equal(root.get(field.getName()), Long.parseLong(pageQuery.getSearch())));
|