From 3100b6b8fba2a0925a87db5a5a0fddfba87f6486 Mon Sep 17 00:00:00 2001 From: DOHA Date: Wed, 28 Jan 2015 17:56:05 +0200 Subject: [PATCH] modify spring query --- spring-security-rest-full/.classpath | 15 ++++-- ...te.eclipse.console.hibernateBuilder.launch | 7 +++ spring-security-rest-full/.project | 7 ++- .../.settings/org.eclipse.jdt.core.prefs | 1 + .../org.eclipse.wst.common.component | 2 + spring-security-rest-full/pom.xml | 2 +- .../dao/MyUserPredicatesBuilder.java | 16 ++++++- .../persistence/dao/UserSpecification.java | 12 +---- .../dao/UserSpecificationsBuilder.java | 16 ++++++- .../persistence/service/impl/UserService.java | 42 ----------------- .../web/controller/UserController.java | 46 +++++++++++++------ .../persistence/query/JPAQuerydslTest.java | 41 ++++++----------- 12 files changed, 105 insertions(+), 102 deletions(-) create mode 100644 spring-security-rest-full/.externalToolBuilders/org.hibernate.eclipse.console.hibernateBuilder.launch delete mode 100644 spring-security-rest-full/src/main/java/org/baeldung/persistence/service/impl/UserService.java diff --git a/spring-security-rest-full/.classpath b/spring-security-rest-full/.classpath index 7049a8b952..aef5aab76d 100644 --- a/spring-security-rest-full/.classpath +++ b/spring-security-rest-full/.classpath @@ -1,18 +1,23 @@ - + - - + + + + + + + - + @@ -23,7 +28,7 @@ - + diff --git a/spring-security-rest-full/.externalToolBuilders/org.hibernate.eclipse.console.hibernateBuilder.launch b/spring-security-rest-full/.externalToolBuilders/org.hibernate.eclipse.console.hibernateBuilder.launch new file mode 100644 index 0000000000..9bc0284d26 --- /dev/null +++ b/spring-security-rest-full/.externalToolBuilders/org.hibernate.eclipse.console.hibernateBuilder.launch @@ -0,0 +1,7 @@ + + + + + + + diff --git a/spring-security-rest-full/.project b/spring-security-rest-full/.project index e9390d617f..188b8b07fe 100644 --- a/spring-security-rest-full/.project +++ b/spring-security-rest-full/.project @@ -26,8 +26,13 @@ - org.hibernate.eclipse.console.hibernateBuilder + org.eclipse.ui.externaltools.ExternalToolBuilder + full,incremental, + + LaunchConfigHandle + <project>/.externalToolBuilders/org.hibernate.eclipse.console.hibernateBuilder.launch + diff --git a/spring-security-rest-full/.settings/org.eclipse.jdt.core.prefs b/spring-security-rest-full/.settings/org.eclipse.jdt.core.prefs index df81356514..9b54696b56 100644 --- a/spring-security-rest-full/.settings/org.eclipse.jdt.core.prefs +++ b/spring-security-rest-full/.settings/org.eclipse.jdt.core.prefs @@ -5,6 +5,7 @@ org.eclipse.jdt.core.compiler.annotation.nonnullbydefault=org.eclipse.jdt.annota org.eclipse.jdt.core.compiler.annotation.nullable=org.eclipse.jdt.annotation.Nullable org.eclipse.jdt.core.compiler.annotation.nullanalysis=disabled org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.methodParameters=do not generate org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7 org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve org.eclipse.jdt.core.compiler.compliance=1.7 diff --git a/spring-security-rest-full/.settings/org.eclipse.wst.common.component b/spring-security-rest-full/.settings/org.eclipse.wst.common.component index 3b942952f2..7ad7c118a0 100644 --- a/spring-security-rest-full/.settings/org.eclipse.wst.common.component +++ b/spring-security-rest-full/.settings/org.eclipse.wst.common.component @@ -5,6 +5,8 @@ + + diff --git a/spring-security-rest-full/pom.xml b/spring-security-rest-full/pom.xml index c9a785ee54..0a6b72d545 100644 --- a/spring-security-rest-full/pom.xml +++ b/spring-security-rest-full/pom.xml @@ -327,7 +327,7 @@ - + com.mysema.maven 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 1f7f3bf197..bd6c91daea 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 @@ -8,7 +8,21 @@ import org.baeldung.web.util.SearchCriteria; import com.mysema.query.types.expr.BooleanExpression; public class MyUserPredicatesBuilder { - public static BooleanExpression buildUserPredicates(final List params) { + private List params; + + public MyUserPredicatesBuilder() { + params = new ArrayList(); + } + + public MyUserPredicatesBuilder with(final String key, final String operation, final Object value) { + final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder(); + final List newParams = new ArrayList(params); + newParams.add(new SearchCriteria(key, operation, value)); + builder.params = newParams; + return builder; + } + + public BooleanExpression build() { if (params.size() == 0) return null; diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/UserSpecification.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/UserSpecification.java index 8de25ea494..7d37874fd2 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/UserSpecification.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/UserSpecification.java @@ -11,11 +11,7 @@ import org.springframework.data.jpa.domain.Specification; public class UserSpecification implements Specification { - private SearchCriteria criteria; - - public UserSpecification() { - - } + private final SearchCriteria criteria; public UserSpecification(final SearchCriteria criteria) { super(); @@ -26,10 +22,6 @@ public class UserSpecification implements Specification { return criteria; } - public void setCriteria(final SearchCriteria criteria) { - this.criteria = criteria; - } - @Override public Predicate toPredicate(final Root root, final CriteriaQuery query, final CriteriaBuilder builder) { if (criteria.getOperation().equalsIgnoreCase(">")) { @@ -38,7 +30,7 @@ public class UserSpecification implements Specification { return builder.lessThanOrEqualTo(root. get(criteria.getKey()), criteria.getValue().toString()); } else if (criteria.getOperation().equalsIgnoreCase(":")) { if (root.get(criteria.getKey()).getJavaType() == String.class) { - return builder.like(root.get(criteria.getKey()), "%" + criteria.getValue() + "%"); + return builder.like(root. get(criteria.getKey()), "%" + criteria.getValue() + "%"); } else { return builder.equal(root.get(criteria.getKey()), criteria.getValue()); } diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/UserSpecificationsBuilder.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/UserSpecificationsBuilder.java index 60ae6f2a4c..7c074a9b40 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/UserSpecificationsBuilder.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/UserSpecificationsBuilder.java @@ -10,7 +10,21 @@ import org.springframework.data.jpa.domain.Specifications; public class UserSpecificationsBuilder { - public static Specification buildUserSpecs(final List params) { + private List params; + + public UserSpecificationsBuilder() { + params = new ArrayList(); + } + + public UserSpecificationsBuilder with(final String key, final String operation, final Object value) { + final UserSpecificationsBuilder builder = new UserSpecificationsBuilder(); + final List newParams = new ArrayList(params); + newParams.add(new SearchCriteria(key, operation, value)); + builder.params = newParams; + return builder; + } + + public Specification build() { if (params.size() == 0) return null; diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/impl/UserService.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/impl/UserService.java deleted file mode 100644 index ef22d2853b..0000000000 --- a/spring-security-rest-full/src/main/java/org/baeldung/persistence/service/impl/UserService.java +++ /dev/null @@ -1,42 +0,0 @@ -package org.baeldung.persistence.service.impl; - -import java.util.List; - -import org.baeldung.persistence.dao.IUserDAO; -import org.baeldung.persistence.dao.UserRepository; -import org.baeldung.persistence.dao.UserSpecificationsBuilder; -import org.baeldung.persistence.model.User; -import org.baeldung.web.util.SearchCriteria; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.jpa.domain.Specification; -import org.springframework.stereotype.Service; -import org.springframework.transaction.annotation.Transactional; - -@Service -@Transactional -public class UserService { - @Autowired - private IUserDAO dao; - - @Autowired - private UserRepository repository; - - public UserService() { - super(); - } - - public void saveUser(final User user) { - dao.save(user); - } - - public List searchUser(final List params) { - return dao.searchUser(params); - } - - public List searchBySpecification(final List params) { - final Specification spec = UserSpecificationsBuilder.buildUserSpecs(params); - if (spec == null) - return repository.findAll(); - return repository.findAll(spec); - } -} 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 ded1b299b4..f63b097071 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 @@ -5,26 +5,35 @@ import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; +import org.baeldung.persistence.dao.IUserDAO; +import org.baeldung.persistence.dao.MyUserPredicatesBuilder; +import org.baeldung.persistence.dao.MyUserRepository; +import org.baeldung.persistence.dao.UserRepository; +import org.baeldung.persistence.dao.UserSpecificationsBuilder; import org.baeldung.persistence.model.MyUser; import org.baeldung.persistence.model.User; -import org.baeldung.persistence.service.impl.MyUserService; -import org.baeldung.persistence.service.impl.UserService; import org.baeldung.web.util.SearchCriteria; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.jpa.domain.Specification; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; +import com.mysema.query.types.expr.BooleanExpression; + @Controller public class UserController { @Autowired - private UserService service; + private IUserDAO service; @Autowired - private MyUserService myService; + private UserRepository dao; + + @Autowired + private MyUserRepository mydao; public UserController() { super(); @@ -47,32 +56,39 @@ public class UserController { @RequestMapping(method = RequestMethod.GET, value = "/users/spec") @ResponseBody - public List findAllBySpecification(@RequestParam(value = "search", required = false) final String search) { - final List params = new ArrayList(); - + public List findAllBySpecification(@RequestParam(value = "search") final String search) { + UserSpecificationsBuilder builder = new UserSpecificationsBuilder(); if (search != null) { final Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),"); final Matcher matcher = pattern.matcher(search + ","); while (matcher.find()) { - params.add(new SearchCriteria(matcher.group(1), matcher.group(2), matcher.group(3))); + builder = builder.with(matcher.group(1), matcher.group(2), matcher.group(3)); } } - return service.searchBySpecification(params); + final Specification spec = builder.build(); + if (spec == null) + return dao.findAll(); + else + return dao.findAll(spec); } @RequestMapping(method = RequestMethod.GET, value = "/myusers") @ResponseBody - public Iterable findAllByQuerydsl(@RequestParam(value = "search", required = false) final String search) { - final List params = new ArrayList(); + public Iterable findAllByQuerydsl(@RequestParam(value = "search") final String search) { + MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder(); if (search != null) { final Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),"); final Matcher matcher = pattern.matcher(search + ","); while (matcher.find()) { - params.add(new SearchCriteria(matcher.group(1), matcher.group(2), matcher.group(3))); + builder = builder.with(matcher.group(1), matcher.group(2), matcher.group(3)); } } - return myService.search(params); + final BooleanExpression exp = builder.build(); + if (exp == null) + return mydao.findAll(); + else + return mydao.findAll(exp); } @RequestMapping(method = RequestMethod.GET, value = "/users/new") @@ -83,7 +99,7 @@ public class UserController { user.setLastName(last); user.setEmail("john@doe.com"); user.setAge(age); - service.saveUser(user); + dao.save(user); return user.getId(); } @@ -95,7 +111,7 @@ public class UserController { user.setLastName(last); user.setEmail("john@doe.com"); user.setAge(age); - myService.save(user); + mydao.save(user); return user.getId(); } } 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 0a53831c41..5afd69b8be 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 @@ -6,13 +6,10 @@ import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInA import static org.hamcrest.collection.IsIterableContainingInOrder.contains; import static org.hamcrest.core.IsNot.not; -import java.util.ArrayList; -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.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 +26,7 @@ import org.springframework.transaction.annotation.Transactional; public class JPAQuerydslTest { @Autowired - private MyUserService service; + private MyUserRepository repo; private MyUser userJohn; @@ -42,32 +39,29 @@ public class JPAQuerydslTest { userJohn.setLastName("Doe"); userJohn.setEmail("john@doe.com"); userJohn.setAge(22); - service.save(userJohn); + repo.save(userJohn); userTom = new MyUser(); userTom.setFirstName("Tom"); userTom.setLastName("Doe"); userTom.setEmail("tom@doe.com"); userTom.setAge(26); - service.save(userTom); + repo.save(userTom); } @Test public void givenLast_whenGettingListOfUsers_thenCorrect() { - final List params = new ArrayList(); - params.add(new SearchCriteria("lastName", ":", "Doe")); + final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder().with("lastName", ":", "Doe"); - final Iterable results = service.search(params); + final Iterable results = repo.findAll(builder.build()); assertThat(results, containsInAnyOrder(userJohn, userTom)); } @Test public void givenFirstAndLastName_whenGettingListOfUsers_thenCorrect() { - final List params = new ArrayList(); - params.add(new SearchCriteria("firstName", ":", "John")); - params.add(new SearchCriteria("lastName", ":", "Doe")); + final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder().with("firstName", ":", "John").with("lastName", ":", "Doe"); - final Iterable results = service.search(params); + final Iterable results = repo.findAll(builder.build()); assertThat(results, contains(userJohn)); assertThat(results, not(contains(userTom))); @@ -75,11 +69,9 @@ public class JPAQuerydslTest { @Test public void givenLastAndAge_whenGettingListOfUsers_thenCorrect() { - final List params = new ArrayList(); - params.add(new SearchCriteria("lastName", ":", "Doe")); - params.add(new SearchCriteria("age", ">", "25")); + final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder().with("lastName", ":", "Doe").with("age", ">", "25"); - final Iterable results = service.search(params); + final Iterable results = repo.findAll(builder.build()); assertThat(results, contains(userTom)); assertThat(results, not(contains(userJohn))); @@ -87,20 +79,17 @@ public class JPAQuerydslTest { @Test public void givenWrongFirstAndLast_whenGettingListOfUsers_thenCorrect() { - final List params = new ArrayList(); - params.add(new SearchCriteria("firstName", ":", "Adam")); - params.add(new SearchCriteria("lastName", ":", "Fox")); + final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder().with("firstName", ":", "Adam").with("lastName", ":", "Fox"); - final Iterable results = service.search(params); + final Iterable results = repo.findAll(builder.build()); assertThat(results, emptyIterable()); } @Test public void givenPartialFirst_whenGettingListOfUsers_thenCorrect() { - final List params = new ArrayList(); - params.add(new SearchCriteria("firstName", ":", "jo")); + final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder().with("firstName", ":", "jo"); - final Iterable results = service.search(params); + final Iterable results = repo.findAll(builder.build()); assertThat(results, contains(userJohn)); assertThat(results, not(contains(userTom)));