diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/MyUserPrdicates.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/MyUserPrdicates.java deleted file mode 100644 index 6c3ed32149..0000000000 --- a/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/MyUserPrdicates.java +++ /dev/null @@ -1,23 +0,0 @@ -package org.baeldung.persistence.dao; - -import org.baeldung.persistence.model.QMyUser; - -import com.mysema.query.types.expr.BooleanExpression; - -public class MyUserPrdicates { - - public static BooleanExpression firstNameIsLike(final String term) { - final QMyUser user = QMyUser.myUser; - return user.firstName.containsIgnoreCase(term); - } - - public static BooleanExpression lastNameIsLike(final String term) { - final QMyUser user = QMyUser.myUser; - return user.lastName.containsIgnoreCase(term); - } - - public static BooleanExpression ageIsGreaterThan(final int minAge) { - final QMyUser user = QMyUser.myUser; - return user.age.goe(minAge); - } -} diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/MyUserPredicate.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/MyUserPredicate.java new file mode 100644 index 0000000000..7a00e7490c --- /dev/null +++ b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/MyUserPredicate.java @@ -0,0 +1,62 @@ +package org.baeldung.persistence.dao; + +import org.baeldung.persistence.model.MyUser; +import org.baeldung.web.util.SearchCriteria; + +import com.mysema.query.types.expr.BooleanExpression; +import com.mysema.query.types.path.NumberPath; +import com.mysema.query.types.path.PathBuilder; +import com.mysema.query.types.path.StringPath; + +public class MyUserPredicate { + + private SearchCriteria criteria; + + public MyUserPredicate() { + + } + + public MyUserPredicate(final SearchCriteria criteria) { + this.criteria = criteria; + } + + public BooleanExpression getPredicate() { + final PathBuilder entityPath = new PathBuilder(MyUser.class, "myUser"); + + if (isNumeric(criteria.getValue().toString())) { + System.out.println("Nuumber"); + final NumberPath path = entityPath.getNumber(criteria.getKey(), Integer.class); + final int value = Integer.parseInt(criteria.getValue().toString()); + if (criteria.getOperation().equalsIgnoreCase(":")) { + return path.eq(value); + } else if (criteria.getOperation().equalsIgnoreCase(">")) { + return path.goe(value); + } else if (criteria.getOperation().equalsIgnoreCase("<")) { + return path.loe(value); + } + } else { + final StringPath path = entityPath.getString(criteria.getKey()); + if (criteria.getOperation().equalsIgnoreCase(":")) { + return path.containsIgnoreCase(criteria.getValue().toString()); + } + } + return null; + } + + public SearchCriteria getCriteria() { + return criteria; + } + + public void setCriteria(final SearchCriteria criteria) { + this.criteria = criteria; + } + + public static boolean isNumeric(final String str) { + try { + Integer.parseInt(str); + } catch (final NumberFormatException e) { + return false; + } + return true; + } +} diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/MyUserPredicatesBuilder.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/MyUserPredicatesBuilder.java index ef59011e45..1f7f3bf197 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/MyUserPredicatesBuilder.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/MyUserPredicatesBuilder.java @@ -1,32 +1,25 @@ package org.baeldung.persistence.dao; -import static org.baeldung.persistence.dao.MyUserPrdicates.ageIsGreaterThan; -import static org.baeldung.persistence.dao.MyUserPrdicates.firstNameIsLike; -import static org.baeldung.persistence.dao.MyUserPrdicates.lastNameIsLike; - import java.util.ArrayList; import java.util.List; -import java.util.Map; + +import org.baeldung.web.util.SearchCriteria; import com.mysema.query.types.expr.BooleanExpression; public class MyUserPredicatesBuilder { - public static BooleanExpression buildUserPredicates(final Map params) { + public static BooleanExpression buildUserPredicates(final List params) { if (params.size() == 0) return null; final List predicates = new ArrayList(); - String key, value; + MyUserPredicate predicate; - for (final Map.Entry param : params.entrySet()) { - key = param.getKey(); - value = param.getValue().toString(); - if (key.equalsIgnoreCase("age")) { - predicates.add(ageIsGreaterThan(Integer.parseInt(value))); - } else if (key.equalsIgnoreCase("firstName")) { - predicates.add(firstNameIsLike(value)); - } else if (key.equalsIgnoreCase("lastName")) { - predicates.add(lastNameIsLike(value)); + for (final SearchCriteria param : params) { + predicate = new MyUserPredicate(param); + final BooleanExpression exp = predicate.getPredicate(); + if (exp != null) { + predicates.add(exp); } } diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/impl/MyUserService.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/impl/MyUserService.java index e92c97e42c..20bc67a01d 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/impl/MyUserService.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/impl/MyUserService.java @@ -1,10 +1,11 @@ package org.baeldung.persistence.service.impl; -import java.util.Map; +import java.util.List; import org.baeldung.persistence.dao.MyUserPredicatesBuilder; import org.baeldung.persistence.dao.MyUserRepository; import org.baeldung.persistence.model.MyUser; +import org.baeldung.web.util.SearchCriteria; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -22,7 +23,7 @@ public class MyUserService { super(); } - public Iterable search(final Map params) { + public Iterable search(final List params) { final BooleanExpression predicate = MyUserPredicatesBuilder.buildUserPredicates(params); if (predicate == null) return repository.findAll(); diff --git a/spring-security-rest-full/src/main/java/org/baeldung/web/controller/UserController.java b/spring-security-rest-full/src/main/java/org/baeldung/web/controller/UserController.java index 837d7ccf3b..ded1b299b4 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/web/controller/UserController.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/web/controller/UserController.java @@ -1,9 +1,7 @@ package org.baeldung.web.controller; import java.util.ArrayList; -import java.util.HashMap; import java.util.List; -import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -65,13 +63,13 @@ public class UserController { @RequestMapping(method = RequestMethod.GET, value = "/myusers") @ResponseBody public Iterable findAllByQuerydsl(@RequestParam(value = "search", required = false) final String search) { - final Map params = new HashMap(); + final List params = new ArrayList(); if (search != null) { - final Pattern pattern = Pattern.compile("(\\w+?):(\\w+?),"); + final Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),"); final Matcher matcher = pattern.matcher(search + ","); while (matcher.find()) { - params.put(matcher.group(1), matcher.group(2)); + params.add(new SearchCriteria(matcher.group(1), matcher.group(2), matcher.group(3))); } } return myService.search(params); diff --git a/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPACriteriaQueryTest.java b/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPACriteriaQueryTest.java index 02f402a77e..9a2716232d 100644 --- a/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPACriteriaQueryTest.java +++ b/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPACriteriaQueryTest.java @@ -1,12 +1,14 @@ package org.baeldung.persistence.query; -import static org.junit.Assert.assertEquals; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.collection.IsIn.isIn; +import static org.hamcrest.core.IsNot.not; import java.util.ArrayList; import java.util.List; +import org.baeldung.persistence.dao.IUserDAO; import org.baeldung.persistence.model.User; -import org.baeldung.persistence.service.impl.UserService; import org.baeldung.spring.PersistenceConfig; import org.baeldung.web.util.SearchCriteria; import org.junit.Before; @@ -26,7 +28,7 @@ import org.springframework.transaction.annotation.Transactional; public class JPACriteriaQueryTest { @Autowired - private UserService userService; + private IUserDAO userApi; private User userJohn; @@ -39,14 +41,14 @@ public class JPACriteriaQueryTest { userJohn.setLastName("Doe"); userJohn.setEmail("john@doe.com"); userJohn.setAge(22); - userService.saveUser(userJohn); + userApi.save(userJohn); userTom = new User(); userTom.setFirstName("Tom"); userTom.setLastName("Doe"); userTom.setEmail("tom@doe.com"); userTom.setAge(26); - userService.saveUser(userTom); + userApi.save(userTom); } @Test @@ -55,10 +57,10 @@ public class JPACriteriaQueryTest { params.add(new SearchCriteria("firstName", ":", "John")); params.add(new SearchCriteria("lastName", ":", "Doe")); - final List result = userService.searchUser(params); + final List results = userApi.searchUser(params); - assertEquals(1, result.size()); - assertEquals(userJohn.getEmail(), result.get(0).getEmail()); + assertThat(userJohn, isIn(results)); + assertThat(userTom, not(isIn(results))); } @Test @@ -66,8 +68,9 @@ public class JPACriteriaQueryTest { final List params = new ArrayList(); params.add(new SearchCriteria("lastName", ":", "Doe")); - final List result = userService.searchUser(params); - assertEquals(2, result.size()); + final List results = userApi.searchUser(params); + assertThat(userJohn, isIn(results)); + assertThat(userTom, isIn(results)); } @Test @@ -76,10 +79,10 @@ public class JPACriteriaQueryTest { params.add(new SearchCriteria("lastName", ":", "Doe")); params.add(new SearchCriteria("age", ">", "25")); - final List result = userService.searchUser(params); + final List results = userApi.searchUser(params); - assertEquals(1, result.size()); - assertEquals(userTom.getEmail(), result.get(0).getEmail()); + assertThat(userTom, isIn(results)); + assertThat(userJohn, not(isIn(results))); } @Test @@ -88,8 +91,9 @@ public class JPACriteriaQueryTest { params.add(new SearchCriteria("firstName", ":", "Adam")); params.add(new SearchCriteria("lastName", ":", "Fox")); - final List result = userService.searchUser(params); - assertEquals(0, result.size()); + final List results = userApi.searchUser(params); + assertThat(userJohn, not(isIn(results))); + assertThat(userTom, not(isIn(results))); } @Test @@ -97,9 +101,9 @@ public class JPACriteriaQueryTest { final List params = new ArrayList(); params.add(new SearchCriteria("firstName", ":", "jo")); - final List result = userService.searchUser(params); + final List results = userApi.searchUser(params); - assertEquals(1, result.size()); - assertEquals(userJohn.getEmail(), result.get(0).getEmail()); + assertThat(userJohn, isIn(results)); + assertThat(userTom, not(isIn(results))); } } \ No newline at end of file diff --git a/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPAQuerydslTest.java b/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPAQuerydslTest.java index fd0d83432d..2f2978378d 100644 --- a/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPAQuerydslTest.java +++ b/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPAQuerydslTest.java @@ -1,17 +1,18 @@ package org.baeldung.persistence.query; -import static org.baeldung.persistence.dao.MyUserPrdicates.ageIsGreaterThan; -import static org.baeldung.persistence.dao.MyUserPrdicates.firstNameIsLike; -import static org.baeldung.persistence.dao.MyUserPrdicates.lastNameIsLike; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.collection.IsEmptyIterable.emptyIterable; import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder; import static org.hamcrest.collection.IsIterableContainingInOrder.contains; import static org.hamcrest.core.IsNot.not; -import org.baeldung.persistence.dao.MyUserRepository; +import java.util.ArrayList; +import java.util.List; + import org.baeldung.persistence.model.MyUser; +import org.baeldung.persistence.service.impl.MyUserService; import org.baeldung.spring.PersistenceConfig; +import org.baeldung.web.util.SearchCriteria; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -29,7 +30,7 @@ import org.springframework.transaction.annotation.Transactional; public class JPAQuerydslTest { @Autowired - private MyUserRepository repository; + private MyUserService service; private MyUser userJohn; @@ -42,49 +43,67 @@ public class JPAQuerydslTest { userJohn.setLastName("Doe"); userJohn.setEmail("john@doe.com"); userJohn.setAge(22); - repository.save(userJohn); + service.save(userJohn); userTom = new MyUser(); userTom.setFirstName("Tom"); userTom.setLastName("Doe"); userTom.setEmail("tom@doe.com"); userTom.setAge(26); - repository.save(userTom); + service.save(userTom); } @Test public void givenLast_whenGettingListOfUsers_thenCorrect() { - final Iterable result = repository.findAll(lastNameIsLike("doe")); - assertThat(result, containsInAnyOrder(userJohn, userTom)); + final List params = new ArrayList(); + params.add(new SearchCriteria("lastName", ":", "Doe")); + + final Iterable results = service.search(params); + assertThat(results, containsInAnyOrder(userJohn, userTom)); } @Test public void givenFirstAndLastName_whenGettingListOfUsers_thenCorrect() { - final Iterable result = repository.findAll(lastNameIsLike("doe").and(firstNameIsLike("john"))); + final List params = new ArrayList(); + params.add(new SearchCriteria("firstName", ":", "John")); + params.add(new SearchCriteria("lastName", ":", "Doe")); - assertThat(result, contains(userJohn)); - assertThat(result, not(contains(userTom))); + final Iterable results = service.search(params); + + assertThat(results, contains(userJohn)); + assertThat(results, not(contains(userTom))); } @Test public void givenLastAndAge_whenGettingListOfUsers_thenCorrect() { - final Iterable result = repository.findAll(lastNameIsLike("doe").and(ageIsGreaterThan(25))); + final List params = new ArrayList(); + params.add(new SearchCriteria("lastName", ":", "Doe")); + params.add(new SearchCriteria("age", ">", "25")); - assertThat(result, contains(userTom)); - assertThat(result, not(contains(userJohn))); + final Iterable results = service.search(params); + + assertThat(results, contains(userTom)); + assertThat(results, not(contains(userJohn))); } @Test public void givenWrongFirstAndLast_whenGettingListOfUsers_thenCorrect() { - final Iterable result = repository.findAll(lastNameIsLike("Adam").and(firstNameIsLike("Fox"))); - assertThat(result, emptyIterable()); + final List params = new ArrayList(); + params.add(new SearchCriteria("firstName", ":", "Adam")); + params.add(new SearchCriteria("lastName", ":", "Fox")); + + final Iterable results = service.search(params); + assertThat(results, emptyIterable()); } @Test public void givenPartialFirst_whenGettingListOfUsers_thenCorrect() { - final Iterable result = repository.findAll(firstNameIsLike("jo")); + final List params = new ArrayList(); + params.add(new SearchCriteria("firstName", ":", "jo")); - assertThat(result, contains(userJohn)); - assertThat(result, not(contains(userTom))); + final Iterable results = service.search(params); + + assertThat(results, contains(userJohn)); + assertThat(results, not(contains(userTom))); } -} +} \ No newline at end of file diff --git a/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPASpecificationsTest.java b/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPASpecificationsTest.java index ff5a7a2485..7ff447c285 100644 --- a/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPASpecificationsTest.java +++ b/spring-security-rest-full/src/test/java/org/baeldung/persistence/query/JPASpecificationsTest.java @@ -1,6 +1,8 @@ package org.baeldung.persistence.query; -import static org.junit.Assert.assertEquals; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.collection.IsIn.isIn; +import static org.hamcrest.core.IsNot.not; import java.util.List; @@ -53,46 +55,48 @@ public class JPASpecificationsTest { @Test public void givenLast_whenGettingListOfUsers_thenCorrect() { final UserSpecification spec = new UserSpecification(new SearchCriteria("lastName", ":", "doe")); - final List result = repository.findAll(spec); + final List results = repository.findAll(spec); - assertEquals(2, result.size()); + assertThat(userJohn, isIn(results)); + assertThat(userTom, isIn(results)); } @Test public void givenFirstAndLastName_whenGettingListOfUsers_thenCorrect() { final UserSpecification spec = new UserSpecification(new SearchCriteria("firstName", ":", "john")); final UserSpecification spec1 = new UserSpecification(new SearchCriteria("lastName", ":", "doe")); - final List result = repository.findAll(Specifications.where(spec).and(spec1)); + final List results = repository.findAll(Specifications.where(spec).and(spec1)); - assertEquals(1, result.size()); - assertEquals(userJohn.getEmail(), result.get(0).getEmail()); + assertThat(userJohn, isIn(results)); + assertThat(userTom, not(isIn(results))); } @Test public void givenLastAndAge_whenGettingListOfUsers_thenCorrect() { final UserSpecification spec = new UserSpecification(new SearchCriteria("age", ">", "25")); final UserSpecification spec1 = new UserSpecification(new SearchCriteria("lastName", ":", "doe")); - final List result = repository.findAll(Specifications.where(spec).and(spec1)); + final List results = repository.findAll(Specifications.where(spec).and(spec1)); - assertEquals(1, result.size()); - assertEquals(userTom.getEmail(), result.get(0).getEmail()); + assertThat(userTom, isIn(results)); + assertThat(userJohn, not(isIn(results))); } @Test public void givenWrongFirstAndLast_whenGettingListOfUsers_thenCorrect() { final UserSpecification spec = new UserSpecification(new SearchCriteria("firstName", ":", "Adam")); final UserSpecification spec1 = new UserSpecification(new SearchCriteria("lastName", ":", "Fox")); - final List result = repository.findAll(Specifications.where(spec).and(spec1)); + final List results = repository.findAll(Specifications.where(spec).and(spec1)); - assertEquals(0, result.size()); + assertThat(userJohn, not(isIn(results))); + assertThat(userTom, not(isIn(results))); } @Test public void givenPartialFirst_whenGettingListOfUsers_thenCorrect() { final UserSpecification spec = new UserSpecification(new SearchCriteria("firstName", ":", "jo")); - final List result = repository.findAll(spec); + final List results = repository.findAll(spec); - assertEquals(1, result.size()); - assertEquals(userJohn.getEmail(), result.get(0).getEmail()); + assertThat(userJohn, isIn(results)); + assertThat(userTom, not(isIn(results))); } } \ No newline at end of file