← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 19055: Added dependency commons-jexl. Impl class ExpressionUtils which provides utilility methods for Je...

 

------------------------------------------------------------
revno: 19055
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Wed 2015-04-29 13:33:31 +0200
message:
  Added dependency commons-jexl. Impl class ExpressionUtils which provides utilility methods for JexlEngine. Evaluates Velocity-like expressions, including logical operators. Useful for program rule conditions and program indicator expressions.
added:
  dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ExpressionUtils.java
  dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/ExpressionUtilsTest.java
  expr.patch
modified:
  dhis-2/dhis-support/dhis-support-system/pom.xml
  dhis-2/pom.xml


--
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-system/pom.xml'
--- dhis-2/dhis-support/dhis-support-system/pom.xml	2015-04-15 14:58:13 +0000
+++ dhis-2/dhis-support/dhis-support-system/pom.xml	2015-04-29 11:33:31 +0000
@@ -171,6 +171,10 @@
       <groupId>com.google.guava</groupId>
       <artifactId>guava</artifactId>
     </dependency>
+    <dependency>
+	  <groupId>org.apache.commons</groupId>
+	  <artifactId>commons-jexl</artifactId>
+    </dependency>
   </dependencies>
   <properties>
     <rootDir>../../</rootDir>

=== added file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ExpressionUtils.java'
--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ExpressionUtils.java	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ExpressionUtils.java	2015-04-29 11:33:31 +0000
@@ -0,0 +1,66 @@
+package org.hisp.dhis.system.util;
+
+/*
+ * 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 java.util.Map;
+
+import org.apache.commons.jexl2.Expression;
+import org.apache.commons.jexl2.JexlContext;
+import org.apache.commons.jexl2.JexlEngine;
+import org.apache.commons.jexl2.MapContext;
+
+/**
+ * @author Lars Helge Overland
+ */
+public class ExpressionUtils
+{
+    private static final JexlEngine JEXL = new JexlEngine();
+
+    static 
+    {
+        JEXL.setCache( 512 );
+        JEXL.setSilent( false );
+    }
+    
+    public static Object evaluate( String expression, Map<String, Object> vars )
+    {
+        Expression exp = JEXL.createExpression( expression );
+        
+        JexlContext context = vars != null ? new MapContext( vars ) : new MapContext( vars );
+                
+        return exp.evaluate( context );
+    }
+    
+    public static boolean isTrue( String expression, Map<String, Object> vars )
+    {
+        Boolean result = (Boolean) evaluate( expression, vars );
+        
+        return result != null ? result : false;
+    }    
+}

=== added 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	1970-01-01 00:00:00 +0000
+++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/ExpressionUtilsTest.java	2015-04-29 11:33:31 +0000
@@ -0,0 +1,81 @@
+package org.hisp.dhis.system.util;
+
+/*
+ * 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 static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import org.junit.Test;
+
+/**
+ * @author Lars Helge Overland
+ */
+public class ExpressionUtilsTest
+{
+    @Test
+    public void testIsTrue()
+    {
+        assertTrue( ExpressionUtils.isTrue( "2 > 1", null ) );
+        assertTrue( ExpressionUtils.isTrue( "(2 * 3) == 6", null ) );
+        assertTrue( ExpressionUtils.isTrue( "\"a\" == \"a\"", null ) );
+        assertTrue( ExpressionUtils.isTrue( "'b' == 'b'", null ) );
+        assertTrue( ExpressionUtils.isTrue( "('b' == 'b') && ('c' == 'c')", null ) );
+        assertTrue( ExpressionUtils.isTrue( "'goat' == 'goat'", null ) );
+        
+        assertFalse( ExpressionUtils.isTrue( "2 < 1", null ) );
+        assertFalse( ExpressionUtils.isTrue( "(2 * 3) == 8", null ) );
+        assertFalse( ExpressionUtils.isTrue( "\"a\" == \"b\"", null ) );
+        assertFalse( ExpressionUtils.isTrue( "'b' == 'c'", null ) );
+        assertFalse( ExpressionUtils.isTrue( "'goat' == 'cow'", null ) );
+    }
+    
+    @Test
+    public void testIsTrueWithVars()
+    {
+        Map<String, Object> vars = new HashMap<String, Object>();
+        
+        vars.put( "v1", "4" );
+        vars.put( "v2", "12" );
+        vars.put( "v3", "goat" );
+        vars.put( "v4", "horse" );
+        
+        assertTrue( ExpressionUtils.isTrue( "v1 > 1", vars ) );
+        assertTrue( ExpressionUtils.isTrue( "v2 < 18", vars ) );
+        assertTrue( ExpressionUtils.isTrue( "v3 == 'goat'", vars ) );
+        assertTrue( ExpressionUtils.isTrue( "v4 == 'horse'", vars ) );
+
+        assertFalse( ExpressionUtils.isTrue( "v1 < 1", vars ) );
+        assertFalse( ExpressionUtils.isTrue( "v2 > 18", vars ) );
+        assertFalse( ExpressionUtils.isTrue( "v3 == 'cow'", vars ) );
+        assertFalse( ExpressionUtils.isTrue( "v4 == 'goat'", vars ) );
+    }
+}

=== modified file 'dhis-2/pom.xml'
--- dhis-2/pom.xml	2015-03-31 08:46:53 +0000
+++ dhis-2/pom.xml	2015-04-29 11:33:31 +0000
@@ -584,6 +584,11 @@
         <version>2.4.2</version>
       </dependency>
       <dependency>
+	    <groupId>org.apache.commons</groupId>
+	    <artifactId>commons-jexl</artifactId>
+	    <version>2.1.1</version>
+      </dependency>
+      <dependency>
         <groupId>junit</groupId>
         <artifactId>junit</artifactId>
         <version>4.12</version>
@@ -781,6 +786,7 @@
         <artifactId>postgresql</artifactId>
         <version>9.3-1102-jdbc41</version>
       </dependency>
+      
       <!--Reporting -->
       <dependency>
         <groupId>net.sf.jasperreports</groupId>

=== added file 'expr.patch'
--- expr.patch	1970-01-01 00:00:00 +0000
+++ expr.patch	2015-04-29 11:33:31 +0000
@@ -0,0 +1,196 @@
+=== modified file 'dhis-2/dhis-support/dhis-support-system/pom.xml'
+--- dhis-2/dhis-support/dhis-support-system/pom.xml	2015-04-15 14:58:13 +0000
++++ dhis-2/dhis-support/dhis-support-system/pom.xml	2015-04-29 11:11:42 +0000
+@@ -171,6 +171,10 @@
+       <groupId>com.google.guava</groupId>
+       <artifactId>guava</artifactId>
+     </dependency>
++    <dependency>
++	  <groupId>org.apache.commons</groupId>
++	  <artifactId>commons-jexl</artifactId>
++    </dependency>
+   </dependencies>
+   <properties>
+     <rootDir>../../</rootDir>
+
+=== added file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ExpressionUtils.java'
+--- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ExpressionUtils.java	1970-01-01 00:00:00 +0000
++++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/ExpressionUtils.java	2015-04-29 11:02:25 +0000
+@@ -0,0 +1,66 @@
++package org.hisp.dhis.system.util;
++
++/*
++ * 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 java.util.Map;
++
++import org.apache.commons.jexl2.Expression;
++import org.apache.commons.jexl2.JexlContext;
++import org.apache.commons.jexl2.JexlEngine;
++import org.apache.commons.jexl2.MapContext;
++
++/**
++ * @author Lars Helge Overland
++ */
++public class ExpressionUtils
++{
++    private static final JexlEngine JEXL = new JexlEngine();
++
++    static 
++    {
++        JEXL.setCache( 512 );
++        JEXL.setSilent( false );
++    }
++    
++    public static Object evaluate( String expression, Map<String, Object> vars )
++    {
++        Expression exp = JEXL.createExpression( expression );
++        
++        JexlContext context = vars != null ? new MapContext( vars ) : new MapContext( vars );
++                
++        return exp.evaluate( context );
++    }
++    
++    public static boolean isTrue( String expression, Map<String, Object> vars )
++    {
++        Boolean result = (Boolean) evaluate( expression, vars );
++        
++        return result != null ? result : false;
++    }    
++}
+
+=== added 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	1970-01-01 00:00:00 +0000
++++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/ExpressionUtilsTest.java	2015-04-29 11:11:12 +0000
+@@ -0,0 +1,81 @@
++package org.hisp.dhis.system.util;
++
++/*
++ * 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 static org.junit.Assert.assertFalse;
++import static org.junit.Assert.assertTrue;
++
++import java.util.HashMap;
++import java.util.Map;
++
++import org.junit.Test;
++
++/**
++ * @author Lars Helge Overland
++ */
++public class ExpressionUtilsTest
++{
++    @Test
++    public void testIsTrue()
++    {
++        assertTrue( ExpressionUtils.isTrue( "2 > 1", null ) );
++        assertTrue( ExpressionUtils.isTrue( "(2 * 3) == 6", null ) );
++        assertTrue( ExpressionUtils.isTrue( "\"a\" == \"a\"", null ) );
++        assertTrue( ExpressionUtils.isTrue( "'b' == 'b'", null ) );
++        assertTrue( ExpressionUtils.isTrue( "('b' == 'b') && ('c' == 'c')", null ) );
++        assertTrue( ExpressionUtils.isTrue( "'goat' == 'goat'", null ) );
++        
++        assertFalse( ExpressionUtils.isTrue( "2 < 1", null ) );
++        assertFalse( ExpressionUtils.isTrue( "(2 * 3) == 8", null ) );
++        assertFalse( ExpressionUtils.isTrue( "\"a\" == \"b\"", null ) );
++        assertFalse( ExpressionUtils.isTrue( "'b' == 'c'", null ) );
++        assertFalse( ExpressionUtils.isTrue( "'goat' == 'cow'", null ) );
++    }
++    
++    @Test
++    public void testIsTrueWithVars()
++    {
++        Map<String, Object> vars = new HashMap<String, Object>();
++        
++        vars.put( "v1", "4" );
++        vars.put( "v2", "12" );
++        vars.put( "v3", "goat" );
++        vars.put( "v4", "horse" );
++        
++        assertTrue( ExpressionUtils.isTrue( "v1 > 1", vars ) );
++        assertTrue( ExpressionUtils.isTrue( "v2 < 18", vars ) );
++        assertTrue( ExpressionUtils.isTrue( "v3 == 'goat'", vars ) );
++        assertTrue( ExpressionUtils.isTrue( "v4 == 'horse'", vars ) );
++
++        assertFalse( ExpressionUtils.isTrue( "v1 < 1", vars ) );
++        assertFalse( ExpressionUtils.isTrue( "v2 > 18", vars ) );
++        assertFalse( ExpressionUtils.isTrue( "v3 == 'cow'", vars ) );
++        assertFalse( ExpressionUtils.isTrue( "v4 == 'goat'", vars ) );
++    }
++}
+
+=== modified file 'dhis-2/pom.xml'
+--- dhis-2/pom.xml	2015-03-31 08:46:53 +0000
++++ dhis-2/pom.xml	2015-04-29 11:12:30 +0000
+@@ -584,6 +584,11 @@
+         <version>2.4.2</version>
+       </dependency>
+       <dependency>
++	    <groupId>org.apache.commons</groupId>
++	    <artifactId>commons-jexl</artifactId>
++	    <version>2.1.1</version>
++      </dependency>
++      <dependency>
+         <groupId>junit</groupId>
+         <artifactId>junit</artifactId>
+         <version>4.12</version>
+@@ -781,6 +786,7 @@
+         <artifactId>postgresql</artifactId>
+         <version>9.3-1102-jdbc41</version>
+       </dependency>
++      
+       <!--Reporting -->
+       <dependency>
+         <groupId>net.sf.jasperreports</groupId>
+