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:
ahamedm 2017-03-28 18:50:00 +04:00 committed by pedja4
parent 70d8fecc54
commit ef91c379b7
8 changed files with 250 additions and 221 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
*/bin/*
*.class
# Package Files #

View File

@ -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;
}
}

View File

@ -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;
@ -12,53 +13,64 @@ import org.springframework.data.jpa.domain.Specifications;
public class GenericSpecificationsBuilder {
private final List<SpecSearchCriteria> params;
private final List<SpecSearchCriteria> params;
public GenericSpecificationsBuilder() {
this.params = new ArrayList<>();
}
public GenericSpecificationsBuilder() {
this.params = new ArrayList<>();
}
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 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) {
SearchOperation op = SearchOperation.getSimpleOperation(operation.charAt(0));
if (op != null) {
if (op == SearchOperation.EQUALITY) // the operation may be complex operation
{
final boolean startWithAsterisk = prefix != null && prefix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
final boolean endWithAsterisk = suffix != null && suffix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
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
{
final boolean startWithAsterisk = prefix != null && prefix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
final boolean endWithAsterisk = suffix != null && suffix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
if (startWithAsterisk && endWithAsterisk) {
op = SearchOperation.CONTAINS;
} else if (startWithAsterisk) {
op = SearchOperation.ENDS_WITH;
} else if (endWithAsterisk) {
op = SearchOperation.STARTS_WITH;
}
}
params.add(new SpecSearchCriteria(precedenceIndicator, key, op, value));
}
return this;
}
if (startWithAsterisk && endWithAsterisk) {
op = SearchOperation.CONTAINS;
} else if (startWithAsterisk) {
op = SearchOperation.ENDS_WITH;
} else if (endWithAsterisk) {
op = SearchOperation.STARTS_WITH;
}
}
params.add(new SpecSearchCriteria(precedenceIndicator, key, op, value));
}
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;
}
}

View File

@ -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)));
}

View File

@ -1,141 +1,157 @@
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 org.baeldung.persistence.dao.rsql.CustomRsqlVisitor;
import org.baeldung.persistence.model.MyUser;
import org.baeldung.persistence.model.User;
import org.baeldung.web.util.SearchCriteria;
import org.baeldung.web.util.SearchOperation;
import org.springframework.beans.factory.annotation.Autowired;
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 java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
//@EnableSpringDataWebSupport
@Controller
@RequestMapping(value = "/auth/")
public class UserController {
@Autowired
private IUserDAO service;
@Autowired
private UserRepository dao;
@Autowired
private MyUserRepository myUserRepository;
public UserController() {
super();
}
// API - READ
@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>();
if (search != null) {
final Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),");
final Matcher matcher = pattern.matcher(search + ",");
while (matcher.find()) {
params.add(new SearchCriteria(matcher.group(1), matcher.group(2), matcher.group(3)));
}
}
return service.searchUser(params);
}
@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 + ",");
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();
return dao.findAll(spec);
}
@RequestMapping(method = RequestMethod.GET, value = "/users/espec")
@ResponseBody
public List<User> findAllByOptionalSpecification(@RequestParam(value = "search") final String search) {
final 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 + ",");
while (matcher.find()) {
builder.with(matcher.group(1), matcher.group(2), matcher.group(3), matcher.group(5), matcher.group(4), matcher.group(6));
}
return builder.build();
}
@RequestMapping(method = RequestMethod.GET, value = "/myusers")
@ResponseBody
public Iterable<MyUser> findAllByQuerydsl(@RequestParam(value = "search") final String search) {
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.with(matcher.group(1), matcher.group(2), matcher.group(3));
}
}
final 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>());
return dao.findAll(spec);
}
@RequestMapping(method = RequestMethod.GET, value = "/api/myusers")
@ResponseBody
public Iterable<MyUser> findAllByWebQuerydsl(@QuerydslPredicate(root = MyUser.class) final Predicate predicate) {
return myUserRepository.findAll(predicate);
}
// API - WRITE
@RequestMapping(method = RequestMethod.POST, value = "/users")
@ResponseStatus(HttpStatus.CREATED)
public void create(@RequestBody final User resource) {
Preconditions.checkNotNull(resource);
dao.save(resource);
}
@RequestMapping(method = RequestMethod.POST, value = "/myusers")
@ResponseStatus(HttpStatus.CREATED)
public void addMyUser(@RequestBody final MyUser resource) {
Preconditions.checkNotNull(resource);
myUserRepository.save(resource);
}
}
package org.baeldung.web.controller;
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;
import org.baeldung.web.util.SearchCriteria;
import org.baeldung.web.util.SearchOperation;
import org.springframework.beans.factory.annotation.Autowired;
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.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 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
@RequestMapping(value = "/auth/")
public class UserController {
@Autowired
private IUserDAO service;
@Autowired
private UserRepository dao;
@Autowired
private MyUserRepository myUserRepository;
public UserController() {
super();
}
// API - READ
@RequestMapping(method = RequestMethod.GET, value = "/users")
@ResponseBody
public List<User> findAll(@RequestParam(value = "search", required = false) String search) {
List<SearchCriteria> params = new ArrayList<SearchCriteria>();
if (search != null) {
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)));
}
}
return service.searchUser(params);
}
@RequestMapping(method = RequestMethod.GET, value = "/users/spec")
@ResponseBody
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));
}
Specification<User> spec = builder.build();
return dao.findAll(spec);
}
@GetMapping(value = "/users/espec")
@ResponseBody
public List<User> findAllByOrPredicate(@RequestParam(value = "search") String search) {
Specification<User> spec = resolveSpecification(search);
return dao.findAll(spec);
}
protected Specification<User> resolveSpecification(String 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));
}
return builder.build();
}
@RequestMapping(method = RequestMethod.GET, value = "/myusers")
@ResponseBody
public Iterable<MyUser> findAllByQuerydsl(@RequestParam(value = "search") String search) {
MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder();
if (search != null) {
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));
}
}
BooleanExpression exp = builder.build();
return myUserRepository.findAll(exp);
}
@RequestMapping(method = RequestMethod.GET, value = "/users/rsql")
@ResponseBody
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) Predicate predicate) {
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);
}
}

View File

@ -4,10 +4,10 @@ public enum SearchOperation {
EQUALITY, NEGATION, GREATER_THAN, LESS_THAN, LIKE, STARTS_WITH, ENDS_WITH, CONTAINS;
public static final String[] SIMPLE_OPERATION_SET = { ":", "!", ">", "<", "~" };
public static final String LOW_PRECEDENCE_INDICATOR="'";
public static final String ZERO_OR_MORE_REGEX="*";
public static final String OR_PREDICATE_FLAG = "'";
public static final String ZERO_OR_MORE_REGEX = "*";
public static SearchOperation getSimpleOperation(final char input) {
switch (input) {

View File

@ -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;
}
}

View File

@ -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)));