← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 18534: implemented Criteria optimizations for IN-filter

 

------------------------------------------------------------
revno: 18534
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Mon 2015-03-09 14:27:50 +0530
message:
  implemented Criteria optimizations for IN-filter
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/query/Restrictions.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/CriteriaQueryEngine.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/DefaultQueryService.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-api/src/main/java/org/hisp/dhis/query/Restrictions.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/query/Restrictions.java	2015-02-23 13:06:26 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/query/Restrictions.java	2015-03-09 08:57:50 +0000
@@ -28,6 +28,8 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import java.util.Collection;
+
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
@@ -84,6 +86,11 @@
         return new Restriction( path, Operator.IN, values );
     }
 
+    public static Restriction in( String path, Collection<?> values )
+    {
+        return new Restriction( path, Operator.IN, values );
+    }
+
     private Restrictions()
     {
     }

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/CriteriaQueryEngine.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/CriteriaQueryEngine.java	2015-02-25 20:12:45 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/CriteriaQueryEngine.java	2015-03-09 08:57:50 +0000
@@ -28,13 +28,6 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import org.apache.commons.lang3.time.DateUtils;
 import org.hibernate.Criteria;
 import org.hibernate.criterion.Criterion;
 import org.hibernate.criterion.Disjunction;
@@ -43,8 +36,16 @@
 import org.hisp.dhis.hibernate.HibernateGenericStore;
 import org.hisp.dhis.schema.Property;
 import org.hisp.dhis.schema.Schema;
+import org.hisp.dhis.system.util.DateUtils;
 import org.springframework.beans.factory.annotation.Autowired;
 
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Date;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
  */
@@ -150,6 +151,11 @@
             parameters.add( getValue( property, parameter ) );
         }
 
+        if ( parameters.isEmpty() )
+        {
+            return null;
+        }
+
         switch ( restriction.getOperator() )
         {
             case EQ:
@@ -190,7 +196,12 @@
             }
             case IN:
             {
-                return Restrictions.in( property.getFieldName(), parameters );
+                if ( !Collection.class.isInstance( parameters.get( 0 ) ) || ((Collection) parameters.get( 0 )).isEmpty() )
+                {
+                    return null;
+                }
+
+                return Restrictions.in( property.getFieldName(), (Collection) parameters.get( 0 ) );
             }
         }
 
@@ -300,22 +311,7 @@
         }
         else if ( Date.class.isAssignableFrom( klass ) )
         {
-            try
-            {
-                return DateUtils.parseDate( value,
-                    "yyyy-MM-dd'T'HH:mm:ssZ",
-                    "yyyy-MM-dd'T'HH:mm:ss",
-                    "yyyy-MM-dd'T'HH:mm",
-                    "yyyy-MM-dd'T'HH",
-                    "yyyy-MM-dd",
-                    "yyyy-MM",
-                    "yyyyMMdd",
-                    "yyyyMM",
-                    "yyyy" );
-            }
-            catch ( Exception ignored )
-            {
-            }
+            return DateUtils.parseDate( value );
         }
 
         return null;

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/DefaultQueryService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/DefaultQueryService.java	2015-02-23 11:17:55 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/DefaultQueryService.java	2015-03-09 08:57:50 +0000
@@ -28,6 +28,7 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+import com.google.common.collect.Lists;
 import org.hisp.dhis.common.IdentifiableObject;
 import org.hisp.dhis.schema.Property;
 import org.hisp.dhis.schema.Schema;
@@ -35,6 +36,7 @@
 import org.springframework.beans.factory.annotation.Autowired;
 
 import java.util.ArrayList;
+import java.util.Collection;
 import java.util.Iterator;
 import java.util.List;
 
@@ -190,8 +192,24 @@
             {
                 return Restrictions.ilike( split[0], "%" + split[2] + "%" );
             }
+            case "in":
+            {
+                return Restrictions.in( split[0], parseInOperator( split[2] ) );
+            }
         }
 
         return null;
     }
+
+    private Collection<String> parseInOperator( String value )
+    {
+        if ( value == null || !value.startsWith( "[" ) || !value.endsWith( "]" ) )
+        {
+            return Lists.newArrayList();
+        }
+
+        String[] split = value.substring( 1, value.length() - 1 ).split( "," );
+
+        return Lists.newArrayList( split );
+    }
 }