From f0e18fc212ac1f598c4aae57ef367cf21edb4f5a Mon Sep 17 00:00:00 2001 From: eugenp Date: Thu, 29 Jan 2015 14:14:36 +0200 Subject: [PATCH] builder improvements --- .../dao/UserSpecificationsBuilder.java | 24 ++++++++----------- .../web/controller/UserController.java | 24 +++++++++---------- 2 files changed, 22 insertions(+), 26 deletions(-) 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 7c074a9b40..a3d7dc849d 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 @@ -8,40 +8,36 @@ import org.baeldung.web.util.SearchCriteria; import org.springframework.data.jpa.domain.Specification; import org.springframework.data.jpa.domain.Specifications; -public class UserSpecificationsBuilder { +public final class UserSpecificationsBuilder { - private List params; + private final 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; + // API + + public final UserSpecificationsBuilder with(final String key, final String operation, final Object value) { + params.add(new SearchCriteria(key, operation, value)); + return this; } public Specification build() { - if (params.size() == 0) + if (params.size() == 0) { return null; + } final List> specs = new ArrayList>(); - for (final SearchCriteria param : params) { specs.add(new UserSpecification(param)); } - if (specs.size() == 0) - return null; - Specification result = specs.get(0); for (int i = 1; i < specs.size(); i++) { result = Specifications.where(result).and(specs.get(i)); } return result; - } + } 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 f63b097071..853715ea94 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 @@ -39,11 +39,12 @@ public class UserController { super(); } + // API - READ + @RequestMapping(method = RequestMethod.GET, value = "/users") @ResponseBody public List findAll(@RequestParam(value = "search", required = false) final String search) { final List params = new ArrayList(); - if (search != null) { final Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),"); final Matcher matcher = pattern.matcher(search + ","); @@ -57,19 +58,15 @@ public class UserController { @RequestMapping(method = RequestMethod.GET, value = "/users/spec") @ResponseBody 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()) { - builder = builder.with(matcher.group(1), matcher.group(2), matcher.group(3)); - } + final UserSpecificationsBuilder builder = new UserSpecificationsBuilder(); + final Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),"); + final Matcher matcher = pattern.matcher(search + ","); + while (matcher.find()) { + builder.with(matcher.group(1), matcher.group(2), matcher.group(3)); } + final Specification spec = builder.build(); - if (spec == null) - return dao.findAll(); - else - return dao.findAll(spec); + return dao.findAll(spec); } @RequestMapping(method = RequestMethod.GET, value = "/myusers") @@ -91,6 +88,8 @@ public class UserController { return mydao.findAll(exp); } + // API - WRITE + @RequestMapping(method = RequestMethod.GET, value = "/users/new") @ResponseBody public long addUser(@RequestParam("first") final String first, @RequestParam("last") final String last, @RequestParam("age") final int age) { @@ -114,4 +113,5 @@ public class UserController { mydao.save(user); return user.getId(); } + }