cxf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From serg...@apache.org
Subject svn commit: r1520298 - in /cxf/trunk/rt/rs/extensions/search/src: main/java/org/apache/cxf/jaxrs/ext/search/ main/java/org/apache/cxf/jaxrs/ext/search/jpa/ main/java/org/apache/cxf/jaxrs/ext/search/lucene/ main/java/org/apache/cxf/jaxrs/ext/search/sql/...
Date Thu, 05 Sep 2013 13:23:32 GMT
Author: sergeyb
Date: Thu Sep  5 13:23:31 2013
New Revision: 1520298

URL: http://svn.apache.org/r1520298
Log:
[CXF-5253] Updating JPA visitor to support exact match for non wildcard strings by default

Added:
    cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/SearchUtilsTest.java
  (with props)
Modified:
    cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchUtils.java
    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/lucene/LuceneQueryVisitor.java
    cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitor.java
    cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/visitor/AbstractSearchConditionVisitor.java

Modified: cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchUtils.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchUtils.java?rev=1520298&r1=1520297&r2=1520298&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchUtils.java
(original)
+++ cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/SearchUtils.java
Thu Sep  5 13:23:31 2013
@@ -52,6 +52,20 @@ public final class SearchUtils {
         return visitor.getQuery();
     }
     
+    public static String toSqlWildcardString(String value, boolean alwaysWildcard) {
+        if (!value.contains("*")) {
+            return alwaysWildcard ? "%" + value + "%" : value;
+        }
+        
+        if (value.startsWith("*")) { 
+            value = "%" + value.substring(1);
+        }
+        if (value.endsWith("*")) { 
+            value = value.substring(0, value.length() - 1) + "%";
+        }
+        return value;
+    }
+    
     public static void startSqlQuery(StringBuilder sb, 
                                      String table,
                                      String tableAlias,

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=1520298&r1=1520297&r2=1520298&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
Thu Sep  5 13:23:31 2013
@@ -40,6 +40,7 @@ import org.apache.cxf.jaxrs.ext.search.C
 import org.apache.cxf.jaxrs.ext.search.OrSearchCondition;
 import org.apache.cxf.jaxrs.ext.search.PrimitiveStatement;
 import org.apache.cxf.jaxrs.ext.search.SearchCondition;
+import org.apache.cxf.jaxrs.ext.search.SearchUtils;
 import org.apache.cxf.jaxrs.ext.search.collections.CollectionCheckInfo;
 import org.apache.cxf.jaxrs.ext.search.visitor.AbstractSearchConditionVisitor;
 
@@ -196,11 +197,12 @@ public abstract class AbstractJPATypedQu
             break;
         case EQUALS:
             if (clazz.equals(String.class)) {
-                String theValue = value.toString();
-                if (theValue.contains("*")) {
-                    theValue = ((String)value).replaceAll("\\*", "");
+                String theValue = SearchUtils.toSqlWildcardString(value.toString(), isWildcardStringMatch());
+                if (theValue.contains("%")) {
+                    pred = builder.like((Expression<String>)exp, theValue);
+                } else {
+                    pred = builder.equal(exp, clazz.cast(value));
                 }
-                pred = builder.like((Expression<String>)exp, "%" + theValue + "%");
             } else {
                 pred = builder.equal(exp, clazz.cast(value));
             }

Modified: cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/lucene/LuceneQueryVisitor.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/lucene/LuceneQueryVisitor.java?rev=1520298&r1=1520297&r2=1520298&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/lucene/LuceneQueryVisitor.java
(original)
+++ cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/lucene/LuceneQueryVisitor.java
Thu Sep  5 13:23:31 2013
@@ -140,18 +140,18 @@ public class LuceneQueryVisitor<T> exten
         Query query = null;
         if (cls == String.class) {
             String strValue = value.toString();
-            int wildCardIndex = strValue.indexOf('*'); 
+            boolean isWildCard = strValue.contains("*") || super.isWildcardStringMatch();

             
             String theContentsFieldName = getContentsFieldName(name);
             if (theContentsFieldName == null) {
                 Term term = new Term(name, strValue);
                 
-                if (wildCardIndex == -1) {
+                if (!isWildCard) {
                     query = new TermQuery(term);
                 } else {
                     query = new WildcardQuery(term);
                 } 
-            } else if (wildCardIndex == -1) {
+            } else if (!isWildCard) {
                 PhraseQuery pquery = new PhraseQuery();
                 pquery.add(new Term(theContentsFieldName, name));
                 pquery.add(new Term(theContentsFieldName, strValue));

Modified: cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitor.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitor.java?rev=1520298&r1=1520297&r2=1520298&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitor.java
(original)
+++ cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/sql/SQLPrinterVisitor.java
Thu Sep  5 13:23:31 2013
@@ -70,16 +70,16 @@ public class SQLPrinterVisitor<T> extend
                 String name = getRealPropertyName(statement.getProperty());
                 String value = getPropertyValue(name, statement.getValue());
                 validatePropertyValue(name, value);
-                String rvalStr = value.replaceAll("\\*", "%");
                 
+                value = SearchUtils.toSqlWildcardString(value, isWildcardStringMatch());
                                 
                 if (tableAlias != null) {
                     name = tableAlias + "." + name;
                 }
                 
                 sb.append(name).append(" ").append(
-                            SearchUtils.conditionTypeToSqlOperator(sc.getConditionType(),
rvalStr))
-                            .append(" ").append("'").append(rvalStr).append("'");
+                            SearchUtils.conditionTypeToSqlOperator(sc.getConditionType(),
value))
+                            .append(" ").append("'").append(value).append("'");
             }
         } else {
             boolean first = true;

Modified: cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/visitor/AbstractSearchConditionVisitor.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/visitor/AbstractSearchConditionVisitor.java?rev=1520298&r1=1520297&r2=1520298&view=diff
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/visitor/AbstractSearchConditionVisitor.java
(original)
+++ cxf/trunk/rt/rs/extensions/search/src/main/java/org/apache/cxf/jaxrs/ext/search/visitor/AbstractSearchConditionVisitor.java
Thu Sep  5 13:23:31 2013
@@ -37,6 +37,7 @@ public abstract class AbstractSearchCond
     private Map<String, String> fieldMap;
     private Map<String, Class<?>> primitiveFieldTypeMap;
     private PropertyValidator<Object> validator;
+    private boolean wildcardStringMatch;
     
     protected AbstractSearchConditionVisitor(Map<String, String> fieldMap) {
         this.fieldMap = fieldMap;
@@ -169,4 +170,12 @@ public abstract class AbstractSearchCond
     public void setValidator(PropertyValidator<Object> validator) {
         this.validator = validator;
     }
+
+    public boolean isWildcardStringMatch() {
+        return wildcardStringMatch;
+    }
+
+    public void setWildcardStringMatch(boolean wildcardStringMatch) {
+        this.wildcardStringMatch = wildcardStringMatch;
+    }
 }

Added: cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/SearchUtilsTest.java
URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/SearchUtilsTest.java?rev=1520298&view=auto
==============================================================================
--- cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/SearchUtilsTest.java
(added)
+++ cxf/trunk/rt/rs/extensions/search/src/test/java/org/apache/cxf/jaxrs/ext/search/SearchUtilsTest.java
Thu Sep  5 13:23:31 2013
@@ -0,0 +1,55 @@
+/**
+ * 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 org.junit.Assert;
+import org.junit.Test;
+
+public class SearchUtilsTest extends Assert {
+
+    @Test
+    public void testSqlWildcardString() {
+        assertEquals("abc", SearchUtils.toSqlWildcardString("abc", false));
+    }
+    
+    @Test
+    public void testSqlWildcardStringAlways() {
+        assertEquals("%abc%", SearchUtils.toSqlWildcardString("abc", true));
+    }
+    
+    @Test
+    public void testSqlWildcardString2() {
+        assertEquals("%abc", SearchUtils.toSqlWildcardString("*abc", false));
+    }
+    
+    @Test
+    public void testSqlWildcardString3() {
+        assertEquals("abc%", SearchUtils.toSqlWildcardString("abc*", false));
+    }
+    
+    @Test
+    public void testSqlWildcardString4() {
+        assertEquals("%abc%", SearchUtils.toSqlWildcardString("*abc*", false));
+    }
+    
+    @Test
+    public void testSqlWildcardString5() {
+        assertEquals("%", SearchUtils.toSqlWildcardString("*", false));
+    }
+}

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

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



Mime
View raw message