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

« back to all changes in this revision

Viewing changes to local/in/dhis-service-aggregationengine-default/src/main/java/org/hisp/dhis/aggregation/impl/cache/MemoryAggregationCache.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.aggregation.impl.cache;
 
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
 
 
36
import org.hisp.dhis.aggregation.AggregationService;
 
37
import org.hisp.dhis.dataelement.DataElement;
 
38
import org.hisp.dhis.dataelement.DataElementCategoryOptionCombo;
 
39
import org.hisp.dhis.organisationunit.OrganisationUnit;
 
40
import org.hisp.dhis.organisationunit.OrganisationUnitHierarchy;
 
41
import org.hisp.dhis.organisationunit.OrganisationUnitStore;
 
42
import org.hisp.dhis.period.Period;
 
43
import org.hisp.dhis.period.PeriodService;
 
44
 
 
45
/**
 
46
 * @author Lars Helge Overland
 
47
 * @version $Id: MemoryAggregationCache.java 5280 2008-05-28 10:10:29Z larshelg $
 
48
 */
 
49
public class MemoryAggregationCache
 
50
    implements AggregationCache
 
51
{
 
52
    private Map<String, Collection<Integer>> childrenCache = new HashMap<String, Collection<Integer>>();
 
53
 
 
54
    private Map<String, Collection<OrganisationUnitHierarchy>> hierarchyCache = new HashMap<String, Collection<OrganisationUnitHierarchy>>();
 
55
    
 
56
    private Map<String, Period> periodCache = new HashMap<String, Period>();
 
57
    
 
58
    private Map<String, Collection<Integer>> periodIdCache = new HashMap<String, Collection<Integer>>();
 
59
    
 
60
    private static final String SEPARATOR = "-";
 
61
 
 
62
    // -------------------------------------------------------------------------
 
63
    // Dependencies
 
64
    // -------------------------------------------------------------------------
 
65
 
 
66
    private OrganisationUnitStore organisationUnitStore;
 
67
 
 
68
    public void setOrganisationUnitStore( OrganisationUnitStore organisationUnitStore )
 
69
    {
 
70
        this.organisationUnitStore = organisationUnitStore;
 
71
    }
 
72
    
 
73
    private PeriodService periodService;
 
74
 
 
75
    public void setPeriodService( PeriodService periodService )
 
76
    {
 
77
        this.periodService = periodService;
 
78
    }
 
79
    
 
80
    private AggregationService aggregationService;
 
81
 
 
82
    public void setAggregationService( AggregationService aggregationService )
 
83
    {
 
84
        this.aggregationService = aggregationService;
 
85
    }
 
86
 
 
87
    // -------------------------------------------------------------------------
 
88
    // AggregationCache implementation
 
89
    // -------------------------------------------------------------------------
 
90
 
 
91
    public Collection<Integer> getChildren( OrganisationUnitHierarchy hierarchy, int parentId )
 
92
    {
 
93
        String key = hierarchy.getId() + SEPARATOR + parentId;
 
94
 
 
95
        Collection<Integer> children = childrenCache.get( key );
 
96
 
 
97
        if ( children != null )
 
98
        {
 
99
            return children;
 
100
        }
 
101
 
 
102
        children = organisationUnitStore.getChildren( hierarchy, parentId );
 
103
        
 
104
        childrenCache.put( key, children );
 
105
        
 
106
        return children;
 
107
    }
 
108
 
 
109
    public Collection<OrganisationUnitHierarchy> getOrganisationUnitHierarchies( Date startDate, Date endDate )
 
110
    {
 
111
        String key = startDate.toString() + SEPARATOR + endDate.toString();
 
112
 
 
113
        Collection<OrganisationUnitHierarchy> hierarchies = hierarchyCache.get( key );
 
114
 
 
115
        if ( hierarchies != null )
 
116
        {
 
117
            return hierarchies;
 
118
        }
 
119
 
 
120
        hierarchies = organisationUnitStore.getOrganisationUnitHierarchies( startDate, endDate );
 
121
        
 
122
        hierarchyCache.put( key, hierarchies );
 
123
        
 
124
        return hierarchies;
 
125
    }
 
126
    
 
127
    public Period getPeriod( int periodId )
 
128
    {
 
129
        String key = String.valueOf( periodId );
 
130
        
 
131
        Period period = periodCache.get( key );
 
132
        
 
133
        if ( period != null )
 
134
        {
 
135
            return period;
 
136
        }
 
137
        
 
138
        period = periodService.getPeriod( periodId );
 
139
        
 
140
        periodCache.put( key, period );
 
141
        
 
142
        return period;
 
143
    }
 
144
    
 
145
    public Collection<Integer> getPeriodIds( Date startDate, Date endDate )
 
146
    {
 
147
        String key = startDate.toString() + SEPARATOR + endDate.toString();
 
148
        
 
149
        Collection<Integer> periodIds = periodIdCache.get( key );
 
150
        
 
151
        if ( periodIds != null )
 
152
        {
 
153
            return periodIds;
 
154
        }
 
155
        
 
156
        Collection<Period> periods = periodService.getIntersectingPeriods( startDate, endDate );
 
157
        
 
158
        periodIds = new ArrayList<Integer>();
 
159
        
 
160
        for ( Period period : periods )
 
161
        {
 
162
            periodIds.add( period.getId() );
 
163
        }
 
164
        
 
165
        periodIdCache.put( key, periodIds );
 
166
        
 
167
        return periodIds;
 
168
    }    
 
169
    
 
170
    public double getAggregatedDataValue( DataElement dataElement, DataElementCategoryOptionCombo optionCombo, Date startDate, Date endDate, OrganisationUnit organisationUnit )
 
171
    {
 
172
        //String key = dataElement.getId() + SEPARATOR + optionCombo.getId() + "-" + startDate.toString() + "-" + endDate.toString() + "-" + organisationUnit.getId();
 
173
        
 
174
        /*Double value = aggregatedValueCache.get( key );
 
175
        
 
176
        if ( value != null )
 
177
        {
 
178
            return value.doubleValue();
 
179
        }*/
 
180
        
 
181
        Double value = aggregationService.getAggregatedDataValue( dataElement, optionCombo, startDate, endDate, organisationUnit );
 
182
        
 
183
        /*if ( value != AggregationService.NO_VALUES_REGISTERED )
 
184
        {
 
185
            aggregatedValueCache.put( key, value );
 
186
        }*/
 
187
        
 
188
        return value.doubleValue();
 
189
    }
 
190
}