1
package org.hisp.dhis.period;
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.Calendar;
32
import java.util.Collection;
33
import java.util.Date;
34
import java.util.Iterator;
35
import java.util.List;
37
import org.hisp.dhis.dataelement.DataElement;
38
import org.hisp.dhis.source.Source;
39
import org.hisp.dhis.system.util.DateUtils;
42
* @author Kristian Nordal
43
* @version $Id: DefaultPeriodService.java 5983 2008-10-17 17:42:44Z larshelg $
45
public class DefaultPeriodService
46
implements PeriodService
48
// -------------------------------------------------------------------------
50
// -------------------------------------------------------------------------
52
private PeriodStore periodStore;
54
public void setPeriodStore( PeriodStore periodStore )
56
this.periodStore = periodStore;
59
// -------------------------------------------------------------------------
61
// -------------------------------------------------------------------------
63
public int addPeriod( Period period )
65
return periodStore.addPeriod( period );
68
public void deletePeriod( Period period )
70
periodStore.deletePeriod( period );
73
public Period getPeriod( int id )
75
return periodStore.getPeriod( id );
78
public Period getPeriod( Date startDate, Date endDate, PeriodType periodType )
80
return periodStore.getPeriod( startDate, endDate, periodType );
83
public Collection<Period> getAllPeriods()
85
return periodStore.getAllPeriods();
88
public Collection<Period> getPeriods( Collection<Integer> identifiers )
90
Collection<Period> objects = new ArrayList<Period>();
92
for ( Integer id : identifiers )
94
objects.add( getPeriod( id ) );
100
public Collection<Period> getPeriodsByPeriodType( PeriodType periodType )
102
return periodStore.getPeriodsByPeriodType( periodType );
105
public Collection<Period> getPeriodsBetweenDates( Date startDate, Date endDate )
107
return periodStore.getPeriodsBetweenDates( startDate, endDate );
110
public Collection<Period> getPeriodsBetweenDates( PeriodType periodType, Date startDate, Date endDate )
112
return periodStore.getPeriodsBetweenDates( periodType, startDate, endDate );
115
public Collection<Period> getIntersectingPeriodsByPeriodType( PeriodType periodType, Date startDate, Date endDate )
117
return periodStore.getIntersectingPeriodsByPeriodType( periodType, startDate, endDate );
120
public Collection<Period> getIntersectingPeriods( Date startDate, Date endDate )
122
return periodStore.getIntersectingPeriods( startDate, endDate );
125
public Collection<Period> getBoundaryPeriods( Period period, Collection<Period> periods )
127
Collection<Period> immutablePeriods = new ArrayList<Period>( periods );
129
Iterator<Period> iterator = immutablePeriods.iterator();
131
while( iterator.hasNext() )
133
Period iterated = iterator.next();
135
if ( !DateUtils.strictlyBetween( period.getStartDate(), iterated.getStartDate(), iterated.getEndDate() ) &&
136
!DateUtils.strictlyBetween( period.getEndDate(), iterated.getStartDate(), iterated.getEndDate() ) )
142
return immutablePeriods;
145
public Collection<Period> getInclusivePeriods( Period period, Collection<Period> periods )
147
Collection<Period> immutablePeriods = new ArrayList<Period>( periods );
149
Iterator<Period> iterator = immutablePeriods.iterator();
151
while( iterator.hasNext() )
153
Period iterated = iterator.next();
155
if ( !DateUtils.between( iterated.getStartDate(), period.getStartDate(), period.getEndDate() ) ||
156
!DateUtils.between( iterated.getEndDate(), period.getStartDate(), period.getEndDate() ) )
162
return immutablePeriods;
165
public Collection<Period> getPeriods( Period period, Collection<DataElement> dataElements, Collection<? extends Source> sources )
167
return periodStore.getPeriods( period, dataElements, sources );
170
public Period getRelativePeriod( Date date, int startMonths, int endMonths )
172
if ( startMonths >= endMonths )
174
throw new IllegalArgumentException( "End months must be greater than start months" );
177
PeriodType periodType = periodStore.getPeriodType( RelativePeriodType.class );
179
// ---------------------------------------------------------------------
181
// ---------------------------------------------------------------------
183
if ( periodType == null )
185
periodType = new RelativePeriodType();
187
periodStore.addPeriodType( periodType );
190
Calendar cal = PeriodType.createCalendarInstance( date );
192
cal.add( Calendar.MONTH, startMonths );
193
cal.set( Calendar.DAY_OF_MONTH, cal.getActualMinimum( Calendar.DAY_OF_MONTH ) );
195
Date startDate = cal.getTime();
197
cal = PeriodType.createCalendarInstance( date );
199
cal.add( Calendar.MONTH, endMonths - 1 );
200
cal.set( Calendar.DAY_OF_MONTH, cal.getActualMaximum( Calendar.DAY_OF_MONTH ) );
202
Date endDate = cal.getTime();
204
Period period = new Period( periodType, startDate, endDate );
206
// ---------------------------------------------------------------------
207
// Persist period if it does not exist
208
// ---------------------------------------------------------------------
210
Period persistedPeriod = getPeriod( startDate, endDate, periodType );
212
if ( persistedPeriod == null )
218
period = persistedPeriod;
224
public Period getRelativePeriod( Date date, int months )
228
throw new IllegalArgumentException( "Months cannot be zero" );
231
PeriodType periodType = periodStore.getPeriodType( RelativePeriodType.class );
233
// ---------------------------------------------------------------------
235
// ---------------------------------------------------------------------
237
if ( periodType == null )
239
periodType = new RelativePeriodType();
241
periodStore.addPeriodType( periodType );
244
Calendar cal = PeriodType.createCalendarInstance( date );
246
Date startDate = null;
251
cal.set( Calendar.DAY_OF_MONTH, cal.getActualMinimum( Calendar.DAY_OF_MONTH ) );
253
startDate = cal.getTime();
255
cal.add( Calendar.MONTH, months - 1 );
256
cal.set( Calendar.DAY_OF_MONTH, cal.getActualMaximum( Calendar.DAY_OF_MONTH ) );
258
endDate = cal.getTime();
262
cal.set( Calendar.DAY_OF_MONTH, cal.getActualMaximum( Calendar.DAY_OF_MONTH ) );
264
endDate = cal.getTime();
266
cal.add( Calendar.MONTH, months + 1 );
267
cal.set( Calendar.DAY_OF_MONTH, cal.getActualMinimum( Calendar.DAY_OF_MONTH ) );
269
startDate = cal.getTime();
272
Period period = new Period( periodType, startDate, endDate );
274
// ---------------------------------------------------------------------
275
// Persist period if it does not exist
276
// ---------------------------------------------------------------------
278
Period persistedPeriod = getPeriod( startDate, endDate, periodType );
280
if ( persistedPeriod == null )
286
period = persistedPeriod;
292
// -------------------------------------------------------------------------
294
// -------------------------------------------------------------------------
296
public PeriodType getPeriodType( int id )
298
return periodStore.getPeriodType( id );
301
public List<PeriodType> getAllPeriodTypes()
303
return PeriodType.getAvailablePeriodTypes();
306
public PeriodType getPeriodTypeByName( String name )
308
return PeriodType.getPeriodTypeByName( name );