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 java.util.ArrayList;
31
import java.util.Collection;
32
import java.util.Date;
33
import java.util.HashMap;
35
import java.util.Map.Entry;
37
import org.hisp.dhis.dataelement.Operand;
38
import org.hisp.dhis.datamart.CrossTabDataValue;
39
import org.hisp.dhis.organisationunit.OrganisationUnit;
40
import org.hisp.dhis.organisationunit.OrganisationUnitHierarchy;
41
import org.hisp.dhis.period.Period;
43
import static org.hisp.dhis.system.util.MathUtils.getDays;
44
import static org.hisp.dhis.system.util.MathUtils.getFloor;
47
* @author Lars Helge Overland
48
* @version $Id: SumBoolAggregator.java 6049 2008-10-28 09:36:17Z larshelg $
50
public class SumBoolAggregator
51
extends DataElementAggregator
53
public Map<Operand, Double> getAggregatedValues( final Map<Operand, Integer> operandIndexMap, final Period period, final OrganisationUnit unit )
55
final OrganisationUnitHierarchy hierarchy = aggregationCache.getLatestOrganisationUnitHierarchy();
57
final Collection<CrossTabDataValue> crossTabValues =
58
getCrossTabDataValues( operandIndexMap, period.getStartDate(), period.getEndDate(), unit.getId(), hierarchy );
60
final Map<Operand, Double[]> entries = getAggregate( crossTabValues, period.getStartDate(),
61
period.getEndDate(), period.getStartDate(), period.getEndDate() ); // <data element id, [total value, total relevant days]>
63
final Map<Operand, Double> values = new HashMap<Operand, Double>( entries.size() ); // <Operand, total value>
65
for ( final Entry<Operand, Double[]> entry : entries.entrySet() )
67
if ( entry.getValue() != null && entry.getValue()[ 1 ] > 0 )
69
values.put( entry.getKey(), getFloor( entry.getValue()[ 0 ] ) );
76
protected Collection<CrossTabDataValue> getCrossTabDataValues( final Map<Operand, Integer> operandIndexMap,
77
final Date startDate, final Date endDate, final int parentId, final OrganisationUnitHierarchy hierarchy )
79
final Collection<Integer> sourceIds = aggregationCache.getChildren( hierarchy, parentId );
81
final Collection<Period> periods = aggregationCache.getIntersectingPeriods( startDate, endDate );
83
final Collection<Integer> periodIds = new ArrayList<Integer>( periods.size() );
85
for ( final Period period : periods )
87
periodIds.add( period.getId() );
90
return dataMartStore.getCrossTabDataValues( operandIndexMap, periodIds, sourceIds );
93
protected Map<Operand, Double[]> getAggregate( final Collection<CrossTabDataValue> crossTabValues,
94
final Date startDate, final Date endDate, final Date aggregationStartDate, final Date aggregationEndDate )
96
final Map<Operand, Double[]> totalSums = new HashMap<Operand, Double[]>(); // <Operand, [total value, total relevant days]>
99
Date currentStartDate = null;
100
Date currentEndDate = null;
102
double duration = 0.0;
104
double relevantDays = 0.0;
106
double existingValue = 0.0;
107
double existingRelevantDays = 0.0;
109
for ( final CrossTabDataValue crossTabValue : crossTabValues )
111
period = aggregationCache.getPeriod( crossTabValue.getPeriodId() );
113
currentStartDate = period.getStartDate();
114
currentEndDate = period.getEndDate();
116
duration = getDays( currentEndDate ) - getDays( currentStartDate );
120
for ( final Entry<Operand, String> entry : crossTabValue.getValueMap().entrySet() ) // <Operand, value>
122
if ( entry.getValue() != null )
129
if ( currentStartDate.compareTo( startDate ) >= 0 && currentEndDate.compareTo( endDate ) <= 0 )
131
relevantDays = ( getDays( endDate ) - getDays( startDate ) );
134
else if ( currentStartDate.compareTo( startDate ) <= 0 && currentEndDate.compareTo( endDate ) >= 0 )
136
relevantDays = ( getDays( endDate ) - getDays( startDate ) );
137
factor = relevantDays / duration;
139
else if ( currentStartDate.compareTo( startDate ) <= 0 && currentEndDate.compareTo( startDate ) >= 0
140
&& currentEndDate.compareTo( endDate ) <= 0 )
142
relevantDays = ( getDays( currentEndDate ) - getDays( startDate ) );
143
factor = relevantDays / duration;
145
else if ( currentStartDate.compareTo( startDate ) >= 0 && currentStartDate.compareTo( endDate ) <= 0
146
&& currentEndDate.compareTo( endDate ) >= 0 )
148
relevantDays = ( getDays( endDate ) - getDays( currentStartDate ) );
149
factor = relevantDays / duration;
152
if ( entry.getValue().toLowerCase().equals( TRUE ) )
157
value = value * factor;
159
existingValue = totalSums.containsKey( entry.getKey() ) ? totalSums.get( entry.getKey() )[ 0 ] : 0;
160
existingRelevantDays = totalSums.containsKey( entry.getKey() ) ? totalSums.get( entry.getKey() )[ 1 ] : 0;
162
final Double[] values = { ( value + existingValue ), ( relevantDays + existingRelevantDays ) };
164
totalSums.put( entry.getKey(), values );