commit
aa989f6e4f
|
@ -7,28 +7,25 @@ import org.baeldung.web.util.SearchCriteria;
|
|||
|
||||
import com.mysema.query.types.expr.BooleanExpression;
|
||||
|
||||
public class MyUserPredicatesBuilder {
|
||||
private List<SearchCriteria> params;
|
||||
public final class MyUserPredicatesBuilder {
|
||||
private final List<SearchCriteria> params;
|
||||
|
||||
public MyUserPredicatesBuilder() {
|
||||
params = new ArrayList<SearchCriteria>();
|
||||
}
|
||||
|
||||
public MyUserPredicatesBuilder with(final String key, final String operation, final Object value) {
|
||||
final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder();
|
||||
final List<SearchCriteria> newParams = new ArrayList<SearchCriteria>(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<BooleanExpression> predicates = new ArrayList<BooleanExpression>();
|
||||
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;
|
||||
|
||||
}
|
||||
}
|
||||
|
|
|
@ -72,20 +72,16 @@ public class UserController {
|
|||
@RequestMapping(method = RequestMethod.GET, value = "/myusers")
|
||||
@ResponseBody
|
||||
public Iterable<MyUser> 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
|
||||
|
|
Loading…
Reference in New Issue