cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1338133 - in /cxf/branches/2.4.x-fixes: ./ rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/search/ rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/search/ rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/search/sql/
Date Mon, 14 May 2012 10:13:37 GMT
Author: sergeyb
Date: Mon May 14 10:13:37 2012
New Revision: 1338133

URL: http://svn.apache.org/viewvc?rev=1338133&view=rev
Log:
Merged revisions 1338130 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/branches/2.5.x-fixes

................
  r1338130 | sergeyb | 2012-05-14 11:09:49 +0100 (Mon, 14 May 2012) | 9 lines
  
  Merged revisions 1338124 via svnmerge from 
  https://svn.apache.org/repos/asf/cxf/trunk
  
  ........
    r1338124 | sergeyb | 2012-05-14 10:43:10 +0100 (Mon, 14 May 2012) | 1 line
    
    [CXF-4314] Adding a search bean for capturing the properties with different names
  ........
................

Added:
    cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchBean.java
      - copied unchanged from r1338130, cxf/branches/2.5.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchBean.java
Modified:
    cxf/branches/2.4.x-fixes/   (props changed)
    cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/search/FiqlParser.java
    cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/search/PrimitiveSearchCondition.java
    cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/search/SimpleSearchCondition.java
    cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/search/SearchContextImplTest.java
    cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitorTest.java

Propchange: cxf/branches/2.4.x-fixes/
------------------------------------------------------------------------------
  Merged /cxf/branches/2.5.x-fixes:r1338130
  Merged /cxf/trunk:r1338124

Propchange: cxf/branches/2.4.x-fixes/
------------------------------------------------------------------------------
Binary property 'svnmerge-integrated' - no diff available.

Modified: cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/search/FiqlParser.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/search/FiqlParser.java?rev=1338133&r1=1338132&r2=1338133&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/search/FiqlParser.java
(original)
+++ cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/search/FiqlParser.java
Mon May 14 10:13:37 2012
@@ -77,6 +77,7 @@ public class FiqlParser<T> {
     }
 
     private Beanspector<T> beanspector;
+    private Class<T> conditionClass;
     private Map<String, String> properties;
     /**
      * Creates FIQL parser.
@@ -96,7 +97,9 @@ public class FiqlParser<T> {
      * @param contextProperties            
      */
     public FiqlParser(Class<T> tclass, Map<String, String> contextProperties)
{
-        beanspector = new Beanspector<T>(tclass);
+        beanspector = SearchBean.class.isAssignableFrom(tclass) 
+            ? null : new Beanspector<T>(tclass);
+        conditionClass = tclass;
         properties = contextProperties;
     }
 
@@ -226,7 +229,7 @@ public class FiqlParser<T> {
         Object castedValue = value;
         Class<?> valueType;
         try {
-            valueType = beanspector.getAccessorType(setter);
+            valueType = beanspector != null ? beanspector.getAccessorType(setter) : String.class;
         } catch (Exception e) {
             throw new FiqlParseException(e);
         }
@@ -308,7 +311,7 @@ public class FiqlParser<T> {
                     break;
                 }
             }
-            if (!hasSubtree && AND.equals(operator)) {
+            if (!hasSubtree && AND.equals(operator) && beanspector != null)
{
                 try {
                     // Optimization: single SimpleSearchCondition for 'AND' conditions
                     Map<String, ConditionType> map = new LinkedHashMap<String, ConditionType>();
@@ -380,10 +383,17 @@ public class FiqlParser<T> {
             return pojo.getClass().getName().startsWith("java.lang");
         }
         
+        @SuppressWarnings("unchecked")
         private T createTemplate(String setter, Object val) throws FiqlParseException {
             try {
-                beanspector.instantiate().setValue(setter, val);
-                return beanspector.getBean();
+                if (beanspector != null) {
+                    beanspector.instantiate().setValue(setter, val);
+                    return beanspector.getBean();
+                } else {
+                    SearchBean bean = (SearchBean)conditionClass.newInstance();
+                    bean.set(setter, value.toString());
+                    return (T)bean;
+                }
             } catch (Throwable e) {
                 throw new FiqlParseException(e);
             }

Modified: cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/search/PrimitiveSearchCondition.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/search/PrimitiveSearchCondition.java?rev=1338133&r1=1338132&r2=1338133&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/search/PrimitiveSearchCondition.java
(original)
+++ cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/search/PrimitiveSearchCondition.java
Mon May 14 10:13:37 2012
@@ -39,7 +39,8 @@ public class PrimitiveSearchCondition<T>
         this.condition = condition;
         this.cType = ct;
         if (propertyName != null) {
-            this.beanspector = new Beanspector<T>(condition);
+            this.beanspector = SearchBean.class.isAssignableFrom(condition.getClass()) 
+                ? null : new Beanspector<T>(condition);
         }
     }
     
@@ -80,7 +81,11 @@ public class PrimitiveSearchCondition<T>
 
     private Object getValue(String getter, T pojo) {
         try {
-            return beanspector.swap(pojo).getValue(getter);
+            if (beanspector != null) {
+                return beanspector.swap(pojo).getValue(getter);
+            } else {
+                return ((SearchBean)pojo).get(getter);
+            }
         } catch (Throwable e) {
             return null;
         }
@@ -99,7 +104,7 @@ public class PrimitiveSearchCondition<T>
         return pojo.getClass().getName().startsWith("java.lang");
     }
 
-    @SuppressWarnings("unchecked")
+    @SuppressWarnings({ "unchecked", "rawtypes" })
     private boolean compare(Object lval, ConditionType cond, Object rval) {
         boolean compares = true;
         if (cond == ConditionType.EQUALS || cond == ConditionType.NOT_EQUALS) {

Modified: cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/search/SimpleSearchCondition.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/search/SimpleSearchCondition.java?rev=1338133&r1=1338132&r2=1338133&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/search/SimpleSearchCondition.java
(original)
+++ cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/main/java/org/apache/cxf/jaxrs/ext/search/SimpleSearchCondition.java
Mon May 14 10:13:37 2012
@@ -135,12 +135,16 @@ public class SimpleSearchCondition<T> im
             List<SearchCondition<T>> list = new ArrayList<SearchCondition<T>>();
             Map<String, Object> get2val = getGettersAndValues();
             
-            for (String getter : get2val.keySet()) {
+            Set<String> keySet = get2val != null ? get2val.keySet()
+                : ((SearchBean)condition).getKeySet();
+            
+            for (String getter : keySet) {
                 ConditionType ct = getters2operators == null ? sharedType : getters2operators.get(getter);
                 if (ct == null) {
                     continue;
                 }
-                Object rval = get2val.get(getter);
+                Object rval = get2val != null 
+                    ? get2val.get(getter) : ((SearchBean)condition).get(getter);
                 if (rval == null) {
                     continue;
                 }
@@ -236,16 +240,19 @@ public class SimpleSearchCondition<T> im
      * @return template (condition) object getters mapped to their non-null values
      */
     private Map<String, Object> getGettersAndValues() {
-        
-        Map<String, Object> getters2values = new HashMap<String, Object>();
-        Beanspector<T> beanspector = new Beanspector<T>(condition);
-        for (String getter : beanspector.getGettersNames()) {
-            Object value = getValue(beanspector, getter, condition);
-            getters2values.put(getter, value);
-        }
-        //we do not need compare class objects
-        getters2values.keySet().remove("class");
-        return getters2values;
+        if (!SearchBean.class.isAssignableFrom(condition.getClass())) {
+            Map<String, Object> getters2values = new HashMap<String, Object>();
+            Beanspector<T> beanspector = new Beanspector<T>(condition);
+            for (String getter : beanspector.getGettersNames()) {
+                Object value = getValue(beanspector, getter, condition);
+                getters2values.put(getter, value);
+            }
+            //we do not need compare class objects
+            getters2values.keySet().remove("class");
+            return getters2values; 
+        } else {
+            return null;
+        }
     }
 
     private Object getValue(Beanspector<T> beanspector, String getter, T pojo) {

Modified: cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/search/SearchContextImplTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/search/SearchContextImplTest.java?rev=1338133&r1=1338132&r2=1338133&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/search/SearchContextImplTest.java
(original)
+++ cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/search/SearchContextImplTest.java
Mon May 14 10:13:37 2012
@@ -36,6 +36,12 @@ public class SearchContextImplTest exten
             SearchContextImpl.SEARCH_QUERY + "=" + "name==CXF%20Rocks;id=gt=123");
     }
     
+    @Test
+    public void testFiqlSearchBean() {
+        doTestFiqlSearchBean(
+            SearchContextImpl.SEARCH_QUERY + "=" + "name==CXF%20Rocks;id=gt=123");
+    }
+    
     @Test(expected = IllegalArgumentException.class)
     public void testIllegalConditionType() {
         SearchContext context = new SearchContextImpl(new MessageImpl());
@@ -72,4 +78,40 @@ public class SearchContextImplTest exten
         assertEquals(1, found.size());
         assertEquals(new Book("CXF Rocks", 125L), found.get(0));
     }
+    
+    private void doTestFiqlSearchBean(String queryString) {
+        Message m = new MessageImpl();
+        m.put(Message.QUERY_STRING, queryString);
+        SearchContext context = new SearchContextImpl(m);
+        SearchCondition<SearchBean> sc = context.getCondition(SearchBean.class);
+        assertNotNull(sc);
+        
+        List<SearchBean> beans = new ArrayList<SearchBean>();
+        SearchBean sb1 = new SearchBean();
+        sb1.set("name", "CXF is cool");
+        beans.add(sb1);
+        SearchBean sb2 = new SearchBean();
+        sb2.set("name", "CXF Rocks");
+        sb2.set("id", "124");
+        beans.add(sb2);
+        
+        List<SearchBean> found = sc.findAll(beans);
+        assertEquals(1, found.size());
+        assertEquals(sb2, found.get(0));
+        
+        assertTrue(sc instanceof AndSearchCondition);
+        assertNull(sc.getStatement());
+        List<SearchCondition<SearchBean>> scs = sc.getSearchConditions();
+        assertEquals(2, scs.size());
+        SearchCondition<SearchBean> sc1 = scs.get(0);
+        assertEquals("name", sc1.getStatement().getProperty());
+        SearchCondition<SearchBean> sc2 = scs.get(1);
+        assertEquals("id", sc2.getStatement().getProperty());
+        
+        assertTrue("123".equals(sc1.getStatement().getValue())
+                   && "CXF Rocks".equals(sc2.getStatement().getValue())
+                   || "123".equals(sc2.getStatement().getValue())
+                   && "CXF Rocks".equals(sc1.getStatement().getValue()));
+        
+    }
 }

Modified: cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitorTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitorTest.java?rev=1338133&r1=1338132&r2=1338133&view=diff
==============================================================================
--- cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitorTest.java
(original)
+++ cxf/branches/2.4.x-fixes/rt/frontend/jaxrs/src/test/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitorTest.java
Mon May 14 10:13:37 2012
@@ -23,6 +23,7 @@ import java.util.Date;
 
 import org.apache.cxf.jaxrs.ext.search.FiqlParseException;
 import org.apache.cxf.jaxrs.ext.search.FiqlParser;
+import org.apache.cxf.jaxrs.ext.search.SearchBean;
 import org.apache.cxf.jaxrs.ext.search.SearchCondition;
 
 import org.junit.Assert;
@@ -45,6 +46,18 @@ public class SQLPrinterVisitorTest exten
     }
     
     @Test
+    public void testSQL1WithSearchBean() throws FiqlParseException {
+        FiqlParser<SearchBean> beanParser = new FiqlParser<SearchBean>(SearchBean.class);
+        SearchCondition<SearchBean> filter = beanParser.parse("name==ami*;level=gt=10");
+        SQLPrinterVisitor<SearchBean> visitor = new SQLPrinterVisitor<SearchBean>("table");
+        filter.accept(visitor);
+        String sql = visitor.getResult();
+        
+        assertTrue("SELECT * FROM table WHERE (name LIKE 'ami%') AND (level > '10')".equals(sql)
+                   || "SELECT * FROM table WHERE (level > '10') AND (name LIKE 'ami%')".equals(sql));
+    }
+    
+    @Test
     public void testSQL2() throws FiqlParseException {
         SearchCondition<Condition> filter = parser.parse("name==ami*,level=gt=10");
         SQLPrinterVisitor<Condition> visitor = new SQLPrinterVisitor<Condition>("table");
@@ -67,6 +80,19 @@ public class SQLPrinterVisitorTest exten
     }
     
     @Test
+    public void testSQL3WithSearchBean() throws FiqlParseException {
+        FiqlParser<SearchBean> beanParser = new FiqlParser<SearchBean>(SearchBean.class);
+        SearchCondition<SearchBean> filter = beanParser.parse("name==foo*;(name!=*bar,level=gt=10)");
+        SQLPrinterVisitor<SearchBean> visitor = new SQLPrinterVisitor<SearchBean>("table");
+        filter.accept(visitor);
+        String sql = visitor.getResult();
+        assertTrue(("SELECT * FROM table WHERE (name LIKE 'foo%') AND ((name NOT LIKE '%bar')
"
+                   + "OR (level > '10'))").equals(sql)
+                   || ("SELECT * FROM table WHERE (name LIKE 'foo%') AND "
+                   + "((level > '10') OR (name NOT LIKE '%bar'))").equals(sql));
+    }
+    
+    @Test
     public void testSQL4() throws FiqlParseException {
         SearchCondition<Condition> filter = parser.parse("(name==test,level==18);(name==test1,level!=19)");
         SQLPrinterVisitor<Condition> visitor = new SQLPrinterVisitor<Condition>("table");



Mime
View raw message