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

« back to all changes in this revision

Viewing changes to dhis-2/dhis-web/dhis-web-dataentry/src/main/java/org/hisp/dhis/de/state/DefaultSelectedStateManager.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.de.state;
 
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.Date;
 
32
import java.util.HashSet;
 
33
import java.util.Iterator;
 
34
import java.util.List;
 
35
import java.util.Map;
 
36
import java.util.Set;
 
37
 
 
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;
 
53
 
 
54
import com.opensymphony.xwork.ActionContext;
 
55
 
 
56
/**
 
57
 * @author Torgeir Lorange Ostby
 
58
 * @version $Id: DefaultSelectedStateManager.java 5282 2008-05-28 10:41:06Z
 
59
 *          larshelg $
 
60
 */
 
61
public class DefaultSelectedStateManager
 
62
    implements SelectedStateManager
 
63
{
 
64
    private static final Log LOG = LogFactory.getLog( DefaultSelectedStateManager.class );
 
65
 
 
66
    public static final String SESSION_KEY_SELECTED_DATASET_ID = "data_entry_selected_dataset_id";
 
67
 
 
68
    public static final String SESSION_KEY_SELECTED_PERIOD_INDEX = "data_entry_selected_period_index";
 
69
 
 
70
    public static final String SESSION_KEY_BASE_PERIOD = "data_entry_base_period";
 
71
 
 
72
    // -------------------------------------------------------------------------
 
73
    // Dependencies
 
74
    // -------------------------------------------------------------------------
 
75
 
 
76
    private DataSetService dataSetService;
 
77
 
 
78
    public void setDataSetService( DataSetService dataSetService )
 
79
    {
 
80
        this.dataSetService = dataSetService;
 
81
    }
 
82
 
 
83
    private OrganisationUnitSelectionManager selectionManager;
 
84
 
 
85
    public void setSelectionManager( OrganisationUnitSelectionManager selectionManager )
 
86
    {
 
87
        this.selectionManager = selectionManager;
 
88
    }
 
89
 
 
90
    private UserStore userStore;
 
91
 
 
92
    public void setUserStore( UserStore userStore )
 
93
    {
 
94
        this.userStore = userStore;
 
95
    }
 
96
 
 
97
    private CurrentUserService currentUserService;
 
98
 
 
99
    public void setCurrentUserService( CurrentUserService currentUserService )
 
100
    {
 
101
        this.currentUserService = currentUserService;
 
102
    }
 
103
 
 
104
    // -------------------------------------------------------------------------
 
105
    // Cache
 
106
    // -------------------------------------------------------------------------
 
107
 
 
108
    private ThreadLocal<List<Period>> generatedPeriodsCache = new ThreadLocal<List<Period>>();
 
109
 
 
110
    // -------------------------------------------------------------------------
 
111
    // SelectedStateManager implementation
 
112
    // -------------------------------------------------------------------------
 
113
 
 
114
    public OrganisationUnit getSelectedOrganisationUnit()
 
115
    {
 
116
        return selectionManager.getSelectedOrganisationUnit();
 
117
    }
 
118
 
 
119
    @SuppressWarnings( "unchecked" )
 
120
    public void setSelectedDataSet( DataSet dataSet )
 
121
    {
 
122
        getSession().put( SESSION_KEY_SELECTED_DATASET_ID, dataSet.getId() );
 
123
    }
 
124
 
 
125
    public DataSet getSelectedDataSet()
 
126
    {
 
127
        Integer id = (Integer) getSession().get( SESSION_KEY_SELECTED_DATASET_ID );
 
128
 
 
129
        if ( id == null )
 
130
        {
 
131
            return null;
 
132
        }
 
133
 
 
134
        return dataSetService.getDataSet( id );
 
135
    }
 
136
 
 
137
    public void clearSelectedDataSet()
 
138
    {
 
139
        getSession().remove( SESSION_KEY_SELECTED_DATASET_ID );
 
140
    }
 
141
 
 
142
    @SuppressWarnings( "unchecked" )
 
143
    public void setSelectedPeriodIndex( Integer index )
 
144
    {
 
145
        getSession().put( SESSION_KEY_SELECTED_PERIOD_INDEX, index );
 
146
    }
 
147
 
 
148
    public Integer getSelectedPeriodIndex()
 
149
    {
 
150
        return (Integer) getSession().get( SESSION_KEY_SELECTED_PERIOD_INDEX );
 
151
    }
 
152
 
 
153
    public Period getSelectedPeriod()
 
154
    {
 
155
        Integer index = getSelectedPeriodIndex();
 
156
 
 
157
        if ( index == null )
 
158
        {
 
159
            return null;
 
160
        }
 
161
 
 
162
        List<Period> periods = getPeriodList();
 
163
 
 
164
        if ( index >= 0 && index < periods.size() )
 
165
        {
 
166
            return periods.get( index );
 
167
        }
 
168
 
 
169
        return null;
 
170
    }
 
171
 
 
172
    public void clearSelectedPeriod()
 
173
    {
 
174
        getSession().remove( SESSION_KEY_SELECTED_PERIOD_INDEX );
 
175
    }
 
176
 
 
177
    public List<Period> getPeriodList()
 
178
    {
 
179
        List<Period> periods = generatedPeriodsCache.get();
 
180
        Period basePeriod = getBasePeriod();
 
181
 
 
182
        if ( periods == null || periods.size() == 0
 
183
            || !periods.get( 0 ).getPeriodType().equals( basePeriod.getPeriodType() ) || !periods.contains( basePeriod ) )
 
184
        {
 
185
            CalendarPeriodType periodType = (CalendarPeriodType) getPeriodType();
 
186
 
 
187
            LOG.debug( "Generated periods cache invalid, generating new periods based on " + basePeriod );
 
188
 
 
189
            periods = periodType.generatePeriods( basePeriod );
 
190
 
 
191
            generatedPeriodsCache.set( periods );
 
192
        }
 
193
 
 
194
        return periods;
 
195
    }
 
196
 
 
197
    @SuppressWarnings( "unchecked" )
 
198
    public void nextPeriodSpan()
 
199
    {
 
200
        List<Period> periods = getPeriodList();
 
201
        CalendarPeriodType periodType = (CalendarPeriodType) getPeriodType();
 
202
 
 
203
        Period basePeriod = periods.get( periods.size() - 1 );
 
204
        Period newBasePeriod = periodType.getNextPeriod( basePeriod );
 
205
 
 
206
        if ( newBasePeriod.getStartDate().before( new Date() ) ) // Future periods not allowed
 
207
        {
 
208
            getSession().put( SESSION_KEY_BASE_PERIOD, newBasePeriod );
 
209
 
 
210
            generatedPeriodsCache.remove();
 
211
        }
 
212
    }
 
213
 
 
214
    @SuppressWarnings( "unchecked" )
 
215
    public void previousPeriodSpan()
 
216
    {
 
217
        List<Period> periods = getPeriodList();
 
218
        CalendarPeriodType periodType = (CalendarPeriodType) getPeriodType();
 
219
 
 
220
        Period basePeriod = periods.get( 0 );
 
221
        Period newBasePeriod = periodType.getPreviousPeriod( basePeriod );
 
222
 
 
223
        getSession().put( SESSION_KEY_BASE_PERIOD, newBasePeriod );
 
224
 
 
225
        generatedPeriodsCache.remove();
 
226
    }
 
227
 
 
228
    // -------------------------------------------------------------------------
 
229
    // Support methods
 
230
    // -------------------------------------------------------------------------
 
231
 
 
232
    private PeriodType getPeriodType()
 
233
    {
 
234
        DataSet dataSet = getSelectedDataSet();
 
235
 
 
236
        if ( dataSet == null )
 
237
        {
 
238
            throw new IllegalStateException( "Cannot ask for PeriodType when no DataSet is selected" );
 
239
        }
 
240
 
 
241
        return dataSet.getPeriodType();
 
242
    }
 
243
 
 
244
    @SuppressWarnings( "unchecked" )
 
245
    private Period getBasePeriod()
 
246
    {
 
247
        Period basePeriod = (Period) getSession().get( SESSION_KEY_BASE_PERIOD );
 
248
        PeriodType periodType = getPeriodType();
 
249
 
 
250
        if ( basePeriod == null )
 
251
        {
 
252
            LOG.debug( "getBasePeriod(): Base period is null, creating new." );
 
253
 
 
254
            basePeriod = periodType.createPeriod();
 
255
            getSession().put( SESSION_KEY_BASE_PERIOD, basePeriod );
 
256
        }
 
257
        else if ( !basePeriod.getPeriodType().equals( periodType ) )
 
258
        {
 
259
            LOG.debug( "getBasePeriod(): Wrong type of base period, transforming." );
 
260
 
 
261
            basePeriod = periodType.createPeriod( basePeriod.getStartDate() );
 
262
            getSession().put( SESSION_KEY_BASE_PERIOD, basePeriod );
 
263
        }
 
264
 
 
265
        return basePeriod;
 
266
    }
 
267
 
 
268
    @SuppressWarnings( "unchecked" )
 
269
    private static final Map getSession()
 
270
    {
 
271
        return ActionContext.getContext().getSession();
 
272
    }
 
273
 
 
274
    public boolean hasDataSetMultiDimensionalDataElement( DataSet dataSet )
 
275
    {
 
276
        int numberOfTotalColumns = 1;
 
277
 
 
278
        if ( dataSet.getDataElements().size() > 0 )
 
279
        {
 
280
            for ( DataElement de : dataSet.getDataElements() )
 
281
            {
 
282
                for ( DataElementCategory category : de.getCategoryCombo().getCategories() )
 
283
                {
 
284
                    numberOfTotalColumns = numberOfTotalColumns * category.getCategoryOptions().size();
 
285
                }
 
286
 
 
287
                if ( numberOfTotalColumns > 1 )
 
288
                {
 
289
                    return true;
 
290
                }
 
291
            }
 
292
        }
 
293
 
 
294
        return false;
 
295
    }
 
296
 
 
297
    public List<DataSet> loadDataSetsForSelectedOrgUnit( OrganisationUnit organisationUnit )
 
298
    {
 
299
        List<DataSet> dataSets = new ArrayList<DataSet>( dataSetService.getDataSetsBySource( organisationUnit ) );
 
300
 
 
301
        if ( !currentUserService.currentUserIsSuper() )
 
302
        {
 
303
            UserCredentials userCredentials = userStore.getUserCredentials( currentUserService.getCurrentUser() );
 
304
 
 
305
            Set<DataSet> dataSetUserAuthorityGroups = new HashSet<DataSet>();
 
306
 
 
307
            for ( UserAuthorityGroup userAuthorityGroup : userCredentials.getUserAuthorityGroups() )
 
308
            {
 
309
                dataSetUserAuthorityGroups.addAll( userAuthorityGroup.getDataSets() );
 
310
            }
 
311
 
 
312
            dataSets.retainAll( dataSetUserAuthorityGroups );
 
313
        }
 
314
 
 
315
        // ---------------------------------------------------------------------
 
316
        // Remove DataSets which don't have a CalendarPeriodType
 
317
        // ---------------------------------------------------------------------
 
318
 
 
319
        Iterator<DataSet> iterator = dataSets.iterator();
 
320
 
 
321
        while ( iterator.hasNext() )
 
322
        {
 
323
            DataSet dataSet = iterator.next();
 
324
 
 
325
            if ( !(dataSet.getPeriodType() instanceof CalendarPeriodType) )
 
326
            {
 
327
                iterator.remove();
 
328
            }
 
329
        }
 
330
 
 
331
        return dataSets;
 
332
    }
 
333
}