deltaspike-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From daniels...@apache.org
Subject deltaspike git commit: Fixed: DELTASPIKE-911 Criteria API - Add support for accept multiple columns in orderBy
Date Wed, 03 Jun 2015 20:43:36 GMT
Repository: deltaspike
Updated Branches:
  refs/heads/master 1c2d45cd0 -> 4b0168dbf


Fixed: DELTASPIKE-911 Criteria API - Add support for accept multiple columns in orderBy


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

Branch: refs/heads/master
Commit: 4b0168dbf7dc273a6b480a92b3821923e29f0ccb
Parents: 1c2d45c
Author: Daniel Cunha (soro) <danielsoro@gmail.com>
Authored: Tue May 26 16:52:14 2015 -0300
Committer: Daniel Cunha (soro) <danielsoro@apache.org>
Committed: Wed Jun 3 17:42:51 2015 -0300

----------------------------------------------------------------------
 .../data/impl/criteria/QueryCriteria.java       | 11 ++-
 .../data/impl/criteria/processor/OrderBy.java   | 83 +++++++++++++++++---
 .../data/impl/criteria/CriteriaTest.java        | 16 +++-
 .../test/service/SimpleCriteriaRepository.java  |  7 ++
 4 files changed, 103 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/deltaspike/blob/4b0168db/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 79ffbed..89e6b42 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
@@ -81,6 +81,7 @@ public class QueryCriteria<C, R> implements Criteria<C, R>
     private final boolean ignoreNull = true;
     private boolean distinct = false;
 
+    private final OrderBy orderByProcessor = new OrderBy();
     private final List<PredicateBuilder<C>> builders = new LinkedList<PredicateBuilder<C>>();
     private final List<QueryProcessor<C>> processors = new LinkedList<QueryProcessor<C>>();
     private final List<QuerySelection<? super C, ?>> selections = new LinkedList<QuerySelection<?
super C, ?>>();
@@ -241,14 +242,14 @@ public class QueryCriteria<C, R> implements Criteria<C, R>
     @Override
     public <P> Criteria<C, R> orderAsc(SingularAttribute<? super C, P>
att)
     {
-        add(new OrderBy<C, P>(att, OrderDirection.ASC));
+        addOrderBy(att, OrderDirection.ASC);
         return this;
     }
 
     @Override
     public <P> Criteria<C, R> orderDesc(SingularAttribute<? super C, P>
att)
     {
-        add(new OrderBy<C, P>(att, OrderDirection.DESC));
+        addOrderBy(att, OrderDirection.DESC);
         return this;
     }
 
@@ -294,6 +295,7 @@ public class QueryCriteria<C, R> implements Criteria<C, R>
 
     void applyProcessors(CriteriaQuery<?> query, CriteriaBuilder builder, From<C,
C> from)
     {
+        orderByProcessor.process(query, builder, from);
         for (QueryProcessor<C> proc : processors)
         {
             proc.process(query, builder, from);
@@ -335,6 +337,11 @@ public class QueryCriteria<C, R> implements Criteria<C, R>
         processors.add(proc);
     }
 
+    private void addOrderBy(SingularAttribute<? super C, ?> att, OrderDirection orderDirection)
+    {
+        orderByProcessor.add(att, orderDirection);
+    }
+
     private Selection<?>[] prepareSelections(CriteriaQuery<?> query, CriteriaBuilder
builder, From<C, C> root)
     {
         List<Selection<?>> result = new ArrayList<Selection<?>>(selections.size());

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/4b0168db/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/processor/OrderBy.java
----------------------------------------------------------------------
diff --git a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/processor/OrderBy.java
b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/processor/OrderBy.java
index 8228320..2840c76 100644
--- a/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/processor/OrderBy.java
+++ b/deltaspike/modules/data/impl/src/main/java/org/apache/deltaspike/data/impl/criteria/processor/OrderBy.java
@@ -20,34 +20,95 @@ package org.apache.deltaspike.data.impl.criteria.processor;
 
 import javax.persistence.criteria.CriteriaBuilder;
 import javax.persistence.criteria.CriteriaQuery;
+import javax.persistence.criteria.Order;
 import javax.persistence.criteria.Path;
 import javax.persistence.metamodel.SingularAttribute;
 
 import org.apache.deltaspike.data.impl.builder.OrderDirection;
 
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
 public class OrderBy<P, V> implements QueryProcessor<P>
 {
 
-    private final SingularAttribute<? super P, V> att;
-    private final OrderDirection dir;
+    private final List<OrderByDefinition> orderByDefinitions = new ArrayList<OrderByDefinition>();
 
-    public OrderBy(SingularAttribute<? super P, V> att, OrderDirection dir)
+    public void add(SingularAttribute<? super P, V> att, OrderDirection dir)
     {
-        this.att = att;
-        this.dir = dir;
+        orderByDefinitions.add(new OrderByDefinition(att, dir));
     }
 
     @Override
     public <R> void process(CriteriaQuery<R> query, CriteriaBuilder builder,
Path<P> path)
     {
-        switch (dir)
+        List<Order> orders = new ArrayList<Order>();
+        Iterator<OrderByDefinition> iterator = orderByDefinitions.iterator();
+        while (iterator.hasNext())
         {
-            case ASC:
-                query.orderBy(builder.asc(path.get(att)));
-                break;
-            default:
-                query.orderBy(builder.desc(path.get(att)));
+            OrderByDefinition orderByDefinition = iterator.next();
+            switch (orderByDefinition.getDir())
+            {
+                case ASC:
+                    orders.add(builder.asc(path.get(orderByDefinition.getAtt())));
+                    break;
+                default:
+                    orders.add(builder.desc(path.get(orderByDefinition.getAtt())));
+            }
         }
+        query.orderBy(orders);
     }
 
+    private class OrderByDefinition
+    {
+        private final SingularAttribute<? super P, V> att;
+        private final OrderDirection dir;
+
+        public OrderByDefinition(SingularAttribute<? super P, V> att, OrderDirection
dir)
+        {
+            this.att = att;
+            this.dir = dir;
+        }
+
+        public SingularAttribute<? super P, V> getAtt()
+        {
+            return att;
+        }
+
+        public OrderDirection getDir()
+        {
+            return dir;
+        }
+
+        @Override
+        public boolean equals(Object o)
+        {
+            if (this == o)
+            {
+                return true;
+            }
+            if (o == null || getClass() != o.getClass())
+            {
+                return false;
+            }
+
+            OrderByDefinition that = (OrderByDefinition) o;
+
+            if (att != null ? !att.equals(that.att) : that.att != null)
+            {
+                return false;
+            }
+            return dir == that.dir;
+
+        }
+
+        @Override
+        public int hashCode()
+        {
+            int result = att != null ? att.hashCode() : 0;
+            result = 31 * result + (dir != null ? dir.hashCode() : 0);
+            return result;
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/4b0168db/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 44a3f32..3a459ff 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
@@ -420,6 +420,21 @@ public class CriteriaTest extends TransactionalTestCase
         assertEquals(superName, result.getSuperName());
     }
 
+    @Test
+    public void should_apply_multiply_orderby()
+    {
+        // given
+        createSimple("a", 1);
+        createSimple("b", 2);
+
+        // when
+        final List<Simple> orderByNameAndCounter = repo.findOrderByNameAndCounter();
+
+        // then
+        assertEquals(new Integer(2), orderByNameAndCounter.get(0).getCounter());
+        assertEquals(new Integer(1), orderByNameAndCounter.get(1).getCounter());
+    }
+
     @Override
     protected EntityManager getEntityManager()
     {
@@ -434,5 +449,4 @@ public class CriteriaTest extends TransactionalTestCase
         entityManager.flush();
         return result;
     }
-
 }

http://git-wip-us.apache.org/repos/asf/deltaspike/blob/4b0168db/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 35c87fd..5273cf2 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
@@ -121,4 +121,11 @@ public abstract class SimpleCriteriaRepository extends AbstractEntityRepository<
                 .getResultList();
     }
 
+   public List<Simple> findOrderByNameAndCounter() {
+      return criteria()
+              .orderDesc(Simple_.counter)
+              .orderAsc(Simple_.name)
+              .getResultList();
+   }
+
 }


Mime
View raw message