1 package org.opensync.engine.server;
2
3 import java.io.*;
4 import java.util.*;
5 import java.text.*;
6
7 /***
8 * This class gives the necessary information to schedule a task
9 * If a date is given, the task will start at the specific date and time. One shot. Others paramters will be ignored.
10 * If days of month are given, the task will start all months at the specific days and time. Others paramters will be ignored.
11 * If days of week are given, the task will start all week at the specific days and time.
12 * Use repeat to repeat the task with a period interval. Intraday only.
13 */
14
15 public class Schedule implements Serializable {
16 /***
17 * The date to start the task
18 *
19 */
20 private Date date;
21 /***
22 * The days of month to start the task
23 *
24 */
25 private int[]daysOfMonth;
26 /***
27 * The days of week to start the task
28 *
29 */
30 private int[]daysOfWeek;
31 /***
32 * The hour of week to start the task
33 *
34 */
35 private int hour;
36 /***
37 * The minute of week to start the task
38 *
39 */
40 private int minute;
41 /***
42 * The number of repetition
43 *
44 */
45 private int repeat;
46 /***
47 * The period between two repetition (in minute)
48 *
49 *
50 */
51 private int period;
52 /***
53 * Is a http request
54 *
55 */
56 private boolean httpRequest;
57
58 /***
59 * Construct a schedule
60 *
61 *
62 */
63 public Schedule() {
64 }
65 /***
66 * Set the date
67 *
68 * @param date the date (dd/MM/yyyy)
69 *
70 */
71 public void setDate(Date date) {
72 this.date = date;
73 }
74 /***
75 * Get the date
76 *
77 */
78 public Date getDate() {
79 return date;
80 }
81 /***
82 * Set the days of month.
83 *
84 * @param daysOfMonth days of month of the schedulerEntry (-1 if every day).
85 * This attribute is exclusive with daysOfWeek.
86 * Allowed values 1-31.
87 */
88 public void setDaysOfMonth(int[]daysOfMonth) {
89 this.daysOfMonth = daysOfMonth;
90 }
91 /***
92 * Get the days of month
93 *
94 */
95 public int[] getDaysOfMonth() {
96 return daysOfMonth;
97 }
98 /***
99 * Get the days of month in String format
100 *
101 */
102 public String getStrDaysOfMonth() {
103 if(daysOfMonth == null){
104 return "";
105 }
106 StringBuffer buffer = new StringBuffer();
107 for(int i = 0;i<daysOfMonth.length;i++){
108 buffer.append(daysOfMonth[i]);
109 }
110 return buffer.toString();
111 }
112 /***
113 * Set the hour
114 *
115 * @param hour hour of the schedulerEntry. Allowed values 0-23
116 */
117 public void setHour(int hour) {
118 this.hour = hour;
119 }
120 /***
121 * get the hour
122 *
123 */
124 public int getHour() {
125 return hour;
126 }
127 /***
128 * Set the repeat option.
129 * Use it when you want to repeat the task.
130 *
131 * @param repeat the number to repeat
132 */
133 public void setRepeat(int repeat) {
134 this.repeat = repeat;
135 }
136 /***
137 * Get the repeat number
138 *
139 */
140 public int getRepeat() {
141 return repeat;
142 }
143 /***
144 * Set the period between two repetition.
145 *
146 * @param period the repetition period in minute
147 */
148 public void setPeriod(int period) {
149 this.period = period;
150 }
151 /***
152 * Get the period between two repetition.
153 *
154 */
155 public int getPeriod() {
156 return period;
157 }
158 /***
159 * Set the days of week.
160 *
161 * @param daysOfWeek day of week of the schedulerEntry (-1 if every day).
162 * This attribute is exclusive with dayOfMonth.
163 * Allowed values 1-7 (1 = Sunday, 2 = Monday, ...). java.util.Calendar constants can be used.
164 */
165 public void setDaysOfWeek(int[]daysOfWeek) {
166 this.daysOfWeek = daysOfWeek;
167 }
168 /***
169 * Get the days of week
170 *
171 */
172 public int[] getDaysOfWeek() {
173 return daysOfWeek;
174 }
175 /***
176 * Get the days of week in String format
177 *
178 */
179 public String getStrDaysOfWeek() {
180 if(daysOfWeek == null){
181 return "";
182 }
183 StringBuffer buffer = new StringBuffer();
184 for(int i = 0;i<daysOfWeek.length;i++){
185 buffer.append(daysOfWeek[i]);
186 }
187 return buffer.toString();
188 }
189 /***
190 * Check if the schedule is valid.
191 *
192 */
193 public boolean isValid(){
194 return date != null || httpRequest
195 || (daysOfMonth != null && !daysOfMonth.equals(""))
196 || (daysOfWeek != null && !daysOfWeek.equals(""));
197 }
198 /***
199 * Return the full date, time comprise
200 *
201 */
202 public Date getDateTime(){
203 SimpleDateFormat dateFormat = new SimpleDateFormat("ddMMyyyy");
204 SimpleDateFormat dateTimeFormat = new SimpleDateFormat("ddMMyyyyHHmm");
205 try {
206 return dateTimeFormat.parse(dateFormat.format(getDate()) + hour + minute);
207 }
208 catch (Exception ex) {
209 throw new RuntimeException(ex.getMessage());
210 }
211 }
212 /***
213 * Convert a schedule to a string. (Use for debug only)
214 *
215 *
216 */
217 public String toString(){
218 SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
219 SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
220
221 StringBuffer buffer = new StringBuffer();
222 buffer.append("Schedule{\n");
223 buffer.append("\tdate : ").append(date == null ? "" : dateFormat.format(date)).append("\n");
224 buffer.append("\tdaysOfMonth : ").append(getStrDaysOfMonth()).append("\n");
225 buffer.append("\tdaysOfWeek : ").append(getStrDaysOfWeek()).append("\n");
226 buffer.append("\ttime : ").append(hour).append(":").append(minute).append("\n");
227 buffer.append("\trepeat : ").append(repeat).append("\n");
228 buffer.append("\tperiod : ").append(period).append("\n");
229 return buffer.toString();
230 }
231 /***
232 * Set the minute
233 *
234 * @param minute minute of the schedulerEntry. Allowed values 0-59
235 */
236 public void setMinute(int minute) {
237 this.minute = minute;
238 }
239 /***
240 * Get the hour
241 *
242 */
243 public int getMinute() {
244 return minute;
245 }
246 /***
247 * The scheduling is made by an http request
248 *
249 * @param httpRequest
250 */
251 public void setHttpRequest(boolean httpRequest) {
252 this.httpRequest = httpRequest;
253 }
254 /***
255 * Return <code>true</code> if the scheduling is made by an http request
256 *
257 */
258 public boolean isHttpRequest() {
259 return httpRequest;
260 }
261 }