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

« back to all changes in this revision

Viewing changes to dhis-2/dhis-web/dhis-web-reporting/src/main/java/org/hisp/dhis/reporting/dataset/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.reporting.dataset.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.Collection;
 
31
import java.util.Date;
 
32
import java.util.Iterator;
 
33
import java.util.List;
 
34
import java.util.Map;
 
35
 
 
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;
 
46
 
 
47
import com.opensymphony.xwork.ActionContext;
 
48
 
 
49
/**
 
50
 * @author Torgeir Lorange Ostby
 
51
 * @version $Id: DefaultSelectedStateManager.java 5282 2008-05-28 10:41:06Z
 
52
 *          larshelg $
 
53
 */
 
54
public class DefaultSelectedStateManager
 
55
    implements SelectedStateManager
 
56
{
 
57
    private static final Log LOG = LogFactory.getLog( DefaultSelectedStateManager.class );
 
58
 
 
59
    public static final String SESSION_KEY_SELECTED_DATASET_ID = "dataset_report_selected_dataset_id";
 
60
 
 
61
    public static final String SESSION_KEY_SELECTED_PERIOD_INDEX = "dataset_report_selected_period_index";
 
62
 
 
63
    public static final String SESSION_KEY_BASE_PERIOD = "dataset_report_base_period";
 
64
 
 
65
    // -------------------------------------------------------------------------
 
66
    // Dependencies
 
67
    // -------------------------------------------------------------------------
 
68
 
 
69
    private PeriodService periodService;
 
70
 
 
71
    public void setPeriodService( PeriodService periodService )
 
72
    {
 
73
        this.periodService = periodService;
 
74
    }
 
75
 
 
76
    private DataSetService dataSetService;
 
77
 
 
78
    public void setDataSetService( DataSetService dataSetService )
 
79
    {
 
80
        this.dataSetService = dataSetService;
 
81
    }
 
82
 
 
83
    private SelectionTreeManager selectionTreeManager;
 
84
 
 
85
    public void setSelectionTreeManager( SelectionTreeManager selectionTreeManager )
 
86
    {
 
87
        this.selectionTreeManager = selectionTreeManager;
 
88
    }
 
89
 
 
90
    // -------------------------------------------------------------------------
 
91
    // Cache
 
92
    // -------------------------------------------------------------------------
 
93
 
 
94
    private ThreadLocal<List<Period>> generatedPeriodsCache = new ThreadLocal<List<Period>>();
 
95
 
 
96
    // -------------------------------------------------------------------------
 
97
    // SelectedStateManager implementation
 
98
    // -------------------------------------------------------------------------
 
99
 
 
100
    public OrganisationUnit getSelectedOrganisationUnit()
 
101
    {
 
102
        return selectionTreeManager.getSelectedOrganisationUnit();
 
103
    }
 
104
 
 
105
    @SuppressWarnings( "unchecked" )
 
106
    public void setSelectedDataSet( DataSet dataSet )
 
107
    {
 
108
        getSession().put( SESSION_KEY_SELECTED_DATASET_ID, dataSet.getId() );
 
109
    }
 
110
 
 
111
    public DataSet getSelectedDataSet()
 
112
    {
 
113
        Integer id = (Integer) getSession().get( SESSION_KEY_SELECTED_DATASET_ID );
 
114
 
 
115
        if ( id == null )
 
116
        {
 
117
            return null;
 
118
        }
 
119
 
 
120
        return dataSetService.getDataSet( id );
 
121
    }
 
122
 
 
123
    public void clearSelectedDataSet()
 
124
    {
 
125
        getSession().remove( SESSION_KEY_SELECTED_DATASET_ID );
 
126
    }
 
127
 
 
128
    @SuppressWarnings( "unchecked" )
 
129
    public void setSelectedPeriodIndex( Integer index )
 
130
    {
 
131
        getSession().put( SESSION_KEY_SELECTED_PERIOD_INDEX, index );
 
132
    }
 
133
 
 
134
    public Integer getSelectedPeriodIndex()
 
135
    {
 
136
        return (Integer) getSession().get( SESSION_KEY_SELECTED_PERIOD_INDEX );
 
137
    }
 
138
 
 
139
    public Period getSelectedPeriod()
 
140
    {
 
141
        Integer index = getSelectedPeriodIndex();
 
142
 
 
143
        if ( index == null )
 
144
        {
 
145
            return null;
 
146
        }
 
147
 
 
148
        List<Period> periods = getPeriodList();
 
149
 
 
150
        if ( index >= 0 && index < periods.size() )
 
151
        {
 
152
            Period selectedPeriod = periods.get( index );
 
153
 
 
154
            Period period = null;
 
155
 
 
156
            for ( Period p : periodService.getAllPeriods() )
 
157
            {
 
158
                if ( selectedPeriod.equals( p ) )
 
159
                {
 
160
                    period = p;
 
161
                    
 
162
                    return period;
 
163
                }
 
164
            }
 
165
        }
 
166
 
 
167
        return null;
 
168
    }
 
169
 
 
170
    public void clearSelectedPeriod()
 
171
    {
 
172
        getSession().remove( SESSION_KEY_SELECTED_PERIOD_INDEX );
 
173
    }
 
174
 
 
175
    public List<Period> getPeriodList()
 
176
    {
 
177
        List<Period> periods = generatedPeriodsCache.get();
 
178
        Period basePeriod = getBasePeriod();
 
179
 
 
180
        if ( periods == null || periods.size() == 0
 
181
            || !periods.get( 0 ).getPeriodType().equals( basePeriod.getPeriodType() ) || !periods.contains( basePeriod ) )
 
182
        {
 
183
            CalendarPeriodType periodType = (CalendarPeriodType) getPeriodType();
 
184
 
 
185
            LOG.debug( "Generated periods cache invalid, generating new periods based on " + basePeriod );
 
186
 
 
187
            periods = periodType.generatePeriods( basePeriod );
 
188
 
 
189
            generatedPeriodsCache.set( periods );
 
190
        }
 
191
 
 
192
        Date now = new Date();
 
193
 
 
194
        Iterator<Period> iterator = periods.iterator();
 
195
        
 
196
        Collection<Period> persistedPeriods = periodService.getAllPeriods();
 
197
 
 
198
        while ( iterator.hasNext() )
 
199
        {
 
200
            Period period = iterator.next();
 
201
 
 
202
            if ( period.getStartDate().after( now ) || !persistedPeriods.contains( period ))
 
203
            {
 
204
                iterator.remove();
 
205
            }
 
206
        }
 
207
 
 
208
        return periods;
 
209
    }
 
210
 
 
211
    @SuppressWarnings( "unchecked" )
 
212
    public void nextPeriodSpan()
 
213
    {
 
214
        List<Period> periods = getPeriodList();
 
215
        CalendarPeriodType periodType = (CalendarPeriodType) getPeriodType();
 
216
 
 
217
        Period basePeriod = periods.get( periods.size() - 1 );
 
218
        Period newBasePeriod = periodType.getNextPeriod( basePeriod );
 
219
 
 
220
        getSession().put( SESSION_KEY_BASE_PERIOD, newBasePeriod );
 
221
 
 
222
        generatedPeriodsCache.remove();
 
223
    }
 
224
 
 
225
    @SuppressWarnings( "unchecked" )
 
226
    public void previousPeriodSpan()
 
227
    {
 
228
        List<Period> periods = getPeriodList();
 
229
        CalendarPeriodType periodType = (CalendarPeriodType) getPeriodType();
 
230
 
 
231
        Period basePeriod = periods.get( 0 );
 
232
        Period newBasePeriod = periodType.getPreviousPeriod( basePeriod );
 
233
 
 
234
        getSession().put( SESSION_KEY_BASE_PERIOD, newBasePeriod );
 
235
 
 
236
        generatedPeriodsCache.remove();
 
237
    }
 
238
 
 
239
    // -------------------------------------------------------------------------
 
240
    // Support methods
 
241
    // -------------------------------------------------------------------------
 
242
 
 
243
    private PeriodType getPeriodType()
 
244
    {
 
245
        DataSet dataSet = getSelectedDataSet();
 
246
 
 
247
        if ( dataSet == null )
 
248
        {
 
249
            throw new IllegalStateException( "Cannot ask for PeriodType when no DataSet is selected" );
 
250
        }
 
251
 
 
252
        return dataSet.getPeriodType();
 
253
    }
 
254
 
 
255
    @SuppressWarnings( "unchecked" )
 
256
    private Period getBasePeriod()
 
257
    {
 
258
        Period basePeriod = (Period) getSession().get( SESSION_KEY_BASE_PERIOD );
 
259
        PeriodType periodType = getPeriodType();
 
260
 
 
261
        if ( basePeriod == null )
 
262
        {
 
263
            LOG.debug( "getBasePeriod(): Base period is null, creating new." );
 
264
 
 
265
            basePeriod = periodType.createPeriod();
 
266
            getSession().put( SESSION_KEY_BASE_PERIOD, basePeriod );
 
267
        }
 
268
        else if ( !basePeriod.getPeriodType().equals( periodType ) )
 
269
        {
 
270
            LOG.debug( "getBasePeriod(): Wrong type of base period, transforming." );
 
271
 
 
272
            basePeriod = periodType.createPeriod( basePeriod.getStartDate() );
 
273
            getSession().put( SESSION_KEY_BASE_PERIOD, basePeriod );
 
274
        }
 
275
        
 
276
        if ( periodService.getAllPeriods().contains( basePeriod ))
 
277
        {
 
278
                return basePeriod;
 
279
        }
 
280
        
 
281
        CalendarPeriodType calendarPeriodType = (CalendarPeriodType) getPeriodType();
 
282
        return calendarPeriodType.getPreviousPeriod( basePeriod );
 
283
               
 
284
    }
 
285
 
 
286
    @SuppressWarnings( "unchecked" )
 
287
    private static final Map getSession()
 
288
    {
 
289
        return ActionContext.getContext().getSession();
 
290
    }   
 
291
  
 
292
}