~mortenoh/+junk/dhis2-detailed-import-export

« back to all changes in this revision

Viewing changes to dhis-2/dhis-services/dhis-service-datamart-default/src/main/java/org/hisp/dhis/datamart/aggregation/dataelement/SumBoolAggregator.java

  • Committer: larshelge at gmail
  • Date: 2009-03-03 16:46:36 UTC
  • Revision ID: larshelge@gmail.com-20090303164636-2sjlrquo7ib1gf7r
Initial check-in

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package org.hisp.dhis.datamart.aggregation.dataelement;
 
2
 
 
3
/*
 
4
 * Copyright (c) 2004-2007, University of Oslo
 
5
 * All rights reserved.
 
6
 *
 
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.
 
17
 *
 
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.
 
28
 */
 
29
 
 
30
import java.util.ArrayList;
 
31
import java.util.Collection;
 
32
import java.util.Date;
 
33
import java.util.HashMap;
 
34
import java.util.Map;
 
35
import java.util.Map.Entry;
 
36
 
 
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;
 
42
 
 
43
import static org.hisp.dhis.system.util.MathUtils.getDays;
 
44
import static org.hisp.dhis.system.util.MathUtils.getFloor;
 
45
 
 
46
/**
 
47
 * @author Lars Helge Overland
 
48
 * @version $Id: SumBoolAggregator.java 6049 2008-10-28 09:36:17Z larshelg $
 
49
 */
 
50
public class SumBoolAggregator
 
51
    extends DataElementAggregator
 
52
{
 
53
    public Map<Operand, Double> getAggregatedValues( final Map<Operand, Integer> operandIndexMap, final Period period, final OrganisationUnit unit )
 
54
    {
 
55
        final OrganisationUnitHierarchy hierarchy = aggregationCache.getLatestOrganisationUnitHierarchy();
 
56
        
 
57
        final Collection<CrossTabDataValue> crossTabValues = 
 
58
            getCrossTabDataValues( operandIndexMap, period.getStartDate(), period.getEndDate(), unit.getId(), hierarchy );
 
59
        
 
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]>
 
62
 
 
63
        final Map<Operand, Double> values = new HashMap<Operand, Double>( entries.size() ); // <Operand, total value>
 
64
        
 
65
        for ( final Entry<Operand, Double[]> entry : entries.entrySet() )
 
66
        {
 
67
            if ( entry.getValue() != null && entry.getValue()[ 1 ] > 0 )
 
68
            {
 
69
                values.put( entry.getKey(), getFloor( entry.getValue()[ 0 ] ) );
 
70
            }
 
71
        }
 
72
        
 
73
        return values;
 
74
    }
 
75
 
 
76
    protected Collection<CrossTabDataValue> getCrossTabDataValues( final Map<Operand, Integer> operandIndexMap, 
 
77
        final Date startDate, final Date endDate, final int parentId, final OrganisationUnitHierarchy hierarchy )
 
78
    {
 
79
        final Collection<Integer> sourceIds = aggregationCache.getChildren( hierarchy, parentId );
 
80
        
 
81
        final Collection<Period> periods = aggregationCache.getIntersectingPeriods( startDate, endDate );
 
82
        
 
83
        final Collection<Integer> periodIds = new ArrayList<Integer>( periods.size() );
 
84
        
 
85
        for ( final Period period : periods )
 
86
        {
 
87
            periodIds.add( period.getId() );
 
88
        }
 
89
        
 
90
        return dataMartStore.getCrossTabDataValues( operandIndexMap, periodIds, sourceIds );
 
91
    }
 
92
    
 
93
    protected Map<Operand, Double[]> getAggregate( final Collection<CrossTabDataValue> crossTabValues, 
 
94
        final Date startDate, final Date endDate, final Date aggregationStartDate, final Date aggregationEndDate )
 
95
    {
 
96
        final Map<Operand, Double[]> totalSums = new HashMap<Operand, Double[]>(); // <Operand, [total value, total relevant days]>
 
97
 
 
98
        Period period = null;
 
99
        Date currentStartDate = null;
 
100
        Date currentEndDate = null;
 
101
        
 
102
        double duration = 0.0;
 
103
        double value = 0.0;
 
104
        double relevantDays = 0.0;
 
105
        double factor = 0.0;
 
106
        double existingValue = 0.0;
 
107
        double existingRelevantDays = 0.0;
 
108
        
 
109
        for ( final CrossTabDataValue crossTabValue : crossTabValues )
 
110
        {
 
111
            period = aggregationCache.getPeriod( crossTabValue.getPeriodId() );
 
112
            
 
113
            currentStartDate = period.getStartDate();
 
114
            currentEndDate = period.getEndDate();
 
115
            
 
116
            duration = getDays( currentEndDate ) - getDays( currentStartDate );
 
117
            
 
118
            if ( duration > 0 )
 
119
            {
 
120
                for ( final Entry<Operand, String> entry : crossTabValue.getValueMap().entrySet() ) // <Operand, value>
 
121
                {
 
122
                    if ( entry.getValue() != null )
 
123
                    {
 
124
                        value = 0;
 
125
                        
 
126
                        relevantDays = 0;
 
127
                        factor = 0;
 
128
 
 
129
                        if ( currentStartDate.compareTo( startDate ) >= 0 && currentEndDate.compareTo( endDate ) <= 0 )
 
130
                        {
 
131
                            relevantDays = ( getDays( endDate ) - getDays( startDate ) );
 
132
                            factor = 1;
 
133
                        }
 
134
                        else if ( currentStartDate.compareTo( startDate ) <= 0 && currentEndDate.compareTo( endDate ) >= 0 )
 
135
                        {
 
136
                            relevantDays = ( getDays( endDate ) - getDays( startDate ) );
 
137
                            factor = relevantDays / duration;
 
138
                        }
 
139
                        else if ( currentStartDate.compareTo( startDate ) <= 0 && currentEndDate.compareTo( startDate ) >= 0
 
140
                            && currentEndDate.compareTo( endDate ) <= 0 )
 
141
                        {
 
142
                            relevantDays = ( getDays( currentEndDate ) - getDays( startDate ) );
 
143
                            factor = relevantDays / duration;
 
144
                        }
 
145
                        else if ( currentStartDate.compareTo( startDate ) >= 0 && currentStartDate.compareTo( endDate ) <= 0
 
146
                            && currentEndDate.compareTo( endDate ) >= 0 )
 
147
                        {
 
148
                            relevantDays = ( getDays( endDate ) - getDays( currentStartDate ) );
 
149
                            factor = relevantDays / duration;
 
150
                        }
 
151
 
 
152
                        if ( entry.getValue().toLowerCase().equals( TRUE ) )
 
153
                        {
 
154
                            value = 1;
 
155
                        }
 
156
                        
 
157
                        value = value * factor;
 
158
                        
 
159
                        existingValue = totalSums.containsKey( entry.getKey() ) ? totalSums.get( entry.getKey() )[ 0 ] : 0;
 
160
                        existingRelevantDays = totalSums.containsKey( entry.getKey() ) ? totalSums.get( entry.getKey() )[ 1 ] : 0;
 
161
 
 
162
                        final Double[] values = { ( value + existingValue ), ( relevantDays + existingRelevantDays ) };
 
163
                        
 
164
                        totalSums.put( entry.getKey(), values );
 
165
                    }
 
166
                }
 
167
            }
 
168
        }
 
169
        
 
170
        return totalSums;
 
171
    }
 
172
}
 
173
 
 
174
 
 
175
 
 
176