This commit is contained in:
pivovarit 2017-05-17 11:34:34 +03:00
parent faa15d50ce
commit ee770f3d5f
10 changed files with 53 additions and 64 deletions

View File

@ -12,27 +12,23 @@ public class MyUserPredicate {
private SearchCriteria criteria; private SearchCriteria criteria;
public MyUserPredicate() {
}
public MyUserPredicate(final SearchCriteria criteria) { public MyUserPredicate(final SearchCriteria criteria) {
this.criteria = criteria; this.criteria = criteria;
} }
public BooleanExpression getPredicate() { public BooleanExpression getPredicate() {
final PathBuilder<MyUser> entityPath = new PathBuilder<MyUser>(MyUser.class, "myUser"); final PathBuilder<MyUser> entityPath = new PathBuilder<>(MyUser.class, "myUser");
if (isNumeric(criteria.getValue().toString())) { if (isNumeric(criteria.getValue().toString())) {
System.out.println("Nuumber");
final NumberPath<Integer> path = entityPath.getNumber(criteria.getKey(), Integer.class); final NumberPath<Integer> path = entityPath.getNumber(criteria.getKey(), Integer.class);
final int value = Integer.parseInt(criteria.getValue().toString()); final int value = Integer.parseInt(criteria.getValue().toString());
if (criteria.getOperation().equalsIgnoreCase(":")) { switch (criteria.getOperation()) {
return path.eq(value); case ":":
} else if (criteria.getOperation().equalsIgnoreCase(">")) { return path.eq(value);
return path.goe(value); case ">":
} else if (criteria.getOperation().equalsIgnoreCase("<")) { return path.goe(value);
return path.loe(value); case "<":
return path.loe(value);
} }
} else { } else {
final StringPath path = entityPath.getString(criteria.getKey()); final StringPath path = entityPath.getString(criteria.getKey());

View File

@ -1,17 +1,16 @@
package org.baeldung.persistence.dao; package org.baeldung.persistence.dao;
import com.querydsl.core.types.dsl.BooleanExpression;
import org.baeldung.web.util.SearchCriteria;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.baeldung.web.util.SearchCriteria;
import com.querydsl.core.types.dsl.BooleanExpression;
public final class MyUserPredicatesBuilder { public final class MyUserPredicatesBuilder {
private final List<SearchCriteria> params; private final List<SearchCriteria> params;
public MyUserPredicatesBuilder() { public MyUserPredicatesBuilder() {
params = new ArrayList<SearchCriteria>(); params = new ArrayList<>();
} }
public MyUserPredicatesBuilder with(final String key, final String operation, final Object value) { public MyUserPredicatesBuilder with(final String key, final String operation, final Object value) {
@ -24,7 +23,7 @@ public final class MyUserPredicatesBuilder {
return null; return null;
} }
final List<BooleanExpression> predicates = new ArrayList<BooleanExpression>(); final List<BooleanExpression> predicates = new ArrayList<>();
MyUserPredicate predicate; MyUserPredicate predicate;
for (final SearchCriteria param : params) { for (final SearchCriteria param : params) {
predicate = new MyUserPredicate(param); predicate = new MyUserPredicate(param);

View File

@ -1,5 +1,6 @@
package org.baeldung.persistence.dao; package org.baeldung.persistence.dao;
import com.querydsl.core.types.dsl.StringExpression;
import org.baeldung.persistence.model.MyUser; import org.baeldung.persistence.model.MyUser;
import org.baeldung.persistence.model.QMyUser; import org.baeldung.persistence.model.QMyUser;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
@ -8,11 +9,13 @@ import org.springframework.data.querydsl.binding.QuerydslBinderCustomizer;
import org.springframework.data.querydsl.binding.QuerydslBindings; import org.springframework.data.querydsl.binding.QuerydslBindings;
import com.querydsl.core.types.dsl.StringPath; import com.querydsl.core.types.dsl.StringPath;
import org.springframework.data.querydsl.binding.SingleValueBinding;
public interface MyUserRepository extends JpaRepository<MyUser, Long>, QueryDslPredicateExecutor<MyUser>, QuerydslBinderCustomizer<QMyUser> { public interface MyUserRepository extends JpaRepository<MyUser, Long>, QueryDslPredicateExecutor<MyUser>, QuerydslBinderCustomizer<QMyUser> {
@Override @Override
default public void customize(final QuerydslBindings bindings, final QMyUser root) { default public void customize(final QuerydslBindings bindings, final QMyUser root) {
bindings.bind(String.class).first((final StringPath path, final String value) -> path.containsIgnoreCase(value)); bindings.bind(String.class)
.first((SingleValueBinding<StringPath, String>) StringExpression::containsIgnoreCase);
bindings.excluding(root.email); bindings.excluding(root.email);
} }

View File

@ -1,6 +1,8 @@
package org.baeldung.persistence.dao; package org.baeldung.persistence.dao;
import java.util.List; import org.baeldung.persistence.model.User;
import org.baeldung.web.util.SearchCriteria;
import org.springframework.stereotype.Repository;
import javax.persistence.EntityManager; import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext; import javax.persistence.PersistenceContext;
@ -8,10 +10,7 @@ import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery; 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 java.util.List;
import org.baeldung.persistence.model.User;
import org.baeldung.web.util.SearchCriteria;
import org.springframework.stereotype.Repository;
@Repository @Repository
public class UserDAO implements IUserDAO { public class UserDAO implements IUserDAO {
@ -42,8 +41,7 @@ public class UserDAO implements IUserDAO {
} }
query.where(predicate); query.where(predicate);
final List<User> result = entityManager.createQuery(query).getResultList(); return entityManager.createQuery(query).getResultList();
return result;
} }
@Override @Override

View File

@ -1,14 +1,14 @@
package org.baeldung.persistence.dao; package org.baeldung.persistence.dao;
import org.baeldung.persistence.model.User;
import org.baeldung.web.util.SpecSearchCriteria;
import org.springframework.data.jpa.domain.Specification;
import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery; 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.baeldung.web.util.SpecSearchCriteria;
import org.springframework.data.jpa.domain.Specification;
public class UserSpecification implements Specification<User> { public class UserSpecification implements Specification<User> {
private SpecSearchCriteria criteria; private SpecSearchCriteria criteria;
@ -30,17 +30,17 @@ public class UserSpecification implements Specification<User> {
case NEGATION: case NEGATION:
return builder.notEqual(root.get(criteria.getKey()), criteria.getValue()); return builder.notEqual(root.get(criteria.getKey()), criteria.getValue());
case GREATER_THAN: case GREATER_THAN:
return builder.greaterThan(root.<String> get(criteria.getKey()), criteria.getValue().toString()); return builder.greaterThan(root.get(criteria.getKey()), criteria.getValue().toString());
case LESS_THAN: case LESS_THAN:
return builder.lessThan(root.<String> get(criteria.getKey()), criteria.getValue().toString()); return builder.lessThan(root.get(criteria.getKey()), criteria.getValue().toString());
case LIKE: case LIKE:
return builder.like(root.<String> get(criteria.getKey()), criteria.getValue().toString()); return builder.like(root.get(criteria.getKey()), criteria.getValue().toString());
case STARTS_WITH: case STARTS_WITH:
return builder.like(root.<String> get(criteria.getKey()), criteria.getValue() + "%"); return builder.like(root.get(criteria.getKey()), criteria.getValue() + "%");
case ENDS_WITH: case ENDS_WITH:
return builder.like(root.<String> get(criteria.getKey()), "%" + criteria.getValue()); return builder.like(root.get(criteria.getKey()), "%" + criteria.getValue());
case CONTAINS: case CONTAINS:
return builder.like(root.<String> get(criteria.getKey()), "%" + criteria.getValue() + "%"); return builder.like(root.get(criteria.getKey()), "%" + criteria.getValue() + "%");
default: default:
return null; return null;
} }

View File

@ -1,20 +1,20 @@
package org.baeldung.persistence.dao; package org.baeldung.persistence.dao;
import java.util.ArrayList;
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;
public UserSpecificationsBuilder() { public UserSpecificationsBuilder() {
params = new ArrayList<SpecSearchCriteria>(); params = new ArrayList<>();
} }
// API // API

View File

@ -1,14 +1,13 @@
package org.baeldung.persistence.dao.rsql; package org.baeldung.persistence.dao.rsql;
import java.util.ArrayList;
import java.util.List;
import org.springframework.data.jpa.domain.Specifications;
import cz.jirutka.rsql.parser.ast.ComparisonNode; import cz.jirutka.rsql.parser.ast.ComparisonNode;
import cz.jirutka.rsql.parser.ast.LogicalNode; import cz.jirutka.rsql.parser.ast.LogicalNode;
import cz.jirutka.rsql.parser.ast.LogicalOperator; import cz.jirutka.rsql.parser.ast.LogicalOperator;
import cz.jirutka.rsql.parser.ast.Node; import cz.jirutka.rsql.parser.ast.Node;
import org.springframework.data.jpa.domain.Specifications;
import java.util.ArrayList;
import java.util.List;
public class GenericRsqlSpecBuilder<T> { public class GenericRsqlSpecBuilder<T> {
@ -50,8 +49,7 @@ public class GenericRsqlSpecBuilder<T> {
} }
public Specifications<T> createSpecification(final ComparisonNode comparisonNode) { public Specifications<T> createSpecification(final ComparisonNode comparisonNode) {
final Specifications<T> result = Specifications.where(new GenericRsqlSpecification<T>(comparisonNode.getSelector(), comparisonNode.getOperator(), comparisonNode.getArguments())); return Specifications.where(new GenericRsqlSpecification<T>(comparisonNode.getSelector(), comparisonNode.getOperator(), comparisonNode.getArguments()));
return result;
} }
} }

View File

@ -1,16 +1,14 @@
package org.baeldung.persistence.dao.rsql; package org.baeldung.persistence.dao.rsql;
import java.util.ArrayList; import cz.jirutka.rsql.parser.ast.ComparisonOperator;
import java.util.List; import org.springframework.data.jpa.domain.Specification;
import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery; 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 java.util.ArrayList;
import org.springframework.data.jpa.domain.Specification; import java.util.List;
import cz.jirutka.rsql.parser.ast.ComparisonOperator;
public class GenericRsqlSpecification<T> implements Specification<T> { public class GenericRsqlSpecification<T> implements Specification<T> {
@ -33,7 +31,7 @@ public class GenericRsqlSpecification<T> implements Specification<T> {
case EQUAL: { case EQUAL: {
if (argument instanceof String) { if (argument instanceof String) {
return builder.like(root.<String> get(property), argument.toString().replace('*', '%')); return builder.like(root.get(property), argument.toString().replace('*', '%'));
} else if (argument == null) { } else if (argument == null) {
return builder.isNull(root.get(property)); return builder.isNull(root.get(property));
} else { } else {

View File

@ -3,23 +3,22 @@ package org.baeldung.persistence.dao.rsql;
import cz.jirutka.rsql.parser.ast.ComparisonOperator; import cz.jirutka.rsql.parser.ast.ComparisonOperator;
import cz.jirutka.rsql.parser.ast.RSQLOperators; import cz.jirutka.rsql.parser.ast.RSQLOperators;
import java.util.Arrays;
public enum RsqlSearchOperation { public enum RsqlSearchOperation {
EQUAL(RSQLOperators.EQUAL), NOT_EQUAL(RSQLOperators.NOT_EQUAL), GREATER_THAN(RSQLOperators.GREATER_THAN), GREATER_THAN_OR_EQUAL(RSQLOperators.GREATER_THAN_OR_EQUAL), LESS_THAN(RSQLOperators.LESS_THAN), LESS_THAN_OR_EQUAL( EQUAL(RSQLOperators.EQUAL), NOT_EQUAL(RSQLOperators.NOT_EQUAL), GREATER_THAN(RSQLOperators.GREATER_THAN), GREATER_THAN_OR_EQUAL(RSQLOperators.GREATER_THAN_OR_EQUAL), LESS_THAN(RSQLOperators.LESS_THAN), LESS_THAN_OR_EQUAL(
RSQLOperators.LESS_THAN_OR_EQUAL), IN(RSQLOperators.IN), NOT_IN(RSQLOperators.NOT_IN); RSQLOperators.LESS_THAN_OR_EQUAL), IN(RSQLOperators.IN), NOT_IN(RSQLOperators.NOT_IN);
private ComparisonOperator operator; private ComparisonOperator operator;
private RsqlSearchOperation(final ComparisonOperator operator) { RsqlSearchOperation(final ComparisonOperator operator) {
this.operator = operator; this.operator = operator;
} }
public static RsqlSearchOperation getSimpleOperator(final ComparisonOperator operator) { public static RsqlSearchOperation getSimpleOperator(final ComparisonOperator operator) {
for (final RsqlSearchOperation operation : values()) { return Arrays.stream(values())
if (operation.getOperator() == operator) { .filter(operation -> operation.getOperator() == operator)
return operation; .findAny().orElse(null);
}
}
return null;
} }
public ComparisonOperator getOperator() { public ComparisonOperator getOperator() {

View File

@ -81,9 +81,7 @@ public class User {
if (getClass() != obj.getClass()) if (getClass() != obj.getClass())
return false; return false;
final User user = (User) obj; final User user = (User) obj;
if (!email.equals(user.email)) return email.equals(user.email);
return false;
return true;
} }
@Override @Override