1
package org.hisp.dhis.aggregation.impl.cache;
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;
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;
46
* @author Lars Helge Overland
47
* @version $Id: MemoryAggregationCache.java 5280 2008-05-28 10:10:29Z larshelg $
49
public class MemoryAggregationCache
50
implements AggregationCache
52
private Map<String, Collection<Integer>> childrenCache = new HashMap<String, Collection<Integer>>();
54
private Map<String, Collection<OrganisationUnitHierarchy>> hierarchyCache = new HashMap<String, Collection<OrganisationUnitHierarchy>>();
56
private Map<String, Period> periodCache = new HashMap<String, Period>();
58
private Map<String, Collection<Integer>> periodIdCache = new HashMap<String, Collection<Integer>>();
60
private static final String SEPARATOR = "-";
62
// -------------------------------------------------------------------------
64
// -------------------------------------------------------------------------
66
private OrganisationUnitStore organisationUnitStore;
68
public void setOrganisationUnitStore( OrganisationUnitStore organisationUnitStore )
70
this.organisationUnitStore = organisationUnitStore;
73
private PeriodService periodService;
75
public void setPeriodService( PeriodService periodService )
77
this.periodService = periodService;
80
private AggregationService aggregationService;
82
public void setAggregationService( AggregationService aggregationService )
84
this.aggregationService = aggregationService;
87
// -------------------------------------------------------------------------
88
// AggregationCache implementation
89
// -------------------------------------------------------------------------
91
public Collection<Integer> getChildren( OrganisationUnitHierarchy hierarchy, int parentId )
93
String key = hierarchy.getId() + SEPARATOR + parentId;
95
Collection<Integer> children = childrenCache.get( key );
97
if ( children != null )
102
children = organisationUnitStore.getChildren( hierarchy, parentId );
104
childrenCache.put( key, children );
109
public Collection<OrganisationUnitHierarchy> getOrganisationUnitHierarchies( Date startDate, Date endDate )
111
String key = startDate.toString() + SEPARATOR + endDate.toString();
113
Collection<OrganisationUnitHierarchy> hierarchies = hierarchyCache.get( key );
115
if ( hierarchies != null )
120
hierarchies = organisationUnitStore.getOrganisationUnitHierarchies( startDate, endDate );
122
hierarchyCache.put( key, hierarchies );
127
public Period getPeriod( int periodId )
129
String key = String.valueOf( periodId );
131
Period period = periodCache.get( key );
133
if ( period != null )
138
period = periodService.getPeriod( periodId );
140
periodCache.put( key, period );
145
public Collection<Integer> getPeriodIds( Date startDate, Date endDate )
147
String key = startDate.toString() + SEPARATOR + endDate.toString();
149
Collection<Integer> periodIds = periodIdCache.get( key );
151
if ( periodIds != null )
156
Collection<Period> periods = periodService.getIntersectingPeriods( startDate, endDate );
158
periodIds = new ArrayList<Integer>();
160
for ( Period period : periods )
162
periodIds.add( period.getId() );
165
periodIdCache.put( key, periodIds );
170
public double getAggregatedDataValue( DataElement dataElement, DataElementCategoryOptionCombo optionCombo, Date startDate, Date endDate, OrganisationUnit organisationUnit )
172
//String key = dataElement.getId() + SEPARATOR + optionCombo.getId() + "-" + startDate.toString() + "-" + endDate.toString() + "-" + organisationUnit.getId();
174
/*Double value = aggregatedValueCache.get( key );
178
return value.doubleValue();
181
Double value = aggregationService.getAggregatedDataValue( dataElement, optionCombo, startDate, endDate, organisationUnit );
183
/*if ( value != AggregationService.NO_VALUES_REGISTERED )
185
aggregatedValueCache.put( key, value );
188
return value.doubleValue();