1
package org.hisp.dhis.de.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.ArrayList;
31
import java.util.Date;
32
import java.util.HashSet;
33
import java.util.Iterator;
34
import java.util.List;
38
import org.apache.commons.logging.Log;
39
import org.apache.commons.logging.LogFactory;
40
import org.hisp.dhis.dataelement.DataElement;
41
import org.hisp.dhis.dataelement.DataElementCategory;
42
import org.hisp.dhis.dataset.DataSet;
43
import org.hisp.dhis.dataset.DataSetService;
44
import org.hisp.dhis.organisationunit.OrganisationUnit;
45
import org.hisp.dhis.ouwt.manager.OrganisationUnitSelectionManager;
46
import org.hisp.dhis.period.CalendarPeriodType;
47
import org.hisp.dhis.period.Period;
48
import org.hisp.dhis.period.PeriodType;
49
import org.hisp.dhis.user.CurrentUserService;
50
import org.hisp.dhis.user.UserAuthorityGroup;
51
import org.hisp.dhis.user.UserCredentials;
52
import org.hisp.dhis.user.UserStore;
54
import com.opensymphony.xwork.ActionContext;
57
* @author Torgeir Lorange Ostby
58
* @version $Id: DefaultSelectedStateManager.java 5282 2008-05-28 10:41:06Z
61
public class DefaultSelectedStateManager
62
implements SelectedStateManager
64
private static final Log LOG = LogFactory.getLog( DefaultSelectedStateManager.class );
66
public static final String SESSION_KEY_SELECTED_DATASET_ID = "data_entry_selected_dataset_id";
68
public static final String SESSION_KEY_SELECTED_PERIOD_INDEX = "data_entry_selected_period_index";
70
public static final String SESSION_KEY_BASE_PERIOD = "data_entry_base_period";
72
// -------------------------------------------------------------------------
74
// -------------------------------------------------------------------------
76
private DataSetService dataSetService;
78
public void setDataSetService( DataSetService dataSetService )
80
this.dataSetService = dataSetService;
83
private OrganisationUnitSelectionManager selectionManager;
85
public void setSelectionManager( OrganisationUnitSelectionManager selectionManager )
87
this.selectionManager = selectionManager;
90
private UserStore userStore;
92
public void setUserStore( UserStore userStore )
94
this.userStore = userStore;
97
private CurrentUserService currentUserService;
99
public void setCurrentUserService( CurrentUserService currentUserService )
101
this.currentUserService = currentUserService;
104
// -------------------------------------------------------------------------
106
// -------------------------------------------------------------------------
108
private ThreadLocal<List<Period>> generatedPeriodsCache = new ThreadLocal<List<Period>>();
110
// -------------------------------------------------------------------------
111
// SelectedStateManager implementation
112
// -------------------------------------------------------------------------
114
public OrganisationUnit getSelectedOrganisationUnit()
116
return selectionManager.getSelectedOrganisationUnit();
119
@SuppressWarnings( "unchecked" )
120
public void setSelectedDataSet( DataSet dataSet )
122
getSession().put( SESSION_KEY_SELECTED_DATASET_ID, dataSet.getId() );
125
public DataSet getSelectedDataSet()
127
Integer id = (Integer) getSession().get( SESSION_KEY_SELECTED_DATASET_ID );
134
return dataSetService.getDataSet( id );
137
public void clearSelectedDataSet()
139
getSession().remove( SESSION_KEY_SELECTED_DATASET_ID );
142
@SuppressWarnings( "unchecked" )
143
public void setSelectedPeriodIndex( Integer index )
145
getSession().put( SESSION_KEY_SELECTED_PERIOD_INDEX, index );
148
public Integer getSelectedPeriodIndex()
150
return (Integer) getSession().get( SESSION_KEY_SELECTED_PERIOD_INDEX );
153
public Period getSelectedPeriod()
155
Integer index = getSelectedPeriodIndex();
162
List<Period> periods = getPeriodList();
164
if ( index >= 0 && index < periods.size() )
166
return periods.get( index );
172
public void clearSelectedPeriod()
174
getSession().remove( SESSION_KEY_SELECTED_PERIOD_INDEX );
177
public List<Period> getPeriodList()
179
List<Period> periods = generatedPeriodsCache.get();
180
Period basePeriod = getBasePeriod();
182
if ( periods == null || periods.size() == 0
183
|| !periods.get( 0 ).getPeriodType().equals( basePeriod.getPeriodType() ) || !periods.contains( basePeriod ) )
185
CalendarPeriodType periodType = (CalendarPeriodType) getPeriodType();
187
LOG.debug( "Generated periods cache invalid, generating new periods based on " + basePeriod );
189
periods = periodType.generatePeriods( basePeriod );
191
generatedPeriodsCache.set( periods );
197
@SuppressWarnings( "unchecked" )
198
public void nextPeriodSpan()
200
List<Period> periods = getPeriodList();
201
CalendarPeriodType periodType = (CalendarPeriodType) getPeriodType();
203
Period basePeriod = periods.get( periods.size() - 1 );
204
Period newBasePeriod = periodType.getNextPeriod( basePeriod );
206
if ( newBasePeriod.getStartDate().before( new Date() ) ) // Future periods not allowed
208
getSession().put( SESSION_KEY_BASE_PERIOD, newBasePeriod );
210
generatedPeriodsCache.remove();
214
@SuppressWarnings( "unchecked" )
215
public void previousPeriodSpan()
217
List<Period> periods = getPeriodList();
218
CalendarPeriodType periodType = (CalendarPeriodType) getPeriodType();
220
Period basePeriod = periods.get( 0 );
221
Period newBasePeriod = periodType.getPreviousPeriod( basePeriod );
223
getSession().put( SESSION_KEY_BASE_PERIOD, newBasePeriod );
225
generatedPeriodsCache.remove();
228
// -------------------------------------------------------------------------
230
// -------------------------------------------------------------------------
232
private PeriodType getPeriodType()
234
DataSet dataSet = getSelectedDataSet();
236
if ( dataSet == null )
238
throw new IllegalStateException( "Cannot ask for PeriodType when no DataSet is selected" );
241
return dataSet.getPeriodType();
244
@SuppressWarnings( "unchecked" )
245
private Period getBasePeriod()
247
Period basePeriod = (Period) getSession().get( SESSION_KEY_BASE_PERIOD );
248
PeriodType periodType = getPeriodType();
250
if ( basePeriod == null )
252
LOG.debug( "getBasePeriod(): Base period is null, creating new." );
254
basePeriod = periodType.createPeriod();
255
getSession().put( SESSION_KEY_BASE_PERIOD, basePeriod );
257
else if ( !basePeriod.getPeriodType().equals( periodType ) )
259
LOG.debug( "getBasePeriod(): Wrong type of base period, transforming." );
261
basePeriod = periodType.createPeriod( basePeriod.getStartDate() );
262
getSession().put( SESSION_KEY_BASE_PERIOD, basePeriod );
268
@SuppressWarnings( "unchecked" )
269
private static final Map getSession()
271
return ActionContext.getContext().getSession();
274
public boolean hasDataSetMultiDimensionalDataElement( DataSet dataSet )
276
int numberOfTotalColumns = 1;
278
if ( dataSet.getDataElements().size() > 0 )
280
for ( DataElement de : dataSet.getDataElements() )
282
for ( DataElementCategory category : de.getCategoryCombo().getCategories() )
284
numberOfTotalColumns = numberOfTotalColumns * category.getCategoryOptions().size();
287
if ( numberOfTotalColumns > 1 )
297
public List<DataSet> loadDataSetsForSelectedOrgUnit( OrganisationUnit organisationUnit )
299
List<DataSet> dataSets = new ArrayList<DataSet>( dataSetService.getDataSetsBySource( organisationUnit ) );
301
if ( !currentUserService.currentUserIsSuper() )
303
UserCredentials userCredentials = userStore.getUserCredentials( currentUserService.getCurrentUser() );
305
Set<DataSet> dataSetUserAuthorityGroups = new HashSet<DataSet>();
307
for ( UserAuthorityGroup userAuthorityGroup : userCredentials.getUserAuthorityGroups() )
309
dataSetUserAuthorityGroups.addAll( userAuthorityGroup.getDataSets() );
312
dataSets.retainAll( dataSetUserAuthorityGroups );
315
// ---------------------------------------------------------------------
316
// Remove DataSets which don't have a CalendarPeriodType
317
// ---------------------------------------------------------------------
319
Iterator<DataSet> iterator = dataSets.iterator();
321
while ( iterator.hasNext() )
323
DataSet dataSet = iterator.next();
325
if ( !(dataSet.getPeriodType() instanceof CalendarPeriodType) )