← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 18585: add criteria opt for in and eq queries

 

------------------------------------------------------------
revno: 18585
committer: Morten Olav Hansen <mortenoh@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2015-03-12 00:53:05 +0530
message:
  add criteria opt for in and eq queries
added:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/QueryUtils.java
modified:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/DefaultQueryService.java
  dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/objectfilter/ops/Op.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/DefaultQueryService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/DefaultQueryService.java	2015-03-10 10:03:22 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/DefaultQueryService.java	2015-03-11 19:23:05 +0000
@@ -92,6 +92,8 @@
         List<Restriction> restrictions = new ArrayList<>();
         List<String> candidates = getRestrictionCandidates( schema, filters );
 
+        System.err.println( "candidates: " + candidates );
+
         if ( candidates.isEmpty() )
         {
             return restrictions;
@@ -141,7 +143,7 @@
 
         Property property = schema.getProperty( split[0] );
 
-        if ( property == null || !property.isPersisted() || !property.isSimple() || Enum.class.isAssignableFrom( property.getKlass() ) )
+        if ( property == null || !property.isPersisted() || !property.isSimple() )
         {
             return null;
         }
@@ -150,39 +152,39 @@
         {
             case "eq":
             {
-                return Restrictions.eq( split[0], split[2] );
+                return Restrictions.eq( split[0], QueryUtils.getValue( property.getKlass(), split[2] ) );
             }
             case "ne":
             {
-                return Restrictions.ne( split[0], split[2] );
+                return Restrictions.ne( split[0], QueryUtils.getValue( property.getKlass(), split[2] ) );
             }
             case "neq":
             {
-                return Restrictions.ne( split[0], split[2] );
+                return Restrictions.ne( split[0], QueryUtils.getValue( property.getKlass(), split[2] ) );
             }
             case "gt":
             {
-                return Restrictions.gt( split[0], split[2] );
+                return Restrictions.gt( split[0], QueryUtils.getValue( property.getKlass(), split[2] ) );
             }
             case "lt":
             {
-                return Restrictions.lt( split[0], split[2] );
+                return Restrictions.lt( split[0], QueryUtils.getValue( property.getKlass(), split[2] ) );
             }
             case "gte":
             {
-                return Restrictions.ge( split[0], split[2] );
+                return Restrictions.ge( split[0], QueryUtils.getValue( property.getKlass(), split[2] ) );
             }
             case "ge":
             {
-                return Restrictions.ge( split[0], split[2] );
+                return Restrictions.ge( split[0], QueryUtils.getValue( property.getKlass(), split[2] ) );
             }
             case "lte":
             {
-                return Restrictions.le( split[0], split[2] );
+                return Restrictions.le( split[0], QueryUtils.getValue( property.getKlass(), split[2] ) );
             }
             case "le":
             {
-                return Restrictions.le( split[0], split[2] );
+                return Restrictions.le( split[0], QueryUtils.getValue( property.getKlass(), split[2] ) );
             }
             case "like":
             {

=== added file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/QueryUtils.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/QueryUtils.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/query/QueryUtils.java	2015-03-11 19:23:05 +0000
@@ -0,0 +1,123 @@
+package org.hisp.dhis.query;
+
+/*
+ * 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 com.google.common.base.Enums;
+import com.google.common.base.Optional;
+import com.google.common.collect.Lists;
+import org.hisp.dhis.system.util.DateUtils;
+
+import java.util.Collection;
+import java.util.Date;
+
+/**
+ * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
+ */
+public final class QueryUtils
+{
+    @SuppressWarnings( "unchecked" )
+    static public <T> T getValue( Class<?> klass, String value )
+    {
+        if ( klass.isInstance( value ) )
+        {
+            return (T) value;
+        }
+
+        if ( Boolean.class.isAssignableFrom( klass ) )
+        {
+            try
+            {
+                return (T) Boolean.valueOf( value );
+            }
+            catch ( Exception ignored )
+            {
+            }
+        }
+        else if ( Integer.class.isAssignableFrom( klass ) )
+        {
+            try
+            {
+                return (T) Integer.valueOf( value );
+            }
+            catch ( Exception ignored )
+            {
+            }
+        }
+        else if ( Float.class.isAssignableFrom( klass ) )
+        {
+            try
+            {
+                return (T) Float.valueOf( value );
+            }
+            catch ( Exception ignored )
+            {
+            }
+        }
+        else if ( Double.class.isAssignableFrom( klass ) )
+        {
+            try
+            {
+                return (T) Double.valueOf( value );
+            }
+            catch ( Exception ignored )
+            {
+            }
+        }
+        else if ( Date.class.isAssignableFrom( klass ) )
+        {
+            return (T) DateUtils.parseDate( value );
+        }
+        else if ( Enum.class.isAssignableFrom( klass ) )
+        {
+            Optional<? extends Enum> enumValue = Enums.getIfPresent( (Class<? extends Enum>) klass, value );
+
+            if ( enumValue.isPresent() )
+            {
+                return (T) enumValue.get();
+            }
+        }
+        else if ( Collection.class.isAssignableFrom( klass ) )
+        {
+            if ( value == null || !value.startsWith( "[" ) || !value.endsWith( "]" ) )
+            {
+                return null;
+            }
+
+            String[] split = value.substring( 1, value.length() - 1 ).split( "," );
+            return (T) Lists.newArrayList( split );
+        }
+
+        return null;
+    }
+
+    private QueryUtils()
+    {
+
+    }
+}

=== modified file 'dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/objectfilter/ops/Op.java'
--- dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/objectfilter/ops/Op.java	2015-03-09 08:11:37 +0000
+++ dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/objectfilter/ops/Op.java	2015-03-11 19:23:05 +0000
@@ -28,11 +28,7 @@
  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
-import com.google.common.collect.Lists;
-import org.hisp.dhis.system.util.DateUtils;
-
-import java.util.Collection;
-import java.util.Date;
+import org.hisp.dhis.query.QueryUtils;
 
 /**
  * @author Morten Olav Hansen <mortenoh@xxxxxxxxx>
@@ -56,70 +52,9 @@
         return value;
     }
 
-    @SuppressWarnings( "unchecked" )
     public <T> T getValue( Class<?> klass )
     {
-        if ( klass.isInstance( value ) )
-        {
-            return (T) value;
-        }
-
-        if ( Boolean.class.isAssignableFrom( klass ) )
-        {
-            try
-            {
-                return (T) Boolean.valueOf( value );
-            }
-            catch ( Exception ignored )
-            {
-            }
-        }
-        else if ( Integer.class.isAssignableFrom( klass ) )
-        {
-            try
-            {
-                return (T) Integer.valueOf( value );
-            }
-            catch ( Exception ignored )
-            {
-            }
-        }
-        else if ( Float.class.isAssignableFrom( klass ) )
-        {
-            try
-            {
-                return (T) Float.valueOf( value );
-            }
-            catch ( Exception ignored )
-            {
-            }
-        }
-        else if ( Double.class.isAssignableFrom( klass ) )
-        {
-            try
-            {
-                return (T) Double.valueOf( value );
-            }
-            catch ( Exception ignored )
-            {
-            }
-        }
-        else if ( Date.class.isAssignableFrom( klass ) )
-        {
-            return (T) DateUtils.parseDate( value );
-        }
-        else if ( Collection.class.isAssignableFrom( klass ) )
-        {
-            if ( value == null || !value.startsWith( "[" ) || !value.endsWith( "]" ) )
-            {
-                return null;
-            }
-
-            String[] split = value.substring( 1, value.length() - 1 ).split( "," );
-            return (T) Lists.newArrayList( split );
-        }
-
-        return null;
+        return QueryUtils.getValue( klass, value );
     }
 
     public abstract OpStatus evaluate( Object object );