=== modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/aggregation/AggregationService.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/aggregation/AggregationService.java 2011-01-25 20:50:16 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/aggregation/AggregationService.java 2011-03-16 20:50:40 +0000 @@ -30,6 +30,7 @@ import java.util.Date; import org.hisp.dhis.dataelement.DataElement; +import org.hisp.dhis.dataelement.DataElementCategoryOption; import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo; import org.hisp.dhis.indicator.Indicator; import org.hisp.dhis.organisationunit.OrganisationUnit; @@ -58,6 +59,21 @@ OrganisationUnit organisationUnit ); /** + * Calculates the aggregated value based on the aggregation operator defined + * in the given dataelement. + * + * @param dataElement the DataElement to aggregate over. + * @param categoryOption the DataElementCategoryOption to aggregate over. + * @param startDate the start date of the aggregation period. + * @param endDate the end date of the aggregation period. + * @param organisationUnit the OrganisationUnit to aggregate over. + * @return the aggregated value or null if no values are found. + * @throws AggregationStoreException + */ + Double getAggregatedDataValue( DataElement dataElement, Date startDate, Date endDate, OrganisationUnit organisationUnit, + DataElementCategoryOption categoryOption ); + + /** * Calculates the aggregated value of the given indicator. * * @param indicator the Indicator to aggregate over. === modified file 'dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataelement/DataElementCategoryCombo.java' --- dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataelement/DataElementCategoryCombo.java 2011-02-15 13:50:33 +0000 +++ dhis-2/dhis-api/src/main/java/org/hisp/dhis/dataelement/DataElementCategoryCombo.java 2011-03-16 20:50:40 +0000 @@ -102,6 +102,16 @@ return categoryOptions; } + + public boolean doTotal() + { + return optionCombos != null && optionCombos.size() > 1; + } + + public boolean doSubTotals() + { + return categories != null && categories.size() > 1; + } // ------------------------------------------------------------------------- // hashCode, equals and toString === modified file 'dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/impl/DefaultAggregationService.java' --- dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/impl/DefaultAggregationService.java 2010-07-10 14:50:48 +0000 +++ dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/impl/DefaultAggregationService.java 2011-03-16 20:50:40 +0000 @@ -27,17 +27,22 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +import java.util.ArrayList; import java.util.Date; +import java.util.List; import org.hisp.dhis.aggregation.AggregationService; import org.hisp.dhis.aggregation.impl.cache.AggregationCache; import org.hisp.dhis.aggregation.impl.dataelement.AbstractDataElementAggregation; import org.hisp.dhis.aggregation.impl.indicator.IndicatorAggregation; import org.hisp.dhis.dataelement.DataElement; +import org.hisp.dhis.dataelement.DataElementCategoryOption; import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo; import org.hisp.dhis.indicator.Indicator; import org.hisp.dhis.organisationunit.OrganisationUnit; import static org.hisp.dhis.system.util.DateUtils.*; +import static org.hisp.dhis.system.util.MathUtils.getAverage; +import static org.hisp.dhis.system.util.MathUtils.getSum; import static org.hisp.dhis.dataelement.DataElement.*; @@ -114,6 +119,18 @@ return dataElementAggregation.getAggregatedValue( dataElement, optionCombo, startDate, endDate, organisationUnit ); } + public Double getAggregatedDataValue( DataElement dataElement, Date startDate, Date endDate, OrganisationUnit organisationUnit, DataElementCategoryOption categoryOption ) + { + final List values = new ArrayList(); + + for ( DataElementCategoryOptionCombo optionCombo : categoryOption.getCategoryOptionCombos() ) + { + values.add( getAggregatedDataValue( dataElement, optionCombo, startDate, endDate, organisationUnit ) ); + } + + return dataElement.getAggregationOperator().equals( AGGREGATION_OPERATOR_SUM ) ? getSum( values ) : getAverage( values ); + } + // ------------------------------------------------------------------------- // Indicator // ------------------------------------------------------------------------- === modified file 'dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/impl/dataelement/AbstractDataElementAggregation.java' --- dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/impl/dataelement/AbstractDataElementAggregation.java 2010-07-10 15:19:02 +0000 +++ dhis-2/dhis-services/dhis-service-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/impl/dataelement/AbstractDataElementAggregation.java 2011-03-16 20:50:40 +0000 @@ -80,8 +80,7 @@ * @param aggregationEndDate End date * @param organisationUnit The organisationUnit */ - public abstract Double getAggregatedValue( DataElement dataElement, DataElementCategoryOptionCombo optionCombo, Date startDate, Date endDate, - OrganisationUnit organisationUnit ); + public abstract Double getAggregatedValue( DataElement dataElement, DataElementCategoryOptionCombo optionCombo, Date startDate, Date endDate, OrganisationUnit organisationUnit ); /** * Retrieves the datavalues registered for the given dataelement === modified file 'dhis-2/dhis-services/dhis-service-importexport/src/test/java/org/hisp/dhis/importexport/dxf2/service/DataValueSetServiceTest.java' --- dhis-2/dhis-services/dhis-service-importexport/src/test/java/org/hisp/dhis/importexport/dxf2/service/DataValueSetServiceTest.java 2011-02-25 18:08:38 +0000 +++ dhis-2/dhis-services/dhis-service-importexport/src/test/java/org/hisp/dhis/importexport/dxf2/service/DataValueSetServiceTest.java 2011-03-16 20:50:40 +0000 @@ -27,7 +27,10 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -import static junit.framework.Assert.*; +import static junit.framework.Assert.assertEquals; +import static junit.framework.Assert.assertNull; +import static junit.framework.Assert.assertTrue; +import static junit.framework.Assert.fail; import java.io.IOException; import java.io.InputStream; @@ -53,7 +56,6 @@ import org.hisp.dhis.importexport.ImportService; import org.hisp.dhis.importexport.ImportStrategy; import org.hisp.dhis.importexport.dxf2.model.DataValueSet; -import org.hisp.dhis.importexport.dxf2.service.DataValueSetService; import org.hisp.dhis.importexport.util.ImportExportUtils; import org.hisp.dhis.period.WeeklyPeriodType; import org.junit.Ignore; === modified file 'dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MathUtils.java' --- dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MathUtils.java 2011-02-04 21:33:00 +0000 +++ dhis-2/dhis-support/dhis-support-system/src/main/java/org/hisp/dhis/system/util/MathUtils.java 2011-03-16 20:50:40 +0000 @@ -29,6 +29,7 @@ import java.math.BigDecimal; import java.math.MathContext; +import java.util.List; import java.util.Random; import java.util.regex.Pattern; @@ -267,4 +268,35 @@ return max; } + + /** + * Returns the average of the given values. + * + * @param values the values. + * @return the average. + */ + public static Double getAverage( List values ) + { + Double sum = getSum( values ); + + return sum / values.size(); + } + + /** + * Returns the sum of the given values. + * + * @param values the values. + * @return the sum. + */ + public static Double getSum( List values ) + { + Double sum = 0.0; + + for ( Double value : values ) + { + sum += value; + } + + return sum; + } } === modified file 'dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/MathUtilsTest.java' --- dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/MathUtilsTest.java 2011-02-04 21:33:00 +0000 +++ dhis-2/dhis-support/dhis-support-system/src/test/java/org/hisp/dhis/system/util/MathUtilsTest.java 2011-03-16 20:50:40 +0000 @@ -30,8 +30,15 @@ import static junit.framework.Assert.assertEquals; import static junit.framework.Assert.assertFalse; import static junit.framework.Assert.assertTrue; +import static org.hisp.dhis.expression.Operator.equal_to; +import static org.hisp.dhis.expression.Operator.greater_than; +import static org.hisp.dhis.expression.Operator.greater_than_or_equal_to; +import static org.hisp.dhis.expression.Operator.less_than; +import static org.hisp.dhis.expression.Operator.less_than_or_equal_to; +import static org.hisp.dhis.expression.Operator.not_equal_to; import static org.hisp.dhis.system.util.MathUtils.expressionIsTrue; -import static org.hisp.dhis.expression.Operator.*; + +import java.util.Arrays; import org.junit.Test; @@ -105,4 +112,10 @@ assertFalse( MathUtils.isNumeric( " " ) ); assertFalse( MathUtils.isNumeric( null ) ); } + + @Test + public void testGetAverage() + { + assertEquals( 7.5, MathUtils.getAverage( Arrays.asList( 5.0, 5.0, 10.0, 10.0 ) ) ); + } }