1 package org.opensync.schedule;
2
3 import java.io.*;
4 import java.util.*;
5 import java.text.*;
6
7
8 /***
9 * This class reprsents a entry of the scheduler
10 *
11 * @author SOFTMED
12 */
13 public class SchedulerEntry implements Serializable, Comparable {
14 /***
15 * The date
16 *
17 */
18 protected Date date;
19 /***
20 * The hour
21 *
22 */
23 protected int hour = 0;
24 /***
25 * The minute
26 *
27 */
28 protected int minute = 0;
29 /***
30 * The day of week
31 *
32 */
33 protected int dayOfWeek = -1;
34 /***
35 * The day of month
36 *
37 */
38 protected int dayOfMonth = -1;
39
40 /***
41 * The runnable object to start
42 *
43 */
44 private Runnable runnable;
45 /***
46 * The real time
47 *
48 *
49 */
50 private long time;
51 /***
52 * Is a time scheduling
53 *
54 */
55 private boolean timeSchedule;
56 /***
57 * The name of the entry
58 *
59 */
60 private String name;
61
62 /***
63 * Construct a schedule entry
64 *
65 * @param name the name of the entry
66 * @param date the date to start the runnable
67 * @param runnable the runnable object to start
68 */
69 public SchedulerEntry(String name,Date date,Runnable runnable) {
70 timeSchedule = true;
71 this.name = name;
72 this.date = date;
73 this.runnable = runnable;
74 }
75 /***
76 * Construct a schedule entry
77 *
78 * @param name the name of the entry
79 * @param runnable the runnable object to start
80 */
81 public SchedulerEntry(String name,Runnable runnable) {
82 timeSchedule = false;
83 this.name = name;
84 this.runnable = runnable;
85 }
86 /***
87 * Construct a schedule entry
88 *
89 * @param name the name of the entry
90 * @param day
91 * @param hour
92 * @param minute
93 * @param runnable the runnable object to start
94 * @param ofWeek
95 */
96 public SchedulerEntry(
97 String name,int day,int hour,int minute,Runnable runnable,boolean ofWeek) {
98 timeSchedule = true;
99 this.name = name;
100 this.hour = hour;
101 this.minute = minute;
102 this.dayOfWeek = ofWeek ? day : -1;
103 this.dayOfMonth = ofWeek ? -1 : day;
104 this.runnable = runnable;
105 }
106 /***
107 * @param date
108 */
109 /***
110 * Get the date
111 *
112 */
113 public Date getDate() {
114 return date;
115 }
116 /***
117 * Use for debug only
118 *
119 */
120 public String toString(){
121 SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
122 if(isTimeSchedule()){
123 return format.format(new Date(time)) + " " + name;
124 }
125 else{
126 return format.format("Http request " + name);
127 }
128 }
129 /***
130 * Compare the date of the entry
131 *
132 * @param o the entry to compare
133 */
134 public int compareTo(Object o) {
135 if(super.equals(o)){
136 return 0;
137 }
138 else{
139 SchedulerEntry a = (SchedulerEntry)o;
140 return (time <= a.getTime() ? -1 : 1);
141
142 }
143 }
144 /***
145 * @param hour
146 */
147 /***
148 * Get the hour
149 *
150 */
151 public int getHour() {
152 return hour;
153 }
154 /***
155 * @param minute
156 */
157 /***
158 * Get the minute
159 *
160 */
161 public int getMinute() {
162 return minute;
163 }
164 /***
165 * @param dayOfWeek
166 */
167 /***
168 * Get the day of week
169 *
170 */
171 public int getDayOfWeek() {
172 return dayOfWeek;
173 }
174 /***
175 * @param time
176 */
177 /***
178 * Get the time
179 *
180 */
181 public long getTime() {
182 return time;
183 }
184 /***
185 * @param dayOfMonth
186 */
187 /***
188 * Get the day of month
189 *
190 */
191 public int getDayOfMonth() {
192 return dayOfMonth;
193 }
194 /***
195 * Get the runnable object
196 *
197 */
198 public Runnable getRunnable() {
199 return runnable;
200 }
201 /****/
202 public int hashCode() {
203 return runnable.hashCode();
204 }
205 /***
206 * Set next time to start the runnable object
207 *
208 */
209 public void setNextTime(){
210 if(getDate() != null){
211 time = getDate().getTime();
212 }
213 else{
214 Calendar cal = GregorianCalendar.getInstance();
215 if(hour == -1){
216 cal.add(Calendar.SECOND,1);
217 hour = 0;
218 time = cal.getTime().getTime();
219 }
220 else if(dayOfMonth != -1){
221 if(dayOfMonth < cal.get(Calendar.DAY_OF_MONTH)){
222 cal.add(Calendar.MONTH,1);
223 }
224 else if(dayOfMonth == cal.get(Calendar.DAY_OF_MONTH) && isPastTime(hour,minute)){
225 cal.add(Calendar.MONTH,1);
226 }
227 cal.set(Calendar.DAY_OF_MONTH,dayOfMonth);
228 }
229 else if(dayOfWeek != -1){
230 if(dayOfWeek < cal.get(Calendar.DAY_OF_WEEK)){
231 cal.add(Calendar.DATE,7);
232 }
233 else if(dayOfWeek == cal.get(Calendar.DAY_OF_WEEK) && isPastTime(hour,minute)){
234 cal.add(Calendar.DATE,7);
235 }
236 cal.set(Calendar.DAY_OF_WEEK,dayOfWeek);
237 }
238 else if(isPastTime(hour,minute)){
239 cal.add(Calendar.DATE,1);
240 }
241 cal.set(Calendar.HOUR_OF_DAY,hour);
242 cal.set(Calendar.MINUTE,minute);
243 cal.set(Calendar.SECOND,0);
244 time = cal.getTime().getTime();
245 }
246 }
247 /***
248 * Test if the time is a past time
249 *
250 * @param hour the hout to test
251 * @param minute
252 */
253 protected boolean isPastTime(int hour,int minute){
254 Calendar cal = GregorianCalendar.getInstance();
255 if(cal.get(Calendar.HOUR_OF_DAY) > hour){
256 return true;
257 }
258 else if(cal.get(Calendar.HOUR_OF_DAY) == hour){
259 return cal.get(Calendar.MINUTE) >= minute;
260
261 }
262 else{
263 return false;
264 }
265 }
266 /***
267 * Is a time scheduling
268 *
269 */
270 public boolean isTimeSchedule() {
271 return timeSchedule;
272 }
273 /***
274 * Get the name of the entry
275 *
276 */
277 public String getName() {
278 return name;
279 }
280 }