← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 19878: Event analytics / program indicators. Impl support for conditionals / function d2:condition( bool...

 

------------------------------------------------------------
revno: 19878
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2015-08-28 00:22:39 +0200
message:
  Event analytics / program indicators. Impl support for conditionals / function d2:condition( boolean-expr, true-val, false-val ).
added:
  dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/sqlfunc/ConditionalSqlFunction.java
modified:
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/program/DefaultProgramIndicatorService.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/program/ProgramIndicatorServiceTest.java
  dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/sqlfunc/OneIfZeroOrPositiveSqlFunction.java
  dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/sqlfunc/SqlFunction.java
  dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/sqlfunc/ZeroIfNegativeSqlFunction.java
  dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/util/ExpressionFunctions.java
  dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/util/ExpressionUtils.java
  dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/util/TextUtils.java
  dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/ExpressionUtilsTest.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/program/DefaultProgramIndicatorService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/program/DefaultProgramIndicatorService.java	2015-08-27 17:18:00 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/program/DefaultProgramIndicatorService.java	2015-08-27 22:22:39 +0000
@@ -38,6 +38,7 @@
 import java.util.Set;
 import java.util.regex.Matcher;
 
+import org.hisp.dhis.commons.sqlfunc.ConditionalSqlFunction;
 import org.hisp.dhis.commons.sqlfunc.DaysBetweenSqlFunction;
 import org.hisp.dhis.commons.sqlfunc.OneIfZeroOrPositiveSqlFunction;
 import org.hisp.dhis.commons.sqlfunc.SqlFunction;
@@ -75,7 +76,8 @@
     private static final Map<String, SqlFunction> SQL_FUNC_MAP = ImmutableMap.<String, SqlFunction>builder().
         put( ZeroIfNegativeSqlFunction.KEY, new ZeroIfNegativeSqlFunction() ).
         put( OneIfZeroOrPositiveSqlFunction.KEY, new OneIfZeroOrPositiveSqlFunction() ).
-        put( DaysBetweenSqlFunction.KEY, new DaysBetweenSqlFunction() ).build();
+        put( DaysBetweenSqlFunction.KEY, new DaysBetweenSqlFunction() ).
+        put( ConditionalSqlFunction.KEY, new ConditionalSqlFunction() ).build();
     
     // -------------------------------------------------------------------------
     // Dependencies

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/program/ProgramIndicatorServiceTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/program/ProgramIndicatorServiceTest.java	2015-08-27 17:18:00 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/program/ProgramIndicatorServiceTest.java	2015-08-27 22:22:39 +0000
@@ -539,6 +539,16 @@
         assertEquals( expected, programIndicatorService.getAnalyticsSQl( expression ) );        
     }
 
+    @Test
+    public void testGetAnalyticsSqlWithFunctionsD()
+    {
+        String col1 = COL_QUOTE + deA.getUid() + COL_QUOTE;
+        String expected = "case when (" + col1 + " > 3) then '10' else '5' end";
+        String expression = "d2:condition(" + col1 + " > 3,10,5)";
+
+        assertEquals( expected, programIndicatorService.getAnalyticsSQl( expression ) );        
+    }
+
     @Test( expected = IllegalStateException.class )
     public void testGetAnalyticsSqlWithFunctionsInvalid()
     {

=== added file 'dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/sqlfunc/ConditionalSqlFunction.java'
--- dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/sqlfunc/ConditionalSqlFunction.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/sqlfunc/ConditionalSqlFunction.java	2015-08-27 22:22:39 +0000
@@ -0,0 +1,48 @@
+package org.hisp.dhis.commons.sqlfunc;
+
+/*
+ * 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.
+ */
+
+/**
+ * Function which evaluates conditional statements.
+ * 
+ * @author Lars Helge Overland
+ */
+public class ConditionalSqlFunction
+    implements SqlFunction
+{
+    public static final String KEY = "condition";
+
+    @Override
+    public String evaluate( String arg1, String arg2, String arg3 )
+    {
+        String conditional = arg1.replaceAll( "^\"|^'|\"$|'$", "" );
+        
+        return "case when (" + conditional + ") then " + arg2 + " else " + arg3 + " end";
+    }
+}

=== modified file 'dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/sqlfunc/OneIfZeroOrPositiveSqlFunction.java'
--- dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/sqlfunc/OneIfZeroOrPositiveSqlFunction.java	2015-08-27 17:18:00 +0000
+++ dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/sqlfunc/OneIfZeroOrPositiveSqlFunction.java	2015-08-27 22:22:39 +0000
@@ -40,7 +40,7 @@
     public static final String KEY = "oizp";
     
     @Override
-    public String evaluate( String arg1, String arg2, String arg )
+    public String evaluate( String arg1, String arg2, String arg3 )
     {
         return "case when " + arg1 + " >= 0 then 1 else 0 end";
     }

=== modified file 'dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/sqlfunc/SqlFunction.java'
--- dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/sqlfunc/SqlFunction.java	2015-08-27 17:18:00 +0000
+++ dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/sqlfunc/SqlFunction.java	2015-08-27 22:22:39 +0000
@@ -42,5 +42,5 @@
      * 
      * @return the result of the evaluation.
      */
-    String evaluate( String arg1, String arg2, String arg );
+    String evaluate( String arg1, String arg2, String arg3 );
 }

=== modified file 'dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/sqlfunc/ZeroIfNegativeSqlFunction.java'
--- dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/sqlfunc/ZeroIfNegativeSqlFunction.java	2015-08-27 17:18:00 +0000
+++ dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/sqlfunc/ZeroIfNegativeSqlFunction.java	2015-08-27 22:22:39 +0000
@@ -40,7 +40,7 @@
     public static final String KEY = "zing";
     
     @Override
-    public String evaluate( String arg1, String arg2, String arg )
+    public String evaluate( String arg1, String arg2, String arg3 )
     {
         return "case when " + arg1 + " < 0 then 0 else " + arg1 + " end";
     }

=== modified file 'dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/util/ExpressionFunctions.java'
--- dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/util/ExpressionFunctions.java	2015-08-27 20:45:58 +0000
+++ dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/util/ExpressionFunctions.java	2015-08-27 22:22:39 +0000
@@ -71,6 +71,20 @@
         
         return ( value.doubleValue() >= 0d ) ? 1d : 0d;
     }
+
+    /**
+     * Functions which will return the true value if the condition is true, false
+     * value if not.
+     * 
+     * @param condititon the condition.
+     * @param trueValue the true value.
+     * @param falseValue the false value.
+     * @return a String.
+     */
+    public static Object condition( String condititon, Object trueValue, Object falseValue )
+    {
+        return ExpressionUtils.isTrue( condititon, null ) ? trueValue : falseValue;        
+    }
     
     /**
      * Function which will return the number of days between the two given dates.

=== modified file 'dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/util/ExpressionUtils.java'
--- dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/util/ExpressionUtils.java	2015-08-27 20:45:58 +0000
+++ dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/util/ExpressionUtils.java	2015-08-27 22:22:39 +0000
@@ -104,35 +104,18 @@
     {
         Object result = evaluate( expression, vars );
         
-        if ( result == null || !isNumeric( String.valueOf( result ) ) )
-        {
-            throw new IllegalStateException( "Result must be not null and numeric: " + result );
+        if ( result == null )
+        {
+            throw new IllegalStateException( "Result must be not null" );
+        }
+        
+        if ( !isNumeric( String.valueOf( result ) ) )
+        {
+            throw new IllegalStateException( "Result must be numeric: " + result + ", " + result.getClass() );
         }
         
         return Double.valueOf( String.valueOf( result ) );
     }
-
-    /**
-     * Evaluates the given expression. The given variables will be substituted 
-     * in the expression. Converts the result of the evaluation to an Integer.
-     * Throws an IllegalStateException if the result could not be converted to
-     * a Double
-     * 
-     * @param expression the expression.
-     * @param vars the variables, can be null.
-     * @return the result of the evaluation.
-     */
-    public static Integer evaluateToInteger( String expression, Map<String, Object> vars )
-    {
-        Object result = evaluate( expression, vars );
-        
-        if ( result == null || !isNumeric( String.valueOf( result ) ) )
-        {
-            throw new IllegalStateException( "Result must be not null and numeric: " + result );
-        }
-        
-        return Integer.valueOf( String.valueOf( result ) );
-    }
     
     /**
      * Evaluates the given expression to true or false. The given variables will 

=== modified file 'dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/util/TextUtils.java'
--- dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/util/TextUtils.java	2015-08-25 14:09:25 +0000
+++ dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/util/TextUtils.java	2015-08-27 22:22:39 +0000
@@ -523,7 +523,7 @@
         
         return null;
     }
-    
+        
     /**
      * Indicates whether the given string contains any of the given search
      * strings. The operation ignores case and leading and trailing blanks.

=== modified file 'dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/ExpressionUtilsTest.java'
--- dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/ExpressionUtilsTest.java	2015-08-27 20:45:58 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/ExpressionUtilsTest.java	2015-08-27 22:22:39 +0000
@@ -53,19 +53,14 @@
         assertEquals( 5d, ExpressionUtils.evaluateToDouble( "2 + 3", null ), DELTA );
         assertEquals( 15.6, ExpressionUtils.evaluateToDouble( "12.4 + 3.2", null ), DELTA );
         assertEquals( 2.0, ExpressionUtils.evaluateToDouble( "2 > 1 ? 2.0 : 1.0", null ), DELTA );
+        assertEquals( 1.0, ExpressionUtils.evaluateToDouble( "2 > 4 ? 2.0 : 1.0", null ), DELTA );
         assertEquals( 3d, ExpressionUtils.evaluateToDouble( "d2:zing(3)", null ), DELTA );        
         assertEquals( 2d, ExpressionUtils.evaluateToDouble( "d2:zing(-3) + 2.0", null ), DELTA );
         assertEquals( 4d, ExpressionUtils.evaluateToDouble( "d2:zing(-1) + 4 + d2:zing(-2)", null ), DELTA );
         assertEquals( 0d, ExpressionUtils.evaluateToDouble( "d2:oizp(-4)", null ), DELTA );
         assertEquals( 1d, ExpressionUtils.evaluateToDouble( "d2:oizp(0)", null ), DELTA );
         assertEquals( 2d, ExpressionUtils.evaluateToDouble( "d2:oizp(-4) + d2:oizp(0) + d2:oizp(3.0)", null ), DELTA );
-    }
-
-    @Test
-    public void testEvaluateToInteger()
-    {
-        assertEquals( 21l, (long) ExpressionUtils.evaluateToInteger( "7*3", null ) );
-        assertEquals( 3l, (long) ExpressionUtils.evaluateToInteger( "d2:daysBetween('2015-03-01','2015-03-04')", null ) );
+        assertEquals( 3d, ExpressionUtils.evaluateToDouble( "d2:daysBetween('2015-03-01','2015-03-04')", null ), DELTA );
     }
 
     @Test
@@ -81,6 +76,16 @@
         assertEquals( 0d, ExpressionUtils.evaluateToDouble( "d2:zing(v2)", vars ), DELTA );
         assertEquals( 4d, ExpressionUtils.evaluateToDouble( "d2:zing(v1) + d2:zing(v2)", vars ), DELTA );
     }
+
+    @Test
+    public void testEvaluate()
+    {
+        assertEquals( 4, ExpressionUtils.evaluate( "d2:condition('3 > 2',4,3)", null ) );
+        assertEquals( 3, ExpressionUtils.evaluate( "d2:condition('5 > 7',4,3)", null ) );
+        assertEquals( "yes", ExpressionUtils.evaluate( "d2:condition(\"'goat' == 'goat'\",'yes','no')", null ) );
+        assertEquals( "no", ExpressionUtils.evaluate( "d2:condition(\"'goat' != 'goat'\",'yes','no')", null ) );
+        assertEquals( "indoor", ExpressionUtils.evaluate( "d2:condition(\"'weather' == 'nice'\",'beach','indoor')", null ) );
+    }
     
     @Test
     public void testIsTrue()
@@ -165,6 +170,7 @@
         assertTrue( ExpressionUtils.isValid( "d2:zing(1)", null ) );
         assertTrue( ExpressionUtils.isValid( "d2:daysBetween('2015-02-01','2015-04-02')", null ) );
         assertTrue( ExpressionUtils.isValid( "(d2:zing(1)+d2:zing(1))*50/1", null ) );
+        assertTrue( ExpressionUtils.isValid( "d2:condition('1 > 100',5,100)", null ) );
         assertTrue( ExpressionUtils.isValid( "1/(1/100)", null ) );
         assertTrue( ExpressionUtils.isValid( "SUM(1)", null ) );
         assertTrue( ExpressionUtils.isValid( "average(2+1)", null ) );