From 34222efc2757fd2df26bbc63fb4add7c3930e461 Mon Sep 17 00:00:00 2001 From: DOHA Date: Fri, 30 Jan 2015 00:34:23 +0200 Subject: [PATCH] fix predicate builder --- .../dao/MyUserPredicatesBuilder.java | 19 ++++++------------- .../web/controller/UserController.java | 10 +++------- 2 files changed, 9 insertions(+), 20 deletions(-) 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 bd6c91daea..5e08bde273 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 @@ -7,28 +7,25 @@ import org.baeldung.web.util.SearchCriteria; import com.mysema.query.types.expr.BooleanExpression; -public class MyUserPredicatesBuilder { - private List params; +public final class MyUserPredicatesBuilder { + private final 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; + params.add(new SearchCriteria(key, operation, value)); + return this; } public BooleanExpression build() { - if (params.size() == 0) + if (params.size() == 0) { return null; + } final List predicates = new ArrayList(); MyUserPredicate predicate; - for (final SearchCriteria param : params) { predicate = new MyUserPredicate(param); final BooleanExpression exp = predicate.getPredicate(); @@ -37,14 +34,10 @@ public class MyUserPredicatesBuilder { } } - if (predicates.size() == 0) - return null; - BooleanExpression result = predicates.get(0); for (int i = 1; i < predicates.size(); i++) { result = result.and(predicates.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 853715ea94..df439bb810 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 @@ -72,20 +72,16 @@ public class UserController { @RequestMapping(method = RequestMethod.GET, value = "/myusers") @ResponseBody public Iterable findAllByQuerydsl(@RequestParam(value = "search") final String search) { - MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder(); - + final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder(); 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)); + builder.with(matcher.group(1), matcher.group(2), matcher.group(3)); } } final BooleanExpression exp = builder.build(); - if (exp == null) - return mydao.findAll(); - else - return mydao.findAll(exp); + return mydao.findAll(exp); } // API - WRITE