← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 19259: Impl ExpressionUtils.isBoolean

 

------------------------------------------------------------
revno: 19259
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Tue 2015-06-02 15:46:21 +0200
message:
  Impl ExpressionUtils.isBoolean
modified:
  dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/util/ExpressionUtils.java
  dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/ExpressionUtilsTest.java
  dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/webapp/dhis-web-maintenance-program/programIndicatorForm.vm


--
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/util/ExpressionUtils.java'
--- dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/util/ExpressionUtils.java	2015-05-21 15:20:54 +0000
+++ dhis-2/dhis-support/dhis-support-commons/src/main/java/org/hisp/dhis/util/ExpressionUtils.java	2015-06-02 13:46:21 +0000
@@ -31,6 +31,7 @@
 import org.apache.commons.jexl2.Expression;
 import org.apache.commons.jexl2.JexlContext;
 import org.apache.commons.jexl2.JexlEngine;
+import org.apache.commons.jexl2.JexlException;
 import org.apache.commons.jexl2.MapContext;
 
 import java.util.Map;
@@ -75,8 +76,30 @@
      */
     public static boolean isTrue( String expression, Map<String, Object> vars )
     {
-        Boolean result = (Boolean) evaluate( expression, vars );
+        Object result = evaluate( expression, vars );
         
-        return result != null ? result : false;
-    }    
+        return ( result != null && result instanceof Boolean ) ? (Boolean) result : false;
+    }
+    
+    /**
+     * Indicates whether the given expression is valid and evaluates to true or
+     * false.
+     * 
+     * @param expression the expression.
+     * @param vars the variables, can be null.
+     * @return true or false.
+     */
+    public static boolean isBoolean( String expression, Map<String, Object> vars )
+    {
+        try
+        {
+            Object result = evaluate( expression, vars );
+            
+            return ( result instanceof Boolean );
+        }
+        catch ( JexlException ex )
+        {
+            return false;
+        }
+    }
 }

=== 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-05-28 14:33:21 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/ExpressionUtilsTest.java	2015-06-02 13:46:21 +0000
@@ -83,4 +83,24 @@
         assertFalse( ExpressionUtils.isTrue( "v4 == 'goat'", vars ) );
         assertFalse( ExpressionUtils.isTrue( "v4 == \"goat\"", vars ) );
     }
+    
+    @Test
+    public void testIsBoolean()
+    {
+        Map<String, Object> vars = new HashMap<String, Object>();
+        
+        vars.put( "uA2hsh8j26j", "FEMALE" );
+        vars.put( "v2", "12" );
+        
+        assertTrue( ExpressionUtils.isBoolean( "2 > 1", null ) );
+        assertTrue( ExpressionUtils.isBoolean( "(2 * 3) == 6", null ) );
+        assertTrue( ExpressionUtils.isBoolean( "\"a\" == \"a\"", null ) );
+        assertTrue( ExpressionUtils.isBoolean( "'b' == 'b'", null ) );
+        assertTrue( ExpressionUtils.isBoolean( "('b' == 'b') && ('c' == 'c')", null ) );
+        assertTrue( ExpressionUtils.isBoolean( "'goat' == 'goat'", null ) );
+
+        assertFalse( ExpressionUtils.isBoolean( "4", null ) );
+        assertFalse( ExpressionUtils.isBoolean( "3 + 2", null ) );
+        assertFalse( ExpressionUtils.isBoolean( "someinvalid expr", null ) );
+    }
 }

=== modified file 'dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/webapp/dhis-web-maintenance-program/programIndicatorForm.vm'
--- dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/webapp/dhis-web-maintenance-program/programIndicatorForm.vm	2015-06-02 12:18:46 +0000
+++ dhis-2/dhis-web/dhis-web-maintenance/dhis-web-maintenance-program/src/main/webapp/dhis-web-maintenance-program/programIndicatorForm.vm	2015-06-02 13:46:21 +0000
@@ -108,7 +108,7 @@
     </tr>	
 	<tr>
 		<td>
-			<textarea style="width:662px" id="expression" name="expression" onkeyup='getConditionDescription();' >$!encoder.htmlEncode($!programIndicator.expression)</textarea>
+			<textarea style="width:665px" id="expression" name="expression" onkeyup='getConditionDescription();' >$!encoder.htmlEncode($!programIndicator.expression)</textarea>
 		</td>
 	</tr>	
 	<tr>