cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1338124 - in /cxf/trunk/rt/rs/extensions/search/src: main/java/org/apache/cxf/jaxrs/ext/search/ test/java/org/apache/cxf/jaxrs/ext/search/ test/java/org/apache/cxf/jaxrs/ext/search/sql/
Date Mon, 14 May 2012 09:43:10 GMT
Author: sergeyb
Date: Mon May 14 09:43:10 2012
New Revision: 1338124

URL: http://svn.apache.org/viewvc?rev=1338124&view=rev
Log:
[CXF-4314] Adding a search bean for capturing the properties with different names

Added:
    cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchBean.java
  (with props)
Modified:
    cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/FiqlParser.java
    cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/PrimitiveSearchCondition.java
    cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SimpleSearchCondition.java
    cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/SearchContextImplTest.java
    cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitorTest.java

Modified: cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/FiqlParser.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/FiqlParser.java?rev=1338124&r1=1338123&r2=1338124&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/FiqlParser.java
(original)
+++ cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/FiqlParser.java
Mon May 14 09:43:10 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/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/PrimitiveSearchCondition.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/PrimitiveSearchCondition.java?rev=1338124&r1=1338123&r2=1338124&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/PrimitiveSearchCondition.java
(original)
+++ cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/PrimitiveSearchCondition.java
Mon May 14 09:43:10 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;
         }

Added: cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchBean.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchBean.java?rev=1338124&view=auto
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchBean.java
(added)
+++ cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchBean.java
Mon May 14 09:43:10 2012
@@ -0,0 +1,57 @@
+/**
+ * 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;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Utility bean for simplifying the way Search expressions can be captured and
+ * subsequently introspected or converted into different language expressions  
+ */
+public class SearchBean {
+    private Map<String, String> values = new HashMap<String, String>(1);
+    
+    public void set(String name, String value) {
+        values.put(name, value);
+    }
+    
+    public String get(String name) {
+        return values.get(name);
+    }
+    
+    public Set<String> getKeySet() {
+        return values.keySet();
+    }
+    
+    @Override
+    public int hashCode() {
+        return values.hashCode();
+    }
+    
+    @Override
+    public boolean equals(Object o) {
+        if (o instanceof SearchBean) {
+            return values.equals(((SearchBean)o).values);
+        } else {
+            return false;
+        }
+    }
+}

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

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

Modified: cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SimpleSearchCondition.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SimpleSearchCondition.java?rev=1338124&r1=1338123&r2=1338124&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SimpleSearchCondition.java
(original)
+++ cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SimpleSearchCondition.java
Mon May 14 09:43:10 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/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/SearchContextImplTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/SearchContextImplTest.java?rev=1338124&r1=1338123&r2=1338124&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/SearchContextImplTest.java
(original)
+++ cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/SearchContextImplTest.java
Mon May 14 09:43:10 2012
@@ -35,6 +35,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());
@@ -71,4 +77,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/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitorTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitorTest.java?rev=1338124&r1=1338123&r2=1338124&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitorTest.java
(original)
+++ cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitorTest.java
Mon May 14 09:43:10 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