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

« back to all changes in this revision

Viewing changes to dhis-2/dhis-api/src/main/java/org/hisp/dhis/period/WeeklyPeriodType.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.Date;
 
33
import java.util.List;
 
34
 
 
35
/**
 
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.
 
39
 * 
 
40
 * @author Torgeir Lorange Ostby
 
41
 * @version $Id: WeeklyPeriodType.java 2976 2007-03-03 22:50:19Z torgeilo $
 
42
 */
 
43
public class WeeklyPeriodType
 
44
    extends CalendarPeriodType
 
45
{
 
46
    /**
 
47
     * The name of the WeeklyPeriodType, which is "Weekly".
 
48
     */
 
49
    public static final String NAME = "Weekly";
 
50
 
 
51
    // -------------------------------------------------------------------------
 
52
    // PeriodType functionality
 
53
    // -------------------------------------------------------------------------
 
54
 
 
55
    @Override
 
56
    public String getName()
 
57
    {
 
58
        return NAME;
 
59
    }
 
60
 
 
61
    @Override
 
62
    public Period createPeriod()
 
63
    {
 
64
        return createPeriod( createCalendarInstance() );
 
65
    }
 
66
 
 
67
    @Override
 
68
    public Period createPeriod( Date date )
 
69
    {
 
70
        return createPeriod( createCalendarInstance( date ) );
 
71
    }
 
72
 
 
73
    private Period createPeriod( Calendar cal )
 
74
    {
 
75
        cal.set( Calendar.DAY_OF_WEEK, Calendar.MONDAY );
 
76
 
 
77
        Date startDate = cal.getTime();
 
78
 
 
79
        cal.add( Calendar.DAY_OF_YEAR, 6 );
 
80
 
 
81
        return new Period( this, startDate, cal.getTime() );
 
82
    }
 
83
 
 
84
    // -------------------------------------------------------------------------
 
85
    // CalendarPeriodType functionality
 
86
    // -------------------------------------------------------------------------
 
87
 
 
88
    @Override
 
89
    public Period getNextPeriod( Period period )
 
90
    {
 
91
        Calendar cal = createCalendarInstance( period.getStartDate() );
 
92
        cal.set( Calendar.DAY_OF_WEEK, Calendar.MONDAY );
 
93
        cal.add( Calendar.WEEK_OF_YEAR, 1 );
 
94
 
 
95
        Date startDate = cal.getTime();
 
96
 
 
97
        cal.add( Calendar.DAY_OF_YEAR, 6 );
 
98
 
 
99
        return new Period( this, startDate, cal.getTime() );
 
100
    }
 
101
 
 
102
    @Override
 
103
    public Period getPreviousPeriod( Period period )
 
104
    {
 
105
        Calendar cal = createCalendarInstance( period.getStartDate() );
 
106
        cal.set( Calendar.DAY_OF_WEEK, Calendar.MONDAY );
 
107
        cal.add( Calendar.WEEK_OF_YEAR, -1 );
 
108
 
 
109
        Date startDate = cal.getTime();
 
110
 
 
111
        cal.add( Calendar.DAY_OF_YEAR, 6 );
 
112
 
 
113
        return new Period( this, startDate, cal.getTime() );
 
114
    }
 
115
 
 
116
    /**
 
117
     * Generates weekly Periods for the whole year in which the given Period's
 
118
     * startDate exists.
 
119
     */
 
120
    @Override
 
121
    public List<Period> generatePeriods( Period period )
 
122
    {
 
123
        Calendar cal = createCalendarInstance( period.getStartDate() );
 
124
 
 
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
 
128
        // of the end date
 
129
        // ---------------------------------------------------------------------
 
130
 
 
131
        if ( period.getPeriodType().equals( this ) )
 
132
        {
 
133
            Calendar cal2 = createCalendarInstance( period.getEndDate() );
 
134
 
 
135
            if ( cal.get( Calendar.YEAR ) != cal2.get( Calendar.YEAR ) )
 
136
            {
 
137
                // Use saturday as Calendar has sunday as first day of week.
 
138
                cal2.add( Calendar.DAY_OF_WEEK, -1 );
 
139
 
 
140
                if ( cal2.get( Calendar.WEEK_OF_YEAR ) == 1 )
 
141
                {
 
142
                    cal = cal2;
 
143
                    cal2 = null;
 
144
                }
 
145
            }
 
146
        }
 
147
 
 
148
        // ---------------------------------------------------------------------
 
149
        // Generate weeks
 
150
        // ---------------------------------------------------------------------
 
151
 
 
152
        cal.set( Calendar.WEEK_OF_YEAR, 1 );
 
153
        cal.set( Calendar.DAY_OF_WEEK, Calendar.MONDAY );
 
154
 
 
155
        int firstWeek = cal.get( Calendar.WEEK_OF_YEAR );
 
156
 
 
157
        ArrayList<Period> weeks = new ArrayList<Period>();
 
158
 
 
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 );
 
163
 
 
164
        while ( cal.get( Calendar.WEEK_OF_YEAR ) != firstWeek )
 
165
        {
 
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 );
 
170
        }
 
171
 
 
172
        return weeks;
 
173
    }
 
174
}