deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From t...@apache.org
Subject [2/2] git commit: DELTASPIKE-639 Count method signature fix
Date Thu, 12 Jun 2014 18:26:51 GMT
DELTASPIKE-639 Count method signature fix


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

Branch: refs/heads/master
Commit: 11af2fa08063970c31338e05970f2cc2e88c7b64
Parents: 2af2ccf
Author: Thomas Hug <Thomas.Hug@ctp.com>
Authored: Thu Jun 12 11:15:50 2014 -0700
Committer: Thomas Hug <Thomas.Hug@ctp.com>
Committed: Thu Jun 12 11:23:42 2014 -0700

----------------------------------------------------------------------
 .../data/api/criteria/CriteriaSupport.java      | 13 +++++-
 .../data/impl/criteria/QueryCriteria.java       | 20 +++++++--
 .../impl/criteria/selection/numeric/Count.java  | 15 +++----
 .../selection/numeric/CountDistinct.java        | 44 ++++++++++++++++++++
 .../impl/handler/CriteriaSupportHandler.java    | 11 ++++-
 .../data/impl/criteria/CriteriaTest.java        | 16 +++++++
 .../test/service/SimpleCriteriaRepository.java  |  8 ++++
 7 files changed, 112 insertions(+), 15 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/11af2fa0/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/criteria/CriteriaSupport.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/criteria/CriteriaSupport.java
b/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/criteria/CriteriaSupport.java
index ae28097..2dd386d 100644
--- a/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/criteria/CriteriaSupport.java
+++ b/deltaspike/modules/data/api/src/main/java/org/apache/deltaspike/data/api/criteria/CriteriaSupport.java
@@ -27,7 +27,7 @@ import javax.persistence.metamodel.SingularAttribute;
 
 /**
  * Interface to be added to a repository for criteria support.
- * 
+ *
  * @param <E>   Entity type.
  */
 public interface CriteriaSupport<E>
@@ -88,7 +88,16 @@ public interface CriteriaSupport<E>
      * @param attribute Attribute to use in the aggregate.
      * @return          {@link QuerySelection} part of a {@link Criteria#select(Class, QuerySelection...)}
call.
      */
-    <N extends Number> QuerySelection<E, N> count(SingularAttribute<E, N>
attribute);
+    QuerySelection<E, Long> count(SingularAttribute<E, ?> attribute);
+
+    /**
+     * Create a query selection for the
+     * {@link javax.persistence.criteria.CriteriaBuilder#countDistinct(javax.persistence.criteria.Expression)}
+     * over an attribute.
+     * @param attribute Attribute to use in the aggregate.
+     * @return          {@link QuerySelection} part of a {@link Criteria#select(Class, QuerySelection...)}
call.
+     */
+    QuerySelection<E, Long> countDistinct(SingularAttribute<E, ?> attribute);
 
     /**
      * Create a query selection for the

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/11af2fa0/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/QueryCriteria.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/QueryCriteria.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/QueryCriteria.java
index 0c054e3..6c0aa11 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/QueryCriteria.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/QueryCriteria.java
@@ -138,7 +138,7 @@ public class QueryCriteria<C, R> implements Criteria<C, R>
         try
         {
             CriteriaBuilder builder = entityManager.getCriteriaBuilder();
-            CriteriaQuery<R> query = builder.createQuery(resultClass);
+            CriteriaQuery<?> query = createCriteriaQuery(builder);
             From<C, C> root = query.from(entityClass);
             if (!selections.isEmpty())
             {
@@ -151,7 +151,7 @@ public class QueryCriteria<C, R> implements Criteria<C, R>
                 query.where(predicates.toArray(new Predicate[predicates.size()]));
             }
             applyProcessors(query, builder, root);
-            return entityManager.createQuery(query);
+            return (TypedQuery<R>) entityManager.createQuery(query);
         }
         catch (RuntimeException e)
         {
@@ -290,7 +290,7 @@ public class QueryCriteria<C, R> implements Criteria<C, R>
     // Package criteria methods
     // --------------------------------------------------------------------
 
-    void applyProcessors(CriteriaQuery<R> query, CriteriaBuilder builder, From<C,
C> from)
+    void applyProcessors(CriteriaQuery<?> query, CriteriaBuilder builder, From<C,
C> from)
     {
         for (QueryProcessor<C> proc : processors)
         {
@@ -333,7 +333,7 @@ public class QueryCriteria<C, R> implements Criteria<C, R>
         processors.add(proc);
     }
 
-    private Selection<?>[] prepareSelections(CriteriaQuery<R> query, CriteriaBuilder
builder, From<C, C> root)
+    private Selection<?>[] prepareSelections(CriteriaQuery<?> query, CriteriaBuilder
builder, From<C, C> root)
     {
         List<Selection<?>> result = new ArrayList<Selection<?>>(selections.size());
         for (QuerySelection<? super C, ?> selection : selections)
@@ -343,6 +343,18 @@ public class QueryCriteria<C, R> implements Criteria<C, R>
         return result.toArray(new Selection<?>[] {});
     }
 
+    private CriteriaQuery<?> createCriteriaQuery(CriteriaBuilder builder)
+    {
+        if (resultClass.getName().startsWith("java.lang"))
+        {
+            return builder.createQuery();
+        }
+        else
+        {
+            return builder.createQuery(resultClass);
+        }
+    }
+
     // --------------------------------------------------------------------
     // Predicates
     // --------------------------------------------------------------------

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/11af2fa0/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/selection/numeric/Count.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/selection/numeric/Count.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/selection/numeric/Count.java
index 8a2c2fb..7bb3688 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/selection/numeric/Count.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/selection/numeric/Count.java
@@ -24,20 +24,21 @@ import javax.persistence.criteria.Path;
 import javax.persistence.criteria.Selection;
 import javax.persistence.metamodel.SingularAttribute;
 
-import org.apache.deltaspike.data.impl.criteria.selection.SingularAttributeSelection;
+import org.apache.deltaspike.data.api.criteria.QuerySelection;
 
-public class Count<P, N extends Number> extends SingularAttributeSelection<P, N>
+public class Count<P> implements QuerySelection<P, Long>
 {
 
-    public Count(SingularAttribute<P, N> attribute)
+    private final SingularAttribute<P, ?> attribute;
+
+    public Count(SingularAttribute<P, ?> attribute)
     {
-        super(attribute);
+        this.attribute = attribute;
     }
 
     @Override
-    @SuppressWarnings("unchecked")
-    public <R> Selection<N> toSelection(CriteriaQuery<R> query, CriteriaBuilder
builder, Path<? extends P> path)
+    public <R> Selection<Long> toSelection(CriteriaQuery<R> query, CriteriaBuilder
builder, Path<? extends P> path)
     {
-        return (Selection<N>) builder.count(path.get(attribute));
+        return builder.count(path.get(attribute));
     }
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/11af2fa0/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/selection/numeric/CountDistinct.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/selection/numeric/CountDistinct.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/selection/numeric/CountDistinct.java
new file mode 100644
index 0000000..024279b
--- /dev/null
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/selection/numeric/CountDistinct.java
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.deltaspike.data.impl.criteria.selection.numeric;
+
+import javax.persistence.criteria.CriteriaBuilder;
+import javax.persistence.criteria.CriteriaQuery;
+import javax.persistence.criteria.Path;
+import javax.persistence.criteria.Selection;
+import javax.persistence.metamodel.SingularAttribute;
+
+import org.apache.deltaspike.data.api.criteria.QuerySelection;
+
+public class CountDistinct<P> implements QuerySelection<P, Long>
+{
+
+    private final SingularAttribute<P, ?> attribute;
+
+    public CountDistinct(SingularAttribute<P, ?> attribute)
+    {
+        this.attribute = attribute;
+    }
+
+    @Override
+    public <R> Selection<Long> toSelection(CriteriaQuery<R> query, CriteriaBuilder
builder, Path<? extends P> path)
+    {
+        return builder.countDistinct(path.get(attribute));
+    }
+}
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/11af2fa0/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/CriteriaSupportHandler.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/CriteriaSupportHandler.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/CriteriaSupportHandler.java
index cac105f..c65588f 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/CriteriaSupportHandler.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/handler/CriteriaSupportHandler.java
@@ -34,6 +34,7 @@ import org.apache.deltaspike.data.impl.criteria.selection.AttributeQuerySelectio
 import org.apache.deltaspike.data.impl.criteria.selection.numeric.Abs;
 import org.apache.deltaspike.data.impl.criteria.selection.numeric.Avg;
 import org.apache.deltaspike.data.impl.criteria.selection.numeric.Count;
+import org.apache.deltaspike.data.impl.criteria.selection.numeric.CountDistinct;
 import org.apache.deltaspike.data.impl.criteria.selection.numeric.Max;
 import org.apache.deltaspike.data.impl.criteria.selection.numeric.Min;
 import org.apache.deltaspike.data.impl.criteria.selection.numeric.Modulo;
@@ -92,9 +93,15 @@ public class CriteriaSupportHandler<E> extends AbstractDelegateQueryHandler<E>
i
     }
 
     @Override
-    public <N extends Number> QuerySelection<E, N> count(SingularAttribute<E,
N> attribute)
+    public QuerySelection<E, Long> count(SingularAttribute<E, ?> attribute)
     {
-        return new Count<E, N>(attribute);
+        return new Count<E>(attribute);
+    }
+
+    @Override
+    public QuerySelection<E, Long> countDistinct(SingularAttribute<E, ?> attribute)
+    {
+        return new CountDistinct<E>(attribute);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/11af2fa0/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/criteria/CriteriaTest.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/criteria/CriteriaTest.java
b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/criteria/CriteriaTest.java
index 931f42c..2cfa21c 100644
--- a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/criteria/CriteriaTest.java
+++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/impl/criteria/CriteriaTest.java
@@ -341,6 +341,22 @@ public class CriteriaTest extends TransactionalTestCase
         assertNull(result2);
     }
 
+    @Test
+    public void should_create_count_criteria()
+    {
+        // given
+        final String name = "should_create_count_criteria";
+        createSimple(name, 10);
+        createSimple(name, 11);
+
+        // when
+        Long result = repo.criteriaCount(name);
+
+        // then
+        assertNotNull(result);
+        assertEquals(1l, result.longValue());
+    }
+
     @Override
     protected EntityManager getEntityManager()
     {

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/11af2fa0/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/SimpleCriteriaRepository.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/SimpleCriteriaRepository.java
b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/SimpleCriteriaRepository.java
index a4cca1c..244587b 100644
--- a/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/SimpleCriteriaRepository.java
+++ b/deltaspike/modules/data/impl/src/test/java/org/apache/deltaspike/data/test/service/SimpleCriteriaRepository.java
@@ -54,6 +54,14 @@ public abstract class SimpleCriteriaRepository extends AbstractEntityRepository<
                 .getAnyResult();
     }
 
+    public Long criteriaCount(String name)
+    {
+        return criteria()
+                .select(Long.class, countDistinct(Simple_.name))
+                .eq(Simple_.name, name)
+                .getSingleResult();
+    }
+
     @SuppressWarnings("unchecked")
     public Statistics queryWithSelect(String name)
     {


Mime
View raw message