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

« back to all changes in this revision

Viewing changes to dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/period/DefaultPeriodService.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.period;
 
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.Calendar;
 
32
import java.util.Collection;
 
33
import java.util.Date;
 
34
import java.util.Iterator;
 
35
import java.util.List;
 
36
 
 
37
import org.hisp.dhis.dataelement.DataElement;
 
38
import org.hisp.dhis.source.Source;
 
39
import org.hisp.dhis.system.util.DateUtils;
 
40
 
 
41
/**
 
42
 * @author Kristian Nordal
 
43
 * @version $Id: DefaultPeriodService.java 5983 2008-10-17 17:42:44Z larshelg $
 
44
 */
 
45
public class DefaultPeriodService
 
46
    implements PeriodService
 
47
{
 
48
    // -------------------------------------------------------------------------
 
49
    // Dependencies
 
50
    // -------------------------------------------------------------------------
 
51
 
 
52
    private PeriodStore periodStore;
 
53
 
 
54
    public void setPeriodStore( PeriodStore periodStore )
 
55
    {
 
56
        this.periodStore = periodStore;
 
57
    }
 
58
 
 
59
    // -------------------------------------------------------------------------
 
60
    // Period
 
61
    // -------------------------------------------------------------------------
 
62
 
 
63
    public int addPeriod( Period period )
 
64
    {
 
65
        return periodStore.addPeriod( period );
 
66
    }
 
67
 
 
68
    public void deletePeriod( Period period )
 
69
    {
 
70
        periodStore.deletePeriod( period );
 
71
    }
 
72
 
 
73
    public Period getPeriod( int id )
 
74
    {
 
75
        return periodStore.getPeriod( id );
 
76
    }
 
77
 
 
78
    public Period getPeriod( Date startDate, Date endDate, PeriodType periodType )
 
79
    {
 
80
        return periodStore.getPeriod( startDate, endDate, periodType );
 
81
    }
 
82
    
 
83
    public Collection<Period> getAllPeriods()
 
84
    {
 
85
        return periodStore.getAllPeriods();
 
86
    }
 
87
    
 
88
    public Collection<Period> getPeriods( Collection<Integer> identifiers )
 
89
    {
 
90
        Collection<Period> objects = new ArrayList<Period>();
 
91
        
 
92
        for ( Integer id : identifiers )
 
93
        {
 
94
            objects.add( getPeriod( id ) );
 
95
        }
 
96
        
 
97
        return objects;
 
98
    }
 
99
    
 
100
    public Collection<Period> getPeriodsByPeriodType( PeriodType periodType )
 
101
    {
 
102
        return periodStore.getPeriodsByPeriodType( periodType );
 
103
    }
 
104
 
 
105
    public Collection<Period> getPeriodsBetweenDates( Date startDate, Date endDate )
 
106
    {
 
107
        return periodStore.getPeriodsBetweenDates( startDate, endDate );
 
108
    }
 
109
    
 
110
    public Collection<Period> getPeriodsBetweenDates( PeriodType periodType, Date startDate, Date endDate )
 
111
    {
 
112
        return periodStore.getPeriodsBetweenDates( periodType, startDate, endDate );
 
113
    }
 
114
    
 
115
    public Collection<Period> getIntersectingPeriodsByPeriodType( PeriodType periodType, Date startDate, Date endDate )
 
116
    {
 
117
        return periodStore.getIntersectingPeriodsByPeriodType( periodType, startDate, endDate );
 
118
    }
 
119
    
 
120
    public Collection<Period> getIntersectingPeriods( Date startDate, Date endDate )
 
121
    {
 
122
        return periodStore.getIntersectingPeriods( startDate, endDate );
 
123
    }
 
124
 
 
125
    public Collection<Period> getBoundaryPeriods( Period period, Collection<Period> periods )
 
126
    {
 
127
        Collection<Period> immutablePeriods = new ArrayList<Period>( periods );
 
128
        
 
129
        Iterator<Period> iterator = immutablePeriods.iterator();
 
130
        
 
131
        while( iterator.hasNext() )
 
132
        {
 
133
             Period iterated = iterator.next();
 
134
             
 
135
             if ( !DateUtils.strictlyBetween( period.getStartDate(), iterated.getStartDate(), iterated.getEndDate() ) &&
 
136
                 !DateUtils.strictlyBetween( period.getEndDate(), iterated.getStartDate(), iterated.getEndDate() ) )
 
137
             {
 
138
                 iterator.remove();
 
139
             }
 
140
        }
 
141
        
 
142
        return immutablePeriods;
 
143
    }
 
144
    
 
145
    public Collection<Period> getInclusivePeriods( Period period, Collection<Period> periods )
 
146
    {
 
147
        Collection<Period> immutablePeriods = new ArrayList<Period>( periods );
 
148
        
 
149
        Iterator<Period> iterator = immutablePeriods.iterator();
 
150
        
 
151
        while( iterator.hasNext() )
 
152
        {
 
153
            Period iterated = iterator.next();
 
154
            
 
155
            if ( !DateUtils.between( iterated.getStartDate(), period.getStartDate(), period.getEndDate() ) ||
 
156
                !DateUtils.between( iterated.getEndDate(), period.getStartDate(), period.getEndDate() ) )
 
157
            {
 
158
                iterator.remove();
 
159
            }
 
160
        }
 
161
        
 
162
        return immutablePeriods;
 
163
    }
 
164
 
 
165
    public Collection<Period> getPeriods( Period period, Collection<DataElement> dataElements, Collection<? extends Source> sources )
 
166
    {
 
167
        return periodStore.getPeriods( period, dataElements, sources );
 
168
    }
 
169
    
 
170
    public Period getRelativePeriod( Date date, int startMonths, int endMonths )
 
171
    {
 
172
        if ( startMonths >= endMonths )
 
173
        {
 
174
            throw new IllegalArgumentException( "End months must be greater than start months" );
 
175
        }
 
176
        
 
177
        PeriodType periodType = periodStore.getPeriodType( RelativePeriodType.class );
 
178
 
 
179
        // ---------------------------------------------------------------------
 
180
        // Reload PeriodType
 
181
        // ---------------------------------------------------------------------
 
182
 
 
183
        if ( periodType == null )
 
184
        {
 
185
            periodType = new RelativePeriodType();
 
186
            
 
187
            periodStore.addPeriodType( periodType );
 
188
        }
 
189
        
 
190
        Calendar cal = PeriodType.createCalendarInstance( date );
 
191
        
 
192
        cal.add( Calendar.MONTH, startMonths );
 
193
        cal.set( Calendar.DAY_OF_MONTH, cal.getActualMinimum( Calendar.DAY_OF_MONTH ) );
 
194
        
 
195
        Date startDate = cal.getTime();
 
196
        
 
197
        cal = PeriodType.createCalendarInstance( date );
 
198
        
 
199
        cal.add( Calendar.MONTH, endMonths - 1 );
 
200
        cal.set( Calendar.DAY_OF_MONTH, cal.getActualMaximum( Calendar.DAY_OF_MONTH ) );
 
201
        
 
202
        Date endDate = cal.getTime();
 
203
        
 
204
        Period period = new Period( periodType, startDate, endDate );
 
205
 
 
206
        // ---------------------------------------------------------------------
 
207
        // Persist period if it does not exist
 
208
        // ---------------------------------------------------------------------
 
209
 
 
210
        Period persistedPeriod = getPeriod( startDate, endDate, periodType );
 
211
        
 
212
        if ( persistedPeriod == null )
 
213
        {
 
214
            addPeriod( period );
 
215
        }
 
216
        else
 
217
        {
 
218
            period = persistedPeriod;
 
219
        }
 
220
        
 
221
        return period;
 
222
    }
 
223
    
 
224
    public Period getRelativePeriod( Date date, int months )
 
225
    {
 
226
        if ( months == 0 )
 
227
        {
 
228
            throw new IllegalArgumentException( "Months cannot be zero" );
 
229
        }
 
230
        
 
231
        PeriodType periodType = periodStore.getPeriodType( RelativePeriodType.class );
 
232
 
 
233
        // ---------------------------------------------------------------------
 
234
        // Reload PeriodType
 
235
        // ---------------------------------------------------------------------
 
236
 
 
237
        if ( periodType == null )
 
238
        {
 
239
            periodType = new RelativePeriodType();
 
240
            
 
241
            periodStore.addPeriodType( periodType );
 
242
        }
 
243
        
 
244
        Calendar cal = PeriodType.createCalendarInstance( date );
 
245
 
 
246
        Date startDate = null;
 
247
        Date endDate = null;
 
248
        
 
249
        if ( months > 0 )
 
250
        {        
 
251
            cal.set( Calendar.DAY_OF_MONTH, cal.getActualMinimum( Calendar.DAY_OF_MONTH ) );
 
252
            
 
253
            startDate = cal.getTime();
 
254
    
 
255
            cal.add( Calendar.MONTH, months - 1 );        
 
256
            cal.set( Calendar.DAY_OF_MONTH, cal.getActualMaximum( Calendar.DAY_OF_MONTH ) );
 
257
            
 
258
            endDate = cal.getTime();
 
259
        }
 
260
        else
 
261
        {
 
262
            cal.set( Calendar.DAY_OF_MONTH, cal.getActualMaximum( Calendar.DAY_OF_MONTH ) );
 
263
            
 
264
            endDate = cal.getTime();
 
265
            
 
266
            cal.add( Calendar.MONTH, months + 1 );
 
267
            cal.set( Calendar.DAY_OF_MONTH, cal.getActualMinimum( Calendar.DAY_OF_MONTH ) );
 
268
            
 
269
            startDate = cal.getTime();
 
270
        }
 
271
        
 
272
        Period period = new Period( periodType, startDate, endDate );
 
273
 
 
274
        // ---------------------------------------------------------------------
 
275
        // Persist period if it does not exist
 
276
        // ---------------------------------------------------------------------
 
277
 
 
278
        Period persistedPeriod = getPeriod( startDate, endDate, periodType );
 
279
        
 
280
        if ( persistedPeriod == null )
 
281
        {
 
282
            addPeriod( period );
 
283
        }
 
284
        else
 
285
        {
 
286
            period = persistedPeriod;
 
287
        }
 
288
        
 
289
        return period;
 
290
    }
 
291
    
 
292
    // -------------------------------------------------------------------------
 
293
    // PeriodType
 
294
    // -------------------------------------------------------------------------
 
295
 
 
296
    public PeriodType getPeriodType( int id )
 
297
    {
 
298
        return periodStore.getPeriodType( id );
 
299
    }
 
300
 
 
301
    public List<PeriodType> getAllPeriodTypes()
 
302
    {
 
303
        return PeriodType.getAvailablePeriodTypes();
 
304
    }
 
305
 
 
306
    public PeriodType getPeriodTypeByName( String name )
 
307
    {
 
308
        return PeriodType.getPeriodTypeByName( name );
 
309
    }
 
310
}