cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1426286 - in /cxf/branches/2.6.x-fixes: ./ rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/ rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/client/ rt/rs/extensions/search/src/test/java/org/apach...
Date Thu, 27 Dec 2012 18:37:04 GMT
Author: sergeyb
Date: Thu Dec 27 18:37:04 2012
New Revision: 1426286

URL: http://svn.apache.org/viewvc?rev=1426286&view=rev
Log:
Merged revisions 1426284 via svnmerge from 
https://svn.apache.org/repos/asf/cxf/trunk

........
  r1426284 | sergeyb | 2012-12-27 18:31:53 +0000 (Thu, 27 Dec 2012) | 1 line
  
  Optional conversion of plain queries to FIQL queries
........

Modified:
    cxf/branches/2.6.x-fixes/   (props changed)
    cxf/branches/2.6.x-fixes/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchContextImpl.java
    cxf/branches/2.6.x-fixes/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/client/FiqlSearchConditionBuilder.java
    cxf/branches/2.6.x-fixes/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/client/PartialCondition.java
    cxf/branches/2.6.x-fixes/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/SearchContextImplTest.java

Propchange: cxf/branches/2.6.x-fixes/
------------------------------------------------------------------------------
  Merged /cxf/trunk:r1426284

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

Modified: cxf/branches/2.6.x-fixes/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchContextImpl.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.6.x-fixes/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchContextImpl.java?rev=1426286&r1=1426285&r2=1426286&view=diff
==============================================================================
--- cxf/branches/2.6.x-fixes/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchContextImpl.java
(original)
+++ cxf/branches/2.6.x-fixes/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchContextImpl.java
Thu Dec 27 18:37:04 2012
@@ -19,7 +19,9 @@
 
 package org.apache.cxf.jaxrs.ext.search;
 
+import java.util.ArrayList;
 import java.util.LinkedHashMap;
+import java.util.List;
 import java.util.Map;
 import java.util.logging.Logger;
 
@@ -27,15 +29,19 @@ import javax.ws.rs.core.MultivaluedMap;
 
 import org.apache.cxf.common.logging.LogUtils;
 import org.apache.cxf.helpers.CastUtils;
+import org.apache.cxf.jaxrs.ext.search.client.CompleteCondition;
+import org.apache.cxf.jaxrs.ext.search.client.SearchConditionBuilder;
 import org.apache.cxf.jaxrs.ext.search.fiql.FiqlParser;
 import org.apache.cxf.jaxrs.utils.InjectionUtils;
 import org.apache.cxf.jaxrs.utils.JAXRSUtils;
 import org.apache.cxf.message.Message;
+import org.apache.cxf.message.MessageUtils;
 
 public class SearchContextImpl implements SearchContext {
 
     public static final String SEARCH_QUERY = "_search";
     public static final String SHORT_SEARCH_QUERY = "_s";
+    private static final String USE_PLAIN_QUERY_PARAMETERS = "search.use.plain.queries";
     private static final Logger LOG = LogUtils.getL7dLogger(SearchContextImpl.class);
     private Message message;
     
@@ -96,20 +102,56 @@ public class SearchContextImpl implement
     public String getSearchExpression() {
         
         String queryStr = (String)message.get(Message.QUERY_STRING);
-        if (queryStr != null 
-            && (queryStr.contains(SHORT_SEARCH_QUERY) || queryStr.contains(SEARCH_QUERY)))
{
+        if (queryStr != null) { 
             MultivaluedMap<String, String> params = 
                 JAXRSUtils.getStructuredParams(queryStr, "&", true, false);
-            if (params.containsKey(SHORT_SEARCH_QUERY)) {
-                return params.getFirst(SHORT_SEARCH_QUERY);
-            } else {
-                return params.getFirst(SEARCH_QUERY);
+            if (queryStr.contains(SHORT_SEARCH_QUERY) || queryStr.contains(SEARCH_QUERY))
{
+                if (params.containsKey(SHORT_SEARCH_QUERY)) {
+                    return params.getFirst(SHORT_SEARCH_QUERY);
+                } else {
+                    return params.getFirst(SEARCH_QUERY);
+                }
+            } else if (MessageUtils.isTrue(message.getContextualProperty(USE_PLAIN_QUERY_PARAMETERS)))
{
+                return convertPlainQueriesToFiqlExp(params);     
             }
+        }
+        return null;
+        
+    }
+    
+    private String convertPlainQueriesToFiqlExp(MultivaluedMap<String, String> params)
{
+        SearchConditionBuilder builder = SearchConditionBuilder.instance();
+        List<CompleteCondition> list = new ArrayList<CompleteCondition>(params.size());
+        
+        for (Map.Entry<String, List<String>> entry : params.entrySet()) {
+            list.add(getOrCondition(builder, entry));
+        }
+        return builder.and(list).query();
+    }
+    
+    private CompleteCondition getOrCondition(SearchConditionBuilder builder,
+                                             Map.Entry<String, List<String>>
entry) {
+        String key = entry.getKey();
+        ConditionType ct = null;
+        if (key.endsWith("From")) { 
+            ct = ConditionType.GREATER_OR_EQUALS;
+            key = key.substring(0, key.length() - 4);
+        } else if (key.endsWith("Till")) {
+            ct = ConditionType.LESS_OR_EQUALS;
+            key = key.substring(0, key.length() - 4);
         } else {
-            return null;
+            ct = ConditionType.EQUALS;
+        }
+        
+        List<CompleteCondition> list = new ArrayList<CompleteCondition>(entry.getValue().size());
+        for (String value : entry.getValue()) {
+            list.add(builder.is(key).comparesTo(ct, value));
         }
+        return builder.or(list);
     }
     
+    
+    
     private <T> SearchConditionParser<T> getParser(Class<T> cls, 
                                                    Map<String, String> beanProperties,
                                                    Map<String, String> parserProperties)
{

Modified: cxf/branches/2.6.x-fixes/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/client/FiqlSearchConditionBuilder.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.6.x-fixes/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/client/FiqlSearchConditionBuilder.java?rev=1426286&r1=1426285&r2=1426286&view=diff
==============================================================================
--- cxf/branches/2.6.x-fixes/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/client/FiqlSearchConditionBuilder.java
(original)
+++ cxf/branches/2.6.x-fixes/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/client/FiqlSearchConditionBuilder.java
Thu Dec 27 18:37:04 2012
@@ -21,6 +21,8 @@ package org.apache.cxf.jaxrs.ext.search.
 import java.text.DateFormat;
 import java.util.Collections;
 import java.util.Date;
+import java.util.Iterator;
+import java.util.List;
 import java.util.Map;
 
 import javax.xml.datatype.Duration;
@@ -77,6 +79,14 @@ public class FiqlSearchConditionBuilder 
                                  CompleteCondition... cn) {
         return new Builder(properties).and(c1, c2, cn);
     }
+    
+    public CompleteCondition and(List<CompleteCondition> conditions) {
+        return new Builder(properties).and(conditions);
+    }
+    
+    public CompleteCondition or(List<CompleteCondition> conditions) {
+        return new Builder(properties).or(conditions);
+    }
 
     public CompleteCondition or(CompleteCondition c1, CompleteCondition c2, CompleteCondition...
cn) {
         return new Builder(properties).or(c1, c2, cn);
@@ -372,6 +382,33 @@ public class FiqlSearchConditionBuilder 
             result += ")";
             return this;
         }
+        
+        public CompleteCondition and(List<CompleteCondition> conditions) {
+            return conditionsList(FiqlParser.AND, conditions);
+        }
+        
+        public CompleteCondition or(List<CompleteCondition> conditions) {
+            return conditionsList(FiqlParser.OR, conditions);
+        }
+        
+        private CompleteCondition conditionsList(String op, List<CompleteCondition>
conditions) {
+            if (conditions.size() == 1) {
+                result += ((Builder)conditions.get(0)).buildPartial(this);
+            } else {
+                result += "(";
+                
+                for (Iterator<CompleteCondition> it = conditions.iterator(); it.hasNext();)
{
+                    result += ((Builder)it.next()).buildPartial(this);
+                    if (it.hasNext()) {
+                        result += op;
+                    }
+                }
+                result += ")";
+            }
+            
+            return this;
+        }
+        
 
         public Property is(String property) {
             Builder b = new Builder(this);

Modified: cxf/branches/2.6.x-fixes/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/client/PartialCondition.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.6.x-fixes/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/client/PartialCondition.java?rev=1426286&r1=1426285&r2=1426286&view=diff
==============================================================================
--- cxf/branches/2.6.x-fixes/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/client/PartialCondition.java
(original)
+++ cxf/branches/2.6.x-fixes/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/client/PartialCondition.java
Thu Dec 27 18:37:04 2012
@@ -18,6 +18,8 @@
  */
 package org.apache.cxf.jaxrs.ext.search.client;
 
+import java.util.List;
+
 /**
  * Part of fluent interface of {@link SearchConditionBuilder}.
  */
@@ -30,4 +32,10 @@ public interface PartialCondition {
 
     /** Disjunct multiple expressions */
     CompleteCondition or(CompleteCondition c1, CompleteCondition c2, CompleteCondition...
cn);
+    
+    /** Conjunct multiple expressions */
+    CompleteCondition and(List<CompleteCondition> conditions);
+    
+    /** Disjunct multiple expressions */
+    CompleteCondition or(List<CompleteCondition> conditions);
 }

Modified: cxf/branches/2.6.x-fixes/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/SearchContextImplTest.java
URL: http://svn.apache.org/viewvc/cxf/branches/2.6.x-fixes/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/SearchContextImplTest.java?rev=1426286&r1=1426285&r2=1426286&view=diff
==============================================================================
--- cxf/branches/2.6.x-fixes/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/SearchContextImplTest.java
(original)
+++ cxf/branches/2.6.x-fixes/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/SearchContextImplTest.java
Thu Dec 27 18:37:04 2012
@@ -30,6 +30,53 @@ import org.junit.Test;
 public class SearchContextImplTest extends Assert {
 
     @Test
+    public void testPlainQuery1() {
+        Message m = new MessageImpl();
+        m.put("search.use.plain.queries", true);
+        m.put(Message.QUERY_STRING, "a=b");
+        String exp = new SearchContextImpl(m).getSearchExpression();
+        assertEquals("a==b", exp);
+    }
+    
+    @Test
+    public void testPlainQuery2() {
+        Message m = new MessageImpl();
+        m.put("search.use.plain.queries", true);
+        m.put(Message.QUERY_STRING, "a=b&a=b1");
+        String exp = new SearchContextImpl(m).getSearchExpression();
+        assertEquals("(a==b,a==b1)", exp);
+    }
+    
+    @Test
+    public void testPlainQuery3() {
+        Message m = new MessageImpl();
+        m.put("search.use.plain.queries", true);
+        m.put(Message.QUERY_STRING, "a=b&c=d");
+        String exp = new SearchContextImpl(m).getSearchExpression();
+        assertEquals("(a==b;c==d)", exp);
+    }
+    
+    @Test
+    public void testPlainQuery4() {
+        Message m = new MessageImpl();
+        m.put("search.use.plain.queries", true);
+        m.put(Message.QUERY_STRING, "a=b&a=b2&c=d&f=g");
+        String exp = new SearchContextImpl(m).getSearchExpression();
+        assertEquals("((a==b,a==b2);c==d;f==g)", exp);
+    }
+    
+    @Test
+    public void testPlainQuery5() {
+        Message m = new MessageImpl();
+        m.put("search.use.plain.queries", true);
+        m.put(Message.QUERY_STRING, "aFrom=1&aTill=3");
+        String exp = new SearchContextImpl(m).getSearchExpression();
+        assertEquals("(a=ge=1;a=le=3)", exp);
+    }
+    
+    
+    
+    @Test
     public void testFiqlSearchCondition() {
         doTestFiqlSearchCondition(
             SearchContextImpl.SEARCH_QUERY + "=" + "name==CXF%20Rocks;id=gt=123");



Mime
View raw message