1
package org.hisp.dhis.reporting.dataset.state;
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.Collection;
31
import java.util.Date;
32
import java.util.Iterator;
33
import java.util.List;
36
import org.apache.commons.logging.Log;
37
import org.apache.commons.logging.LogFactory;
38
import org.hisp.dhis.dataset.DataSet;
39
import org.hisp.dhis.dataset.DataSetService;
40
import org.hisp.dhis.organisationunit.OrganisationUnit;
41
import org.hisp.dhis.oust.manager.SelectionTreeManager;
42
import org.hisp.dhis.period.CalendarPeriodType;
43
import org.hisp.dhis.period.Period;
44
import org.hisp.dhis.period.PeriodService;
45
import org.hisp.dhis.period.PeriodType;
47
import com.opensymphony.xwork.ActionContext;
50
* @author Torgeir Lorange Ostby
51
* @version $Id: DefaultSelectedStateManager.java 5282 2008-05-28 10:41:06Z
54
public class DefaultSelectedStateManager
55
implements SelectedStateManager
57
private static final Log LOG = LogFactory.getLog( DefaultSelectedStateManager.class );
59
public static final String SESSION_KEY_SELECTED_DATASET_ID = "dataset_report_selected_dataset_id";
61
public static final String SESSION_KEY_SELECTED_PERIOD_INDEX = "dataset_report_selected_period_index";
63
public static final String SESSION_KEY_BASE_PERIOD = "dataset_report_base_period";
65
// -------------------------------------------------------------------------
67
// -------------------------------------------------------------------------
69
private PeriodService periodService;
71
public void setPeriodService( PeriodService periodService )
73
this.periodService = periodService;
76
private DataSetService dataSetService;
78
public void setDataSetService( DataSetService dataSetService )
80
this.dataSetService = dataSetService;
83
private SelectionTreeManager selectionTreeManager;
85
public void setSelectionTreeManager( SelectionTreeManager selectionTreeManager )
87
this.selectionTreeManager = selectionTreeManager;
90
// -------------------------------------------------------------------------
92
// -------------------------------------------------------------------------
94
private ThreadLocal<List<Period>> generatedPeriodsCache = new ThreadLocal<List<Period>>();
96
// -------------------------------------------------------------------------
97
// SelectedStateManager implementation
98
// -------------------------------------------------------------------------
100
public OrganisationUnit getSelectedOrganisationUnit()
102
return selectionTreeManager.getSelectedOrganisationUnit();
105
@SuppressWarnings( "unchecked" )
106
public void setSelectedDataSet( DataSet dataSet )
108
getSession().put( SESSION_KEY_SELECTED_DATASET_ID, dataSet.getId() );
111
public DataSet getSelectedDataSet()
113
Integer id = (Integer) getSession().get( SESSION_KEY_SELECTED_DATASET_ID );
120
return dataSetService.getDataSet( id );
123
public void clearSelectedDataSet()
125
getSession().remove( SESSION_KEY_SELECTED_DATASET_ID );
128
@SuppressWarnings( "unchecked" )
129
public void setSelectedPeriodIndex( Integer index )
131
getSession().put( SESSION_KEY_SELECTED_PERIOD_INDEX, index );
134
public Integer getSelectedPeriodIndex()
136
return (Integer) getSession().get( SESSION_KEY_SELECTED_PERIOD_INDEX );
139
public Period getSelectedPeriod()
141
Integer index = getSelectedPeriodIndex();
148
List<Period> periods = getPeriodList();
150
if ( index >= 0 && index < periods.size() )
152
Period selectedPeriod = periods.get( index );
154
Period period = null;
156
for ( Period p : periodService.getAllPeriods() )
158
if ( selectedPeriod.equals( p ) )
170
public void clearSelectedPeriod()
172
getSession().remove( SESSION_KEY_SELECTED_PERIOD_INDEX );
175
public List<Period> getPeriodList()
177
List<Period> periods = generatedPeriodsCache.get();
178
Period basePeriod = getBasePeriod();
180
if ( periods == null || periods.size() == 0
181
|| !periods.get( 0 ).getPeriodType().equals( basePeriod.getPeriodType() ) || !periods.contains( basePeriod ) )
183
CalendarPeriodType periodType = (CalendarPeriodType) getPeriodType();
185
LOG.debug( "Generated periods cache invalid, generating new periods based on " + basePeriod );
187
periods = periodType.generatePeriods( basePeriod );
189
generatedPeriodsCache.set( periods );
192
Date now = new Date();
194
Iterator<Period> iterator = periods.iterator();
196
Collection<Period> persistedPeriods = periodService.getAllPeriods();
198
while ( iterator.hasNext() )
200
Period period = iterator.next();
202
if ( period.getStartDate().after( now ) || !persistedPeriods.contains( period ))
211
@SuppressWarnings( "unchecked" )
212
public void nextPeriodSpan()
214
List<Period> periods = getPeriodList();
215
CalendarPeriodType periodType = (CalendarPeriodType) getPeriodType();
217
Period basePeriod = periods.get( periods.size() - 1 );
218
Period newBasePeriod = periodType.getNextPeriod( basePeriod );
220
getSession().put( SESSION_KEY_BASE_PERIOD, newBasePeriod );
222
generatedPeriodsCache.remove();
225
@SuppressWarnings( "unchecked" )
226
public void previousPeriodSpan()
228
List<Period> periods = getPeriodList();
229
CalendarPeriodType periodType = (CalendarPeriodType) getPeriodType();
231
Period basePeriod = periods.get( 0 );
232
Period newBasePeriod = periodType.getPreviousPeriod( basePeriod );
234
getSession().put( SESSION_KEY_BASE_PERIOD, newBasePeriod );
236
generatedPeriodsCache.remove();
239
// -------------------------------------------------------------------------
241
// -------------------------------------------------------------------------
243
private PeriodType getPeriodType()
245
DataSet dataSet = getSelectedDataSet();
247
if ( dataSet == null )
249
throw new IllegalStateException( "Cannot ask for PeriodType when no DataSet is selected" );
252
return dataSet.getPeriodType();
255
@SuppressWarnings( "unchecked" )
256
private Period getBasePeriod()
258
Period basePeriod = (Period) getSession().get( SESSION_KEY_BASE_PERIOD );
259
PeriodType periodType = getPeriodType();
261
if ( basePeriod == null )
263
LOG.debug( "getBasePeriod(): Base period is null, creating new." );
265
basePeriod = periodType.createPeriod();
266
getSession().put( SESSION_KEY_BASE_PERIOD, basePeriod );
268
else if ( !basePeriod.getPeriodType().equals( periodType ) )
270
LOG.debug( "getBasePeriod(): Wrong type of base period, transforming." );
272
basePeriod = periodType.createPeriod( basePeriod.getStartDate() );
273
getSession().put( SESSION_KEY_BASE_PERIOD, basePeriod );
276
if ( periodService.getAllPeriods().contains( basePeriod ))
281
CalendarPeriodType calendarPeriodType = (CalendarPeriodType) getPeriodType();
282
return calendarPeriodType.getPreviousPeriod( basePeriod );
286
@SuppressWarnings( "unchecked" )
287
private static final Map getSession()
289
return ActionContext.getContext().getSession();