fix predicate builder
This commit is contained in:
parent
f0e18fc212
commit
34222efc27
|
@ -7,28 +7,25 @@ import org.baeldung.web.util.SearchCriteria;
|
||||||
|
|
||||||
import com.mysema.query.types.expr.BooleanExpression;
|
import com.mysema.query.types.expr.BooleanExpression;
|
||||||
|
|
||||||
public class MyUserPredicatesBuilder {
|
public final class MyUserPredicatesBuilder {
|
||||||
private List<SearchCriteria> params;
|
private final List<SearchCriteria> params;
|
||||||
|
|
||||||
public MyUserPredicatesBuilder() {
|
public MyUserPredicatesBuilder() {
|
||||||
params = new ArrayList<SearchCriteria>();
|
params = new ArrayList<SearchCriteria>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public MyUserPredicatesBuilder with(final String key, final String operation, final Object value) {
|
public MyUserPredicatesBuilder with(final String key, final String operation, final Object value) {
|
||||||
final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder();
|
params.add(new SearchCriteria(key, operation, value));
|
||||||
final List<SearchCriteria> newParams = new ArrayList<SearchCriteria>(params);
|
return this;
|
||||||
newParams.add(new SearchCriteria(key, operation, value));
|
|
||||||
builder.params = newParams;
|
|
||||||
return builder;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public BooleanExpression build() {
|
public BooleanExpression build() {
|
||||||
if (params.size() == 0)
|
if (params.size() == 0) {
|
||||||
return null;
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
final List<BooleanExpression> predicates = new ArrayList<BooleanExpression>();
|
final List<BooleanExpression> predicates = new ArrayList<BooleanExpression>();
|
||||||
MyUserPredicate predicate;
|
MyUserPredicate predicate;
|
||||||
|
|
||||||
for (final SearchCriteria param : params) {
|
for (final SearchCriteria param : params) {
|
||||||
predicate = new MyUserPredicate(param);
|
predicate = new MyUserPredicate(param);
|
||||||
final BooleanExpression exp = predicate.getPredicate();
|
final BooleanExpression exp = predicate.getPredicate();
|
||||||
|
@ -37,14 +34,10 @@ public class MyUserPredicatesBuilder {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (predicates.size() == 0)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
BooleanExpression result = predicates.get(0);
|
BooleanExpression result = predicates.get(0);
|
||||||
for (int i = 1; i < predicates.size(); i++) {
|
for (int i = 1; i < predicates.size(); i++) {
|
||||||
result = result.and(predicates.get(i));
|
result = result.and(predicates.get(i));
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -72,20 +72,16 @@ public class UserController {
|
||||||
@RequestMapping(method = RequestMethod.GET, value = "/myusers")
|
@RequestMapping(method = RequestMethod.GET, value = "/myusers")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public Iterable<MyUser> findAllByQuerydsl(@RequestParam(value = "search") final String search) {
|
public Iterable<MyUser> findAllByQuerydsl(@RequestParam(value = "search") final String search) {
|
||||||
MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder();
|
final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder();
|
||||||
|
|
||||||
if (search != null) {
|
if (search != null) {
|
||||||
final Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),");
|
final Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),");
|
||||||
final Matcher matcher = pattern.matcher(search + ",");
|
final Matcher matcher = pattern.matcher(search + ",");
|
||||||
while (matcher.find()) {
|
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();
|
final BooleanExpression exp = builder.build();
|
||||||
if (exp == null)
|
return mydao.findAll(exp);
|
||||||
return mydao.findAll();
|
|
||||||
else
|
|
||||||
return mydao.findAll(exp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// API - WRITE
|
// API - WRITE
|
||||||
|
|
Loading…
Reference in New Issue