← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 20951: add caseSensitive parameter to ContainsOperator, removed old IContainsOperator

 

------------------------------------------------------------
revno: 20951
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2015-11-04 16:50:36 +0700
message:
  add caseSensitive parameter to ContainsOperator, removed old IContainsOperator
removed:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/IContainsOperator.java
modified:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/ContainsOperator.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/query/OperatorTest.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/ContainsOperator.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/ContainsOperator.java	2015-11-04 09:45:59 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/ContainsOperator.java	2015-11-04 09:50:36 +0000
@@ -38,15 +38,25 @@
  */
 public class ContainsOperator extends Operator
 {
-    public ContainsOperator( String arg )
+    private final boolean caseSensitive;
+
+    public ContainsOperator( String arg, boolean caseSensitive )
     {
         super( Typed.from( String.class ), arg );
+        this.caseSensitive = caseSensitive;
     }
 
     @Override
     public Criterion getHibernateCriterion( String propertyName )
     {
-        return Restrictions.like( propertyName, args.get( 0 ), MatchMode.ANYWHERE );
+        if ( caseSensitive )
+        {
+            return Restrictions.like( propertyName, args.get( 0 ), MatchMode.ANYWHERE );
+        }
+        else
+        {
+            return Restrictions.ilike( propertyName, args.get( 0 ), MatchMode.ANYWHERE );
+        }
     }
 
     @Override
@@ -62,7 +72,14 @@
             String s1 = getValue( String.class );
             String s2 = (String) value;
 
-            return s1 != null && s2.contains( s1 );
+            if ( caseSensitive )
+            {
+                return s1 != null && s2.contains( s1 );
+            }
+            else
+            {
+                return s1 != null && s2.toLowerCase().contains( s1.toLowerCase() );
+            }
         }
 
         return false;

=== removed file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/IContainsOperator.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/IContainsOperator.java	2015-11-04 09:45:59 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/operators/IContainsOperator.java	1970-01-01 00:00:00 +0000
@@ -1,70 +0,0 @@
-package org.hisp.dhis.query.operators;
-
-/*
- * Copyright (c) 2004-2015, University of Oslo
- * All rights reserved.
- *
- * Redistribution and use in source and binary forms, with or without
- * modification, are permitted provided that the following conditions are met:
- * Redistributions of source code must retain the above copyright notice, this
- * list of conditions and the following disclaimer.
- *
- * Redistributions in binary form must reproduce the above copyright notice,
- * this list of conditions and the following disclaimer in the documentation
- * and/or other materials provided with the distribution.
- * Neither the name of the HISP project nor the names of its contributors may
- * be used to endorse or promote products derived from this software without
- * specific prior written permission.
- *
- * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
- * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
- * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
- * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
- * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
- * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
- * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
- * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
- * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
- */
-
-import org.hibernate.criterion.Criterion;
-import org.hibernate.criterion.MatchMode;
-import org.hibernate.criterion.Restrictions;
-import org.hisp.dhis.query.Typed;
-
-/**
- * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
- */
-public class IContainsOperator extends Operator
-{
-    public IContainsOperator( String arg )
-    {
-        super( Typed.from( String.class ), arg );
-    }
-
-    @Override
-    public Criterion getHibernateCriterion( String propertyName )
-    {
-        return Restrictions.ilike( propertyName, args.get( 0 ), MatchMode.ANYWHERE );
-    }
-
-    @Override
-    public boolean test( Object value )
-    {
-        if ( args.isEmpty() || value == null )
-        {
-            return false;
-        }
-
-        if ( String.class.isInstance( value ) )
-        {
-            String s1 = getValue( String.class );
-            String s2 = (String) value;
-
-            return s1 != null && s2.toLowerCase().contains( s1.toLowerCase() );
-        }
-
-        return false;
-    }
-}

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/query/OperatorTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/query/OperatorTest.java	2015-11-04 09:45:59 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/query/OperatorTest.java	2015-11-04 09:50:36 +0000
@@ -29,14 +29,13 @@
  */
 
 import org.hisp.dhis.query.operators.BetweenOperator;
+import org.hisp.dhis.query.operators.ContainsOperator;
 import org.hisp.dhis.query.operators.EqualOperator;
 import org.hisp.dhis.query.operators.GreaterEqualOperator;
 import org.hisp.dhis.query.operators.GreaterThanOperator;
-import org.hisp.dhis.query.operators.IContainsOperator;
 import org.hisp.dhis.query.operators.InOperator;
 import org.hisp.dhis.query.operators.LessEqualOperator;
 import org.hisp.dhis.query.operators.LessThanOperator;
-import org.hisp.dhis.query.operators.ContainsOperator;
 import org.hisp.dhis.query.operators.NotEqualOperator;
 import org.hisp.dhis.query.operators.NotNullOperator;
 import org.hisp.dhis.query.operators.NullOperator;
@@ -210,7 +209,7 @@
     @Test
     public void testILikeValidTypes()
     {
-        IContainsOperator operator = new IContainsOperator( "operator" );
+        ContainsOperator operator = new ContainsOperator( "operator", false );
 
         assertTrue( operator.isValid( String.class ) );
         assertFalse( operator.isValid( Number.class ) );
@@ -222,7 +221,7 @@
     @Test
     public void testILike()
     {
-        IContainsOperator operator = new IContainsOperator( "operator" );
+        ContainsOperator operator = new ContainsOperator( "operator", false );
 
         assertTrue( operator.test( "operator" ) );
         assertTrue( operator.test( "OPERATOR" ) );
@@ -232,7 +231,7 @@
     @Test
     public void testLikeValidTypes()
     {
-        ContainsOperator operator = new ContainsOperator( "operator" );
+        ContainsOperator operator = new ContainsOperator( "operator", true );
 
         assertTrue( operator.isValid( String.class ) );
         assertFalse( operator.isValid( Number.class ) );
@@ -244,7 +243,7 @@
     @Test
     public void testLike()
     {
-        ContainsOperator operator = new ContainsOperator( "operator" );
+        ContainsOperator operator = new ContainsOperator( "operator", true );
 
         assertTrue( operator.test( "operator" ) );
         assertFalse( operator.test( "OPERATOR" ) );