KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.oddjob.schedules.schedules;
2
3 import java.io.Serializable JavaDoc;
4 import java.text.ParseException JavaDoc;
5 import java.text.SimpleDateFormat JavaDoc;
6 import java.util.Calendar JavaDoc;
7 import java.util.Date JavaDoc;
8 import java.util.TimeZone JavaDoc;
9
10 import org.oddjob.schedules.CalendarUtils;
11 import org.oddjob.schedules.ConstrainedSchedule;
12 import org.oddjob.schedules.DateUtils;
13 import org.oddjob.schedules.ScheduleContext;
14
15 /**
16  * @oddjob.description A schedule for the days of the week.
17  *
18  * @oddjob.example
19  *
20  * <pre>
21  * &lt;dayofweek on="TUE"/&gt;
22  * </pre>
23  *
24  * @author Rob Gordon
25  */

26
27
28 final public class DayOfWeekSchedule extends ConstrainedSchedule implements Serializable JavaDoc {
29
30     private static final long serialVersionUID = 20050226;
31     
32     private final static String JavaDoc DAY_FORMAT = "EEE";
33     
34     /** The from day int as a string. */
35     private String JavaDoc from;
36     
37     /** The to day int as a string. */
38     private String JavaDoc to;
39     
40     /**
41      * @oddjob.property from
42      * @oddjob.description The from day of the week.
43      * @oddjob.required No. Default to the start of the week. This is
44      * region specific.
45      *
46      * @param from The from date.
47      */

48     public void setFrom(String JavaDoc from) {
49         this.from = from;
50     }
51
52     /*
53      * (non-Javadoc)
54      * @see org.treesched.ConstrainedSchedule#getFrom()
55      */

56     public String JavaDoc getFrom() {
57         return from;
58     }
59     
60     /**
61      * @oddjob.property to
62      * @oddjob.description The to day of the week.
63      * @oddjob.required No. Defaults to the last day of the week.
64      * This is region specific.
65      *
66      * @param to The to day.
67      */

68     public void setTo(String JavaDoc to) {
69         this.to = to;
70     }
71     
72     /*
73      * (non-Javadoc)
74      * @see org.treesched.ConstrainedSchedule#getTo()
75      */

76     public String JavaDoc getTo() {
77         return to;
78     }
79     
80     /**
81      * Parse the day to get the day of the week.
82      *
83      * @param text The day.
84      * @param timeZone The time zone.
85      * @return The day of the week.
86      */

87     static int parseDay(String JavaDoc text, TimeZone JavaDoc timeZone) throws ParseException JavaDoc {
88         SimpleDateFormat JavaDoc f = new SimpleDateFormat JavaDoc(DAY_FORMAT);
89         f.setTimeZone(timeZone);
90         Date JavaDoc d = f.parse(text);
91         return DateUtils.dayOfWeek(d, timeZone);
92     }
93
94     
95     Calendar JavaDoc fromCalendar(Date JavaDoc referenceDate, TimeZone JavaDoc timeZone) {
96         if (from == null) {
97             return CalendarUtils.startOfWeek(referenceDate, timeZone);
98         }
99         else {
100             try {
101                 return CalendarUtils.dayOfWeek(referenceDate,
102                             parseDay(from, timeZone),
103                             timeZone);
104             }
105             catch (ParseException JavaDoc e) {
106                 throw new RuntimeException JavaDoc("Failed to parse from day.", e);
107             }
108         }
109     }
110     
111     Calendar JavaDoc toCalendar(Date JavaDoc referenceDate, TimeZone JavaDoc timeZone) {
112         if (to == null) {
113             return CalendarUtils.endOfWeek(referenceDate, timeZone);
114         }
115         else {
116             try {
117                 Calendar JavaDoc cal = CalendarUtils.dayOfWeek(referenceDate,
118                             parseDay(to, timeZone),
119                             timeZone);
120                 CalendarUtils.setEndOfDay(cal);
121                 return cal;
122             }
123             catch (ParseException JavaDoc e) {
124                 throw new RuntimeException JavaDoc("Failed to parse to day.", e);
125             }
126         }
127     }
128         
129     /*
130      * (non-Javadoc)
131      * @see org.treesched.ConstrainedSchedule#getStartDate(java.util.Date)
132      */

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

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