1
package org.hisp.dhis.datamart.aggregation.dataelement;
4
* Copyright (c) 2004-2007, University of Oslo
7
* Redistribution and use in source and binary forms, with or without
8
* modification, are permitted provided that the following conditions are met:
9
* * Redistributions of source code must retain the above copyright notice, this
10
* list of conditions and the following disclaimer.
11
* * Redistributions in binary form must reproduce the above copyright notice,
12
* this list of conditions and the following disclaimer in the documentation
13
* and/or other materials provided with the distribution.
14
* * Neither the name of the HISP project nor the names of its contributors may
15
* be used to endorse or promote products derived from this software without
16
* specific prior written permission.
18
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
22
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
25
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30
import static org.hisp.dhis.system.util.MathUtils.getDays;
32
import java.util.ArrayList;
33
import java.util.Collection;
34
import java.util.Date;
35
import java.util.HashMap;
37
import java.util.Map.Entry;
39
import org.apache.commons.logging.Log;
40
import org.apache.commons.logging.LogFactory;
41
import org.hisp.dhis.dataelement.Operand;
42
import org.hisp.dhis.datamart.CrossTabDataValue;
43
import org.hisp.dhis.organisationunit.OrganisationUnit;
44
import org.hisp.dhis.organisationunit.OrganisationUnitHierarchy;
45
import org.hisp.dhis.period.Period;
48
* @author Lars Helge Overland
49
* @version $Id: SumIntAggregator.java 6049 2008-10-28 09:36:17Z larshelg $
51
public class SumIntAggregator
52
extends DataElementAggregator
54
private static final Log log = LogFactory.getLog( SumIntAggregator.class );
56
public Map<Operand, Double> getAggregatedValues( final Map<Operand, Integer> operandIndexMap, final Period period, final OrganisationUnit unit )
58
final OrganisationUnitHierarchy hierarchy = aggregationCache.getLatestOrganisationUnitHierarchy();
60
final Collection<CrossTabDataValue> crossTabValues =
61
getCrossTabDataValues( operandIndexMap, period.getStartDate(), period.getEndDate(), unit.getId(), hierarchy );
63
final Map<Operand, Double[]> entries = getAggregate( crossTabValues, period.getStartDate(),
64
period.getEndDate(), period.getStartDate(), period.getEndDate() ); // <Operand, [total value, total relevant days]>
66
final Map<Operand, Double> values = new HashMap<Operand, Double>( entries.size() ); // <Operand, total value>
68
for ( final Entry<Operand, Double[]> entry : entries.entrySet() )
70
if ( entry.getValue() != null && entry.getValue()[ 1 ] > 0 )
72
values.put( entry.getKey(), entry.getValue()[ 0 ] );
79
protected Collection<CrossTabDataValue> getCrossTabDataValues( final Map<Operand, Integer> operandIndexMap,
80
final Date startDate, final Date endDate, final int parentId, final OrganisationUnitHierarchy hierarchy )
82
final Collection<Integer> sourceIds = aggregationCache.getChildren( hierarchy, parentId );
84
final Collection<Period> periods = aggregationCache.getIntersectingPeriods( startDate, endDate );
86
final Collection<Integer> periodIds = new ArrayList<Integer>( periods.size() );
88
for ( final Period period : periods )
90
periodIds.add( period.getId() );
93
return dataMartStore.getCrossTabDataValues( operandIndexMap, periodIds, sourceIds );
96
protected Map<Operand, Double[]> getAggregate( final Collection<CrossTabDataValue> crossTabValues,
97
final Date startDate, final Date endDate, final Date aggregationStartDate, final Date aggregationEndDate )
99
final Map<Operand, Double[]> totalSums = new HashMap<Operand, Double[]>(); // <Operand, [total value, total relevant days]>
101
Period period = null;
102
Date currentStartDate = null;
103
Date currentEndDate = null;
106
double relevantDays = 0.0;
108
double existingValue = 0.0;
109
double existingRelevantDays = 0.0;
111
for ( final CrossTabDataValue crossTabValue : crossTabValues )
113
period = aggregationCache.getPeriod( crossTabValue.getPeriodId() );
115
currentStartDate = period.getStartDate();
116
currentEndDate = period.getEndDate();
118
double duration = getDays( currentEndDate ) - getDays( currentStartDate );
122
for ( final Entry<Operand, String> entry : crossTabValue.getValueMap().entrySet() ) // <Operand, value>
124
if ( entry.getValue() != null )
130
value = Double.parseDouble( entry.getValue() );
132
catch ( NumberFormatException ex )
134
log.warn( "Value skipped, not numeric: '" + entry.getValue() +
135
"', for data element with id: '" + entry.getKey() +
136
"', for period with id: '" + crossTabValue.getPeriodId() +
137
"', for source with id: '" + crossTabValue.getSourceId() + "'" );
143
if ( currentStartDate.compareTo( startDate ) >= 0 && currentEndDate.compareTo( endDate ) <= 0 )
145
relevantDays = getDays( endDate ) - getDays( startDate );
148
else if ( currentStartDate.compareTo( startDate ) <= 0 && currentEndDate.compareTo( endDate ) >= 0 )
150
relevantDays = getDays( endDate ) - getDays( startDate );
151
factor = relevantDays / duration;
153
else if ( currentStartDate.compareTo( startDate ) <= 0 && currentEndDate.compareTo( startDate ) >= 0
154
&& currentEndDate.compareTo( endDate ) <= 0 )
156
relevantDays = getDays( currentEndDate ) - getDays( startDate );
157
factor = relevantDays / duration;
159
else if ( currentStartDate.compareTo( startDate ) >= 0 && currentStartDate.compareTo( endDate ) <= 0
160
&& currentEndDate.compareTo( endDate ) >= 0 )
162
relevantDays = getDays( endDate ) - getDays( currentStartDate );
163
factor = relevantDays / duration;
166
value = value * factor;
168
existingValue = totalSums.containsKey( entry.getKey() ) ? totalSums.get( entry.getKey() )[ 0 ] : 0;
169
existingRelevantDays = totalSums.containsKey( entry.getKey() ) ? totalSums.get( entry.getKey() )[ 1 ] : 0;
171
final Double[] values = { ( value + existingValue ), ( relevantDays + existingRelevantDays ) };
173
totalSums.put( entry.getKey(), values );