dhis2-devs team mailing list archive
-
dhis2-devs team
-
Mailing list archive
-
Message #12184
[Branch ~dhis2-devs-core/dhis2/trunk] Rev 3689: Centralized expression related code
------------------------------------------------------------
revno: 3689
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Fri 2011-05-20 15:18:16 +0200
message:
Centralized expression related code
modified:
dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/impl/indicator/IndicatorAggregation.java
dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/resources/META-INF/dhis/beans.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-services/dhis-service-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/impl/indicator/IndicatorAggregation.java'
--- dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/impl/indicator/IndicatorAggregation.java 2010-12-06 21:06:40 +0000
+++ dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/impl/indicator/IndicatorAggregation.java 2011-05-20 13:18:16 +0000
@@ -33,8 +33,9 @@
import static org.hisp.dhis.system.util.MathUtils.calculateExpression;
import java.util.Date;
-import java.util.regex.Matcher;
-import java.util.regex.Pattern;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
import org.hisp.dhis.aggregation.impl.cache.AggregationCache;
import org.hisp.dhis.dataelement.DataElement;
@@ -52,8 +53,6 @@
*/
public class IndicatorAggregation
{
- private static final Pattern FORMULA_PATTERN = Pattern.compile( ExpressionService.FORMULA_EXPRESSION );
-
// -------------------------------------------------------------------------
// Dependencies
// -------------------------------------------------------------------------
@@ -71,6 +70,13 @@
{
this.dataElementService = dataElementService;
}
+
+ private ExpressionService expressionService;
+
+ public void setExpressionService( ExpressionService expressionService )
+ {
+ this.expressionService = expressionService;
+ }
private DataElementCategoryService categoryService;
@@ -143,46 +149,20 @@
return factor;
}
- /**
- * Converts an expression on the form<br>
- * [34] + [23], where the numbers are IDs of DataElements, to the form<br>
- * 200 + 450, where the numbers are the values of the DataValues registered
- * for the Period and source.
- *
- * @return The generated expression
- */
- private String generateExpression( String formula, Date startDate, Date endDate, OrganisationUnit organisationUnit )
- {
- try
- {
- Matcher matcher = FORMULA_PATTERN.matcher( formula );
-
- StringBuffer buffer = new StringBuffer();
-
- while ( matcher.find() )
- {
- String match = matcher.group();
-
- DataElementOperand operand = DataElementOperand.getOperand( match );
-
- DataElement dataElement = dataElementService.getDataElement( operand.getDataElementId() );
-
- DataElementCategoryOptionCombo optionCombo = !operand.isTotal() ? categoryService.getDataElementCategoryOptionCombo( operand.getOptionComboId() ) : null;
-
- Double aggregatedValue = aggregationCache.getAggregatedDataValue( dataElement, optionCombo, startDate, endDate, organisationUnit );
-
- match = aggregatedValue == null ? ExpressionService.NULL_REPLACEMENT : String.valueOf( aggregatedValue );
-
- matcher.appendReplacement( buffer, match );
- }
-
- matcher.appendTail( buffer );
-
- return buffer.toString();
- }
- catch ( NumberFormatException ex )
+ private String generateExpression( String expression, Date startDate, Date endDate, OrganisationUnit organisationUnit )
+ {
+ Set<DataElementOperand> operands = expressionService.getOperandsInExpression( expression );
+
+ Map<DataElementOperand, Double> valueMap = new HashMap<DataElementOperand, Double>();
+
+ for ( DataElementOperand operand : operands )
{
- throw new RuntimeException( "Illegal DataElement id", ex );
+ DataElement dataElement = dataElementService.getDataElement( operand.getDataElementId() );
+ DataElementCategoryOptionCombo optionCombo = !operand.isTotal() ? categoryService.getDataElementCategoryOptionCombo( operand.getOptionComboId() ) : null;
+
+ valueMap.put( operand, aggregationCache.getAggregatedDataValue( dataElement, optionCombo, startDate, endDate, organisationUnit ) );
}
+
+ return expressionService.generateExpression( expression, valueMap );
}
}
=== modified file 'dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/resources/META-INF/dhis/beans.xml'
--- dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/resources/META-INF/dhis/beans.xml 2010-07-04 11:50:16 +0000
+++ dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/resources/META-INF/dhis/beans.xml 2011-05-20 13:18:16 +0000
@@ -87,6 +87,8 @@
ref="org.hisp.dhis.dataelement.DataElementService"/>
<property name="categoryService"
ref="org.hisp.dhis.dataelement.DataElementCategoryService"/>
+ <property name="expressionService"
+ ref="org.hisp.dhis.expression.ExpressionService"/>
<property name="aggregationCache"
ref="org.hisp.dhis.aggregation.impl.cache.AggregationCache"/>
</bean>