diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/MyUserPredicate.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/MyUserPredicate.java index 63ae35ec1f..5dd4cdd17e 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/MyUserPredicate.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/MyUserPredicate.java @@ -12,27 +12,23 @@ public class MyUserPredicate { private SearchCriteria criteria; - public MyUserPredicate() { - - } - public MyUserPredicate(final SearchCriteria criteria) { this.criteria = criteria; } public BooleanExpression getPredicate() { - final PathBuilder entityPath = new PathBuilder(MyUser.class, "myUser"); + final PathBuilder entityPath = new PathBuilder<>(MyUser.class, "myUser"); if (isNumeric(criteria.getValue().toString())) { - System.out.println("Nuumber"); final NumberPath path = entityPath.getNumber(criteria.getKey(), Integer.class); final int value = Integer.parseInt(criteria.getValue().toString()); - if (criteria.getOperation().equalsIgnoreCase(":")) { - return path.eq(value); - } else if (criteria.getOperation().equalsIgnoreCase(">")) { - return path.goe(value); - } else if (criteria.getOperation().equalsIgnoreCase("<")) { - return path.loe(value); + switch (criteria.getOperation()) { + case ":": + return path.eq(value); + case ">": + return path.goe(value); + case "<": + return path.loe(value); } } else { final StringPath path = entityPath.getString(criteria.getKey()); diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/MyUserPredicatesBuilder.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/MyUserPredicatesBuilder.java index caee59c1ec..c7b9f8358e 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/MyUserPredicatesBuilder.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/MyUserPredicatesBuilder.java @@ -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 params; public MyUserPredicatesBuilder() { - params = new ArrayList(); + 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 predicates = new ArrayList(); + final List predicates = new ArrayList<>(); MyUserPredicate predicate; for (final SearchCriteria param : params) { predicate = new MyUserPredicate(param); diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/MyUserRepository.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/MyUserRepository.java index 029b57016b..82ae1ee841 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/MyUserRepository.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/MyUserRepository.java @@ -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, QueryDslPredicateExecutor, QuerydslBinderCustomizer { @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) StringExpression::containsIgnoreCase); bindings.excluding(root.email); } diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/UserDAO.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/UserDAO.java index 8a3d74153e..8a01f8c97c 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/UserDAO.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/UserDAO.java @@ -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 result = entityManager.createQuery(query).getResultList(); - return result; + return entityManager.createQuery(query).getResultList(); } @Override diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/UserSpecification.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/UserSpecification.java index 2788c46fde..b2d9394500 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/UserSpecification.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/UserSpecification.java @@ -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 { private SpecSearchCriteria criteria; @@ -30,17 +30,17 @@ public class UserSpecification implements Specification { case NEGATION: return builder.notEqual(root.get(criteria.getKey()), criteria.getValue()); case GREATER_THAN: - return builder.greaterThan(root. get(criteria.getKey()), criteria.getValue().toString()); + return builder.greaterThan(root.get(criteria.getKey()), criteria.getValue().toString()); case LESS_THAN: - return builder.lessThan(root. get(criteria.getKey()), criteria.getValue().toString()); + return builder.lessThan(root.get(criteria.getKey()), criteria.getValue().toString()); case LIKE: - return builder.like(root. get(criteria.getKey()), criteria.getValue().toString()); + return builder.like(root.get(criteria.getKey()), criteria.getValue().toString()); case STARTS_WITH: - return builder.like(root. get(criteria.getKey()), criteria.getValue() + "%"); + return builder.like(root.get(criteria.getKey()), criteria.getValue() + "%"); case ENDS_WITH: - return builder.like(root. get(criteria.getKey()), "%" + criteria.getValue()); + return builder.like(root.get(criteria.getKey()), "%" + criteria.getValue()); case CONTAINS: - return builder.like(root. get(criteria.getKey()), "%" + criteria.getValue() + "%"); + return builder.like(root.get(criteria.getKey()), "%" + criteria.getValue() + "%"); default: return null; } diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/UserSpecificationsBuilder.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/UserSpecificationsBuilder.java index a8e5b96acb..918e77720f 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/UserSpecificationsBuilder.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/UserSpecificationsBuilder.java @@ -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 params; public UserSpecificationsBuilder() { - params = new ArrayList(); + params = new ArrayList<>(); } // API diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/rsql/GenericRsqlSpecBuilder.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/rsql/GenericRsqlSpecBuilder.java index 01ec389465..4efec00ed6 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/rsql/GenericRsqlSpecBuilder.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/rsql/GenericRsqlSpecBuilder.java @@ -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 { @@ -50,8 +49,7 @@ public class GenericRsqlSpecBuilder { } public Specifications createSpecification(final ComparisonNode comparisonNode) { - final Specifications result = Specifications.where(new GenericRsqlSpecification(comparisonNode.getSelector(), comparisonNode.getOperator(), comparisonNode.getArguments())); - return result; + return Specifications.where(new GenericRsqlSpecification(comparisonNode.getSelector(), comparisonNode.getOperator(), comparisonNode.getArguments())); } } diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/rsql/GenericRsqlSpecification.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/rsql/GenericRsqlSpecification.java index 6609a87953..937c6d953c 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/rsql/GenericRsqlSpecification.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/rsql/GenericRsqlSpecification.java @@ -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 implements Specification { @@ -33,7 +31,7 @@ public class GenericRsqlSpecification implements Specification { case EQUAL: { if (argument instanceof String) { - return builder.like(root. 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 { diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/rsql/RsqlSearchOperation.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/rsql/RsqlSearchOperation.java index 7f9ad79f63..673e78fbb4 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/rsql/RsqlSearchOperation.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/rsql/RsqlSearchOperation.java @@ -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() { diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/model/User.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/model/User.java index d220f4458e..670d4a2e74 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/persistence/model/User.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/persistence/model/User.java @@ -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