1 package org.apache.turbine.services.schedule;
2
3
4 import java.sql.Date;
5 import java.util.Calendar;
6
7 import org.apache.commons.lang.StringUtils;
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.apache.torque.om.Persistent;
11 import org.apache.turbine.util.TurbineException;
12
13
14
15
16
17
18
19
20
21
22 public class JobEntry
23 extends org.apache.turbine.services.schedule.BaseJobEntry
24 implements Persistent
25 {
26
27
28
29 private static final long serialVersionUID = -5501116588294474363L;
30
31
32 private static Log log = LogFactory.getLog(ScheduleService.LOGGER_NAME);
33
34
35 private boolean jobIsActive = false;
36
37
38 private long runtime = 0;
39
40
41 private static final int SECOND = 0;
42 private static final int MINUTE = 1;
43 private static final int WEEK_DAY = 2;
44 private static final int DAY_OF_MONTH = 3;
45 private static final int DAILY = 4;
46
47
48
49
50 public JobEntry()
51 {
52
53 }
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86 public JobEntry(int sec,
87 int min,
88 int hour,
89 int wd,
90 int day_mo,
91 String task)
92 throws TurbineException
93 {
94 if (StringUtils.isEmpty(task))
95 {
96 throw new TurbineException("Error in JobEntry. " +
97 "Bad Job parameter. Task not set.");
98 }
99
100 setSecond(sec);
101 setMinute(min);
102 setHour(hour);
103 setWeekDay(wd);
104 setDayOfMonth(day_mo);
105 setTask(task);
106
107 calcRunTime();
108 }
109
110
111
112
113
114
115
116
117
118 public int compareTo(Object je)
119 {
120 int result = -1;
121 if (je instanceof JobEntry)
122 {
123 result = getJobId() - ((JobEntry) je).getJobId();
124 }
125 return result;
126 }
127
128
129
130
131
132
133 public void setActive(boolean isActive)
134 {
135 jobIsActive = isActive;
136 }
137
138
139
140
141
142
143
144 public boolean isActive()
145 {
146 return jobIsActive;
147 }
148
149
150
151
152
153
154 public long getNextRuntime()
155 {
156 return runtime;
157 }
158
159
160
161
162
163
164 public Date getNextRunDate()
165 {
166 return new Date(runtime);
167 }
168
169
170
171
172
173
174 public String getNextRunAsString()
175 {
176 return getNextRunDate().toString();
177 }
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195 public void calcRunTime()
196 throws TurbineException
197 {
198 Calendar schedrun = Calendar.getInstance();
199 Calendar now = Calendar.getInstance();
200
201 switch (evaluateJobType())
202 {
203 case SECOND:
204
205 schedrun.add(Calendar.SECOND, getSecond());
206 runtime = schedrun.getTime().getTime();
207 break;
208
209 case MINUTE:
210
211 schedrun.add(Calendar.SECOND, getSecond());
212 schedrun.add(Calendar.MINUTE, getMinute());
213 runtime = schedrun.getTime().getTime();
214 break;
215
216 case WEEK_DAY:
217
218 schedrun.set(Calendar.SECOND, getSecond());
219 schedrun.set(Calendar.MINUTE, getMinute());
220 schedrun.set(Calendar.HOUR_OF_DAY, getHour());
221 schedrun.set(Calendar.DAY_OF_WEEK, getWeekDay());
222
223 if (now.before(schedrun))
224 {
225
226 runtime = schedrun.getTime().getTime();
227 }
228 else
229 {
230
231 schedrun.add(Calendar.DAY_OF_WEEK, 7);
232 runtime = schedrun.getTime().getTime();
233 }
234 break;
235
236 case DAY_OF_MONTH:
237
238 schedrun.set(Calendar.SECOND, getSecond());
239 schedrun.set(Calendar.MINUTE, getMinute());
240 schedrun.set(Calendar.HOUR_OF_DAY, getHour());
241 schedrun.set(Calendar.DAY_OF_MONTH, getDayOfMonth());
242
243 if (now.before(schedrun))
244 {
245
246 runtime = schedrun.getTime().getTime();
247 }
248 else
249 {
250
251 schedrun.add(Calendar.MONTH, 1);
252 runtime = schedrun.getTime().getTime();
253 }
254 break;
255
256 case DAILY:
257
258 schedrun.set(Calendar.SECOND, getSecond());
259 schedrun.set(Calendar.MINUTE, getMinute());
260 schedrun.set(Calendar.HOUR_OF_DAY, getHour());
261
262
263 if (now.before(schedrun))
264 {
265 runtime = schedrun.getTime().getTime();
266 }
267 else
268 {
269
270 schedrun.add(Calendar.HOUR_OF_DAY, 24);
271 runtime = schedrun.getTime().getTime();
272 }
273 break;
274
275 default:
276
277 }
278
279 log.info("Next runtime for task " + this.getTask() + " is " + this.getNextRunDate());
280 }
281
282
283
284
285
286
287
288
289
290
291
292 private int evaluateJobType()
293 throws TurbineException
294 {
295
296
297 if (getDayOfMonth() < 0)
298 {
299
300 if (getWeekDay() < 0)
301 {
302
303 if (getHour() < 0)
304 {
305
306 if (getMinute() < 0)
307 {
308
309 if (getSecond() < 0)
310 throw new TurbineException("Error in JobEntry. Bad Job parameter.");
311
312 return SECOND;
313 }
314 else
315 {
316
317
318 if (getMinute() < 0 || getSecond() < 0)
319 throw new TurbineException("Error in JobEntry. Bad Job parameter.");
320
321 return MINUTE;
322 }
323 }
324 else
325 {
326
327
328 if (getMinute() < 0 || getHour() < 0 || getSecond() < 0)
329 throw new TurbineException("Error in JobEntry. Bad Job parameter.");
330
331 return DAILY;
332 }
333 }
334 else
335 {
336
337
338 if (getMinute() < 0 || getHour() < 0 || getSecond() < 0)
339 throw new TurbineException("Error in JobEntry. Bad Job parameter.");
340
341 return WEEK_DAY;
342 }
343 }
344 else
345 {
346
347
348 if (getMinute() < 0 || getHour() < 0)
349 throw new TurbineException("Error in JobEntry. Bad Job parameter.");
350
351 return DAY_OF_MONTH;
352 }
353 }
354
355 }