KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > oddjob > schedules > schedules > DayOfMonthSchedule


1 package org.oddjob.schedules.schedules;
2
3 import java.io.Serializable JavaDoc;
4 import java.util.Calendar JavaDoc;
5 import java.util.Date JavaDoc;
6 import java.util.TimeZone JavaDoc;
7
8 import org.oddjob.schedules.CalendarUtils;
9 import org.oddjob.schedules.ConstrainedSchedule;
10 import org.oddjob.schedules.DateUtils;
11 import org.oddjob.schedules.ScheduleContext;
12
13 /**
14  * @oddjob.description Implement a schedule based on days of the month. The day of the month is given
15  * as an integer, but unlike the java GregorianCalander, 0 and negative numbers are taken to be
16  * this month, not the previous month. i.e. on="0" is the last day of the month.
17  * <p>
18  *
19  * @oddjob.example
20  *
21  * <pre>
22  * &lt;dayofmonth from="17" to="22"/&gt;
23  * &lt;dayofmonth on="15"/&gt;
24  * </pre>
25  *
26  * @author Rob Gordon
27  */

28
29
30 final public class DayOfMonthSchedule extends ConstrainedSchedule implements Serializable JavaDoc {
31
32     private static final long serialVersionUID = 20050226;
33     
34     /** from day of month. */
35     private Integer JavaDoc fromDay;
36     
37     /** to day of month. */
38     private Integer JavaDoc toDay;
39         
40     /**
41      * @oddjob.property from
42      * @oddjob.description The from day of the month.
43      * @oddjob.required No. Defaults to 1.
44      *
45      * @param from The from date.
46      */

47     public void setFrom(String JavaDoc from) {
48         if (from == null) {
49             fromDay = null;
50         }
51         else {
52             fromDay = new Integer JavaDoc(from);
53         }
54     }
55     
56     /*
57      * (non-Javadoc)
58      * @see org.treesched.ConstrainedSchedule#getFrom()
59      */

60     public String JavaDoc getFrom() {
61         if (fromDay == null) {
62             return null;
63         }
64         return fromDay.toString();
65     }
66     
67     /**
68      * @oddjob.property to
69      * @oddjob.description The to day of the month.
70      * @oddjob.required No. Defaults to the last day of the month.
71      *
72      * @param to The to date.
73      */

74     public void setTo(String JavaDoc to) {
75         if (to == null) {
76             toDay = null;
77         }
78         else {
79             toDay = new Integer JavaDoc(to);
80         }
81     }
82     
83     /*
84      * (non-Javadoc)
85      * @see org.treesched.ConstrainedSchedule#getTo()
86      */

87     public String JavaDoc getTo() {
88         if (toDay == null) {
89             return null;
90         }
91         return toDay.toString();
92     }
93
94     /**
95      * Get a calendar for the from day.
96      *
97      * @param referenceDate The date to get month, year info from.
98      * @param timeZone The time zone.
99      *
100      * @return A calendar.
101      */

102     Calendar JavaDoc fromCalendar(Date JavaDoc referenceDate, TimeZone JavaDoc timeZone) {
103         if (fromDay == null) {
104             return CalendarUtils.startOfMonth(referenceDate, timeZone);
105         }
106         else {
107             return CalendarUtils.dayOfMonth(referenceDate, fromDay.intValue(), timeZone);
108         }
109     }
110     
111     /**
112      * Get a calendar for the to day.
113      *
114      * @param referenceDate The date to get month, yar info from.
115      * @param timeZone The time zone.
116      *
117      * @return A calendar.
118      */

119     Calendar JavaDoc toCalendar(Date JavaDoc referenceDate, TimeZone JavaDoc timeZone) {
120         if (toDay == null) {
121             return CalendarUtils.endOfMonth(referenceDate, timeZone);
122         }
123         else {
124             Calendar JavaDoc cal = CalendarUtils.dayOfMonth(referenceDate, toDay.intValue(), timeZone);
125             CalendarUtils.setEndOfDay(cal);
126             return cal;
127         }
128     }
129     
130     /*
131      * (non-Javadoc)
132      * @see org.treesched.ConstrainedSchedule#getStartDate(java.util.Date)
133      */

134     public Date JavaDoc getStartDate(ScheduleContext context) {
135         Calendar JavaDoc fromCal = fromCalendar(context.getDate(), context.getTimeZone());
136         Calendar JavaDoc toCal = toCalendar(context.getDate(), context.getTimeZone());
137         
138         int nowDay = DateUtils.dayOfMonth(context.getDate(), context.getTimeZone());
139         int fromDay = fromCal.get(Calendar.DAY_OF_MONTH);
140         int toDay = toCal.get(Calendar.DAY_OF_MONTH);
141         
142         if (fromDay > toDay) {
143             if (nowDay > toDay) {
144                 return fromCal.getTime();
145             }
146             else {
147                 fromCal.add(Calendar.MONTH, - 1);
148                 return fromCal.getTime();
149             }
150         }
151         else {
152             if (nowDay > toDay) {
153                 fromCal.add(Calendar.MONTH, 1);
154                 return fromCal.getTime();
155             }
156             else {
157                 return fromCal.getTime();
158             }
159         }
160     }
161     
162     /*
163      * (non-Javadoc)
164      * @see org.treesched.ConstrainedSchedule#getEndDate(java.util.Date)
165      */

166     public Date JavaDoc getEndDate(ScheduleContext context) {
167         Calendar JavaDoc toCal = toCalendar(context.getDate(), context.getTimeZone());
168
169         int nowDay = DateUtils.dayOfMonth(context.getDate(), context.getTimeZone());
170         int toDay = toCal.get(Calendar.DAY_OF_MONTH);
171         
172         if (nowDay > toDay) {
173             toCal.add(Calendar.MONTH, + 1);
174             return toCal.getTime();
175         }
176         else {
177             return toCal.getTime();
178         }
179     }
180 }
181
Popular Tags