← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 19877: Minor

 

------------------------------------------------------------
revno: 19877
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2015-08-27 22:45:58 +0200
message:
  Minor
modified:
  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-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-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 17:18:00 +0000
+++ dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/util/ExpressionFunctions.java	2015-08-27 20:45:58 +0000
@@ -79,7 +79,7 @@
      * @param end the end date.
      * @return number of days between dates.
      */
-    public static Long daysBetween( String start, String end )
+    public static Integer daysBetween( String start, String end )
         throws ParseException
     {
         SimpleDateFormat format = new SimpleDateFormat();
@@ -88,6 +88,6 @@
         Date startDate = format.parse( start );
         Date endDate = format.parse( end );
         
-        return ( startDate.getTime() - endDate.getTime() ) / 31536000000l;
+        return new Long( ( endDate.getTime() - startDate.getTime() ) / 86400000 ).intValue();
     }
 }

=== 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-21 12:47:08 +0000
+++ dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/commons/util/ExpressionUtils.java	2015-08-27 20:45:58 +0000
@@ -113,6 +113,28 @@
     }
 
     /**
+     * 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 
      * be substituted in the expression.
      * 

=== 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 17:18:00 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/ExpressionUtilsTest.java	2015-08-27 20:45:58 +0000
@@ -51,7 +51,8 @@
         assertEquals( 3d, ExpressionUtils.evaluateToDouble( "3", null ), DELTA );
         assertEquals( 3.45, ExpressionUtils.evaluateToDouble( "3.45", null ), DELTA );
         assertEquals( 5d, ExpressionUtils.evaluateToDouble( "2 + 3", null ), DELTA );
-        assertEquals( 15.6, ExpressionUtils.evaluateToDouble( "12.4 + 3.2", 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( 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 );
@@ -61,6 +62,13 @@
     }
 
     @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 ) );
+    }
+
+    @Test
     public void testEvaluateToDoubleWithVars()
     {
         Map<String, Object> vars = new HashMap<String, Object>();