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.Date;
33
import java.util.List;
36
* PeriodType for weekly Periods. A valid weekly Period has startDate set to
37
* monday and endDate set to sunday the same week, assuming monday is the first
38
* day and sunday is the last day of the week.
40
* @author Torgeir Lorange Ostby
41
* @version $Id: WeeklyPeriodType.java 2976 2007-03-03 22:50:19Z torgeilo $
43
public class WeeklyPeriodType
44
extends CalendarPeriodType
47
* The name of the WeeklyPeriodType, which is "Weekly".
49
public static final String NAME = "Weekly";
51
// -------------------------------------------------------------------------
52
// PeriodType functionality
53
// -------------------------------------------------------------------------
56
public String getName()
62
public Period createPeriod()
64
return createPeriod( createCalendarInstance() );
68
public Period createPeriod( Date date )
70
return createPeriod( createCalendarInstance( date ) );
73
private Period createPeriod( Calendar cal )
75
cal.set( Calendar.DAY_OF_WEEK, Calendar.MONDAY );
77
Date startDate = cal.getTime();
79
cal.add( Calendar.DAY_OF_YEAR, 6 );
81
return new Period( this, startDate, cal.getTime() );
84
// -------------------------------------------------------------------------
85
// CalendarPeriodType functionality
86
// -------------------------------------------------------------------------
89
public Period getNextPeriod( Period period )
91
Calendar cal = createCalendarInstance( period.getStartDate() );
92
cal.set( Calendar.DAY_OF_WEEK, Calendar.MONDAY );
93
cal.add( Calendar.WEEK_OF_YEAR, 1 );
95
Date startDate = cal.getTime();
97
cal.add( Calendar.DAY_OF_YEAR, 6 );
99
return new Period( this, startDate, cal.getTime() );
103
public Period getPreviousPeriod( Period period )
105
Calendar cal = createCalendarInstance( period.getStartDate() );
106
cal.set( Calendar.DAY_OF_WEEK, Calendar.MONDAY );
107
cal.add( Calendar.WEEK_OF_YEAR, -1 );
109
Date startDate = cal.getTime();
111
cal.add( Calendar.DAY_OF_YEAR, 6 );
113
return new Period( this, startDate, cal.getTime() );
117
* Generates weekly Periods for the whole year in which the given Period's
121
public List<Period> generatePeriods( Period period )
123
Calendar cal = createCalendarInstance( period.getStartDate() );
125
// ---------------------------------------------------------------------
126
// If the suplied period is the first week of a year where the start
127
// date is in the year before, we want to generate weeks for the year
129
// ---------------------------------------------------------------------
131
if ( period.getPeriodType().equals( this ) )
133
Calendar cal2 = createCalendarInstance( period.getEndDate() );
135
if ( cal.get( Calendar.YEAR ) != cal2.get( Calendar.YEAR ) )
137
// Use saturday as Calendar has sunday as first day of week.
138
cal2.add( Calendar.DAY_OF_WEEK, -1 );
140
if ( cal2.get( Calendar.WEEK_OF_YEAR ) == 1 )
148
// ---------------------------------------------------------------------
150
// ---------------------------------------------------------------------
152
cal.set( Calendar.WEEK_OF_YEAR, 1 );
153
cal.set( Calendar.DAY_OF_WEEK, Calendar.MONDAY );
155
int firstWeek = cal.get( Calendar.WEEK_OF_YEAR );
157
ArrayList<Period> weeks = new ArrayList<Period>();
159
Date startDate = cal.getTime();
160
cal.add( Calendar.DAY_OF_YEAR, 6 );
161
weeks.add( new Period( this, startDate, cal.getTime() ) );
162
cal.add( Calendar.DAY_OF_YEAR, 1 );
164
while ( cal.get( Calendar.WEEK_OF_YEAR ) != firstWeek )
166
startDate = cal.getTime();
167
cal.add( Calendar.DAY_OF_YEAR, 6 );
168
weeks.add( new Period( this, startDate, cal.getTime() ) );
169
cal.add( Calendar.DAY_OF_YEAR, 1 );