cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1426201 - in /cxf/trunk/rt/rs/extensions/search/src: main/java/org/apache/cxf/jaxrs/ext/search/jpa/JPACriteriaQueryVisitor.java test/java/org/apache/cxf/jaxrs/ext/search/jpa/JPATypedQueryVisitorTest.java
Date Thu, 27 Dec 2012 13:40:51 GMT
Author: sergeyb
Date: Thu Dec 27 13:40:51 2012
New Revision: 1426201

URL: http://svn.apache.org/viewvc?rev=1426201&view=rev
Log:
Updating CriteriaQuery visitor with the orderBy utility method

Modified:
    cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/jpa/JPACriteriaQueryVisitor.java
    cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/JPATypedQueryVisitorTest.java

Modified: cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/jpa/JPACriteriaQueryVisitor.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/jpa/JPACriteriaQueryVisitor.java?rev=1426201&r1=1426200&r2=1426201&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/jpa/JPACriteriaQueryVisitor.java
(original)
+++ cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/jpa/JPACriteriaQueryVisitor.java
Thu Dec 27 13:40:51 2012
@@ -25,7 +25,9 @@ import java.util.Map;
 import javax.persistence.EntityManager;
 import javax.persistence.TypedQuery;
 import javax.persistence.criteria.CompoundSelection;
+import javax.persistence.criteria.CriteriaBuilder;
 import javax.persistence.criteria.CriteriaQuery;
+import javax.persistence.criteria.Order;
 import javax.persistence.criteria.Path;
 import javax.persistence.criteria.Selection;
 import javax.persistence.metamodel.SingularAttribute;
@@ -73,7 +75,24 @@ public class JPACriteriaQueryVisitor<T, 
         countQuery.select(getCriteriaBuilder().count(getRoot()));
         return super.getEntityManager().createQuery(countQuery).getSingleResult();
     }
-     
+    
+    public TypedQuery<E> getOrderedTypedQuery(List<SingularAttribute<T, ?>>
attributes, boolean asc) {
+        CriteriaQuery<E> cQuery = orderBy(attributes, asc);
+        return getTypedQuery(cQuery);
+    }
+    
+    public CriteriaQuery<E> orderBy(List<SingularAttribute<T, ?>> attributes,
boolean asc) {
+        CriteriaBuilder cb = getCriteriaBuilder();
+        
+        List<Order> orders = new ArrayList<Order>();
+        for (SingularAttribute<T, ?> attribute : attributes) {
+            Path<?> selection = getRoot().get(attribute);
+            Order order = asc ? cb.asc(selection) : cb.desc(selection);
+            orders.add(order);
+        }
+        return getCriteriaQuery().orderBy(orders);
+    }
+    
     public TypedQuery<E> getArrayTypedQuery(List<SingularAttribute<T, ?>>
attributes) {
         CriteriaQuery<E> cQuery = selectArraySelections(toSelectionsArray(toSelectionsList(attributes,
false)));
         return getTypedQuery(cQuery);

Modified: cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/JPATypedQueryVisitorTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/JPATypedQueryVisitorTest.java?rev=1426201&r1=1426200&r2=1426201&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/JPATypedQueryVisitorTest.java
(original)
+++ cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/JPATypedQueryVisitorTest.java
Thu Dec 27 13:40:51 2012
@@ -289,6 +289,7 @@ public class JPATypedQueryVisitorTest ex
         assertEquals(1L, criteriaQueryBooksCount("id==10"));
     }
     
+    
     @Test
     public void testEqualsCriteriaQueryConstruct() throws Exception {
         List<BookInfo> books = criteriaQueryBooksConstruct("id==10");
@@ -299,6 +300,24 @@ public class JPATypedQueryVisitorTest ex
     }
     
     @Test
+    public void testOrderByAsc() throws Exception {
+        List<Book> books = criteriaQueryBooksOrderBy("reviews=gt=0", true);
+        assertEquals(3, books.size());
+        assertEquals(9, books.get(0).getId());
+        assertEquals(10, books.get(1).getId());
+        assertEquals(11, books.get(2).getId());
+    }
+    
+    @Test
+    public void testOrderByDesc() throws Exception {
+        List<Book> books = criteriaQueryBooksOrderBy("reviews=gt=0", false);
+        assertEquals(3, books.size());
+        assertEquals(11, books.get(0).getId());
+        assertEquals(10, books.get(1).getId());
+        assertEquals(9, books.get(2).getId());
+    }
+    
+    @Test
     public void testEqualsCriteriaQueryArray() throws Exception {
         List<Object[]> books = criteriaQueryBooksArray("id==10");
         assertEquals(1, books.size());
@@ -509,6 +528,20 @@ public class JPATypedQueryVisitorTest ex
         return jpa.count();
     }
     
+    private List<Book> criteriaQueryBooksOrderBy(String expression, boolean asc) throws
Exception {
+        SearchCondition<Book> filter = 
+            new FiqlParser<Book>(Book.class).parse(expression);
+        JPACriteriaQueryVisitor<Book, Book> jpa = 
+            new JPACriteriaQueryVisitor<Book, Book>(em, Book.class, Book.class);
+        filter.accept(jpa);
+        
+        List<SingularAttribute<Book, ?>> selections = 
+            new ArrayList<SingularAttribute<Book, ?>>();
+        selections.add(Book_.id);
+        
+        return jpa.getOrderedTypedQuery(selections, asc).getResultList();
+    }
+    
     private List<BookInfo> criteriaQueryBooksConstruct(String expression) throws Exception
{
         SearchCondition<Book> filter = 
             new FiqlParser<Book>(Book.class).parse(expression);



Mime
View raw message