cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1401772 - in /cxf/trunk/rt/rs/extensions/search/src: main/java/org/apache/cxf/jaxrs/ext/search/jpa/ test/java/org/apache/cxf/jaxrs/ext/search/jpa/
Date Wed, 24 Oct 2012 16:40:04 GMT
Author: sergeyb
Date: Wed Oct 24 16:40:03 2012
New Revision: 1401772

URL: http://svn.apache.org/viewvc?rev=1401772&view=rev
Log:
More JPA2 support, making CriteriaQuery visitor more useful, support for joins to be added
at some later stage

Added:
    cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/Book_.java
  (with props)
Modified:
    cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/jpa/AbstractJPATypedQueryVisitor.java
    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/main/java/org/apache/cxf/jaxrs/ext/search/jpa/JPATypedQueryVisitor.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/AbstractJPATypedQueryVisitor.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/jpa/AbstractJPATypedQueryVisitor.java?rev=1401772&r1=1401771&r2=1401772&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/jpa/AbstractJPATypedQueryVisitor.java
(original)
+++ cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/jpa/AbstractJPATypedQueryVisitor.java
Wed Oct 24 16:40:03 2012
@@ -36,25 +36,45 @@ import org.apache.cxf.jaxrs.ext.search.O
 import org.apache.cxf.jaxrs.ext.search.PrimitiveStatement;
 import org.apache.cxf.jaxrs.ext.search.SearchCondition;
 
-public abstract class AbstractJPATypedQueryVisitor<T, E> 
+public abstract class AbstractJPATypedQueryVisitor<T, T1, E> 
     extends AbstractSearchConditionVisitor<T, E> {
 
     private EntityManager em;
     private Class<T> tClass;
+    private Class<T1> queryClass;
     private Root<T> root;
     private CriteriaBuilder builder;
-    private CriteriaQuery<T> cq;
+    private CriteriaQuery<T1> cq;
     private Stack<List<Predicate>> predStack = new Stack<List<Predicate>>();
     private boolean criteriaFinalized;
     
-    public AbstractJPATypedQueryVisitor(EntityManager em, Class<T> tClass) {
-        this(em, tClass, null);
+    protected AbstractJPATypedQueryVisitor(EntityManager em, Class<T> tClass) {
+        this(em, tClass, null, null);
     }
     
-    public AbstractJPATypedQueryVisitor(EntityManager em, Class<T> tClass, Map<String,
String> fieldMap) {
+    protected AbstractJPATypedQueryVisitor(EntityManager em, Class<T> tClass, Class<T1>
queryClass) {
+        this(em, tClass, queryClass, null);
+    }
+    
+    protected AbstractJPATypedQueryVisitor(EntityManager em, 
+                                        Class<T> tClass, 
+                                        Map<String, String> fieldMap) {
+        this(em, tClass, null, fieldMap);
+    }
+    
+    protected AbstractJPATypedQueryVisitor(EntityManager em, 
+                                        Class<T> tClass, 
+                                        Class<T1> queryClass,
+                                        Map<String, String> fieldMap) {
         super(fieldMap);
         this.em = em;
         this.tClass = tClass;
+        this.queryClass = toQueryClass(queryClass, tClass);
+    }
+    
+    @SuppressWarnings("unchecked")
+    private static <E> Class<E> toQueryClass(Class<E> queryClass, Class<?>
tClass) {
+        return queryClass != null ? queryClass : (Class<E>)tClass;
     }
     
     protected EntityManager getEntityManager() {
@@ -64,7 +84,7 @@ public abstract class AbstractJPATypedQu
     public void visit(SearchCondition<T> sc) {
         if (builder == null) {
             builder = em.getCriteriaBuilder();
-            cq = builder.createQuery(tClass);
+            cq = builder.createQuery(queryClass);
             root = cq.from(tClass);
             predStack.push(new ArrayList<Predicate>());
         }
@@ -92,7 +112,19 @@ public abstract class AbstractJPATypedQu
         }
     }
 
-    public CriteriaQuery<T> getCriteriaQuery() {
+    protected CriteriaBuilder getCriteriaBuilder() {
+        return builder;
+    }
+    
+    protected Class<T1> getQueryClass() {
+        return queryClass;
+    }
+    
+    public Root<T> getRoot() {
+        return root;
+    }
+    
+    public CriteriaQuery<T1> getCriteriaQuery() {
         if (!criteriaFinalized) {
             List<Predicate> predsList = predStack.pop();
             cq.where(predsList.toArray(new Predicate[predsList.size()]));
@@ -112,6 +144,9 @@ public abstract class AbstractJPATypedQu
         value = cv.getValue();    
         
         Path<?> path = getPath(root, name);
+        if (tClass != queryClass) {
+            path.alias(name);
+        }
         
         Predicate pred = null;
         switch (ct) {

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=1401772&r1=1401771&r2=1401772&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
Wed Oct 24 16:40:03 2012
@@ -18,23 +18,77 @@
  */
 package org.apache.cxf.jaxrs.ext.search.jpa;
 
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Map;
 
 import javax.persistence.EntityManager;
+import javax.persistence.criteria.CompoundSelection;
 import javax.persistence.criteria.CriteriaQuery;
+import javax.persistence.criteria.Selection;
+import javax.persistence.metamodel.SingularAttribute;
 
-public class JPACriteriaQueryVisitor<T> extends AbstractJPATypedQueryVisitor<T,
CriteriaQuery<T>> {
+public class JPACriteriaQueryVisitor<T, E> extends AbstractJPATypedQueryVisitor<T,
E, CriteriaQuery<E>> {
 
-    public JPACriteriaQueryVisitor(EntityManager em, Class<T> tClass) {
-        this(em, tClass, null);
+    public JPACriteriaQueryVisitor(EntityManager em, 
+                                   Class<T> tClass,
+                                   Class<E> queryClass) {
+        this(em, tClass, queryClass, null);
     }
     
-    public JPACriteriaQueryVisitor(EntityManager em, Class<T> tClass, Map<String,
String> fieldMap) {
-        super(em, tClass, fieldMap);
+    public JPACriteriaQueryVisitor(EntityManager em, 
+                                   Class<T> tClass,
+                                   Class<E> queryClass,
+                                   Map<String, String> fieldMap) {
+        super(em, tClass, queryClass, fieldMap);
     }
     
-    public CriteriaQuery<T> getQuery() {
+    public CriteriaQuery<E> getQuery() {
         return getCriteriaQuery();
     }
         
+    public CriteriaQuery<E> selectArray(List<SingularAttribute<T, ?>> attributes)
{
+        return selectArraySelections(toSelectionsArray(toSelectionsList(attributes)));
+    }
+    
+    private CriteriaQuery<E> selectArraySelections(Selection<?>... selections)
{
+        @SuppressWarnings("unchecked")
+        CompoundSelection<E> selection = (CompoundSelection<E>)getCriteriaBuilder().array(selections);
+        getQuery().select(selection);
+        return getQuery();
+    }
+    
+    public CriteriaQuery<E> selectConstruct(List<SingularAttribute<T, ?>>
attributes) {
+        return selectConstructSelections(toSelectionsArray(toSelectionsList(attributes)));
+    }
+    
+    private CriteriaQuery<E> selectConstructSelections(Selection<?>... selections)
{
+        getQuery().select(getCriteriaBuilder().construct(getQueryClass(), selections));
+        return getQuery();
+    }
+    
+    public CriteriaQuery<E> selectTuple(List<SingularAttribute<T, ?>> attributes)
{
+        return selectTupleSelections(toSelectionsArray(toSelectionsList(attributes)));
+    }
+    
+    private CriteriaQuery<E> selectTupleSelections(Selection<?>... selections)
{
+        @SuppressWarnings("unchecked")
+        CompoundSelection<E> selection = 
+            (CompoundSelection<E>)getCriteriaBuilder().tuple(selections);
+        getQuery().select(selection);
+        return getQuery();
+    }
+    
+    private List<Selection<?>> toSelectionsList(List<SingularAttribute<T,
?>> attributes) {
+        List<Selection<?>> selections = new ArrayList<Selection<?>>(attributes.size());
+        for (SingularAttribute<T, ?> attr : attributes) {
+            selections.add(getRoot().get(attr));
+        }
+        return selections;
+    }
+    
+    private static Selection<?>[] toSelectionsArray(List<Selection<?>>
selections) {
+        return selections.toArray(new Selection[]{});
+    }
+    
 }

Modified: cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/jpa/JPATypedQueryVisitor.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/jpa/JPATypedQueryVisitor.java?rev=1401772&r1=1401771&r2=1401772&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/jpa/JPATypedQueryVisitor.java
(original)
+++ cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/jpa/JPATypedQueryVisitor.java
Wed Oct 24 16:40:03 2012
@@ -22,8 +22,9 @@ import java.util.Map;
 
 import javax.persistence.EntityManager;
 import javax.persistence.TypedQuery;
+import javax.persistence.criteria.CriteriaQuery;
 
-public class JPATypedQueryVisitor<T> extends AbstractJPATypedQueryVisitor<T, TypedQuery<T>>
{
+public class JPATypedQueryVisitor<T> extends AbstractJPATypedQueryVisitor<T, T,
TypedQuery<T>> {
 
     public JPATypedQueryVisitor(EntityManager em, Class<T> tClass) {
         this(em, tClass, null);
@@ -34,7 +35,7 @@ public class JPATypedQueryVisitor<T> ext
     }
     
     public TypedQuery<T> getQuery() {
-        return getEntityManager().createQuery(getCriteriaQuery());
+        return getEntityManager().createQuery((CriteriaQuery<T>)getCriteriaQuery());
     }
         
 }

Added: cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/Book_.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/Book_.java?rev=1401772&view=auto
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/Book_.java
(added)
+++ cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/Book_.java
Wed Oct 24 16:40:03 2012
@@ -0,0 +1,32 @@
+/**
+ * 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.cxf.jaxrs.ext.search.jpa;
+
+import javax.persistence.metamodel.SingularAttribute;
+
+@javax.persistence.metamodel.StaticMetamodel(Book.class)
+//CHECKSTYLE:OFF
+public final class Book_ {
+    private Book_() {
+        
+    }
+    public static volatile SingularAttribute<Book, Integer> id;
+    public static volatile SingularAttribute<Book, String> title;
+}
+//CHECKSTYLE:ON
\ No newline at end of file

Propchange: cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/Book_.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/Book_.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

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=1401772&r1=1401771&r2=1401772&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
Wed Oct 24 16:40:03 2012
@@ -21,6 +21,7 @@ package org.apache.cxf.jaxrs.ext.search.
 import java.sql.Connection;
 import java.sql.DriverManager;
 import java.text.SimpleDateFormat;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Date;
 import java.util.HashMap;
@@ -30,7 +31,10 @@ import java.util.Map;
 import javax.persistence.EntityManager;
 import javax.persistence.EntityManagerFactory;
 import javax.persistence.Persistence;
+import javax.persistence.Tuple;
 import javax.persistence.TypedQuery;
+import javax.persistence.criteria.CriteriaQuery;
+import javax.persistence.metamodel.SingularAttribute;
 
 import org.apache.cxf.jaxrs.ext.search.SearchCondition;
 import org.apache.cxf.jaxrs.ext.search.SearchConditionVisitor;
@@ -158,6 +162,15 @@ public class JPATypedQueryVisitorTest ex
     }
     
     @Test
+    public void testEqualsCriteriaQueryTuple() throws Exception {
+        List<Tuple> books = criteriaQueryBooks("id==10");
+        assertEquals(1, books.size());
+        Tuple tuple = books.get(0);
+        int tupleId = tuple.get("id", Integer.class);
+        assertEquals(10, tupleId);
+    }
+    
+    @Test
     public void testEqualsAddressQuery() throws Exception {
         List<Book> books = queryBooks("address==Street1",
             Collections.singletonMap("address", "address.street"));
@@ -311,4 +324,21 @@ public class JPATypedQueryVisitorTest ex
         TypedQuery<Book> query = jpa.getQuery();
         return query.getResultList();
     }
+    
+    private List<Tuple> criteriaQueryBooks(String expression) throws Exception {
+        SearchCondition<Book> filter = 
+            new FiqlParser<Book>(Book.class).parse(expression);
+        JPACriteriaQueryVisitor<Book, Tuple> jpa = 
+            new JPACriteriaQueryVisitor<Book, Tuple>(em, Book.class, Tuple.class);
+        filter.accept(jpa);
+        
+        List<SingularAttribute<Book, ?>> selections = 
+            new ArrayList<SingularAttribute<Book, ?>>();
+        selections.add(Book_.id);
+        
+        jpa.selectTuple(selections);
+        
+        CriteriaQuery<Tuple> cquery = jpa.getQuery();
+        return em.createQuery(cquery).getResultList();
+    }
 }



Mime
View raw message