=== 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 2014-02-04 08:24:06 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/expression/ExpressionService.java 2014-02-04 08:38:48 +0000 @@ -123,7 +123,7 @@ Collection getAllExpressions(); Double getIndicatorValue( Indicator indicator, Period period, Map valueMap, - Map constantMap, Integer orgUnitCount, Integer days ); + Map constantMap, Map orgUnitCountMap, Integer days ); /** * Generates the calculated value for the given expression base on the values @@ -134,12 +134,13 @@ * use in the calculation. * @param constantMap the mapping between the constant uid and value to use * in the calculation. - * @param orgUnitCount the number of org units to use in the calculation. + * @param orgUnitCountMap the mapping between organisation unit group uid and + * count of org units to use in the calculation. * @param days the number of days to use in the calculation. * @return the calculated value as a double. */ Double getExpressionValue( Expression expression, Map valueMap, - Map constantMap, Integer orgUnitCount, Integer days ); + Map constantMap, Map orgUnitCountMap, Integer days ); /** * Generates the calculated value for the given expression base on the values @@ -150,7 +151,8 @@ * use in the calculation. * @param constantMap the mapping between the constant uid and value to use * in the calculation. - * @param orgUnitCount the number of org units to use in the calculation. + * @param orgUnitCountMap the mapping between organisation unit group uid and + * count of org units to use in the calculation. * @param days the number of days to use in the calculation. * @param set of data element operands that have values but they are incomplete * (for example due to aggregation from organisationUnit children where @@ -158,7 +160,7 @@ * @return the calculated value as a double. */ Double getExpressionValue( Expression expression, Map valueMap, - Map constantMap, Integer orgUnitCount, Integer days, Set incompleteValues ); + Map constantMap, Map orgUnitCountMap, Integer days, Set incompleteValues ); /** * Returns the uids of the data element totals in the given expression. @@ -319,11 +321,15 @@ * period, and source. * * @param formula formula to parse. - * @param valueMap map containing data element identifiers and aggregated value. - * @param constantMap map between constants and values. - * @param days The number to be substituted with the days expression in the formula. + * @param valueMap the mapping between data element operands and values to + * use in the calculation. + * @param constantMap the mapping between the constant uid and value to use + * in the calculation. + * @param orgUnitCountMap the mapping between organisation unit group uid and + * count of org units to use in the calculation. + * @param days the number of days to use in the calculation. */ - String generateExpression( String expression, Map valueMap, Map constantMap, Integer orgUnitCount, Integer days, boolean nullIfNoValues ); + String generateExpression( String expression, Map valueMap, Map constantMap, Map orgUnitCountMap, Integer days, boolean nullIfNoValues ); /** * Returns all Operands included in the formulas for the given collection of === 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 2014-02-04 08:24:06 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/expression/DefaultExpressionService.java 2014-02-04 08:38:48 +0000 @@ -156,14 +156,14 @@ // ------------------------------------------------------------------------- public Double getIndicatorValue( Indicator indicator, Period period, Map valueMap, - Map constantMap, Integer orgUnitCount, Integer days ) + Map constantMap, Map orgUnitCountMap, Integer days ) { if ( indicator == null || indicator.getExplodedNumeratorFallback() == null || indicator.getExplodedDenominatorFallback() == null ) { return null; } - final String denominatorExpression = generateExpression( indicator.getExplodedDenominatorFallback(), valueMap, constantMap, orgUnitCount, days, false ); + final String denominatorExpression = generateExpression( indicator.getExplodedDenominatorFallback(), valueMap, constantMap, orgUnitCountMap, days, false ); if ( denominatorExpression == null ) { @@ -174,7 +174,7 @@ if ( !isEqual( denominatorValue, 0d ) ) { - final String numeratorExpression = generateExpression( indicator.getExplodedNumeratorFallback(), valueMap, constantMap, orgUnitCount, days, false ); + final String numeratorExpression = generateExpression( indicator.getExplodedNumeratorFallback(), valueMap, constantMap, orgUnitCountMap, days, false ); if ( numeratorExpression == null ) { @@ -194,18 +194,18 @@ } public Double getExpressionValue( Expression expression, Map valueMap, - Map constantMap, Integer orgUnitCount, Integer days ) + Map constantMap, Map orgUnitCountMap, Integer days ) { final String expressionString = generateExpression( expression.getExpression(), valueMap, constantMap, - orgUnitCount, days, expression.isNullIfBlank() ); + orgUnitCountMap, days, expression.isNullIfBlank() ); return expressionString != null ? calculateExpression( expressionString ) : null; } public Double getExpressionValue( Expression expression, Map valueMap, - Map constantMap, Integer orgUnitCount, Integer days, Set incompleteValues ) + Map constantMap, Map orgUnitCountMap, Integer days, Set incompleteValues ) { - final String expressionString = generateExpression( expression.getExpression(), valueMap, constantMap, orgUnitCount, days, + final String expressionString = generateExpression( expression.getExpression(), valueMap, constantMap, orgUnitCountMap, days, expression.isNullIfBlank(), incompleteValues ); return expressionString != null ? calculateExpression( expressionString ) : null; @@ -771,13 +771,13 @@ @Transactional public String generateExpression( String expression, Map valueMap, - Map constantMap, Integer orgUnitCount, Integer days, boolean nullIfNoValues ) + Map constantMap, Map orgUnitCountMap, Integer days, boolean nullIfNoValues ) { - return generateExpression( expression, valueMap, constantMap, orgUnitCount, days, nullIfNoValues, null ); + return generateExpression( expression, valueMap, constantMap, orgUnitCountMap, days, nullIfNoValues, null ); } private String generateExpression( String expression, Map valueMap, - Map constantMap, Integer orgUnitCount, Integer days, boolean nullIfNoValues, Set incompleteValues ) + Map constantMap, Map orgUnitCountMap, Integer days, boolean nullIfNoValues, Set incompleteValues ) { if ( expression == null || expression.isEmpty() ) { @@ -818,7 +818,7 @@ while ( matcher.find() ) { - final Double constant = constantMap.get( matcher.group( 1 ) ); + final Double constant = constantMap != null ? constantMap.get( matcher.group( 1 ) ) : null; String replacement = constant != null ? String.valueOf( constant ) : NULL_REPLACEMENT; @@ -836,7 +836,9 @@ while ( matcher.find() ) { - String replacement = orgUnitCount != null ? String.valueOf( orgUnitCount ) : NULL_REPLACEMENT; + final Integer count = orgUnitCountMap != null ? orgUnitCountMap.get( matcher.group( 1 ) ) : null; + + String replacement = count != null ? String.valueOf( count ) : NULL_REPLACEMENT; matcher.appendReplacement( sb, replacement ); } === 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 2014-01-31 18:08:43 +0000 +++ dhis-2/dhis-services/dhis-service-core/src/test/java/org/hisp/dhis/expression/ExpressionServiceTest.java 2014-02-04 08:38:48 +0000 @@ -354,11 +354,14 @@ Map constantMap = new HashMap(); constantMap.put( constantA.getUid(), 2.0 ); + + Map orgUnitCountMap = new HashMap(); + orgUnitCountMap.put( groupA.getUid(), groupA.getMembers().size() ); assertEquals( "12.0+34.0", expressionService.generateExpression( expressionA, valueMap, constantMap, null, null, false ) ); assertEquals( "12.0+5", expressionService.generateExpression( expressionD, valueMap, constantMap, null, 5, false ) ); assertEquals( "12.0*2.0", expressionService.generateExpression( expressionE, valueMap, constantMap, null, null, false ) ); - assertEquals( "12.0*3", expressionService.generateExpression( expressionH, valueMap, constantMap, 3, null, false ) ); + assertEquals( "12.0*3", expressionService.generateExpression( expressionH, valueMap, constantMap, orgUnitCountMap, null, false ) ); } @Test @@ -388,10 +391,13 @@ Map constantMap = new HashMap(); constantMap.put( constantA.getUid(), 2.0 ); + Map orgUnitCountMap = new HashMap(); + orgUnitCountMap.put( groupA.getUid(), groupA.getMembers().size() ); + assertEquals( 46d, expressionService.getExpressionValue( expA, valueMap, constantMap, null, null ), DELTA ); assertEquals( 17d, expressionService.getExpressionValue( expD, valueMap, constantMap, null, 5 ), DELTA ); assertEquals( 24d, expressionService.getExpressionValue( expE, valueMap, constantMap, null, null ), DELTA ); - assertEquals( 36d, expressionService.getExpressionValue( expH, valueMap, constantMap, 3, null ), DELTA ); + assertEquals( 36d, expressionService.getExpressionValue( expH, valueMap, constantMap, orgUnitCountMap, null ), DELTA ); } @Test