KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.oddjob.schedules.schedules;
2
3 import java.io.Serializable JavaDoc;
4 import java.text.ParseException JavaDoc;
5 import java.util.Date JavaDoc;
6 import java.util.TimeZone JavaDoc;
7
8 import org.apache.log4j.Logger;
9 import org.oddjob.schedules.AbstractSchedule;
10 import org.oddjob.schedules.DateUtils;
11 import org.oddjob.schedules.Interval;
12 import org.oddjob.schedules.Schedule;
13 import org.oddjob.schedules.ScheduleContext;
14 import org.oddjob.util.DateHelper;
15 import org.oddjob.util.OddjobConfigException;
16
17 /**
18  * @oddjob.description Provide a schedule for a
19  * specific date or defining an interval between two dates.
20  * <p>
21  * Any number of nested sub schedules are allowed.
22  *
23  * @oddjob.example
24  * <pre>
25  * &lt;date on="25-dec-04"/&gt;
26  * </pre>
27  *
28  * @author Rob Gordon
29 */

30
31
32 final public class DateSchedule extends AbstractSchedule implements Serializable JavaDoc {
33
34     private static final long serialVersionUID = 20050226;
35     
36     private static Logger logger = Logger.getLogger(DateSchedule.class);
37     
38     
39     private String JavaDoc startDate;
40     private String JavaDoc endDate;
41     
42     /**
43      * @oddjob.property from
44      * @oddjob.description The from date for the schedule. Defaults to the the
45      * 1st January 1970.
46      * @oddjob.required No.
47      *
48      * @param startDateString The from date. May be null which
49      * indicates the beginning of the time.
50      * @throws ParseException If the String isn't a Date.
51      */

52     public void setFrom(String JavaDoc startDateString) {
53         startDate = startDateString;
54     }
55     
56     /**
57      * Get the from Date as a String.
58      *
59      * @return The from date. May be null.
60      */

61     public String JavaDoc getFrom() {
62         return startDate;
63     }
64
65     /**
66      * @oddjob.property to
67      * @oddjob.description The to date for the schedule. Defaults to the
68      * end of time.
69      * @oddjob.required No.
70      *
71      * @param endDateString The end date. May be null.
72      * @throws ParseException If the string isn't a valid date.
73      */

74     public void setTo(String JavaDoc endDateString) {
75         endDate = endDateString;
76     }
77
78     /**
79      * Return the to date as a string.
80      *
81      * @return The to date, may be null.
82      */

83     public String JavaDoc getTo() {
84         return endDate;
85     }
86
87     /**
88      * @oddjob.property on
89      * @oddjob.description A specific date on which to schedule something.
90      * @oddjob.required No.
91      *
92      * @param in The on text.
93      * @throws ParseException If the string isn't a date.
94      */

95     public void setOn(String JavaDoc on) {
96         setFrom(on);
97         setTo(on);
98     }
99
100     
101     /**
102      * Return the start date.
103      *
104      * @return The start date.
105      */

106     public Date JavaDoc getStartDate(ScheduleContext context) {
107         if (startDate == null) {
108             return new Date JavaDoc(Interval.ALWAYS);
109         }
110         
111         TimeZone JavaDoc timeZone = context.getTimeZone();
112         
113         try {
114             return DateHelper.parseDate(startDate, timeZone);
115         } catch (ParseException JavaDoc e) {
116             throw new OddjobConfigException("Failed to parse start date ["
117                     + startDate + "]");
118         }
119         
120     }
121
122     /**
123      * Return the end date.
124      *
125      * @return The end date.
126      */

127     public Date JavaDoc getEndDate(ScheduleContext context) {
128         if (endDate == null) {
129             return new Date JavaDoc(Interval.NEVER_AGAIN);
130         }
131         
132         TimeZone JavaDoc timeZone = context.getTimeZone();
133         
134         try {
135             return DateUtils.endOfDay(DateHelper.parseDate(endDate, timeZone),
136                     timeZone);
137         } catch (ParseException JavaDoc e) {
138             throw new OddjobConfigException("Failed to parse end date ["
139                     + endDate + "]");
140         }
141     }
142
143     /*
144      * (non-Javadoc)
145      * @see org.treesched.Schedule#nextDue(java.util.Date)
146      */

147     public Interval nextDue(ScheduleContext context) {
148         Date JavaDoc now = context.getDate();
149         if (now == null) {
150             return null;
151         }
152         if (now.compareTo(getEndDate(context)) > 0) {
153             return null;
154         }
155         Interval nextInterval = new Interval(
156                 getStartDate(context),
157                 getEndDate(context));
158         Schedule child = getChildSchedule();
159         if (child == null) {
160             return nextInterval;
161         }
162         child.setLimits(nextInterval);
163         Interval childInterval = null;
164         // if now is before the start of the next interval
165
// pass the start of the next interval to the child.
166
if (now.compareTo(nextInterval.getFromDate()) < 0) {
167             childInterval = child.nextDue(
168                     context.spawn(nextInterval.getFromDate()));
169         }
170         else {
171             childInterval = child.nextDue(context);
172         }
173         return childInterval;
174     }
175     /**
176      * Override toString.
177      */

178     public String JavaDoc toString() {
179         
180         return this.getClass().getName() + " from " + getFrom() + " to " + getTo();
181     }
182 }
183
Popular Tags