← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 18858: Added methods to ExpressionService

 

------------------------------------------------------------
revno: 18858
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2015-04-09 20:08:56 +0200
message:
  Added methods to ExpressionService
modified:
  dhis-2/dhis-api/src/main/java/org/hisp/dhis/expression/ExpressionService.java
  dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/expression/DefaultExpressionService.java
  dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/expression/ExpressionServiceTest.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-api/src/main/java/org/hisp/dhis/expression/ExpressionService.java'
--- dhis-2/dhis-api/src/main/java/org/hisp/dhis/expression/ExpressionService.java	2015-04-09 17:07:16 +0000
+++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/expression/ExpressionService.java	2015-04-09 18:08:56 +0000
@@ -173,12 +173,30 @@
     Set<String> getDataElementTotalUids( String expression );
     
     /**
-     * Returns all DataElements included in the given expression string.
+     * Returns all data elements included in the given expression string.
      * 
      * @param expression the expression string.
-     * @return a Set of DataElements included in the expression string.
+     * @return a set of data elements included in the expression string.
      */
     Set<DataElement> getDataElementsInExpression( String expression );
+
+    /**
+     * Returns all data elements included in the given expression string which
+     * represent data element totals.
+     * 
+     * @param expression the expression string.
+     * @return a set of data elements.
+     */
+    Set<DataElement> getDataElementTotalsInExpression( String expression );
+
+    /**
+     * Returns all data elements included in the given expression string which
+     * represent operands with data elements and category option combinations.
+     * 
+     * @param expression the expression string.
+     * @return a set of data elements.
+     */
+    Set<DataElement> getDataElementsWithOptionCombosInExpression( String expression );
     
     /**
      * Returns all OrganisationUnitGroups in the numerator and denominator

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/expression/DefaultExpressionService.java'
--- dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/expression/DefaultExpressionService.java	2015-04-09 17:07:16 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/expression/DefaultExpressionService.java	2015-04-09 18:08:56 +0000
@@ -44,6 +44,7 @@
 import java.util.Map;
 import java.util.Set;
 import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
 import org.apache.commons.lang3.StringUtils;
 import org.apache.commons.logging.Log;
@@ -228,13 +229,32 @@
     @Transactional
     public Set<DataElement> getDataElementsInExpression( String expression )
     {
-        Set<DataElement> dataElementsInExpression = null;
+        return getDataElementsInExpressionInternal( OPERAND_PATTERN, expression );
+    }
+
+    @Override
+    @Transactional
+    public Set<DataElement> getDataElementTotalsInExpression( String expression )
+    {
+        return getDataElementsInExpressionInternal( DATA_ELEMENT_TOTAL_PATTERN, expression );
+    }
+
+    @Override
+    @Transactional
+    public Set<DataElement> getDataElementsWithOptionCombosInExpression( String expression )
+    {
+        return getDataElementsInExpressionInternal( OPTION_COMBO_OPERAND_PATTERN, expression );
+    }
+    
+    private Set<DataElement> getDataElementsInExpressionInternal( Pattern pattern, String expression )
+    {
+        Set<DataElement> dataElements = null;
 
         if ( expression != null )
         {
-            dataElementsInExpression = new HashSet<>();
+            dataElements = new HashSet<>();
 
-            final Matcher matcher = OPERAND_PATTERN.matcher( expression );
+            final Matcher matcher = pattern.matcher( expression );
 
             while ( matcher.find() )
             {
@@ -242,12 +262,12 @@
 
                 if ( dataElement != null )
                 {
-                    dataElementsInExpression.add( dataElement );
+                    dataElements.add( dataElement );
                 }
             }
         }
 
-        return dataElementsInExpression;
+        return dataElements;
     }
     
     @Override

=== modified file 'dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/expression/ExpressionServiceTest.java'
--- dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/expression/ExpressionServiceTest.java	2015-04-09 17:07:16 +0000
+++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/expression/ExpressionServiceTest.java	2015-04-09 18:08:56 +0000
@@ -267,6 +267,32 @@
         assertTrue( dataElements.size() == 2 );
         assertTrue( dataElements.contains( dataElementA ) );
         assertTrue( dataElements.contains( dataElementB ) );
+        
+        dataElements = expressionService.getDataElementsInExpression( expressionG );
+
+        assertTrue( dataElements.size() == 3 );
+        assertTrue( dataElements.contains( dataElementA ) );
+        assertTrue( dataElements.contains( dataElementB ) );
+        assertTrue( dataElements.contains( dataElementC ) );
+    }
+
+    @Test
+    public void testGetDataElementTotalsInExpression()
+    {
+        Set<DataElement> dataElements = expressionService.getDataElementTotalsInExpression( expressionG );
+
+        assertTrue( dataElements.size() == 2 );
+        assertTrue( dataElements.contains( dataElementB ) );
+        assertTrue( dataElements.contains( dataElementC ) );
+    }
+
+    @Test
+    public void testGetDataElementsWithOptionCombosInExpression()
+    {
+        Set<DataElement> dataElements = expressionService.getDataElementsWithOptionCombosInExpression( expressionG );
+
+        assertTrue( dataElements.size() == 1 );
+        assertTrue( dataElements.contains( dataElementA ) );
     }
 
     @Test