deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From daniels...@apache.org
Subject deltaspike git commit: DELTASPIKE-934 @Query metadata should be considered for any method expression
Date Tue, 16 Jun 2015 18:28:39 GMT
Repository: deltaspike
Updated Branches:
  refs/heads/master bcaee613d -> 687e57e89


DELTASPIKE-934 @Query metadata should be considered for any method expression


Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo
Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/687e57e8
Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/687e57e8
Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/687e57e8

Branch: refs/heads/master
Commit: 687e57e8962b13d33d428e05ef5f890317352e10
Parents: bcaee61
Author: Daniel Cunha (soro) <danielsoro@apache.org>
Authored: Thu Jun 11 23:15:05 2015 -0300
Committer: Daniel Cunha (soro) <danielsoro@apache.org>
Committed: Tue Jun 16 15:28:04 2015 -0300

----------------------------------------------------------------------
 .../data/spi/QueryInvocationContext.java        |  9 +--
 .../impl/builder/AnnotatedQueryBuilder.java     | 13 ++--
 .../data/impl/builder/MethodQueryBuilder.java   |  2 +-
 .../data/impl/builder/QueryBuilder.java         | 74 ++----------------
 .../impl/handler/CdiQueryInvocationContext.java | 79 +++++++++++++++++---
 .../impl/handler/EntityRepositoryHandler.java   | 12 +--
 6 files changed, 93 insertions(+), 96 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/687e57e8/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/spi/QueryInvocationContext.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/spi/QueryInvocationContext.java
b/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/spi/QueryInvocationContext.java
index c533ba9..daab9d8 100644
--- a/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/spi/QueryInvocationContext.java
+++ b/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/spi/QueryInvocationContext.java
@@ -18,9 +18,8 @@
  */
 package org.apache.deltaspike.data.spi;
 
-import java.lang.reflect.Method;
-
 import javax.persistence.EntityManager;
+import java.lang.reflect.Method;
 
 /**
  * Expose the current query invocation to extensions.
@@ -41,8 +40,9 @@ public interface QueryInvocationContext
     /**
      * Given the object parameter is an entity, checks if the entity is
      * persisted or not.
-     * @param entity            Entity object, non nullable.
-     * @return                  true if the entity is not persisted, false otherwise and
if no entity.
+     *
+     * @param entity Entity object, non nullable.
+     * @return true if the entity is not persisted, false otherwise and if no entity.
      */
     boolean isNew(Object entity);
 
@@ -55,5 +55,4 @@ public interface QueryInvocationContext
      * The repository method currently executed.
      */
     Method getMethod();
-
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/687e57e8/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/AnnotatedQueryBuilder.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/AnnotatedQueryBuilder.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/AnnotatedQueryBuilder.java
index b44d369..91c4035 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/AnnotatedQueryBuilder.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/AnnotatedQueryBuilder.java
@@ -18,12 +18,6 @@
  */
 package org.apache.deltaspike.data.impl.builder;
 
-import static org.apache.deltaspike.data.impl.util.QueryUtils.isNotEmpty;
-
-import java.lang.reflect.Method;
-
-import javax.persistence.EntityManager;
-
 import org.apache.deltaspike.data.api.Query;
 import org.apache.deltaspike.data.impl.handler.CdiQueryInvocationContext;
 import org.apache.deltaspike.data.impl.meta.MethodType;
@@ -31,6 +25,11 @@ import org.apache.deltaspike.data.impl.meta.QueryInvocation;
 import org.apache.deltaspike.data.impl.param.Parameters;
 import org.apache.deltaspike.data.impl.util.jpa.QueryStringExtractorFactory;
 
+import javax.persistence.EntityManager;
+import java.lang.reflect.Method;
+
+import static org.apache.deltaspike.data.impl.util.QueryUtils.isNotEmpty;
+
 /**
  * Create the query based on method annotations.
  */
@@ -79,7 +78,7 @@ public class AnnotatedQueryBuilder extends QueryBuilder
             context.setQueryString(jpqlQuery);
             result = params.applyTo(entityManager.createQuery(jpqlQuery));
         }
-        return applyRestrictions(context, result);
+        return context.applyRestrictions(result);
     }
 
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/687e57e8/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/MethodQueryBuilder.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/MethodQueryBuilder.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/MethodQueryBuilder.java
index e7ba319..4aa8b30 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/MethodQueryBuilder.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/MethodQueryBuilder.java
@@ -45,7 +45,7 @@ public class MethodQueryBuilder extends QueryBuilder
         context.setQueryString(jpqlQuery);
         params.updateValues(root.getParameterUpdates());
         Query result = params.applyTo(context.getEntityManager().createQuery(jpqlQuery));
-        return applyRestrictions(context, result);
+        return context.applyRestrictions(result);
     }
 
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/687e57e8/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/QueryBuilder.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/QueryBuilder.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/QueryBuilder.java
index 02d7796..8dc97ff 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/QueryBuilder.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/builder/QueryBuilder.java
@@ -18,18 +18,14 @@
  */
 package org.apache.deltaspike.data.impl.builder;
 
-import java.lang.reflect.Method;
-import java.text.MessageFormat;
-import java.util.List;
-
-import javax.persistence.LockModeType;
-import javax.persistence.Query;
-import javax.persistence.QueryHint;
-
 import org.apache.deltaspike.data.api.QueryResult;
 import org.apache.deltaspike.data.api.mapping.QueryInOutMapper;
 import org.apache.deltaspike.data.impl.handler.CdiQueryInvocationContext;
-import org.apache.deltaspike.data.impl.param.Parameters;
+
+import javax.persistence.Query;
+import java.lang.reflect.Method;
+import java.text.MessageFormat;
+import java.util.List;
 
 /**
  * Query builder factory. Delegates to concrete implementations.
@@ -81,66 +77,6 @@ public abstract class QueryBuilder
         return method.getReturnType().isAssignableFrom(List.class);
     }
 
-    protected LockModeType extractLockMode(Method method)
-    {
-        Class<org.apache.deltaspike.data.api.Query> query = org.apache.deltaspike.data.api.Query.class;
-        if (method.isAnnotationPresent(query) &&
-                method.getAnnotation(query).lock() != LockModeType.NONE)
-        {
-            return method.getAnnotation(query).lock();
-        }
-        return null;
-    }
-
-    protected boolean hasLockMode(Method method)
-    {
-        return extractLockMode(method) != null;
-    }
-
-    protected QueryHint[] extractQueryHints(Method method)
-    {
-        Class<org.apache.deltaspike.data.api.Query> query = org.apache.deltaspike.data.api.Query.class;
-        if (method.isAnnotationPresent(query) &&
-                method.getAnnotation(query).hints().length > 0)
-        {
-            return method.getAnnotation(query).hints();
-        }
-        return null;
-    }
-
-    protected boolean hasQueryHints(Method method)
-    {
-        return extractQueryHints(method) != null;
-    }
-
-    protected Query applyRestrictions(CdiQueryInvocationContext context, Query query)
-    {
-        Parameters params = context.getParams();
-        Method method = context.getMethod();
-        if (params.hasSizeRestriction())
-        {
-            query.setMaxResults(params.getSizeRestriciton());
-        }
-        if (params.hasFirstResult())
-        {
-            query.setFirstResult(params.getFirstResult());
-        }
-        if (hasLockMode(method))
-        {
-            query.setLockMode(extractLockMode(method));
-        }
-        if (hasQueryHints(method))
-        {
-            QueryHint[] hints = extractQueryHints(method);
-            for (QueryHint hint : hints)
-            {
-                query.setHint(hint.name(), hint.value());
-            }
-        }
-        query = context.applyJpaQueryPostProcessors(query);
-        return query;
-    }
-
     private boolean isUnmappableResult(Object result)
     {
         return result instanceof QueryResult ||

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/687e57e8/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/CdiQueryInvocationContext.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/CdiQueryInvocationContext.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/CdiQueryInvocationContext.java
index 5b6b892..da0bf05 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/CdiQueryInvocationContext.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/CdiQueryInvocationContext.java
@@ -18,13 +18,6 @@
  */
 package org.apache.deltaspike.data.impl.handler;
 
-import java.lang.reflect.Method;
-import java.util.LinkedList;
-import java.util.List;
-
-import javax.persistence.EntityManager;
-import javax.persistence.Query;
-
 import org.apache.deltaspike.data.api.SingleResultType;
 import org.apache.deltaspike.data.api.mapping.QueryInOutMapper;
 import org.apache.deltaspike.data.impl.meta.RepositoryMethod;
@@ -32,6 +25,14 @@ import org.apache.deltaspike.data.impl.param.Parameters;
 import org.apache.deltaspike.data.impl.util.bean.Destroyable;
 import org.apache.deltaspike.data.spi.QueryInvocationContext;
 
+import javax.persistence.EntityManager;
+import javax.persistence.LockModeType;
+import javax.persistence.Query;
+import javax.persistence.QueryHint;
+import java.lang.reflect.Method;
+import java.util.LinkedList;
+import java.util.List;
+
 public class CdiQueryInvocationContext implements QueryInvocationContext
 {
 
@@ -49,10 +50,10 @@ public class CdiQueryInvocationContext implements QueryInvocationContext
     private String queryString;
 
     public CdiQueryInvocationContext(Object proxy, Method method, Object[] args, RepositoryMethod
repoMethod,
-            EntityManager entityManager)
+                                     EntityManager entityManager)
     {
         this.entityManager = entityManager;
-        this.args = args == null ? new Object[] {} : args;
+        this.args = args == null ? new Object[]{} : args;
         this.params = Parameters.create(method, this.args);
         this.proxy = proxy;
         this.method = method;
@@ -122,6 +123,34 @@ public class CdiQueryInvocationContext implements QueryInvocationContext
         return method;
     }
 
+    public Query applyRestrictions(Query query)
+    {
+        Parameters params = getParams();
+        Method method = getMethod();
+        if (params.hasSizeRestriction())
+        {
+            query.setMaxResults(params.getSizeRestriciton());
+        }
+        if (params.hasFirstResult())
+        {
+            query.setFirstResult(params.getFirstResult());
+        }
+        if (hasLockMode(method))
+        {
+            query.setLockMode(extractLockMode(method));
+        }
+        if (hasQueryHints(method))
+        {
+            QueryHint[] hints = extractQueryHints(method);
+            for (QueryHint hint : hints)
+            {
+                query.setHint(hint.name(), hint.value());
+            }
+        }
+        query = applyJpaQueryPostProcessors(query);
+        return query;
+    }
+
     public Object[] getMethodParameters()
     {
         return args;
@@ -231,4 +260,36 @@ public class CdiQueryInvocationContext implements QueryInvocationContext
         return proxy;
     }
 
+    private boolean hasLockMode(Method method)
+    {
+        return extractLockMode(method) != null;
+    }
+
+    private LockModeType extractLockMode(Method method)
+    {
+        Class<org.apache.deltaspike.data.api.Query> query = org.apache.deltaspike.data.api.Query.class;
+        if (method.isAnnotationPresent(query) &&
+                method.getAnnotation(query).lock() != LockModeType.NONE)
+        {
+            return method.getAnnotation(query).lock();
+        }
+        return null;
+    }
+
+    private QueryHint[] extractQueryHints(Method method)
+    {
+        Class<org.apache.deltaspike.data.api.Query> query = org.apache.deltaspike.data.api.Query.class;
+        if (method.isAnnotationPresent(query) &&
+                method.getAnnotation(query).hints().length > 0)
+        {
+            return method.getAnnotation(query).hints();
+        }
+        return null;
+    }
+
+    private boolean hasQueryHints(Method method)
+    {
+        return extractQueryHints(method) != null;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/687e57e8/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/EntityRepositoryHandler.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/EntityRepositoryHandler.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/EntityRepositoryHandler.java
index aa7fa66..2082e95 100755
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/EntityRepositoryHandler.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/EntityRepositoryHandler.java
@@ -28,7 +28,6 @@ import org.apache.deltaspike.data.impl.property.query.NamedPropertyCriteria;
 import org.apache.deltaspike.data.impl.property.query.PropertyQueries;
 import org.apache.deltaspike.data.impl.util.EntityUtils;
 import org.apache.deltaspike.data.spi.DelegateQueryHandler;
-import org.apache.deltaspike.data.spi.QueryInvocationContext;
 
 import javax.inject.Inject;
 import javax.persistence.EntityManager;
@@ -60,7 +59,7 @@ public class EntityRepositoryHandler<E, PK extends Serializable>
     private static final Logger log = Logger.getLogger(EntityRepositoryHandler.class.getName());
 
     @Inject
-    private QueryInvocationContext context;
+    private CdiQueryInvocationContext context;
 
     @Override
     @RequiresTransaction
@@ -132,7 +131,7 @@ public class EntityRepositoryHandler<E, PK extends Serializable>
     @Override
     public List<E> findAll()
     {
-        return entityManager().createQuery(allQuery(), entityClass()).getResultList();
+        return context.applyRestrictions(entityManager().createQuery(allQuery(), entityClass())).getResultList();
     }
 
     @Override
@@ -147,13 +146,14 @@ public class EntityRepositoryHandler<E, PK extends Serializable>
         {
             query.setMaxResults(max);
         }
-        return query.getResultList();
+        return context.applyRestrictions(query).getResultList();
     }
 
     @Override
     public Long count()
     {
-        return entityManager().createQuery(countQuery(), Long.class).getSingleResult();
+        return (Long) context.applyRestrictions(entityManager().createQuery(countQuery(),
Long.class))
+                .getSingleResult();
     }
 
     @Override
@@ -352,6 +352,7 @@ public class EntityRepositoryHandler<E, PK extends Serializable>
             query.setMaxResults(max);
         }
 
+        context.applyRestrictions(query);
         addParameters(query, example, properties, useLikeOperator);
         return query.getResultList();
     }
@@ -367,6 +368,7 @@ public class EntityRepositoryHandler<E, PK extends Serializable>
         log.log(Level.FINER, "count: Created query {0}", jpqlQuery);
         TypedQuery<Long> query = entityManager().createQuery(jpqlQuery, Long.class);
         addParameters(query, example, properties, useLikeOperator);
+        context.applyRestrictions(query);
         return query.getSingleResult();
     }
 }


Mime
View raw message