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
|
||||
|
||||
# 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;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
@ -18,13 +19,11 @@ public class GenericSpecificationsBuilder {
|
|||
this.params = new ArrayList<>();
|
||||
}
|
||||
|
||||
public final GenericSpecificationsBuilder with(final String key, final String operation, final Object value,
|
||||
final String prefix, final String suffix) {
|
||||
public final GenericSpecificationsBuilder with(final String key, final String operation, final Object value, final String prefix, final String suffix) {
|
||||
return with(null, key, operation, value, prefix, suffix);
|
||||
}
|
||||
|
||||
public final GenericSpecificationsBuilder with(final String precedenceIndicator, final String key,
|
||||
final String operation, final Object value, final String prefix, final String suffix) {
|
||||
public final GenericSpecificationsBuilder with(final String precedenceIndicator, final String key, final String operation, final Object value, final String prefix, final String suffix) {
|
||||
SearchOperation op = SearchOperation.getSimpleOperation(operation.charAt(0));
|
||||
if (op != null) {
|
||||
if (op == SearchOperation.EQUALITY) // the operation may be complex operation
|
||||
|
@ -47,18 +46,31 @@ public class GenericSpecificationsBuilder {
|
|||
|
||||
public <U> Specification<U> build(Function<SpecSearchCriteria, Specification<U>> converter) {
|
||||
|
||||
if (params.size() == 0)
|
||||
if (params.size() == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
params.sort((spec0, spec1) -> Boolean.compare(spec0.isLowPrecedence(), spec1.isLowPrecedence()));
|
||||
params.sort(Comparator.comparing(SpecSearchCriteria::isOrPredicate));
|
||||
|
||||
final List<Specification<U>> specs = params.stream().map(converter).collect(Collectors.toCollection(ArrayList::new));
|
||||
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));
|
||||
result = params
|
||||
.get(idx)
|
||||
.isOrPredicate()
|
||||
? 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;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Comparator;
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.model.User;
|
||||
import org.baeldung.web.util.SearchOperation;
|
||||
import org.baeldung.web.util.SpecSearchCriteria;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.data.jpa.domain.Specifications;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public final class UserSpecificationsBuilder {
|
||||
|
||||
private final List<SpecSearchCriteria> params;
|
||||
|
@ -48,14 +49,17 @@ public final class UserSpecificationsBuilder {
|
|||
if (params.size() == 0)
|
||||
return null;
|
||||
|
||||
params.sort((spec0, spec1) -> {
|
||||
return Boolean.compare(spec0.isLowPrecedence(), spec1.isLowPrecedence());
|
||||
});
|
||||
params.sort(Comparator.comparing(SpecSearchCriteria::isOrPredicate));
|
||||
|
||||
Specification<User> result = new UserSpecification(params.get(0));
|
||||
|
||||
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,12 +1,15 @@
|
|||
package org.baeldung.web.controller;
|
||||
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.querydsl.core.types.Predicate;
|
||||
import com.querydsl.core.types.dsl.BooleanExpression;
|
||||
import cz.jirutka.rsql.parser.RSQLParser;
|
||||
import cz.jirutka.rsql.parser.ast.Node;
|
||||
import org.baeldung.persistence.dao.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.baeldung.persistence.dao.IUserDAO;
|
||||
import org.baeldung.persistence.dao.MyUserPredicatesBuilder;
|
||||
import org.baeldung.persistence.dao.MyUserRepository;
|
||||
import org.baeldung.persistence.dao.UserRepository;
|
||||
import org.baeldung.persistence.dao.UserSpecificationsBuilder;
|
||||
import org.baeldung.persistence.dao.rsql.CustomRsqlVisitor;
|
||||
import org.baeldung.persistence.model.MyUser;
|
||||
import org.baeldung.persistence.model.User;
|
||||
|
@ -17,12 +20,21 @@ import org.springframework.data.jpa.domain.Specification;
|
|||
import org.springframework.data.querydsl.binding.QuerydslPredicate;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.querydsl.core.types.Predicate;
|
||||
import com.querydsl.core.types.dsl.BooleanExpression;
|
||||
|
||||
import cz.jirutka.rsql.parser.RSQLParser;
|
||||
import cz.jirutka.rsql.parser.ast.Node;
|
||||
|
||||
//@EnableSpringDataWebSupport
|
||||
@Controller
|
||||
|
@ -46,11 +58,11 @@ public class UserController {
|
|||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/users")
|
||||
@ResponseBody
|
||||
public List<User> findAll(@RequestParam(value = "search", required = false) final String search) {
|
||||
final List<SearchCriteria> params = new ArrayList<SearchCriteria>();
|
||||
public List<User> findAll(@RequestParam(value = "search", required = false) String search) {
|
||||
List<SearchCriteria> params = new ArrayList<SearchCriteria>();
|
||||
if (search != null) {
|
||||
final Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),");
|
||||
final Matcher matcher = pattern.matcher(search + ",");
|
||||
Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),");
|
||||
Matcher matcher = pattern.matcher(search + ",");
|
||||
while (matcher.find()) {
|
||||
params.add(new SearchCriteria(matcher.group(1), matcher.group(2), matcher.group(3)));
|
||||
}
|
||||
|
@ -60,32 +72,36 @@ public class UserController {
|
|||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/users/spec")
|
||||
@ResponseBody
|
||||
public List<User> findAllBySpecification(@RequestParam(value = "search") final String search) {
|
||||
final UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
|
||||
final String operationSetExper = Joiner.on("|").join(SearchOperation.SIMPLE_OPERATION_SET);
|
||||
final Pattern pattern = Pattern.compile("(\\w+?)(" + operationSetExper + ")(\\p{Punct}?)(\\w+?)(\\p{Punct}?),");
|
||||
final Matcher matcher = pattern.matcher(search + ",");
|
||||
public List<User> findAllBySpecification(@RequestParam(value = "search") String search) {
|
||||
UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
|
||||
String operationSetExper = Joiner
|
||||
.on("|")
|
||||
.join(SearchOperation.SIMPLE_OPERATION_SET);
|
||||
Pattern pattern = Pattern.compile("(\\w+?)(" + operationSetExper + ")(\\p{Punct}?)(\\w+?)(\\p{Punct}?),");
|
||||
Matcher matcher = pattern.matcher(search + ",");
|
||||
while (matcher.find()) {
|
||||
builder.with(matcher.group(1), matcher.group(2), matcher.group(4), matcher.group(3), matcher.group(5));
|
||||
}
|
||||
|
||||
final Specification<User> spec = builder.build();
|
||||
Specification<User> spec = builder.build();
|
||||
return dao.findAll(spec);
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/users/espec")
|
||||
@GetMapping(value = "/users/espec")
|
||||
@ResponseBody
|
||||
public List<User> findAllByOptionalSpecification(@RequestParam(value = "search") final String search) {
|
||||
final Specification<User> spec = resolveSpecification(search);
|
||||
public List<User> findAllByOrPredicate(@RequestParam(value = "search") String search) {
|
||||
Specification<User> spec = resolveSpecification(search);
|
||||
return dao.findAll(spec);
|
||||
}
|
||||
|
||||
protected Specification<User> resolveSpecification(String searchParameters) {
|
||||
|
||||
final UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
|
||||
final String operationSetExper = Joiner.on("|").join(SearchOperation.SIMPLE_OPERATION_SET);
|
||||
final Pattern pattern = Pattern.compile("(\\p{Punct}?)(\\w+?)(" + operationSetExper + ")(\\p{Punct}?)(\\w+?)(\\p{Punct}?),");
|
||||
final Matcher matcher = pattern.matcher(searchParameters + ",");
|
||||
UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
|
||||
String operationSetExper = Joiner
|
||||
.on("|")
|
||||
.join(SearchOperation.SIMPLE_OPERATION_SET);
|
||||
Pattern pattern = Pattern.compile("(\\p{Punct}?)(\\w+?)(" + operationSetExper + ")(\\p{Punct}?)(\\w+?)(\\p{Punct}?),");
|
||||
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));
|
||||
}
|
||||
|
@ -94,30 +110,30 @@ public class UserController {
|
|||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/myusers")
|
||||
@ResponseBody
|
||||
public Iterable<MyUser> findAllByQuerydsl(@RequestParam(value = "search") final String search) {
|
||||
final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder();
|
||||
public Iterable<MyUser> findAllByQuerydsl(@RequestParam(value = "search") String search) {
|
||||
MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder();
|
||||
if (search != null) {
|
||||
final Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),");
|
||||
final Matcher matcher = pattern.matcher(search + ",");
|
||||
Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),");
|
||||
Matcher matcher = pattern.matcher(search + ",");
|
||||
while (matcher.find()) {
|
||||
builder.with(matcher.group(1), matcher.group(2), matcher.group(3));
|
||||
}
|
||||
}
|
||||
final BooleanExpression exp = builder.build();
|
||||
BooleanExpression exp = builder.build();
|
||||
return myUserRepository.findAll(exp);
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/users/rsql")
|
||||
@ResponseBody
|
||||
public List<User> findAllByRsql(@RequestParam(value = "search") final String search) {
|
||||
final Node rootNode = new RSQLParser().parse(search);
|
||||
final Specification<User> spec = rootNode.accept(new CustomRsqlVisitor<User>());
|
||||
public List<User> findAllByRsql(@RequestParam(value = "search") String search) {
|
||||
Node rootNode = new RSQLParser().parse(search);
|
||||
Specification<User> spec = rootNode.accept(new CustomRsqlVisitor<User>());
|
||||
return dao.findAll(spec);
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/api/myusers")
|
||||
@ResponseBody
|
||||
public Iterable<MyUser> findAllByWebQuerydsl(@QuerydslPredicate(root = MyUser.class) final Predicate predicate) {
|
||||
public Iterable<MyUser> findAllByWebQuerydsl(@QuerydslPredicate(root = MyUser.class) Predicate predicate) {
|
||||
return myUserRepository.findAll(predicate);
|
||||
}
|
||||
|
||||
|
@ -125,14 +141,14 @@ public class UserController {
|
|||
|
||||
@RequestMapping(method = RequestMethod.POST, value = "/users")
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
public void create(@RequestBody final User resource) {
|
||||
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 final MyUser resource) {
|
||||
public void addMyUser(@RequestBody MyUser resource) {
|
||||
Preconditions.checkNotNull(resource);
|
||||
myUserRepository.save(resource);
|
||||
|
||||
|
|
|
@ -5,9 +5,9 @@ public enum SearchOperation {
|
|||
|
||||
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) {
|
||||
switch (input) {
|
||||
|
|
|
@ -5,7 +5,7 @@ public class SpecSearchCriteria {
|
|||
private String key;
|
||||
private SearchOperation operation;
|
||||
private Object value;
|
||||
private boolean lowPrecedence;
|
||||
private boolean orPredicate;
|
||||
|
||||
public SpecSearchCriteria() {
|
||||
|
||||
|
@ -18,9 +18,9 @@ public class SpecSearchCriteria {
|
|||
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();
|
||||
this.lowPrecedence = lowPrecedenceIndicator != null && lowPrecedenceIndicator.equals(SearchOperation.LOW_PRECEDENCE_INDICATOR);
|
||||
this.orPredicate = orPredicate != null && orPredicate.equals(SearchOperation.OR_PREDICATE_FLAG);
|
||||
this.key = key;
|
||||
this.operation = operation;
|
||||
this.value = value;
|
||||
|
@ -50,12 +50,12 @@ public class SpecSearchCriteria {
|
|||
this.value = value;
|
||||
}
|
||||
|
||||
public boolean isLowPrecedence() {
|
||||
return lowPrecedence;
|
||||
public boolean isOrPredicate() {
|
||||
return orPredicate;
|
||||
}
|
||||
|
||||
public void setLowPrecedence(boolean lowPrecedence) {
|
||||
this.lowPrecedence = lowPrecedence;
|
||||
public void setOrPredicate(boolean orPredicate) {
|
||||
this.orPredicate = orPredicate;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -70,7 +70,9 @@ public class JPASpecificationIntegrationTest {
|
|||
public void givenFirstAndLastName_whenGettingListOfUsers_thenCorrect() {
|
||||
final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("firstName", SearchOperation.EQUALITY, "john"));
|
||||
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(userTom, not(isIn(results)));
|
||||
|
@ -80,10 +82,13 @@ public class JPASpecificationIntegrationTest {
|
|||
public void givenFirstOrLastName_whenGettingListOfUsers_thenCorrect() {
|
||||
UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
|
||||
|
||||
final SpecSearchCriteria spec = new SpecSearchCriteria("'", "firstName", SearchOperation.EQUALITY, "john");
|
||||
final SpecSearchCriteria spec1 = new SpecSearchCriteria("lastName", SearchOperation.EQUALITY, "doe");
|
||||
SpecSearchCriteria spec = new SpecSearchCriteria("'", "firstName", SearchOperation.EQUALITY, "john");
|
||||
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(userJohn, isIn(results));
|
||||
|
@ -97,7 +102,8 @@ public class JPASpecificationIntegrationTest {
|
|||
builder.with("'", "firstName", ":", "john", 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(userJohn, isIn(results));
|
||||
assertThat(userTom, isIn(results));
|
||||
|
@ -116,7 +122,6 @@ public class JPASpecificationIntegrationTest {
|
|||
public void givenMinAge_whenGettingListOfUsers_thenCorrect() {
|
||||
final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("age", SearchOperation.GREATER_THAN, "25"));
|
||||
final List<User> results = repository.findAll(Specifications.where(spec));
|
||||
|
||||
assertThat(userTom, isIn(results));
|
||||
assertThat(userJohn, not(isIn(results)));
|
||||
}
|
||||
|
@ -125,7 +130,6 @@ public class JPASpecificationIntegrationTest {
|
|||
public void givenFirstNamePrefix_whenGettingListOfUsers_thenCorrect() {
|
||||
final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("firstName", SearchOperation.STARTS_WITH, "jo"));
|
||||
final List<User> results = repository.findAll(spec);
|
||||
|
||||
assertThat(userJohn, isIn(results));
|
||||
assertThat(userTom, not(isIn(results)));
|
||||
}
|
||||
|
@ -134,7 +138,6 @@ public class JPASpecificationIntegrationTest {
|
|||
public void givenFirstNameSuffix_whenGettingListOfUsers_thenCorrect() {
|
||||
final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("firstName", SearchOperation.ENDS_WITH, "n"));
|
||||
final List<User> results = repository.findAll(spec);
|
||||
|
||||
assertThat(userJohn, isIn(results));
|
||||
assertThat(userTom, not(isIn(results)));
|
||||
}
|
||||
|
@ -152,7 +155,9 @@ public class JPASpecificationIntegrationTest {
|
|||
public void givenAgeRange_whenGettingListOfUsers_thenCorrect() {
|
||||
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 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(userTom, not(isIn(results)));
|
||||
|
|
Loading…
Reference in New Issue