BAEL-696 Implement OR in the REST API Query Language (#1518)
* Dependency Injection Types, XML-Config, Java-Config, Test Classes * Formatting done with Formatter Configuration in Eclipse * REST Query Lang - Adv Search Ops - Improvement - C1 * REST Query Lang - Adv Search Ops - Improvement - C2 * BAEL-696 Code formatting * REST Query Lang - Adv Search Ops - Improvement - C3 * BAEL-636: add standalone deployment (#1521) * BAEL-696 Formatting
This commit is contained in:
parent
70d8fecc54
commit
ef91c379b7
|
@ -1,3 +1,5 @@
|
||||||
|
*/bin/*
|
||||||
|
|
||||||
*.class
|
*.class
|
||||||
|
|
||||||
# Package Files #
|
# Package Files #
|
||||||
|
|
|
@ -1,10 +0,0 @@
|
||||||
package org.baeldung.persistence;
|
|
||||||
|
|
||||||
import org.springframework.data.jpa.domain.Specification;
|
|
||||||
|
|
||||||
public interface IEnhancedSpecification<T> extends Specification<T> {
|
|
||||||
|
|
||||||
default boolean isOfLowPrecedence() {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,6 +1,7 @@
|
||||||
package org.baeldung.persistence.dao;
|
package org.baeldung.persistence.dao;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Comparator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
@ -12,53 +13,64 @@ import org.springframework.data.jpa.domain.Specifications;
|
||||||
|
|
||||||
public class GenericSpecificationsBuilder {
|
public class GenericSpecificationsBuilder {
|
||||||
|
|
||||||
private final List<SpecSearchCriteria> params;
|
private final List<SpecSearchCriteria> params;
|
||||||
|
|
||||||
public GenericSpecificationsBuilder() {
|
public GenericSpecificationsBuilder() {
|
||||||
this.params = new ArrayList<>();
|
this.params = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public final GenericSpecificationsBuilder with(final String key, final String operation, final Object value,
|
public final GenericSpecificationsBuilder with(final String key, final String operation, final Object value, final String prefix, final String suffix) {
|
||||||
final String prefix, final String suffix) {
|
return with(null, key, operation, value, prefix, suffix);
|
||||||
return with(null, key, operation, value, prefix, suffix);
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public final GenericSpecificationsBuilder with(final String precedenceIndicator, final String key,
|
public final GenericSpecificationsBuilder with(final String precedenceIndicator, final String key, final String operation, final Object value, final String prefix, final String suffix) {
|
||||||
final String operation, final Object value, final String prefix, final String suffix) {
|
SearchOperation op = SearchOperation.getSimpleOperation(operation.charAt(0));
|
||||||
SearchOperation op = SearchOperation.getSimpleOperation(operation.charAt(0));
|
if (op != null) {
|
||||||
if (op != null) {
|
if (op == SearchOperation.EQUALITY) // the operation may be complex operation
|
||||||
if (op == SearchOperation.EQUALITY) // the operation may be complex operation
|
{
|
||||||
{
|
final boolean startWithAsterisk = prefix != null && prefix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
|
||||||
final boolean startWithAsterisk = prefix != null && prefix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
|
final boolean endWithAsterisk = suffix != null && suffix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
|
||||||
final boolean endWithAsterisk = suffix != null && suffix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
|
|
||||||
|
|
||||||
if (startWithAsterisk && endWithAsterisk) {
|
if (startWithAsterisk && endWithAsterisk) {
|
||||||
op = SearchOperation.CONTAINS;
|
op = SearchOperation.CONTAINS;
|
||||||
} else if (startWithAsterisk) {
|
} else if (startWithAsterisk) {
|
||||||
op = SearchOperation.ENDS_WITH;
|
op = SearchOperation.ENDS_WITH;
|
||||||
} else if (endWithAsterisk) {
|
} else if (endWithAsterisk) {
|
||||||
op = SearchOperation.STARTS_WITH;
|
op = SearchOperation.STARTS_WITH;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
params.add(new SpecSearchCriteria(precedenceIndicator, key, op, value));
|
params.add(new SpecSearchCriteria(precedenceIndicator, key, op, value));
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public <U> Specification<U> build(Function<SpecSearchCriteria, Specification<U>> converter) {
|
public <U> Specification<U> build(Function<SpecSearchCriteria, Specification<U>> converter) {
|
||||||
|
|
||||||
|
if (params.size() == 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
params.sort(Comparator.comparing(SpecSearchCriteria::isOrPredicate));
|
||||||
|
|
||||||
|
final List<Specification<U>> specs = params
|
||||||
|
.stream()
|
||||||
|
.map(converter)
|
||||||
|
.collect(Collectors.toCollection(ArrayList::new));
|
||||||
|
|
||||||
|
Specification<U> result = specs.get(0);
|
||||||
|
|
||||||
|
for (int idx = 1; idx < specs.size(); idx++) {
|
||||||
|
result = params
|
||||||
|
.get(idx)
|
||||||
|
.isOrPredicate()
|
||||||
|
? Specifications
|
||||||
|
.where(result)
|
||||||
|
.or(specs.get(idx))
|
||||||
|
: Specifications
|
||||||
|
.where(result)
|
||||||
|
.and(specs.get(idx));
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
if (params.size() == 0)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
params.sort((spec0, spec1) -> Boolean.compare(spec0.isLowPrecedence(), spec1.isLowPrecedence()));
|
|
||||||
|
|
||||||
final List<Specification<U>> specs = params.stream().map(converter).collect(Collectors.toCollection(ArrayList::new));
|
|
||||||
|
|
||||||
Specification<U> result = specs.get(0);
|
|
||||||
|
|
||||||
for (int idx = 1; idx < specs.size(); idx++) {
|
|
||||||
result=params.get(idx).isLowPrecedence()? Specifications.where(result).or(specs.get(idx)): Specifications.where(result).and(specs.get(idx));
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,14 +1,15 @@
|
||||||
package org.baeldung.persistence.dao;
|
package org.baeldung.persistence.dao;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Comparator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
import org.baeldung.persistence.model.User;
|
import org.baeldung.persistence.model.User;
|
||||||
import org.baeldung.web.util.SearchOperation;
|
import org.baeldung.web.util.SearchOperation;
|
||||||
import org.baeldung.web.util.SpecSearchCriteria;
|
import org.baeldung.web.util.SpecSearchCriteria;
|
||||||
import org.springframework.data.jpa.domain.Specification;
|
import org.springframework.data.jpa.domain.Specification;
|
||||||
import org.springframework.data.jpa.domain.Specifications;
|
import org.springframework.data.jpa.domain.Specifications;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
|
||||||
import java.util.List;
|
|
||||||
|
|
||||||
public final class UserSpecificationsBuilder {
|
public final class UserSpecificationsBuilder {
|
||||||
|
|
||||||
private final List<SpecSearchCriteria> params;
|
private final List<SpecSearchCriteria> params;
|
||||||
|
@ -48,14 +49,17 @@ public final class UserSpecificationsBuilder {
|
||||||
if (params.size() == 0)
|
if (params.size() == 0)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
params.sort((spec0, spec1) -> {
|
params.sort(Comparator.comparing(SpecSearchCriteria::isOrPredicate));
|
||||||
return Boolean.compare(spec0.isLowPrecedence(), spec1.isLowPrecedence());
|
|
||||||
});
|
|
||||||
|
|
||||||
Specification<User> result = new UserSpecification(params.get(0));
|
Specification<User> result = new UserSpecification(params.get(0));
|
||||||
|
|
||||||
for (int i = 1; i < params.size(); i++) {
|
for (int i = 1; i < params.size(); i++) {
|
||||||
result = params.get(i).isLowPrecedence() ? Specifications.where(result).or(new UserSpecification(params.get(i))) : Specifications.where(result).and(new UserSpecification(params.get(i)));
|
result = params.get(i)
|
||||||
|
.isOrPredicate()
|
||||||
|
? Specifications.where(result)
|
||||||
|
.or(new UserSpecification(params.get(i)))
|
||||||
|
: Specifications.where(result)
|
||||||
|
.and(new UserSpecification(params.get(i)));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,141 +1,157 @@
|
||||||
package org.baeldung.web.controller;
|
package org.baeldung.web.controller;
|
||||||
|
|
||||||
import com.google.common.base.Joiner;
|
import java.util.ArrayList;
|
||||||
import com.google.common.base.Preconditions;
|
import java.util.List;
|
||||||
import com.querydsl.core.types.Predicate;
|
import java.util.regex.Matcher;
|
||||||
import com.querydsl.core.types.dsl.BooleanExpression;
|
import java.util.regex.Pattern;
|
||||||
import cz.jirutka.rsql.parser.RSQLParser;
|
|
||||||
import cz.jirutka.rsql.parser.ast.Node;
|
import org.baeldung.persistence.dao.IUserDAO;
|
||||||
import org.baeldung.persistence.dao.*;
|
import org.baeldung.persistence.dao.MyUserPredicatesBuilder;
|
||||||
import org.baeldung.persistence.dao.rsql.CustomRsqlVisitor;
|
import org.baeldung.persistence.dao.MyUserRepository;
|
||||||
import org.baeldung.persistence.model.MyUser;
|
import org.baeldung.persistence.dao.UserRepository;
|
||||||
import org.baeldung.persistence.model.User;
|
import org.baeldung.persistence.dao.UserSpecificationsBuilder;
|
||||||
import org.baeldung.web.util.SearchCriteria;
|
import org.baeldung.persistence.dao.rsql.CustomRsqlVisitor;
|
||||||
import org.baeldung.web.util.SearchOperation;
|
import org.baeldung.persistence.model.MyUser;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.baeldung.persistence.model.User;
|
||||||
import org.springframework.data.jpa.domain.Specification;
|
import org.baeldung.web.util.SearchCriteria;
|
||||||
import org.springframework.data.querydsl.binding.QuerydslPredicate;
|
import org.baeldung.web.util.SearchOperation;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.data.jpa.domain.Specification;
|
||||||
import org.springframework.web.bind.annotation.*;
|
import org.springframework.data.querydsl.binding.QuerydslPredicate;
|
||||||
|
import org.springframework.http.HttpStatus;
|
||||||
import java.util.ArrayList;
|
import org.springframework.stereotype.Controller;
|
||||||
import java.util.List;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import java.util.regex.Matcher;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import java.util.regex.Pattern;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
//@EnableSpringDataWebSupport
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
@Controller
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
@RequestMapping(value = "/auth/")
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
public class UserController {
|
|
||||||
|
import com.google.common.base.Joiner;
|
||||||
@Autowired
|
import com.google.common.base.Preconditions;
|
||||||
private IUserDAO service;
|
import com.querydsl.core.types.Predicate;
|
||||||
|
import com.querydsl.core.types.dsl.BooleanExpression;
|
||||||
@Autowired
|
|
||||||
private UserRepository dao;
|
import cz.jirutka.rsql.parser.RSQLParser;
|
||||||
|
import cz.jirutka.rsql.parser.ast.Node;
|
||||||
@Autowired
|
|
||||||
private MyUserRepository myUserRepository;
|
//@EnableSpringDataWebSupport
|
||||||
|
@Controller
|
||||||
public UserController() {
|
@RequestMapping(value = "/auth/")
|
||||||
super();
|
public class UserController {
|
||||||
}
|
|
||||||
|
@Autowired
|
||||||
// API - READ
|
private IUserDAO service;
|
||||||
|
|
||||||
@RequestMapping(method = RequestMethod.GET, value = "/users")
|
@Autowired
|
||||||
@ResponseBody
|
private UserRepository dao;
|
||||||
public List<User> findAll(@RequestParam(value = "search", required = false) final String search) {
|
|
||||||
final List<SearchCriteria> params = new ArrayList<SearchCriteria>();
|
@Autowired
|
||||||
if (search != null) {
|
private MyUserRepository myUserRepository;
|
||||||
final Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),");
|
|
||||||
final Matcher matcher = pattern.matcher(search + ",");
|
public UserController() {
|
||||||
while (matcher.find()) {
|
super();
|
||||||
params.add(new SearchCriteria(matcher.group(1), matcher.group(2), matcher.group(3)));
|
}
|
||||||
}
|
|
||||||
}
|
// API - READ
|
||||||
return service.searchUser(params);
|
|
||||||
}
|
@RequestMapping(method = RequestMethod.GET, value = "/users")
|
||||||
|
@ResponseBody
|
||||||
@RequestMapping(method = RequestMethod.GET, value = "/users/spec")
|
public List<User> findAll(@RequestParam(value = "search", required = false) String search) {
|
||||||
@ResponseBody
|
List<SearchCriteria> params = new ArrayList<SearchCriteria>();
|
||||||
public List<User> findAllBySpecification(@RequestParam(value = "search") final String search) {
|
if (search != null) {
|
||||||
final UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
|
Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),");
|
||||||
final String operationSetExper = Joiner.on("|").join(SearchOperation.SIMPLE_OPERATION_SET);
|
Matcher matcher = pattern.matcher(search + ",");
|
||||||
final Pattern pattern = Pattern.compile("(\\w+?)(" + operationSetExper + ")(\\p{Punct}?)(\\w+?)(\\p{Punct}?),");
|
while (matcher.find()) {
|
||||||
final Matcher matcher = pattern.matcher(search + ",");
|
params.add(new SearchCriteria(matcher.group(1), matcher.group(2), matcher.group(3)));
|
||||||
while (matcher.find()) {
|
}
|
||||||
builder.with(matcher.group(1), matcher.group(2), matcher.group(4), matcher.group(3), matcher.group(5));
|
}
|
||||||
}
|
return service.searchUser(params);
|
||||||
|
}
|
||||||
final Specification<User> spec = builder.build();
|
|
||||||
return dao.findAll(spec);
|
@RequestMapping(method = RequestMethod.GET, value = "/users/spec")
|
||||||
}
|
@ResponseBody
|
||||||
|
public List<User> findAllBySpecification(@RequestParam(value = "search") String search) {
|
||||||
@RequestMapping(method = RequestMethod.GET, value = "/users/espec")
|
UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
|
||||||
@ResponseBody
|
String operationSetExper = Joiner
|
||||||
public List<User> findAllByOptionalSpecification(@RequestParam(value = "search") final String search) {
|
.on("|")
|
||||||
final Specification<User> spec = resolveSpecification(search);
|
.join(SearchOperation.SIMPLE_OPERATION_SET);
|
||||||
return dao.findAll(spec);
|
Pattern pattern = Pattern.compile("(\\w+?)(" + operationSetExper + ")(\\p{Punct}?)(\\w+?)(\\p{Punct}?),");
|
||||||
}
|
Matcher matcher = pattern.matcher(search + ",");
|
||||||
|
while (matcher.find()) {
|
||||||
protected Specification<User> resolveSpecification(String searchParameters) {
|
builder.with(matcher.group(1), matcher.group(2), matcher.group(4), matcher.group(3), matcher.group(5));
|
||||||
|
}
|
||||||
final UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
|
|
||||||
final String operationSetExper = Joiner.on("|").join(SearchOperation.SIMPLE_OPERATION_SET);
|
Specification<User> spec = builder.build();
|
||||||
final Pattern pattern = Pattern.compile("(\\p{Punct}?)(\\w+?)(" + operationSetExper + ")(\\p{Punct}?)(\\w+?)(\\p{Punct}?),");
|
return dao.findAll(spec);
|
||||||
final Matcher matcher = pattern.matcher(searchParameters + ",");
|
}
|
||||||
while (matcher.find()) {
|
|
||||||
builder.with(matcher.group(1), matcher.group(2), matcher.group(3), matcher.group(5), matcher.group(4), matcher.group(6));
|
@GetMapping(value = "/users/espec")
|
||||||
}
|
@ResponseBody
|
||||||
return builder.build();
|
public List<User> findAllByOrPredicate(@RequestParam(value = "search") String search) {
|
||||||
}
|
Specification<User> spec = resolveSpecification(search);
|
||||||
|
return dao.findAll(spec);
|
||||||
@RequestMapping(method = RequestMethod.GET, value = "/myusers")
|
}
|
||||||
@ResponseBody
|
|
||||||
public Iterable<MyUser> findAllByQuerydsl(@RequestParam(value = "search") final String search) {
|
protected Specification<User> resolveSpecification(String searchParameters) {
|
||||||
final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder();
|
|
||||||
if (search != null) {
|
UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
|
||||||
final Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),");
|
String operationSetExper = Joiner
|
||||||
final Matcher matcher = pattern.matcher(search + ",");
|
.on("|")
|
||||||
while (matcher.find()) {
|
.join(SearchOperation.SIMPLE_OPERATION_SET);
|
||||||
builder.with(matcher.group(1), matcher.group(2), matcher.group(3));
|
Pattern pattern = Pattern.compile("(\\p{Punct}?)(\\w+?)(" + operationSetExper + ")(\\p{Punct}?)(\\w+?)(\\p{Punct}?),");
|
||||||
}
|
Matcher matcher = pattern.matcher(searchParameters + ",");
|
||||||
}
|
while (matcher.find()) {
|
||||||
final BooleanExpression exp = builder.build();
|
builder.with(matcher.group(1), matcher.group(2), matcher.group(3), matcher.group(5), matcher.group(4), matcher.group(6));
|
||||||
return myUserRepository.findAll(exp);
|
}
|
||||||
}
|
return builder.build();
|
||||||
|
}
|
||||||
@RequestMapping(method = RequestMethod.GET, value = "/users/rsql")
|
|
||||||
@ResponseBody
|
@RequestMapping(method = RequestMethod.GET, value = "/myusers")
|
||||||
public List<User> findAllByRsql(@RequestParam(value = "search") final String search) {
|
@ResponseBody
|
||||||
final Node rootNode = new RSQLParser().parse(search);
|
public Iterable<MyUser> findAllByQuerydsl(@RequestParam(value = "search") String search) {
|
||||||
final Specification<User> spec = rootNode.accept(new CustomRsqlVisitor<User>());
|
MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder();
|
||||||
return dao.findAll(spec);
|
if (search != null) {
|
||||||
}
|
Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),");
|
||||||
|
Matcher matcher = pattern.matcher(search + ",");
|
||||||
@RequestMapping(method = RequestMethod.GET, value = "/api/myusers")
|
while (matcher.find()) {
|
||||||
@ResponseBody
|
builder.with(matcher.group(1), matcher.group(2), matcher.group(3));
|
||||||
public Iterable<MyUser> findAllByWebQuerydsl(@QuerydslPredicate(root = MyUser.class) final Predicate predicate) {
|
}
|
||||||
return myUserRepository.findAll(predicate);
|
}
|
||||||
}
|
BooleanExpression exp = builder.build();
|
||||||
|
return myUserRepository.findAll(exp);
|
||||||
// API - WRITE
|
}
|
||||||
|
|
||||||
@RequestMapping(method = RequestMethod.POST, value = "/users")
|
@RequestMapping(method = RequestMethod.GET, value = "/users/rsql")
|
||||||
@ResponseStatus(HttpStatus.CREATED)
|
@ResponseBody
|
||||||
public void create(@RequestBody final User resource) {
|
public List<User> findAllByRsql(@RequestParam(value = "search") String search) {
|
||||||
Preconditions.checkNotNull(resource);
|
Node rootNode = new RSQLParser().parse(search);
|
||||||
dao.save(resource);
|
Specification<User> spec = rootNode.accept(new CustomRsqlVisitor<User>());
|
||||||
}
|
return dao.findAll(spec);
|
||||||
|
}
|
||||||
@RequestMapping(method = RequestMethod.POST, value = "/myusers")
|
|
||||||
@ResponseStatus(HttpStatus.CREATED)
|
@RequestMapping(method = RequestMethod.GET, value = "/api/myusers")
|
||||||
public void addMyUser(@RequestBody final MyUser resource) {
|
@ResponseBody
|
||||||
Preconditions.checkNotNull(resource);
|
public Iterable<MyUser> findAllByWebQuerydsl(@QuerydslPredicate(root = MyUser.class) Predicate predicate) {
|
||||||
myUserRepository.save(resource);
|
return myUserRepository.findAll(predicate);
|
||||||
|
}
|
||||||
}
|
|
||||||
|
// API - WRITE
|
||||||
}
|
|
||||||
|
@RequestMapping(method = RequestMethod.POST, value = "/users")
|
||||||
|
@ResponseStatus(HttpStatus.CREATED)
|
||||||
|
public void create(@RequestBody User resource) {
|
||||||
|
Preconditions.checkNotNull(resource);
|
||||||
|
dao.save(resource);
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(method = RequestMethod.POST, value = "/myusers")
|
||||||
|
@ResponseStatus(HttpStatus.CREATED)
|
||||||
|
public void addMyUser(@RequestBody MyUser resource) {
|
||||||
|
Preconditions.checkNotNull(resource);
|
||||||
|
myUserRepository.save(resource);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
|
@ -4,10 +4,10 @@ public enum SearchOperation {
|
||||||
EQUALITY, NEGATION, GREATER_THAN, LESS_THAN, LIKE, STARTS_WITH, ENDS_WITH, CONTAINS;
|
EQUALITY, NEGATION, GREATER_THAN, LESS_THAN, LIKE, STARTS_WITH, ENDS_WITH, CONTAINS;
|
||||||
|
|
||||||
public static final String[] SIMPLE_OPERATION_SET = { ":", "!", ">", "<", "~" };
|
public static final String[] SIMPLE_OPERATION_SET = { ":", "!", ">", "<", "~" };
|
||||||
|
|
||||||
public static final String LOW_PRECEDENCE_INDICATOR="'";
|
public static final String OR_PREDICATE_FLAG = "'";
|
||||||
|
|
||||||
public static final String ZERO_OR_MORE_REGEX="*";
|
public static final String ZERO_OR_MORE_REGEX = "*";
|
||||||
|
|
||||||
public static SearchOperation getSimpleOperation(final char input) {
|
public static SearchOperation getSimpleOperation(final char input) {
|
||||||
switch (input) {
|
switch (input) {
|
||||||
|
|
|
@ -5,7 +5,7 @@ public class SpecSearchCriteria {
|
||||||
private String key;
|
private String key;
|
||||||
private SearchOperation operation;
|
private SearchOperation operation;
|
||||||
private Object value;
|
private Object value;
|
||||||
private boolean lowPrecedence;
|
private boolean orPredicate;
|
||||||
|
|
||||||
public SpecSearchCriteria() {
|
public SpecSearchCriteria() {
|
||||||
|
|
||||||
|
@ -18,9 +18,9 @@ public class SpecSearchCriteria {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SpecSearchCriteria(final String lowPrecedenceIndicator, final String key, final SearchOperation operation, final Object value) {
|
public SpecSearchCriteria(final String orPredicate, final String key, final SearchOperation operation, final Object value) {
|
||||||
super();
|
super();
|
||||||
this.lowPrecedence = lowPrecedenceIndicator != null && lowPrecedenceIndicator.equals(SearchOperation.LOW_PRECEDENCE_INDICATOR);
|
this.orPredicate = orPredicate != null && orPredicate.equals(SearchOperation.OR_PREDICATE_FLAG);
|
||||||
this.key = key;
|
this.key = key;
|
||||||
this.operation = operation;
|
this.operation = operation;
|
||||||
this.value = value;
|
this.value = value;
|
||||||
|
@ -50,12 +50,12 @@ public class SpecSearchCriteria {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isLowPrecedence() {
|
public boolean isOrPredicate() {
|
||||||
return lowPrecedence;
|
return orPredicate;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setLowPrecedence(boolean lowPrecedence) {
|
public void setOrPredicate(boolean orPredicate) {
|
||||||
this.lowPrecedence = lowPrecedence;
|
this.orPredicate = orPredicate;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -70,7 +70,9 @@ public class JPASpecificationIntegrationTest {
|
||||||
public void givenFirstAndLastName_whenGettingListOfUsers_thenCorrect() {
|
public void givenFirstAndLastName_whenGettingListOfUsers_thenCorrect() {
|
||||||
final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("firstName", SearchOperation.EQUALITY, "john"));
|
final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("firstName", SearchOperation.EQUALITY, "john"));
|
||||||
final UserSpecification spec1 = new UserSpecification(new SpecSearchCriteria("lastName", SearchOperation.EQUALITY, "doe"));
|
final UserSpecification spec1 = new UserSpecification(new SpecSearchCriteria("lastName", SearchOperation.EQUALITY, "doe"));
|
||||||
final List<User> results = repository.findAll(Specifications.where(spec).and(spec1));
|
final List<User> results = repository.findAll(Specifications
|
||||||
|
.where(spec)
|
||||||
|
.and(spec1));
|
||||||
|
|
||||||
assertThat(userJohn, isIn(results));
|
assertThat(userJohn, isIn(results));
|
||||||
assertThat(userTom, not(isIn(results)));
|
assertThat(userTom, not(isIn(results)));
|
||||||
|
@ -80,10 +82,13 @@ public class JPASpecificationIntegrationTest {
|
||||||
public void givenFirstOrLastName_whenGettingListOfUsers_thenCorrect() {
|
public void givenFirstOrLastName_whenGettingListOfUsers_thenCorrect() {
|
||||||
UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
|
UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
|
||||||
|
|
||||||
final SpecSearchCriteria spec = new SpecSearchCriteria("'", "firstName", SearchOperation.EQUALITY, "john");
|
SpecSearchCriteria spec = new SpecSearchCriteria("'", "firstName", SearchOperation.EQUALITY, "john");
|
||||||
final SpecSearchCriteria spec1 = new SpecSearchCriteria("lastName", SearchOperation.EQUALITY, "doe");
|
SpecSearchCriteria spec1 = new SpecSearchCriteria("lastName", SearchOperation.EQUALITY, "doe");
|
||||||
|
|
||||||
final List<User> results = repository.findAll(builder.with(spec1).with(spec).build());
|
List<User> results = repository.findAll(builder
|
||||||
|
.with(spec1)
|
||||||
|
.with(spec)
|
||||||
|
.build());
|
||||||
|
|
||||||
assertThat(results, hasSize(2));
|
assertThat(results, hasSize(2));
|
||||||
assertThat(userJohn, isIn(results));
|
assertThat(userJohn, isIn(results));
|
||||||
|
@ -97,7 +102,8 @@ public class JPASpecificationIntegrationTest {
|
||||||
builder.with("'", "firstName", ":", "john", null, null);
|
builder.with("'", "firstName", ":", "john", null, null);
|
||||||
builder.with(null, "lastName", ":", "doe", null, null);
|
builder.with(null, "lastName", ":", "doe", null, null);
|
||||||
|
|
||||||
final List<User> results = repository.findAll(builder.build(converter));
|
List<User> results = repository.findAll(builder.build(converter));
|
||||||
|
|
||||||
assertThat(results, hasSize(2));
|
assertThat(results, hasSize(2));
|
||||||
assertThat(userJohn, isIn(results));
|
assertThat(userJohn, isIn(results));
|
||||||
assertThat(userTom, isIn(results));
|
assertThat(userTom, isIn(results));
|
||||||
|
@ -116,7 +122,6 @@ public class JPASpecificationIntegrationTest {
|
||||||
public void givenMinAge_whenGettingListOfUsers_thenCorrect() {
|
public void givenMinAge_whenGettingListOfUsers_thenCorrect() {
|
||||||
final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("age", SearchOperation.GREATER_THAN, "25"));
|
final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("age", SearchOperation.GREATER_THAN, "25"));
|
||||||
final List<User> results = repository.findAll(Specifications.where(spec));
|
final List<User> results = repository.findAll(Specifications.where(spec));
|
||||||
|
|
||||||
assertThat(userTom, isIn(results));
|
assertThat(userTom, isIn(results));
|
||||||
assertThat(userJohn, not(isIn(results)));
|
assertThat(userJohn, not(isIn(results)));
|
||||||
}
|
}
|
||||||
|
@ -125,7 +130,6 @@ public class JPASpecificationIntegrationTest {
|
||||||
public void givenFirstNamePrefix_whenGettingListOfUsers_thenCorrect() {
|
public void givenFirstNamePrefix_whenGettingListOfUsers_thenCorrect() {
|
||||||
final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("firstName", SearchOperation.STARTS_WITH, "jo"));
|
final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("firstName", SearchOperation.STARTS_WITH, "jo"));
|
||||||
final List<User> results = repository.findAll(spec);
|
final List<User> results = repository.findAll(spec);
|
||||||
|
|
||||||
assertThat(userJohn, isIn(results));
|
assertThat(userJohn, isIn(results));
|
||||||
assertThat(userTom, not(isIn(results)));
|
assertThat(userTom, not(isIn(results)));
|
||||||
}
|
}
|
||||||
|
@ -134,7 +138,6 @@ public class JPASpecificationIntegrationTest {
|
||||||
public void givenFirstNameSuffix_whenGettingListOfUsers_thenCorrect() {
|
public void givenFirstNameSuffix_whenGettingListOfUsers_thenCorrect() {
|
||||||
final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("firstName", SearchOperation.ENDS_WITH, "n"));
|
final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("firstName", SearchOperation.ENDS_WITH, "n"));
|
||||||
final List<User> results = repository.findAll(spec);
|
final List<User> results = repository.findAll(spec);
|
||||||
|
|
||||||
assertThat(userJohn, isIn(results));
|
assertThat(userJohn, isIn(results));
|
||||||
assertThat(userTom, not(isIn(results)));
|
assertThat(userTom, not(isIn(results)));
|
||||||
}
|
}
|
||||||
|
@ -152,7 +155,9 @@ public class JPASpecificationIntegrationTest {
|
||||||
public void givenAgeRange_whenGettingListOfUsers_thenCorrect() {
|
public void givenAgeRange_whenGettingListOfUsers_thenCorrect() {
|
||||||
final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("age", SearchOperation.GREATER_THAN, "20"));
|
final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("age", SearchOperation.GREATER_THAN, "20"));
|
||||||
final UserSpecification spec1 = new UserSpecification(new SpecSearchCriteria("age", SearchOperation.LESS_THAN, "25"));
|
final UserSpecification spec1 = new UserSpecification(new SpecSearchCriteria("age", SearchOperation.LESS_THAN, "25"));
|
||||||
final List<User> results = repository.findAll(Specifications.where(spec).and(spec1));
|
final List<User> results = repository.findAll(Specifications
|
||||||
|
.where(spec)
|
||||||
|
.and(spec1));
|
||||||
|
|
||||||
assertThat(userJohn, isIn(results));
|
assertThat(userJohn, isIn(results));
|
||||||
assertThat(userTom, not(isIn(results)));
|
assertThat(userTom, not(isIn(results)));
|
||||||
|
|
Loading…
Reference in New Issue