KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.oddjob.schedules.schedules;
2
3 import java.io.Serializable JavaDoc;
4 import java.text.ParseException JavaDoc;
5 import java.util.Calendar JavaDoc;
6 import java.util.Date JavaDoc;
7 import java.util.TimeZone JavaDoc;
8
9 import org.oddjob.OddjobException;
10 import org.oddjob.schedules.ConstrainedSchedule;
11 import org.oddjob.schedules.DateUtils;
12 import org.oddjob.schedules.ScheduleContext;
13 import org.oddjob.util.DateHelper;
14
15 /**
16  * @oddjob.description A schedule for the times of the day. This schedule
17  * enables job to be schedule at a paticular time or a from/to time which
18  * could be used to constrain a sub schedule.
19  * <p>
20  * Please note the property 'on' is used instead of at, this is due to a
21  * lazy developer sharing logic with other constrained schedules such as
22  * {@link org.oddjob.schedules.schedules.DayOfYear}.
23  * <p>
24  * If the 'to' time is less than the 'from' time it is assumed that the 'to'
25  * time is the next day.
26  *
27  *
28  * @oddjob.example
29  *
30  * A simple example.
31  *
32  * <pre>
33  * &lt;time from="10:00" to="14:00"/&gt;
34  * </pre>
35  *
36  * @oddjob.example
37  *
38  * Using time to schedule an interval between 10pm and 4am the next day.
39  *
40  * <pre>
41  * &lt;time from="22:00" to="04:00" &gt;
42  * &lt;interval interval="00:15" /&gt;
43  * &lt;/time&gt;
44  * </pre>
45  *
46  * @author Rob Gordon
47  */

48
49 final public class TimeSchedule extends ConstrainedSchedule implements Serializable JavaDoc {
50     private static final long serialVersionUID = 20050226;
51         
52     private String JavaDoc from;
53     private String JavaDoc to;
54     
55     /**
56      * @oddjob.property from
57      * @oddjob.description The from time.
58      * @oddjob.required No. Default to the start of the day.
59      *
60      * @param from The from date.
61      */

62     public void setFrom(String JavaDoc from) {
63         this.from = from;
64     }
65
66     /*
67      * (non-Javadoc)
68      * @see org.treesched.ConstrainedSchedule#getFrom()
69      */

70     public String JavaDoc getFrom() {
71         return from;
72     }
73         
74     /**
75      * @oddjob.property to
76      * @oddjob.description The to time.
77      * @oddjob.required No. Default to the end of the day.
78      *
79      * @param to The to date.
80      *
81      */

82     public void setTo(String JavaDoc to) {
83         this.to = to;
84     }
85     
86     /*
87      * (non-Javadoc)
88      * @see org.treesched.ConstrainedSchedule#getTo()
89      */

90     public String JavaDoc getTo() {
91         return to;
92     }
93
94     static Date JavaDoc parseTime(String JavaDoc textField, Date JavaDoc referenceDate,
95             TimeZone JavaDoc timeZone, String JavaDoc fieldName) {
96         String JavaDoc dateText = DateHelper.formatDate(referenceDate);
97         try {
98             return DateHelper.parseDateTime(dateText + " " + textField, timeZone);
99         } catch (ParseException JavaDoc e) {
100             throw new OddjobException("Failed to parse " + fieldName
101                     + "[" + textField + "]");
102         }
103     }
104
105     Calendar JavaDoc fromCalendar(Date JavaDoc referenceDate, TimeZone JavaDoc timeZone) {
106         Calendar JavaDoc fromCal = Calendar.getInstance(timeZone);
107         if (from == null) {
108             fromCal.setTime(DateUtils.startOfDay(referenceDate, timeZone));
109         }
110         else {
111             fromCal.setTime(parseTime(from, referenceDate, timeZone, "from"));
112         }
113         return fromCal;
114     }
115     
116     Calendar JavaDoc toCalendar(Date JavaDoc referenceDate, TimeZone JavaDoc timeZone) {
117         Calendar JavaDoc toCal = Calendar.getInstance(timeZone);
118         if (to == null) {
119             toCal.setTime(DateUtils.endOfDay(referenceDate, timeZone));
120         }
121         else {
122             toCal.setTime(parseTime(to, referenceDate, timeZone, "to"));
123         }
124         return toCal;
125     }
126         
127         
128     /*
129      * (non-Javadoc)
130      * @see org.treesched.ConstrainedSchedule#getStartDate(java.util.Date)
131      */

132     public Date JavaDoc getStartDate(ScheduleContext context) {
133         Calendar JavaDoc fromCal = fromCalendar(context.getDate(), context.getTimeZone());
134         Calendar JavaDoc toCal = toCalendar(context.getDate(), context.getTimeZone());
135         
136         if (!fromCal.equals(toCal) && toCal.get(Calendar.MILLISECOND) == 0) {
137             toCal.add(Calendar.MILLISECOND, -1);
138         }
139         
140         Calendar JavaDoc nowCal = Calendar.getInstance(context.getTimeZone());
141         nowCal.setTime(context.getDate());
142         
143         // if from > to ie. 22:00 to 02:00
144
// if it's after 02:00 then next from is 22:00 tonight
145
// else it was 22:00 last night
146
// else its like 10:00 to 14:00
147
// so if it's after 14:00, it 10 am tomorrow
148
// otherwise it's 10 am today.
149
if (DateUtils.compare(fromCal, toCal) > 0) {
150             if (DateUtils.compare(nowCal, toCal) > 0) {
151                 return fromCal.getTime();
152             }
153             else {
154                 fromCal.add(Calendar.DATE, -1);
155                 return fromCal.getTime();
156             }
157         }
158         else {
159             if (DateUtils.compare(nowCal, toCal) > 0) {
160                 fromCal.add(Calendar.DATE, 1);
161                 return fromCal.getTime();
162             }
163             else {
164                 return fromCal.getTime();
165             }
166         }
167     }
168     
169     /*
170      * (non-Javadoc)
171      * @see org.treesched.ConstrainedSchedule#getEndDate(java.util.Date)
172      */

173     public Date JavaDoc getEndDate(ScheduleContext context) {
174         Calendar JavaDoc fromCal = fromCalendar(context.getDate(), context.getTimeZone());
175         Calendar JavaDoc toCal = toCalendar(context.getDate(), context.getTimeZone());
176         
177         if (!fromCal.equals(toCal) && toCal.get(Calendar.MILLISECOND) == 0) {
178             toCal.add(Calendar.MILLISECOND, -1);
179         }
180
181         Calendar JavaDoc nowCal = Calendar.getInstance(context.getTimeZone());
182         nowCal.setTime(context.getDate());
183         
184         // if it's past the to time regardless of the
185
// from time then to to time must be tomorrow.
186
if (DateUtils.compare(nowCal, toCal) > 0) {
187             toCal.add(Calendar.DATE, 1);
188             return toCal.getTime();
189         }
190         else {
191             return toCal.getTime();
192         }
193     }
194
195 }
196
Popular Tags