diff --git a/src/main/java/com/github/tennaito/rsql/jpa/AbstractJpaVisitor.java b/src/main/java/com/github/tennaito/rsql/jpa/AbstractJpaVisitor.java index 25065ea..1d79d55 100755 --- a/src/main/java/com/github/tennaito/rsql/jpa/AbstractJpaVisitor.java +++ b/src/main/java/com/github/tennaito/rsql/jpa/AbstractJpaVisitor.java @@ -23,10 +23,9 @@ */ package com.github.tennaito.rsql.jpa; -import javax.persistence.EntityManager; - import com.github.tennaito.rsql.builder.BuilderTools; import com.github.tennaito.rsql.builder.SimpleBuilderTools; +import com.github.tennaito.rsql.misc.EntityManagerAdapter; import cz.jirutka.rsql.parser.ast.RSQLVisitor; @@ -40,7 +39,7 @@ * @param Result type * @param Entity type */ -public abstract class AbstractJpaVisitor implements RSQLVisitor { +public abstract class AbstractJpaVisitor implements RSQLVisitor { protected Class entityClass; diff --git a/src/main/java/com/github/tennaito/rsql/jpa/JpaCriteriaCountQueryVisitor.java b/src/main/java/com/github/tennaito/rsql/jpa/JpaCriteriaCountQueryVisitor.java index 69dad9b..16387e2 100644 --- a/src/main/java/com/github/tennaito/rsql/jpa/JpaCriteriaCountQueryVisitor.java +++ b/src/main/java/com/github/tennaito/rsql/jpa/JpaCriteriaCountQueryVisitor.java @@ -26,11 +26,12 @@ import java.util.logging.Level; import java.util.logging.Logger; -import javax.persistence.EntityManager; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.Root; +import com.github.tennaito.rsql.misc.EntityManagerAdapter; + import cz.jirutka.rsql.parser.ast.AndNode; import cz.jirutka.rsql.parser.ast.ComparisonNode; import cz.jirutka.rsql.parser.ast.OrNode; @@ -45,7 +46,7 @@ * * @param Entity type */ -public class JpaCriteriaCountQueryVisitor extends AbstractJpaVisitor, T> implements RSQLVisitor, EntityManager> { +public class JpaCriteriaCountQueryVisitor extends AbstractJpaVisitor, T> implements RSQLVisitor, EntityManagerAdapter> { private static final Logger LOG = Logger.getLogger(JpaCriteriaCountQueryVisitor.class.getName()); @@ -77,7 +78,7 @@ protected JpaPredicateVisitor getPredicateVisitor() { /* (non-Javadoc) * @see cz.jirutka.rsql.parser.ast.RSQLVisitor#visit(cz.jirutka.rsql.parser.ast.AndNode, java.lang.Object) */ - public CriteriaQuery visit(AndNode node, EntityManager entityManager) { + public CriteriaQuery visit(AndNode node, EntityManagerAdapter entityManager) { LOG.log(Level.INFO, "Creating CriteriaQuery for AndNode: {0}", node); CriteriaBuilder cb = entityManager.getCriteriaBuilder(); @@ -92,7 +93,7 @@ public CriteriaQuery visit(AndNode node, EntityManager entityManager) { /* (non-Javadoc) * @see cz.jirutka.rsql.parser.ast.RSQLVisitor#visit(cz.jirutka.rsql.parser.ast.OrNode, java.lang.Object) */ - public CriteriaQuery visit(OrNode node, EntityManager entityManager) { + public CriteriaQuery visit(OrNode node, EntityManagerAdapter entityManager) { LOG.log(Level.INFO, "Creating CriteriaQuery for OrNode: {0}", node); CriteriaBuilder cb = entityManager.getCriteriaBuilder(); @@ -107,7 +108,7 @@ public CriteriaQuery visit(OrNode node, EntityManager entityManager) { /* (non-Javadoc) * @see cz.jirutka.rsql.parser.ast.RSQLVisitor#visit(cz.jirutka.rsql.parser.ast.ComparisonNode, java.lang.Object) */ - public CriteriaQuery visit(ComparisonNode node, EntityManager entityManager) { + public CriteriaQuery visit(ComparisonNode node, EntityManagerAdapter entityManager) { LOG.log(Level.INFO, "Creating CriteriaQuery for ComparisonNode: {0}", node); CriteriaBuilder cb = entityManager.getCriteriaBuilder(); @@ -127,4 +128,4 @@ public void setRoot(Root root) { } -} \ No newline at end of file +} diff --git a/src/main/java/com/github/tennaito/rsql/jpa/JpaCriteriaQueryVisitor.java b/src/main/java/com/github/tennaito/rsql/jpa/JpaCriteriaQueryVisitor.java index f9859c6..c6db13c 100755 --- a/src/main/java/com/github/tennaito/rsql/jpa/JpaCriteriaQueryVisitor.java +++ b/src/main/java/com/github/tennaito/rsql/jpa/JpaCriteriaQueryVisitor.java @@ -26,10 +26,11 @@ import java.util.logging.Level; import java.util.logging.Logger; -import javax.persistence.EntityManager; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.From; +import com.github.tennaito.rsql.misc.EntityManagerAdapter; + import cz.jirutka.rsql.parser.ast.AndNode; import cz.jirutka.rsql.parser.ast.ComparisonNode; import cz.jirutka.rsql.parser.ast.OrNode; @@ -44,7 +45,7 @@ * * @param Entity type */ -public class JpaCriteriaQueryVisitor extends AbstractJpaVisitor, T> implements RSQLVisitor, EntityManager> { +public class JpaCriteriaQueryVisitor extends AbstractJpaVisitor, T> implements RSQLVisitor, EntityManagerAdapter> { private static final Logger LOG = Logger.getLogger(JpaCriteriaQueryVisitor.class.getName()); @@ -73,7 +74,7 @@ protected JpaPredicateVisitor getPredicateVisitor() { /* (non-Javadoc) * @see cz.jirutka.rsql.parser.ast.RSQLVisitor#visit(cz.jirutka.rsql.parser.ast.AndNode, java.lang.Object) */ - public CriteriaQuery visit(AndNode node, EntityManager entityManager) { + public CriteriaQuery visit(AndNode node, EntityManagerAdapter entityManager) { LOG.log(Level.INFO, "Creating CriteriaQuery for AndNode: {0}", node); CriteriaQuery criteria = entityManager.getCriteriaBuilder().createQuery(entityClass); From root = criteria.from(entityClass); @@ -83,7 +84,7 @@ public CriteriaQuery visit(AndNode node, EntityManager entityManager) { /* (non-Javadoc) * @see cz.jirutka.rsql.parser.ast.RSQLVisitor#visit(cz.jirutka.rsql.parser.ast.OrNode, java.lang.Object) */ - public CriteriaQuery visit(OrNode node, EntityManager entityManager) { + public CriteriaQuery visit(OrNode node, EntityManagerAdapter entityManager) { LOG.log(Level.INFO, "Creating CriteriaQuery for OrNode: {0}", node); CriteriaQuery criteria = entityManager.getCriteriaBuilder().createQuery(entityClass); From root = criteria.from(entityClass); @@ -93,7 +94,7 @@ public CriteriaQuery visit(OrNode node, EntityManager entityManager) { /* (non-Javadoc) * @see cz.jirutka.rsql.parser.ast.RSQLVisitor#visit(cz.jirutka.rsql.parser.ast.ComparisonNode, java.lang.Object) */ - public CriteriaQuery visit(ComparisonNode node, EntityManager entityManager) { + public CriteriaQuery visit(ComparisonNode node, EntityManagerAdapter entityManager) { LOG.log(Level.INFO, "Creating CriteriaQuery for ComparisonNode: {0}", node); CriteriaQuery criteria = entityManager.getCriteriaBuilder().createQuery(entityClass); From root = criteria.from(entityClass); diff --git a/src/main/java/com/github/tennaito/rsql/jpa/JpaPredicateVisitor.java b/src/main/java/com/github/tennaito/rsql/jpa/JpaPredicateVisitor.java index a429afa..6dc1168 100755 --- a/src/main/java/com/github/tennaito/rsql/jpa/JpaPredicateVisitor.java +++ b/src/main/java/com/github/tennaito/rsql/jpa/JpaPredicateVisitor.java @@ -26,10 +26,11 @@ import java.util.logging.Level; import java.util.logging.Logger; -import javax.persistence.EntityManager; import javax.persistence.criteria.From; import javax.persistence.criteria.Predicate; +import com.github.tennaito.rsql.misc.EntityManagerAdapter; + import cz.jirutka.rsql.parser.ast.AndNode; import cz.jirutka.rsql.parser.ast.ComparisonNode; import cz.jirutka.rsql.parser.ast.OrNode; @@ -44,7 +45,7 @@ * * @param Entity type */ -public class JpaPredicateVisitor extends AbstractJpaVisitor implements RSQLVisitor { +public class JpaPredicateVisitor extends AbstractJpaVisitor implements RSQLVisitor { /** * Logger. @@ -78,7 +79,7 @@ public JpaPredicateVisitor defineRoot(From root) { /* (non-Javadoc) * @see cz.jirutka.rsql.parser.ast.RSQLVisitor#visit(cz.jirutka.rsql.parser.ast.AndNode, java.lang.Object) */ - public Predicate visit(AndNode node, EntityManager entityManager) { + public Predicate visit(AndNode node, EntityManagerAdapter entityManager) { LOG.log(Level.INFO, "Creating Predicate for AndNode: {0}", node); return PredicateBuilder.createPredicate(node, root, entityClass, entityManager, getBuilderTools()); } @@ -86,7 +87,7 @@ public Predicate visit(AndNode node, EntityManager entityManager) { /* (non-Javadoc) * @see cz.jirutka.rsql.parser.ast.RSQLVisitor#visit(cz.jirutka.rsql.parser.ast.OrNode, java.lang.Object) */ - public Predicate visit(OrNode node, EntityManager entityManager) { + public Predicate visit(OrNode node, EntityManagerAdapter entityManager) { LOG.log(Level.INFO, "Creating Predicate for OrNode: {0}", node); return PredicateBuilder.createPredicate(node, root, entityClass, entityManager, getBuilderTools()); } @@ -94,7 +95,7 @@ public Predicate visit(OrNode node, EntityManager entityManager) { /* (non-Javadoc) * @see cz.jirutka.rsql.parser.ast.RSQLVisitor#visit(cz.jirutka.rsql.parser.ast.ComparisonNode, java.lang.Object) */ - public Predicate visit(ComparisonNode node, EntityManager entityManager) { + public Predicate visit(ComparisonNode node, EntityManagerAdapter entityManager) { LOG.log(Level.INFO, "Creating Predicate for ComparisonNode: {0}", node); return PredicateBuilder.createPredicate(node, root, entityClass, entityManager, getBuilderTools()); } diff --git a/src/main/java/com/github/tennaito/rsql/jpa/PredicateBuilder.java b/src/main/java/com/github/tennaito/rsql/jpa/PredicateBuilder.java index 01b7c89..df2e4d5 100755 --- a/src/main/java/com/github/tennaito/rsql/jpa/PredicateBuilder.java +++ b/src/main/java/com/github/tennaito/rsql/jpa/PredicateBuilder.java @@ -24,14 +24,14 @@ */ package com.github.tennaito.rsql.jpa; -import com.github.tennaito.rsql.builder.BuilderTools; -import com.github.tennaito.rsql.parser.ast.ComparisonOperatorProxy; -import cz.jirutka.rsql.parser.ast.ComparisonNode; -import cz.jirutka.rsql.parser.ast.ComparisonOperator; -import cz.jirutka.rsql.parser.ast.LogicalNode; -import cz.jirutka.rsql.parser.ast.Node; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.List; +import java.util.Set; +import java.util.logging.Level; +import java.util.logging.Logger; -import javax.persistence.EntityManager; import javax.persistence.criteria.CriteriaBuilder; import javax.persistence.criteria.Expression; import javax.persistence.criteria.From; @@ -43,13 +43,15 @@ import javax.persistence.metamodel.ManagedType; import javax.persistence.metamodel.Metamodel; import javax.persistence.metamodel.PluralAttribute; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.Date; -import java.util.List; -import java.util.Set; -import java.util.logging.Level; -import java.util.logging.Logger; + +import com.github.tennaito.rsql.builder.BuilderTools; +import com.github.tennaito.rsql.misc.EntityManagerAdapter; +import com.github.tennaito.rsql.parser.ast.ComparisonOperatorProxy; + +import cz.jirutka.rsql.parser.ast.ComparisonNode; +import cz.jirutka.rsql.parser.ast.ComparisonOperator; +import cz.jirutka.rsql.parser.ast.LogicalNode; +import cz.jirutka.rsql.parser.ast.Node; /** * PredicateBuilder @@ -95,11 +97,11 @@ private PredicateBuilder(){ * @param node RSQL AST node. * @param root From that predicate expression paths depends on. * @param entity The main entity of the query. - * @param manager JPA EntityManager. + * @param manager JPA EntityManagerAdapter. * @param misc Facade with all necessary tools for predicate creation. * @return Predicate a predicate representation of the Node. */ - public static Predicate createPredicate(Node node, From root, Class entity, EntityManager manager, BuilderTools misc) { + public static Predicate createPredicate(Node node, From root, Class entity, EntityManagerAdapter manager, BuilderTools misc) { LOG.log(Level.INFO, "Creating Predicate for: {0}", node); if (node instanceof LogicalNode) { @@ -119,11 +121,11 @@ public static Predicate createPredicate(Node node, From root, Class entit * @param logical RSQL AST logical node. * @param root From that predicate expression paths depends on. * @param entity The main entity of the query. - * @param entityManager JPA EntityManager. + * @param entityManager JPA EntityManagerAdapter. * @param misc Facade with all necessary tools for predicate creation. * @return Predicate a predicate representation of the Node. */ - public static Predicate createPredicate(LogicalNode logical, From root, Class entity, EntityManager entityManager, BuilderTools misc) { + public static Predicate createPredicate(LogicalNode logical, From root, Class entity, EntityManagerAdapter entityManager, BuilderTools misc) { LOG.log(Level.INFO, "Creating Predicate for logical node: {0}", logical); CriteriaBuilder builder = entityManager.getCriteriaBuilder(); @@ -149,11 +151,11 @@ public static Predicate createPredicate(LogicalNode logical, From root, Clas * @param comparison RSQL AST comparison node. * @param startRoot From that predicate expression paths depends on. * @param entity The main entity of the query. - * @param entityManager JPA EntityManager. + * @param entityManager JPA EntityManagerAdapter. * @param misc Facade with all necessary tools for predicate creation. * @return Predicate a predicate representation of the Node. */ - public static Predicate createPredicate(ComparisonNode comparison, From startRoot, Class entity, EntityManager entityManager, BuilderTools misc) { + public static Predicate createPredicate(ComparisonNode comparison, From startRoot, Class entity, EntityManagerAdapter entityManager, BuilderTools misc) { if (startRoot == null) { String msg = "From root node was undefined."; LOG.log(Level.SEVERE, msg); @@ -185,12 +187,12 @@ public static Predicate createPredicate(ComparisonNode comparison, From star * * @param propertyPath The property path to find. * @param startRoot From that property path depends on. - * @param entityManager JPA EntityManager. + * @param entityManager JPA EntityManagerAdapter. * @param misc Facade with all necessary tools for predicate creation. * @return The Path for the property path * @throws IllegalArgumentException if attribute of the given property name does not exist */ - public static Path findPropertyPath(String propertyPath, Path startRoot, EntityManager entityManager, BuilderTools misc) { + public static Path findPropertyPath(String propertyPath, Path startRoot, EntityManagerAdapter entityManager, BuilderTools misc) { String[] graph = propertyPath.split("\\."); Metamodel metaModel = entityManager.getMetamodel(); @@ -241,10 +243,10 @@ public static Path findPropertyPath(String propertyPath, Path startRoot, * @param propertyPath Property path that we want to compare. * @param operator Comparison operator. * @param arguments Arguments (1 for binary comparisons, n for multi-value comparisons [in, not in (out)]) - * @param manager JPA EntityManager. + * @param manager JPA EntityManagerAdapter. * @return Predicate a predicate representation. */ - private static Predicate createPredicate(Expression propertyPath, ComparisonOperator operator, List arguments, EntityManager manager) { + private static Predicate createPredicate(Expression propertyPath, ComparisonOperator operator, List arguments, EntityManagerAdapter manager) { LOG.log(Level.INFO, "Creating predicate: propertyPath {0} {1}", new Object[]{operator, arguments}); if (ComparisonOperatorProxy.asEnum(operator) != null) { @@ -345,7 +347,7 @@ private static Predicate createPredicate(Expression propertyPath, ComparisonOper + * @param manager the manager + * @return the predicate + */ - private static Predicate createBetweenThan(Expression propertyPath, Date start, Date end, EntityManager manager) { + private static Predicate createBetweenThan(Expression propertyPath, Date start, Date end, EntityManagerAdapter manager) { CriteriaBuilder builder = manager.getCriteriaBuilder(); return builder.between(propertyPath, start, end); } @@ -356,10 +358,10 @@ private static Predicate createBetweenThan(Expression propertyPath, Date start, * * @param propertyPath Property path that we want to compare. * @param argument Argument with/without wildcards - * @param manager JPA EntityManager. + * @param manager JPA EntityManagerAdapter. * @return Predicate a predicate representation. */ - private static Predicate createLike(Expression propertyPath, String argument, EntityManager manager) { + private static Predicate createLike(Expression propertyPath, String argument, EntityManagerAdapter manager) { String like = argument.replace(LIKE_WILDCARD, '%'); CriteriaBuilder builder = manager.getCriteriaBuilder(); return builder.like(builder.lower(propertyPath), like.toLowerCase()); @@ -369,10 +371,10 @@ private static Predicate createLike(Expression propertyPath, String argu * Apply an "is null" constraint to the property path. * * @param propertyPath Property path that we want to compare. - * @param manager JPA EntityManager. + * @param manager JPA EntityManagerAdapter. * @return Predicate a predicate representation. */ - private static Predicate createIsNull(Expression propertyPath, EntityManager manager) { + private static Predicate createIsNull(Expression propertyPath, EntityManagerAdapter manager) { CriteriaBuilder builder = manager.getCriteriaBuilder(); return builder.isNull(propertyPath); } @@ -382,10 +384,10 @@ private static Predicate createIsNull(Expression propertyPath, EntityManager * * @param propertyPath Property path that we want to compare. * @param argument Argument - * @param manager JPA EntityManager. + * @param manager JPA EntityManagerAdapter. * @return Predicate a predicate representation. */ - private static Predicate createEqual(Expression propertyPath, Object argument, EntityManager manager) { + private static Predicate createEqual(Expression propertyPath, Object argument, EntityManagerAdapter manager) { CriteriaBuilder builder = manager.getCriteriaBuilder(); return builder.equal(propertyPath, argument); } @@ -395,10 +397,10 @@ private static Predicate createEqual(Expression propertyPath, Object argument * * @param propertyPath Property path that we want to compare. * @param argument Argument - * @param manager JPA EntityManager. + * @param manager JPA EntityManagerAdapter. * @return Predicate a predicate representation. */ - private static Predicate createNotEqual(Expression propertyPath, Object argument, EntityManager manager) { + private static Predicate createNotEqual(Expression propertyPath, Object argument, EntityManagerAdapter manager) { CriteriaBuilder builder = manager.getCriteriaBuilder(); return builder.notEqual(propertyPath, argument); } @@ -409,10 +411,10 @@ private static Predicate createNotEqual(Expression propertyPath, Object argum * * @param propertyPath Property path that we want to compare. * @param argument Argument with/without wildcards - * @param manager JPA EntityManager. + * @param manager JPA EntityManagerAdapter. * @return Predicate a predicate representation. */ - private static Predicate createNotLike(Expression propertyPath, String argument, EntityManager manager) { + private static Predicate createNotLike(Expression propertyPath, String argument, EntityManagerAdapter manager) { CriteriaBuilder builder = manager.getCriteriaBuilder(); return builder.not(createLike(propertyPath, argument, manager)); } @@ -421,10 +423,10 @@ private static Predicate createNotLike(Expression propertyPath, String a * Apply an "is not null" constraint to the property path. * * @param propertyPath Property path that we want to compare. - * @param manager JPA EntityManager. + * @param manager JPA EntityManagerAdapter. * @return Predicate a predicate representation. */ - private static Predicate createIsNotNull(Expression propertyPath, EntityManager manager) { + private static Predicate createIsNotNull(Expression propertyPath, EntityManagerAdapter manager) { CriteriaBuilder builder = manager.getCriteriaBuilder(); return builder.isNotNull(propertyPath); } @@ -434,10 +436,10 @@ private static Predicate createIsNotNull(Expression propertyPath, EntityManag * * @param propertyPath Property path that we want to compare. * @param argument Argument number. - * @param manager JPA EntityManager. + * @param manager JPA EntityManagerAdapter. * @return Predicate a predicate representation. */ - private static Predicate createGreaterThan(Expression propertyPath, Number argument, EntityManager manager) { + private static Predicate createGreaterThan(Expression propertyPath, Number argument, EntityManagerAdapter manager) { CriteriaBuilder builder = manager.getCriteriaBuilder(); return builder.gt(propertyPath, argument); } @@ -447,10 +449,10 @@ private static Predicate createGreaterThan(Expression property * * @param propertyPath Property path that we want to compare. * @param argument Argument. - * @param manager JPA EntityManager. + * @param manager JPA EntityManagerAdapter. * @return Predicate a predicate representation. */ - private static > Predicate createGreaterThanComparable(Expression propertyPath, Y argument, EntityManager manager) { + private static > Predicate createGreaterThanComparable(Expression propertyPath, Y argument, EntityManagerAdapter manager) { CriteriaBuilder builder = manager.getCriteriaBuilder(); return builder.greaterThan(propertyPath, argument); } @@ -460,10 +462,10 @@ private static > Predicate createGreaterThanComp * * @param propertyPath Property path that we want to compare. * @param argument Argument number. - * @param manager JPA EntityManager. + * @param manager JPA EntityManagerAdapter. * @return Predicate a predicate representation. */ - private static Predicate createGreaterEqual(Expression propertyPath, Number argument, EntityManager manager) { + private static Predicate createGreaterEqual(Expression propertyPath, Number argument, EntityManagerAdapter manager) { CriteriaBuilder builder = manager.getCriteriaBuilder(); return builder.ge(propertyPath, argument); } @@ -473,10 +475,10 @@ private static Predicate createGreaterEqual(Expression propert * * @param propertyPath Property path that we want to compare. * @param argument Argument. - * @param manager JPA EntityManager. + * @param manager JPA EntityManagerAdapter. * @return Predicate a predicate representation. */ - private static > Predicate createGreaterEqualComparable(Expression propertyPath, Y argument, EntityManager manager) { + private static > Predicate createGreaterEqualComparable(Expression propertyPath, Y argument, EntityManagerAdapter manager) { CriteriaBuilder builder = manager.getCriteriaBuilder(); return builder.greaterThanOrEqualTo(propertyPath, argument); } @@ -486,10 +488,10 @@ private static > Predicate createGreaterEqualCom * * @param propertyPath Property path that we want to compare. * @param argument Argument number. - * @param manager JPA EntityManager. + * @param manager JPA EntityManagerAdapter. * @return Predicate a predicate representation. */ - private static Predicate createLessThan(Expression propertyPath, Number argument, EntityManager manager) { + private static Predicate createLessThan(Expression propertyPath, Number argument, EntityManagerAdapter manager) { CriteriaBuilder builder = manager.getCriteriaBuilder(); return builder.lt(propertyPath, argument); } @@ -499,10 +501,10 @@ private static Predicate createLessThan(Expression propertyPat * * @param propertyPath Property path that we want to compare. * @param argument Argument. - * @param manager JPA EntityManager. + * @param manager JPA EntityManagerAdapter. * @return Predicate a predicate representation. */ - private static > Predicate createLessThanComparable(Expression propertyPath, Y argument, EntityManager manager) { + private static > Predicate createLessThanComparable(Expression propertyPath, Y argument, EntityManagerAdapter manager) { CriteriaBuilder builder = manager.getCriteriaBuilder(); return builder.lessThan(propertyPath, argument); } @@ -512,10 +514,10 @@ private static > Predicate createLessThanCompara * * @param propertyPath Property path that we want to compare. * @param argument Argument number. - * @param manager JPA EntityManager. + * @param manager JPA EntityManagerAdapter. * @return Predicate a predicate representation. */ - private static Predicate createLessEqual(Expression propertyPath, Number argument, EntityManager manager) { + private static Predicate createLessEqual(Expression propertyPath, Number argument, EntityManagerAdapter manager) { CriteriaBuilder builder = manager.getCriteriaBuilder(); return builder.le(propertyPath, argument); } @@ -525,10 +527,10 @@ private static Predicate createLessEqual(Expression propertyPa * * @param propertyPath Property path that we want to compare. * @param argument Argument. - * @param manager JPA EntityManager. + * @param manager JPA EntityManagerAdapter. * @return Predicate a predicate representation. */ - private static > Predicate createLessEqualComparable(Expression propertyPath, Y argument, EntityManager manager) { + private static > Predicate createLessEqualComparable(Expression propertyPath, Y argument, EntityManagerAdapter manager) { CriteriaBuilder builder = manager.getCriteriaBuilder(); return builder.lessThanOrEqualTo(propertyPath, argument); } @@ -538,10 +540,10 @@ private static > Predicate createLessEqualCompar * * @param propertyPath Property path that we want to compare. * @param arguments List of arguments. - * @param manager JPA EntityManager. + * @param manager JPA EntityManagerAdapter. * @return Predicate a predicate representation. */ - private static Predicate createIn(Expression propertyPath, List arguments, EntityManager manager) { + private static Predicate createIn(Expression propertyPath, List arguments, EntityManagerAdapter manager) { return propertyPath.in(arguments); } @@ -550,10 +552,10 @@ private static Predicate createIn(Expression propertyPath, List arguments, * * @param propertyPath Property path that we want to compare. * @param arguments List of arguments. - * @param manager JPA EntityManager. + * @param manager JPA EntityManagerAdapter. * @return Predicate a predicate representation. */ - private static Predicate createNotIn(Expression propertyPath, List arguments, EntityManager manager) { + private static Predicate createNotIn(Expression propertyPath, List arguments, EntityManagerAdapter manager) { CriteriaBuilder builder = manager.getCriteriaBuilder(); return builder.not(createIn(propertyPath,arguments, manager)); } diff --git a/src/main/java/com/github/tennaito/rsql/jpa/PredicateBuilderStrategy.java b/src/main/java/com/github/tennaito/rsql/jpa/PredicateBuilderStrategy.java index 356d99f..544378e 100755 --- a/src/main/java/com/github/tennaito/rsql/jpa/PredicateBuilderStrategy.java +++ b/src/main/java/com/github/tennaito/rsql/jpa/PredicateBuilderStrategy.java @@ -23,11 +23,11 @@ */ package com.github.tennaito.rsql.jpa; -import javax.persistence.EntityManager; import javax.persistence.criteria.From; import javax.persistence.criteria.Predicate; import com.github.tennaito.rsql.builder.BuilderTools; +import com.github.tennaito.rsql.misc.EntityManagerAdapter; import cz.jirutka.rsql.parser.ast.Node; @@ -46,10 +46,10 @@ public interface PredicateBuilderStrategy { * * @param node RSQL AST node. * @param entity The main entity of the query. - * @param manager JPA EntityManager. + * @param manager JPA EntityManagerAdapter. * @param tools Builder tools facade. * @return Predicate a predicate representation of the Node. * @throws IllegalArgumentException When illegal arguments are found. */ - public Predicate createPredicate(Node node, From root, Class entity, EntityManager manager, BuilderTools tools) throws IllegalArgumentException; + public Predicate createPredicate(Node node, From root, Class entity, EntityManagerAdapter manager, BuilderTools tools) throws IllegalArgumentException; } diff --git a/src/main/java/com/github/tennaito/rsql/misc/EntityManagerAdapter.java b/src/main/java/com/github/tennaito/rsql/misc/EntityManagerAdapter.java new file mode 100644 index 0000000..9c3b028 --- /dev/null +++ b/src/main/java/com/github/tennaito/rsql/misc/EntityManagerAdapter.java @@ -0,0 +1,33 @@ +package com.github.tennaito.rsql.misc; + +import java.util.function.Supplier; + +import javax.persistence.EntityManager; +import javax.persistence.criteria.CriteriaBuilder; +import javax.persistence.metamodel.Metamodel; + +public final class EntityManagerAdapter { + + Supplier metamodelSupplier; + + Supplier criteriaBuilderSupplier; + + public EntityManagerAdapter(EntityManager entityManager) { + this(entityManager::getMetamodel, entityManager::getCriteriaBuilder); + } + + public EntityManagerAdapter(Supplier metamodelSupplier, Supplier criteriaBuilderSupplier) { + this.metamodelSupplier = metamodelSupplier; + this.criteriaBuilderSupplier = criteriaBuilderSupplier; + } + + + public Metamodel getMetamodel() { + return metamodelSupplier.get(); + } + + public CriteriaBuilder getCriteriaBuilder() { + return criteriaBuilderSupplier.get(); + } + +} diff --git a/src/test/java/com/github/tennaito/rsql/jpa/JpaVisitorTest.java b/src/test/java/com/github/tennaito/rsql/jpa/JpaVisitorTest.java index c61a0c5..94d79d5 100644 --- a/src/test/java/com/github/tennaito/rsql/jpa/JpaVisitorTest.java +++ b/src/test/java/com/github/tennaito/rsql/jpa/JpaVisitorTest.java @@ -40,7 +40,6 @@ import java.util.List; import java.util.Set; -import javax.persistence.EntityManager; import javax.persistence.criteria.CriteriaQuery; import javax.persistence.criteria.From; import javax.persistence.criteria.Predicate; @@ -51,6 +50,7 @@ import com.github.tennaito.rsql.builder.BuilderTools; import com.github.tennaito.rsql.jpa.entity.Course; +import com.github.tennaito.rsql.misc.EntityManagerAdapter; import com.github.tennaito.rsql.misc.SimpleMapper; import com.github.tennaito.rsql.parser.ast.ComparisonOperatorProxy; @@ -68,7 +68,6 @@ */ public class JpaVisitorTest extends AbstractVisitorTest { - final static XorNode xorNode = new XorNode(new ArrayList()); @Before public void setUp() throws Exception { @@ -80,8 +79,8 @@ public void setUp() throws Exception { public void testUnknowProperty() throws Exception { try { Node rootNode = new RSQLParser().parse("invalid==1"); - RSQLVisitor, EntityManager> visitor = new JpaCriteriaQueryVisitor(); - CriteriaQuery query = rootNode.accept(visitor, entityManager); + RSQLVisitor, EntityManagerAdapter> visitor = new JpaCriteriaQueryVisitor(); + CriteriaQuery query = rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); List courses = entityManager.createQuery(query).getResultList(); fail(); @@ -93,8 +92,8 @@ public void testUnknowProperty() throws Exception { @Test public void testSimpleSelection() throws Exception { Node rootNode = new RSQLParser().parse("id==1"); - RSQLVisitor, EntityManager> visitor = new JpaCriteriaQueryVisitor(); - CriteriaQuery query = rootNode.accept(visitor, entityManager); + RSQLVisitor, EntityManagerAdapter> visitor = new JpaCriteriaQueryVisitor(); + CriteriaQuery query = rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); List courses = entityManager.createQuery(query).getResultList(); assertEquals("Testing Course", courses.get(0).getName()); @@ -104,8 +103,8 @@ public void testSimpleSelection() throws Exception { public void testSimpleSelectionWhenPassingArgumentInTemplate() throws Exception { Node rootNode = new RSQLParser().parse("id==1"); // not a recommended usage - RSQLVisitor, EntityManager> visitor = new JpaCriteriaQueryVisitor(new Course()); - CriteriaQuery query = rootNode.accept(visitor, entityManager); + RSQLVisitor, EntityManagerAdapter> visitor = new JpaCriteriaQueryVisitor(new Course()); + CriteriaQuery query = rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); List courses = entityManager.createQuery(query).getResultList(); assertEquals("Testing Course", courses.get(0).getName()); @@ -115,8 +114,8 @@ public void testSimpleSelectionWhenPassingArgumentInTemplate() throws Exception @Test public void testNotEqualSelection() throws Exception { Node rootNode = new RSQLParser().parse("id!=1"); - RSQLVisitor, EntityManager> visitor = new JpaCriteriaQueryVisitor(); - CriteriaQuery query = rootNode.accept(visitor, entityManager); + RSQLVisitor, EntityManagerAdapter> visitor = new JpaCriteriaQueryVisitor(); + CriteriaQuery query = rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); List courses = entityManager.createQuery(query).getResultList(); assertEquals(0, courses.size()); @@ -125,8 +124,8 @@ public void testNotEqualSelection() throws Exception { @Test public void testGreaterThanSelection() throws Exception { Node rootNode = new RSQLParser().parse("id=gt=1"); - RSQLVisitor, EntityManager> visitor = new JpaCriteriaQueryVisitor(); - CriteriaQuery query = rootNode.accept(visitor, entityManager); + RSQLVisitor, EntityManagerAdapter> visitor = new JpaCriteriaQueryVisitor(); + CriteriaQuery query = rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); List courses = entityManager.createQuery(query).getResultList(); assertEquals(0, courses.size()); @@ -135,8 +134,8 @@ public void testGreaterThanSelection() throws Exception { @Test public void testGreaterThanDate() throws Exception { Node rootNode = new RSQLParser().parse("startDate=gt='2001-01-01'"); - RSQLVisitor, EntityManager> visitor = new JpaCriteriaQueryVisitor(); - CriteriaQuery query = rootNode.accept(visitor, entityManager); + RSQLVisitor, EntityManagerAdapter> visitor = new JpaCriteriaQueryVisitor(); + CriteriaQuery query = rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); List courses = entityManager.createQuery(query).getResultList(); assertEquals(1, courses.size()); @@ -145,8 +144,8 @@ public void testGreaterThanDate() throws Exception { @Test public void testGreaterThanString() throws Exception { Node rootNode = new RSQLParser().parse("code=gt='ABC'"); - RSQLVisitor, EntityManager> visitor = new JpaCriteriaQueryVisitor(); - CriteriaQuery query = rootNode.accept(visitor, entityManager); + RSQLVisitor, EntityManagerAdapter> visitor = new JpaCriteriaQueryVisitor(); + CriteriaQuery query = rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); List courses = entityManager.createQuery(query).getResultList(); assertEquals(1, courses.size()); @@ -156,8 +155,8 @@ public void testGreaterThanString() throws Exception { public void testGreaterThanNotComparable() throws Exception { try { Node rootNode = new RSQLParser().parse("details.teacher=gt='ABC'"); - RSQLVisitor, EntityManager> visitor = new JpaCriteriaQueryVisitor(); - rootNode.accept(visitor, entityManager); + RSQLVisitor, EntityManagerAdapter> visitor = new JpaCriteriaQueryVisitor(); + rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); fail("should have failed since type isn't Comparable"); } catch (IllegalArgumentException e) { assertEquals("Invalid type for comparison operator: =gt= type: com.github.tennaito.rsql.jpa.entity.Teacher must implement Comparable", e.getMessage()); @@ -167,8 +166,8 @@ public void testGreaterThanNotComparable() throws Exception { @Test public void testGreaterThanEqualSelection() throws Exception { Node rootNode = new RSQLParser().parse("id=ge=1"); - RSQLVisitor, EntityManager> visitor = new JpaCriteriaQueryVisitor(); - CriteriaQuery query = rootNode.accept(visitor, entityManager); + RSQLVisitor, EntityManagerAdapter> visitor = new JpaCriteriaQueryVisitor(); + CriteriaQuery query = rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); List courses = entityManager.createQuery(query).getResultList(); assertEquals("Testing Course", courses.get(0).getName()); @@ -177,8 +176,8 @@ public void testGreaterThanEqualSelection() throws Exception { @Test public void testGreaterThanEqualSelectionForDate() throws Exception { Node rootNode = new RSQLParser().parse("startDate=ge='2016-01-01'"); - RSQLVisitor, EntityManager> visitor = new JpaCriteriaQueryVisitor(); - CriteriaQuery query = rootNode.accept(visitor, entityManager); + RSQLVisitor, EntityManagerAdapter> visitor = new JpaCriteriaQueryVisitor(); + CriteriaQuery query = rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); List courses = entityManager.createQuery(query).getResultList(); assertEquals("Testing Course", courses.get(0).getName()); @@ -187,8 +186,8 @@ public void testGreaterThanEqualSelectionForDate() throws Exception { @Test public void testGreaterThanEqualSelectionForString() throws Exception { Node rootNode = new RSQLParser().parse("code=ge='MI-MDW'"); - RSQLVisitor, EntityManager> visitor = new JpaCriteriaQueryVisitor(); - CriteriaQuery query = rootNode.accept(visitor, entityManager); + RSQLVisitor, EntityManagerAdapter> visitor = new JpaCriteriaQueryVisitor(); + CriteriaQuery query = rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); List courses = entityManager.createQuery(query).getResultList(); assertEquals("Testing Course", courses.get(0).getName()); @@ -198,8 +197,8 @@ public void testGreaterThanEqualSelectionForString() throws Exception { public void testGreaterThanEqualNotComparable() throws Exception { try { Node rootNode = new RSQLParser().parse("details.teacher=ge='ABC'"); - RSQLVisitor, EntityManager> visitor = new JpaCriteriaQueryVisitor(); - rootNode.accept(visitor, entityManager); + RSQLVisitor, EntityManagerAdapter> visitor = new JpaCriteriaQueryVisitor(); + rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); fail("should have failed since type isn't Comparable"); } catch (IllegalArgumentException e) { assertEquals("Invalid type for comparison operator: =ge= type: com.github.tennaito.rsql.jpa.entity.Teacher must implement Comparable", e.getMessage()); @@ -209,8 +208,8 @@ public void testGreaterThanEqualNotComparable() throws Exception { @Test public void testLessThanSelection() throws Exception { Node rootNode = new RSQLParser().parse("id=lt=1"); - RSQLVisitor, EntityManager> visitor = new JpaCriteriaQueryVisitor(); - CriteriaQuery query = rootNode.accept(visitor, entityManager); + RSQLVisitor, EntityManagerAdapter> visitor = new JpaCriteriaQueryVisitor(); + CriteriaQuery query = rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); List courses = entityManager.createQuery(query).getResultList(); assertEquals(0, courses.size()); @@ -219,8 +218,8 @@ public void testLessThanSelection() throws Exception { @Test public void testLessThanEqualSelection() throws Exception { Node rootNode = new RSQLParser().parse("id=le=1"); - RSQLVisitor, EntityManager> visitor = new JpaCriteriaQueryVisitor(); - CriteriaQuery query = rootNode.accept(visitor, entityManager); + RSQLVisitor, EntityManagerAdapter> visitor = new JpaCriteriaQueryVisitor(); + CriteriaQuery query = rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); List courses = entityManager.createQuery(query).getResultList(); assertEquals("Testing Course", courses.get(0).getName()); @@ -229,8 +228,8 @@ public void testLessThanEqualSelection() throws Exception { @Test public void testLessThanDate() throws Exception { Node rootNode = new RSQLParser().parse("startDate=lt='2222-02-02'"); - RSQLVisitor, EntityManager> visitor = new JpaCriteriaQueryVisitor(); - CriteriaQuery query = rootNode.accept(visitor, entityManager); + RSQLVisitor, EntityManagerAdapter> visitor = new JpaCriteriaQueryVisitor(); + CriteriaQuery query = rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); List courses = entityManager.createQuery(query).getResultList(); assertEquals(1, courses.size()); @@ -239,8 +238,8 @@ public void testLessThanDate() throws Exception { @Test public void testLessThanString() throws Exception { Node rootNode = new RSQLParser().parse("code=lt='MI-MDZ'"); - RSQLVisitor, EntityManager> visitor = new JpaCriteriaQueryVisitor(); - CriteriaQuery query = rootNode.accept(visitor, entityManager); + RSQLVisitor, EntityManagerAdapter> visitor = new JpaCriteriaQueryVisitor(); + CriteriaQuery query = rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); List courses = entityManager.createQuery(query).getResultList(); assertEquals(1, courses.size()); @@ -250,8 +249,8 @@ public void testLessThanString() throws Exception { public void testLessThanNotComparable() throws Exception { try { Node rootNode = new RSQLParser().parse("details.teacher=lt='ABC'"); - RSQLVisitor, EntityManager> visitor = new JpaCriteriaQueryVisitor(); - rootNode.accept(visitor, entityManager); + RSQLVisitor, EntityManagerAdapter> visitor = new JpaCriteriaQueryVisitor(); + rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); fail("should have failed since type isn't Comparable"); } catch (IllegalArgumentException e) { assertEquals("Invalid type for comparison operator: =lt= type: com.github.tennaito.rsql.jpa.entity.Teacher must implement Comparable", e.getMessage()); @@ -261,8 +260,8 @@ public void testLessThanNotComparable() throws Exception { @Test public void testLessThanEqualSelectionForDate() throws Exception { Node rootNode = new RSQLParser().parse("startDate=le='2100-01-01'"); - RSQLVisitor, EntityManager> visitor = new JpaCriteriaQueryVisitor(); - CriteriaQuery query = rootNode.accept(visitor, entityManager); + RSQLVisitor, EntityManagerAdapter> visitor = new JpaCriteriaQueryVisitor(); + CriteriaQuery query = rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); List courses = entityManager.createQuery(query).getResultList(); assertEquals("Testing Course", courses.get(0).getName()); @@ -271,8 +270,8 @@ public void testLessThanEqualSelectionForDate() throws Exception { @Test public void testLessThanEqualSelectionForString() throws Exception { Node rootNode = new RSQLParser().parse("code=le='MI-MDW'"); - RSQLVisitor, EntityManager> visitor = new JpaCriteriaQueryVisitor(); - CriteriaQuery query = rootNode.accept(visitor, entityManager); + RSQLVisitor, EntityManagerAdapter> visitor = new JpaCriteriaQueryVisitor(); + CriteriaQuery query = rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); List courses = entityManager.createQuery(query).getResultList(); assertEquals("Testing Course", courses.get(0).getName()); @@ -282,8 +281,8 @@ public void testLessThanEqualSelectionForString() throws Exception { public void testLessThanEqualNotComparable() throws Exception { try { Node rootNode = new RSQLParser().parse("details.teacher=le='ABC'"); - RSQLVisitor, EntityManager> visitor = new JpaCriteriaQueryVisitor(); - rootNode.accept(visitor, entityManager); + RSQLVisitor, EntityManagerAdapter> visitor = new JpaCriteriaQueryVisitor(); + rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); fail("should have failed since type isn't Comparable"); } catch (IllegalArgumentException e) { assertEquals("Invalid type for comparison operator: =le= type: com.github.tennaito.rsql.jpa.entity.Teacher must implement Comparable", e.getMessage()); @@ -294,8 +293,8 @@ public void testLessThanEqualNotComparable() throws Exception { @Test public void testInSelection() throws Exception { Node rootNode = new RSQLParser().parse("id=in=(1,2,3,4)"); - RSQLVisitor, EntityManager> visitor = new JpaCriteriaQueryVisitor(); - CriteriaQuery query = rootNode.accept(visitor, entityManager); + RSQLVisitor, EntityManagerAdapter> visitor = new JpaCriteriaQueryVisitor(); + CriteriaQuery query = rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); List courses = entityManager.createQuery(query).getResultList(); assertEquals("Testing Course", courses.get(0).getName()); @@ -304,8 +303,8 @@ public void testInSelection() throws Exception { @Test public void testOutSelection() throws Exception { Node rootNode = new RSQLParser().parse("id=out=(1,2,3,4)"); - RSQLVisitor, EntityManager> visitor = new JpaCriteriaQueryVisitor(); - CriteriaQuery query = rootNode.accept(visitor, entityManager); + RSQLVisitor, EntityManagerAdapter> visitor = new JpaCriteriaQueryVisitor(); + CriteriaQuery query = rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); List courses = entityManager.createQuery(query).getResultList(); assertEquals(0, courses.size()); @@ -314,8 +313,8 @@ public void testOutSelection() throws Exception { @Test public void testLikeSelection() throws Exception { Node rootNode = new RSQLParser().parse("name==*Course"); - RSQLVisitor, EntityManager> visitor = new JpaCriteriaQueryVisitor(); - CriteriaQuery query = rootNode.accept(visitor, entityManager); + RSQLVisitor, EntityManagerAdapter> visitor = new JpaCriteriaQueryVisitor(); + CriteriaQuery query = rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); List courses = entityManager.createQuery(query).getResultList(); assertEquals("Testing Course", courses.get(0).getName()); @@ -324,8 +323,8 @@ public void testLikeSelection() throws Exception { @Test public void testNotLikeSelection() throws Exception { Node rootNode = new RSQLParser().parse("name!=*Course"); - RSQLVisitor, EntityManager> visitor = new JpaCriteriaQueryVisitor(); - CriteriaQuery query = rootNode.accept(visitor, entityManager); + RSQLVisitor, EntityManagerAdapter> visitor = new JpaCriteriaQueryVisitor(); + CriteriaQuery query = rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); List courses = entityManager.createQuery(query).getResultList(); assertEquals(0, courses.size()); @@ -335,8 +334,8 @@ public void testNotLikeSelection() throws Exception { @Test public void testIsNullSelection() throws Exception { Node rootNode = new RSQLParser().parse("name==null"); - RSQLVisitor, EntityManager> visitor = new JpaCriteriaQueryVisitor(); - CriteriaQuery query = rootNode.accept(visitor, entityManager); + RSQLVisitor, EntityManagerAdapter> visitor = new JpaCriteriaQueryVisitor(); + CriteriaQuery query = rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); List courses = entityManager.createQuery(query).getResultList(); assertEquals(0, courses.size()); @@ -345,8 +344,8 @@ public void testIsNullSelection() throws Exception { @Test public void testNotIsNullSelection() throws Exception { Node rootNode = new RSQLParser().parse("name!=null"); - RSQLVisitor, EntityManager> visitor = new JpaCriteriaQueryVisitor(); - CriteriaQuery query = rootNode.accept(visitor, entityManager); + RSQLVisitor, EntityManagerAdapter> visitor = new JpaCriteriaQueryVisitor(); + CriteriaQuery query = rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); List courses = entityManager.createQuery(query).getResultList(); assertEquals("Testing Course", courses.get(0).getName()); @@ -355,9 +354,9 @@ public void testNotIsNullSelection() throws Exception { @Test public void testSetEntity() { Node rootNode = new RSQLParser().parse("id==1"); - RSQLVisitor, EntityManager> visitor = new JpaCriteriaQueryVisitor(); + RSQLVisitor, EntityManagerAdapter> visitor = new JpaCriteriaQueryVisitor(); ((JpaCriteriaQueryVisitor)visitor).setEntityClass(Course.class); - CriteriaQuery query = rootNode.accept(visitor, entityManager); + CriteriaQuery query = rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); List courses = (List)entityManager.createQuery(query).getResultList(); assertEquals(1, courses.size()); } @@ -369,8 +368,8 @@ public void testUndefinedComparisonOperator() { Set set = new HashSet(); set.add(newOp); Node rootNode = new RSQLParser(set).parse("id=def=null"); - RSQLVisitor, EntityManager> visitor = new JpaCriteriaQueryVisitor(); - CriteriaQuery query = rootNode.accept(visitor, entityManager); + RSQLVisitor, EntityManagerAdapter> visitor = new JpaCriteriaQueryVisitor(); + CriteriaQuery query = rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); List courses = entityManager.createQuery(query).getResultList(); fail(); } catch(Exception e) { @@ -390,7 +389,7 @@ public void testDefinedComparisonOperator() { JpaCriteriaQueryVisitor visitor = new JpaCriteriaQueryVisitor(); createDefOperator(visitor); - CriteriaQuery query = rootNode.accept(visitor, entityManager); + CriteriaQuery query = rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); List courses = entityManager.createQuery(query).getResultList(); assertEquals("Testing Course", courses.get(0).getName()); } @@ -399,7 +398,7 @@ private void createDefOperator(JpaCriteriaQueryVisitor visitor) { // define new operator resolver PredicateBuilderStrategy predicateStrategy = new PredicateBuilderStrategy() { public Predicate createPredicate(Node node, From root, Class entity, - EntityManager manager, BuilderTools tools) + EntityManagerAdapter manager, BuilderTools tools) throws IllegalArgumentException { ComparisonNode comp = ((ComparisonNode)node); ComparisonNode def = new ComparisonNode(ComparisonOperatorProxy.EQUAL.getOperator(), comp.getSelector(), comp.getArguments()); @@ -412,8 +411,8 @@ public Predicate createPredicate(Node node, From root, Class entity, @Test public void testAssociationSelection() throws Exception { Node rootNode = new RSQLParser().parse("department.id==1"); - RSQLVisitor, EntityManager> visitor = new JpaCriteriaQueryVisitor(); - CriteriaQuery query = rootNode.accept(visitor, entityManager); + RSQLVisitor, EntityManagerAdapter> visitor = new JpaCriteriaQueryVisitor(); + CriteriaQuery query = rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); List courses = entityManager.createQuery(query).getResultList(); assertEquals("Testing Course", courses.get(0).getName()); @@ -428,7 +427,7 @@ public void testAssociationAliasSelection() throws Exception { ((SimpleMapper)visitor.getBuilderTools().getPropertiesMapper()).addMapping(Course.class, new HashMap()); ((SimpleMapper)visitor.getBuilderTools().getPropertiesMapper()).addMapping(Course.class, "dept", "department"); - CriteriaQuery query = rootNode.accept(visitor, entityManager); + CriteriaQuery query = rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); List courses = entityManager.createQuery(query).getResultList(); assertEquals("Testing Course", courses.get(0).getName()); @@ -445,7 +444,7 @@ public void testAssociationAliasSelectionWithAssociationAlias() throws Exception ((SimpleMapper)visitor.getBuilderTools().getPropertiesMapper()).addMapping(Course.class, new HashMap()); ((SimpleMapper)visitor.getBuilderTools().getPropertiesMapper()).addMapping(Course.class, "dept_id", "department.id"); - CriteriaQuery query = rootNode.accept(visitor, entityManager); + CriteriaQuery query = rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); List courses = entityManager.createQuery(query).getResultList(); assertEquals("Testing Course", courses.get(0).getName()); @@ -456,8 +455,8 @@ public void testAssociationAliasSelectionWithAssociationAlias() throws Exception @Test public void testAndSelection() throws Exception { Node rootNode = new RSQLParser().parse("department.id==1;id==2"); - RSQLVisitor, EntityManager> visitor = new JpaCriteriaQueryVisitor(); - CriteriaQuery query = rootNode.accept(visitor, entityManager); + RSQLVisitor, EntityManagerAdapter> visitor = new JpaCriteriaQueryVisitor(); + CriteriaQuery query = rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); List courses = entityManager.createQuery(query).getResultList(); assertEquals(0, courses.size()); @@ -466,8 +465,8 @@ public void testAndSelection() throws Exception { @Test public void testBasicSelectionCount() throws Exception { Node rootNode = new RSQLParser().parse("department.id==1"); - RSQLVisitor, EntityManager> visitor = new JpaCriteriaCountQueryVisitor(); - CriteriaQuery query = rootNode.accept(visitor, entityManager); + RSQLVisitor, EntityManagerAdapter> visitor = new JpaCriteriaCountQueryVisitor(); + CriteriaQuery query = rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); Long courseCount = entityManager.createQuery(query).getSingleResult(); assertEquals((Long)1l, courseCount); @@ -479,8 +478,8 @@ public void testBasicSelectionCount() throws Exception { @Test public void testAndSelectionCount() throws Exception { Node rootNode = new RSQLParser().parse("department.id==1;id==2"); - RSQLVisitor, EntityManager> visitor = new JpaCriteriaCountQueryVisitor(); - CriteriaQuery query = rootNode.accept(visitor, entityManager); + RSQLVisitor, EntityManagerAdapter> visitor = new JpaCriteriaCountQueryVisitor(); + CriteriaQuery query = rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); Long courseCount = entityManager.createQuery(query).getSingleResult(); assertEquals((Long)0l, courseCount); @@ -489,8 +488,8 @@ public void testAndSelectionCount() throws Exception { @Test public void testOrSelection() throws Exception { Node rootNode = new RSQLParser().parse("department.id==1,id==2"); - RSQLVisitor, EntityManager> visitor = new JpaCriteriaQueryVisitor(); - CriteriaQuery query = rootNode.accept(visitor, entityManager); + RSQLVisitor, EntityManagerAdapter> visitor = new JpaCriteriaQueryVisitor(); + CriteriaQuery query = rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); List courses = entityManager.createQuery(query).getResultList(); assertEquals("Testing Course", courses.get(0).getName()); @@ -499,8 +498,8 @@ public void testOrSelection() throws Exception { @Test public void testOrSelectionCount() throws Exception { Node rootNode = new RSQLParser().parse("department.id==1,id==2"); - RSQLVisitor, EntityManager> visitor = new JpaCriteriaCountQueryVisitor(); - CriteriaQuery query = rootNode.accept(visitor, entityManager); + RSQLVisitor, EntityManagerAdapter> visitor = new JpaCriteriaCountQueryVisitor(); + CriteriaQuery query = rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); Long courseCount = entityManager.createQuery(query).getSingleResult(); assertEquals((Long)1l, courseCount); @@ -509,8 +508,8 @@ public void testOrSelectionCount() throws Exception { @Test public void testVariousNodesSelection() throws Exception { Node rootNode = new RSQLParser().parse("((department.id==1;id==2),id<3);department.id=out=(3,4,5)"); - RSQLVisitor, EntityManager> visitor = new JpaCriteriaQueryVisitor(); - CriteriaQuery query = rootNode.accept(visitor, entityManager); + RSQLVisitor, EntityManagerAdapter> visitor = new JpaCriteriaQueryVisitor(); + CriteriaQuery query = rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); List courses = entityManager.createQuery(query).getResultList(); assertEquals("Testing Course", courses.get(0).getName()); @@ -519,8 +518,8 @@ public void testVariousNodesSelection() throws Exception { @Test public void testNavigateThroughCollectionSelection() throws Exception { Node rootNode = new RSQLParser().parse("department.head.titles.name==Phd"); - RSQLVisitor, EntityManager> visitor = new JpaCriteriaQueryVisitor(); - CriteriaQuery query = rootNode.accept(visitor, entityManager); + RSQLVisitor, EntityManagerAdapter> visitor = new JpaCriteriaQueryVisitor(); + CriteriaQuery query = rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); List courses = entityManager.createQuery(query).getResultList(); assertEquals("Testing Course", courses.get(0).getName()); @@ -552,16 +551,6 @@ public void testSetBuilderTools() throws Exception { assertNull(visitor.getBuilderTools().getPredicateBuilder()); } - @Test - public void testUnsupportedLogicalNode() throws Exception { - try{ - PredicateBuilder.createPredicate(JpaVisitorTest.xorNode, null, Course.class, entityManager, null); - fail(); - } catch (IllegalArgumentException e) { - assertEquals("Unknown operator: ^", e.getMessage()); - } - } - @Test public void testPrivateConstructor() throws Exception { Constructor priv = PredicateBuilder.class.getDeclaredConstructor(); @@ -582,71 +571,12 @@ public R accept(RSQLVisitor visitor, A param) { } } - protected static class XorNode extends LogicalNode { - - final static LogicalOperator XOR = createLogicalOperatorXor(); - - public XorNode(List children) { - super(XOR, children); - } - - public static void setStaticFinalField(Field field, Object value) throws NoSuchFieldException, IllegalAccessException { - // we mark the field to be public - field.setAccessible(true); - // next we change the modifier in the Field instance to - // not be final anymore, thus tricking reflection into - // letting us modify the static final field - Field modifiersField = Field.class.getDeclaredField("modifiers"); - modifiersField.setAccessible(true); - int modifiers = modifiersField.getInt(field); - // blank out the final bit in the modifiers int - modifiers &= ~Modifier.FINAL; - modifiersField.setInt(field, modifiers); - sun.reflect.FieldAccessor fa = sun.reflect.ReflectionFactory.getReflectionFactory().newFieldAccessor(field, false); - fa.set(null, value); - } - - private static LogicalOperator createLogicalOperatorXor() { - LogicalOperator xor = null; - try { - Constructor cstr = LogicalOperator.class.getDeclaredConstructor(String.class, int.class, String.class); - sun.reflect.ReflectionFactory factory = sun.reflect.ReflectionFactory.getReflectionFactory(); - xor = (LogicalOperator) factory.newConstructorAccessor(cstr).newInstance(new Object[]{"XOR", 2, "^"}); - - Field ordinalField = Enum.class.getDeclaredField("ordinal"); - ordinalField.setAccessible(true); - - LogicalOperator[] values = xor.values(); - Field valuesField = LogicalOperator.class.getDeclaredField("ENUM$VALUES"); - valuesField.setAccessible(true); - LogicalOperator[] newValues = Arrays.copyOf(values, values.length + 1); - newValues[newValues.length - 1] = xor; - setStaticFinalField(valuesField, newValues); - int ordinal = newValues.length - 1; - ordinalField.set(xor, ordinal); - } catch (ReflectiveOperationException e) { - // do nothing - e.printStackTrace(); - } - return xor; - } - - @Override - public LogicalNode withChildren(List children) { - return new XorNode(children); - } - - public R accept(RSQLVisitor visitor, A param) { - throw new UnsupportedOperationException(); - } - } - @Test public void testUndefinedRootForPredicate() throws Exception { try { Node rootNode = new RSQLParser().parse("id==1"); - RSQLVisitor visitor = new JpaPredicateVisitor(); - Predicate query = rootNode.accept(visitor, entityManager); + RSQLVisitor visitor = new JpaPredicateVisitor(); + Predicate query = rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); } catch (IllegalArgumentException e) { assertEquals("From root node was undefined.", e.getMessage()); } @@ -655,8 +585,8 @@ public void testUndefinedRootForPredicate() throws Exception { @Test public void testSelectionUsingEmbeddedField() throws Exception { Node rootNode = new RSQLParser().parse("details.description==test"); - RSQLVisitor, EntityManager> visitor = new JpaCriteriaQueryVisitor(); - CriteriaQuery query = rootNode.accept(visitor, entityManager); + RSQLVisitor, EntityManagerAdapter> visitor = new JpaCriteriaQueryVisitor(); + CriteriaQuery query = rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); List courses = entityManager.createQuery(query).getResultList(); assertEquals("Testing Course", courses.get(0).getName()); @@ -665,8 +595,8 @@ public void testSelectionUsingEmbeddedField() throws Exception { @Test public void testSelectionUsingEmbeddedAssociationField() throws Exception { Node rootNode = new RSQLParser().parse("details.teacher.specialtyDescription==Maths"); - RSQLVisitor, EntityManager> visitor = new JpaCriteriaQueryVisitor(); - CriteriaQuery query = rootNode.accept(visitor, entityManager); + RSQLVisitor, EntityManagerAdapter> visitor = new JpaCriteriaQueryVisitor(); + CriteriaQuery query = rootNode.accept(visitor, new EntityManagerAdapter(entityManager)); List courses = entityManager.createQuery(query).getResultList(); assertEquals("Testing Course", courses.get(0).getName());