cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject git commit: [CXF-5806] Adding a patch from Romain Castan
Date Thu, 19 Jun 2014 12:11:22 GMT
Repository: cxf
Updated Branches:
  refs/heads/2.6.x-fixes 464e69973 -> de766416b


[CXF-5806] Adding a patch from Romain Castan


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

Branch: refs/heads/2.6.x-fixes
Commit: de766416b3e7d09d0bfff6995360feaeb442a72d
Parents: 464e699
Author: Sergey Beryozkin <sberyozkin@talend.com>
Authored: Wed Jun 18 21:31:56 2014 +0100
Committer: Sergey Beryozkin <sberyozkin@talend.com>
Committed: Thu Jun 19 13:11:10 2014 +0100

----------------------------------------------------------------------
 .../jpa/AbstractJPATypedQueryVisitor.java       | 52 +++++++++++++++-----
 .../search/jpa/JPATypedQueryVisitorTest.java    | 13 +++--
 .../cxf/jaxrs/ext/search/jpa/Library.java       | 14 ++++++
 3 files changed, 65 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cxf/blob/de766416/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/jpa/AbstractJPATypedQueryVisitor.java
----------------------------------------------------------------------
diff --git a/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/jpa/AbstractJPATypedQueryVisitor.java
b/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/jpa/AbstractJPATypedQueryVisitor.java
index 8421858..343d966 100644
--- a/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/jpa/AbstractJPATypedQueryVisitor.java
+++ b/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/jpa/AbstractJPATypedQueryVisitor.java
@@ -31,6 +31,7 @@ import javax.persistence.TypedQuery;
 import javax.persistence.criteria.CriteriaBuilder;
 import javax.persistence.criteria.CriteriaQuery;
 import javax.persistence.criteria.Expression;
+import javax.persistence.criteria.From;
 import javax.persistence.criteria.Join;
 import javax.persistence.criteria.Path;
 import javax.persistence.criteria.Predicate;
@@ -259,26 +260,55 @@ public abstract class AbstractJPATypedQueryVisitor<T, T1, E>
         if (name.contains(".")) {
             String pre = name.substring(0, name.indexOf('.'));
             String post = name.substring(name.indexOf('.') + 1);
-            return getPath(getNextPath(element, pre, cv, null), 
-                           post, 
-                           cv,
-                           collSize);
+            final Path<?> nextPath = getNextPath(element, pre, post, cv, null);
+            return getPath(nextPath, post, cv, collSize);
         } else {
-            return getNextPath(element, name, cv, collSize);
+            return getNextPath(element, name, null, cv, collSize);
         }
     }
 
-    private Path<?> getNextPath(Path<?> element, String name, ClassValue cv,
CollectionCheckInfo collSize) {
-        if (collSize == null
-            && (cv.isCollection(name) || isJoinProperty(name)) && (element
== root || element instanceof Join)) {
-            return element == root ? root.join(name) : ((Join<?, ?>)element).join(name);
+    private Path<?> getNextPath(Path<?> element, String name, String postName,

+        ClassValue cv, CollectionCheckInfo collSize) {
+        final boolean isCollectionOrJoin = collSize == null
+            && (cv.isCollection(name) || isJoinProperty(name) || existingCollectionInPostName(cv,
postName)) 
+            && (element == root || element instanceof Join);
+        if (isCollectionOrJoin) {
+            final Path<?> path = getExistingJoinProperty((From<?, ?>)element,
name);
+            if (path != null) {
+                return path;
+            } else {
+                return element == root ? root.join(name) : ((Join<?, ?>)element).join(name);
+            }
         } else {
-            return element.get(name);
+            return element.get(name);                
         }
     }
     
     private boolean isJoinProperty(String prop) {
         return joinProperties == null ? false : joinProperties.contains(prop);
     }
-    
+ 
+    private Path<?> getExistingJoinProperty(From<?, ?> element, String prop)
{
+        final Set<?> joins = element.getJoins();
+        for (Object object : joins) {
+            Join<?, ?> join = (Join<?, ?>) object;
+            if (join.getAttribute().getName().equals(prop)) {
+                return join;
+            }
+        }
+        return null;
+    } 
+   
+    private boolean existingCollectionInPostName(ClassValue cv, String postName) {
+        if (postName != null) {
+            final String[] splitName = postName.split("\\.");
+            for (String name : splitName) {
+                if (cv.isCollection(name)) {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/cxf/blob/de766416/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/JPATypedQueryVisitorTest.java
----------------------------------------------------------------------
diff --git a/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/JPATypedQueryVisitorTest.java
b/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/JPATypedQueryVisitorTest.java
index c5fdda0..61fa309 100644
--- a/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/JPATypedQueryVisitorTest.java
+++ b/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/JPATypedQueryVisitorTest.java
@@ -98,7 +98,6 @@ public class JPATypedQueryVisitorTest extends Assert {
             b1.setLibrary(lib);
             b1.getAuthors().add("John");
             em.persist(b1);
-            assertTrue(em.contains(b1));
             
             BookReview br2 = new BookReview();
             br2.setId(2);
@@ -120,7 +119,6 @@ public class JPATypedQueryVisitorTest extends Assert {
             b2.setLibrary(lib);
             b2.getAuthors().add("John");
             em.persist(b2);
-            assertTrue(em.contains(b2));
             
             BookReview br3 = new BookReview();
             br3.setId(3);
@@ -141,7 +139,10 @@ public class JPATypedQueryVisitorTest extends Assert {
             b3.setOwnerInfo(info3);
             b3.setLibrary(lib);
             em.persist(b3);
-            assertTrue(em.contains(b3));
+
+            lib.getBooks().add(b1);
+            lib.getBooks().add(b2);
+            lib.getBooks().add(b3); 
             
             em.getTransaction().commit();
         } catch (Exception ex) {
@@ -192,6 +193,12 @@ public class JPATypedQueryVisitorTest extends Assert {
     }
     
     @Test
+    public void testGetLibraryBook() throws Exception {
+        List<Book> books = queryBooks("library.books.bookTitle==num10");
+        assertEquals(3, books.size());
+    }
+
+    @Test
     public void testQueryCollection() throws Exception {
         List<Book> books = 
             queryBooks("reviews.authors==Ted");

http://git-wip-us.apache.org/repos/asf/cxf/blob/de766416/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/Library.java
----------------------------------------------------------------------
diff --git a/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/Library.java
b/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/Library.java
index 1c24625..c5879a2 100644
--- a/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/Library.java
+++ b/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/jpa/Library.java
@@ -18,14 +18,20 @@
  */
 package org.apache.cxf.jaxrs.ext.search.jpa;
 
+import java.util.LinkedList;
+import java.util.List;
+
 import javax.persistence.Entity;
 import javax.persistence.Id;
+import javax.persistence.OneToMany;
 
 @Entity
 public class Library {
     @Id
     private int id;
     private String address;
+    @OneToMany
+    private List<Book> books = new LinkedList<Book>(); 
     
     public int getId() {
         return id;
@@ -42,4 +48,12 @@ public class Library {
     public void setAddress(String address) {
         this.address = address;
     }
+
+    public List<Book> getBooks() {
+        return books;
+    }
+
+    public void setBooks(List<Book> books) {
+        this.books = books;
+    }
 }


Mime
View raw message