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,26 +12,22 @@ public class MyUserPredicate {
private SearchCriteria criteria;
public MyUserPredicate() {
}
public MyUserPredicate(final SearchCriteria criteria) {
this.criteria = criteria;
}
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())) {
System.out.println("Nuumber");
final NumberPath<Integer> path = entityPath.getNumber(criteria.getKey(), Integer.class);
final int value = Integer.parseInt(criteria.getValue().toString());
if (criteria.getOperation().equalsIgnoreCase(":")) {
switch (criteria.getOperation()) {
case ":":
return path.eq(value);
} else if (criteria.getOperation().equalsIgnoreCase(">")) {
case ">":
return path.goe(value);
} else if (criteria.getOperation().equalsIgnoreCase("<")) {
case "<":
return path.loe(value);
}
} else {

View File

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

View File

@ -1,5 +1,6 @@
package org.baeldung.persistence.dao;
import com.querydsl.core.types.dsl.StringExpression;
import org.baeldung.persistence.model.MyUser;
import org.baeldung.persistence.model.QMyUser;
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 com.querydsl.core.types.dsl.StringPath;
import org.springframework.data.querydsl.binding.SingleValueBinding;
public interface MyUserRepository extends JpaRepository<MyUser, Long>, QueryDslPredicateExecutor<MyUser>, QuerydslBinderCustomizer<QMyUser> {
@Override
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);
}

View File

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

View File

@ -1,14 +1,14 @@
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.CriteriaQuery;
import javax.persistence.criteria.Predicate;
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> {
private SpecSearchCriteria criteria;
@ -30,17 +30,17 @@ public class UserSpecification implements Specification<User> {
case NEGATION:
return builder.notEqual(root.get(criteria.getKey()), criteria.getValue());
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:
return builder.lessThan(root.<String> get(criteria.getKey()), criteria.getValue().toString());
return builder.lessThan(root.get(criteria.getKey()), criteria.getValue().toString());
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:
return builder.like(root.<String> get(criteria.getKey()), criteria.getValue() + "%");
return builder.like(root.get(criteria.getKey()), criteria.getValue() + "%");
case ENDS_WITH:
return builder.like(root.<String> get(criteria.getKey()), "%" + criteria.getValue());
return builder.like(root.get(criteria.getKey()), "%" + criteria.getValue());
case CONTAINS:
return builder.like(root.<String> get(criteria.getKey()), "%" + criteria.getValue() + "%");
return builder.like(root.get(criteria.getKey()), "%" + criteria.getValue() + "%");
default:
return null;
}

View File

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

View File

@ -1,14 +1,13 @@
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.LogicalNode;
import cz.jirutka.rsql.parser.ast.LogicalOperator;
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> {
@ -50,8 +49,7 @@ public class GenericRsqlSpecBuilder<T> {
}
public Specifications<T> createSpecification(final ComparisonNode comparisonNode) {
final Specifications<T> result = Specifications.where(new GenericRsqlSpecification<T>(comparisonNode.getSelector(), comparisonNode.getOperator(), comparisonNode.getArguments()));
return result;
return Specifications.where(new GenericRsqlSpecification<T>(comparisonNode.getSelector(), comparisonNode.getOperator(), comparisonNode.getArguments()));
}
}

View File

@ -1,16 +1,14 @@
package org.baeldung.persistence.dao.rsql;
import java.util.ArrayList;
import java.util.List;
import cz.jirutka.rsql.parser.ast.ComparisonOperator;
import org.springframework.data.jpa.domain.Specification;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import org.springframework.data.jpa.domain.Specification;
import cz.jirutka.rsql.parser.ast.ComparisonOperator;
import java.util.ArrayList;
import java.util.List;
public class GenericRsqlSpecification<T> implements Specification<T> {
@ -33,7 +31,7 @@ public class GenericRsqlSpecification<T> implements Specification<T> {
case EQUAL: {
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) {
return builder.isNull(root.get(property));
} 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.RSQLOperators;
import java.util.Arrays;
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(
RSQLOperators.LESS_THAN_OR_EQUAL), IN(RSQLOperators.IN), NOT_IN(RSQLOperators.NOT_IN);
private ComparisonOperator operator;
private RsqlSearchOperation(final ComparisonOperator operator) {
RsqlSearchOperation(final ComparisonOperator operator) {
this.operator = operator;
}
public static RsqlSearchOperation getSimpleOperator(final ComparisonOperator operator) {
for (final RsqlSearchOperation operation : values()) {
if (operation.getOperator() == operator) {
return operation;
}
}
return null;
return Arrays.stream(values())
.filter(operation -> operation.getOperator() == operator)
.findAny().orElse(null);
}
public ComparisonOperator getOperator() {

View File

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