generalization of the user specific spec
This commit is contained in:
parent
539e5fed63
commit
d5983b0d8c
|
@ -8,18 +8,17 @@ import javax.persistence.criteria.CriteriaQuery;
|
||||||
import javax.persistence.criteria.Predicate;
|
import javax.persistence.criteria.Predicate;
|
||||||
import javax.persistence.criteria.Root;
|
import javax.persistence.criteria.Root;
|
||||||
|
|
||||||
import org.baeldung.persistence.model.User;
|
|
||||||
import org.springframework.data.jpa.domain.Specification;
|
import org.springframework.data.jpa.domain.Specification;
|
||||||
|
|
||||||
import cz.jirutka.rsql.parser.ast.ComparisonOperator;
|
import cz.jirutka.rsql.parser.ast.ComparisonOperator;
|
||||||
|
|
||||||
public class UserRsqlSpecification implements Specification<User> {
|
public class GenericRsqlSpecification<T> implements Specification<T> {
|
||||||
|
|
||||||
private String property;
|
private String property;
|
||||||
private ComparisonOperator operator;
|
private ComparisonOperator operator;
|
||||||
private List<String> arguments;
|
private List<String> arguments;
|
||||||
|
|
||||||
public UserRsqlSpecification(final String property, final ComparisonOperator operator, final List<String> arguments) {
|
public GenericRsqlSpecification(final String property, final ComparisonOperator operator, final List<String> arguments) {
|
||||||
super();
|
super();
|
||||||
this.property = property;
|
this.property = property;
|
||||||
this.operator = operator;
|
this.operator = operator;
|
||||||
|
@ -27,7 +26,7 @@ public class UserRsqlSpecification implements Specification<User> {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Predicate toPredicate(final Root<User> root, final CriteriaQuery<?> query, final CriteriaBuilder builder) {
|
public Predicate toPredicate(final Root<T> root, final CriteriaQuery<?> query, final CriteriaBuilder builder) {
|
||||||
final List<Object> args = castArguments(root);
|
final List<Object> args = castArguments(root);
|
||||||
final Object argument = args.get(0);
|
final Object argument = args.get(0);
|
||||||
switch (RsqlSearchOperation.getSimpleOperator(operator)) {
|
switch (RsqlSearchOperation.getSimpleOperator(operator)) {
|
||||||
|
@ -73,7 +72,7 @@ public class UserRsqlSpecification implements Specification<User> {
|
||||||
|
|
||||||
// === private
|
// === private
|
||||||
|
|
||||||
private List<Object> castArguments(final Root<User> root) {
|
private List<Object> castArguments(final Root<T> root) {
|
||||||
final List<Object> args = new ArrayList<Object>();
|
final List<Object> args = new ArrayList<Object>();
|
||||||
final Class<? extends Object> type = root.get(property).getJavaType();
|
final Class<? extends Object> type = root.get(property).getJavaType();
|
||||||
|
|
|
@ -51,7 +51,7 @@ public class UserRsqlSpecBuilder {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Specifications<User> createSpecification(final ComparisonNode comparisonNode) {
|
public Specifications<User> createSpecification(final ComparisonNode comparisonNode) {
|
||||||
final Specifications<User> result = Specifications.where(new UserRsqlSpecification(comparisonNode.getSelector(), comparisonNode.getOperator(), comparisonNode.getArguments()));
|
final Specifications<User> result = Specifications.where(new GenericRsqlSpecification<User>(comparisonNode.getSelector(), comparisonNode.getOperator(), comparisonNode.getArguments()));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue