KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > ejb > client > models > ScheduledTaskModel


1 /*
2  * This software was designed and created by Jason Carroll.
3  * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4  * The author can be reached at jcarroll@cowsultants.com
5  * ITracker website: http://www.cowsultants.com
6  * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it only under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  */

18
19 package cowsultants.itracker.ejb.client.models;
20
21 import java.util.*;
22
23 /**
24   * This represents a currently scheduled task to be run by the scheduler.
25   */

26 public class ScheduledTaskModel extends GenericModel implements Cloneable JavaDoc {
27     public int[] hours = new int[] { -1 };
28     public int[] mins = new int[] { -1 };
29     public int[] mdays = new int[] { -1 };
30     public int[] months = new int[] { -1 };
31     public int[] wdays = new int[] { -1 };
32     public Date lastRun = null;
33
34     public String JavaDoc className = null;
35     public String JavaDoc[] args = new String JavaDoc[0];
36
37     public ScheduledTaskModel() {
38     }
39
40     public int[] getHours() {
41         return hours;
42     }
43
44     public void setHours(int[] value) {
45         hours = value;
46     }
47
48     public void setHours(String JavaDoc value) {
49         hours = parseString(value);
50     }
51
52     public int[] getMinutes() {
53         return mins;
54     }
55
56     public void setMinutes(int[] value) {
57         mins = value;
58     }
59
60     public void setMinutes(String JavaDoc value) {
61         mins = parseString(value);
62     }
63
64     public int[] getDaysOfMonth() {
65         return mdays;
66     }
67
68     public void setDaysOfMonth(int[] value) {
69         mdays = value;
70     }
71
72     public void setDaysOfMonth(String JavaDoc value) {
73         mdays = parseString(value);
74     }
75
76     public int[] getMonths() {
77         return months;
78     }
79
80     public void setMonths(int[] value) {
81         months = value;
82     }
83
84     public void setMonths(String JavaDoc value) {
85         months = parseString(value);
86     }
87
88     public int[] getWeekdays() {
89         return wdays;
90     }
91
92     public void setWeekdays(int[] value) {
93         wdays = value;
94     }
95
96     public void setWeekdays(String JavaDoc value) {
97         wdays = parseString(value);
98     }
99
100     public Date getLastRun() {
101         return lastRun;
102     }
103
104     public void setLastRun(Date value) {
105         lastRun = value;
106     }
107
108     public String JavaDoc getClassName() {
109         return className;
110     }
111
112     public void setClassName(String JavaDoc value) {
113         className = value;
114     }
115
116     public String JavaDoc[] getArgs() {
117         return args;
118     }
119
120     public void setArgs(String JavaDoc[] value) {
121         args = value;
122     }
123
124     public void setArgs(String JavaDoc value) {
125         if(value != null && ! "".equals(value)) {
126             Vector argsVector = new Vector();
127             StringTokenizer token = new StringTokenizer(value, " ");
128             while(token.hasMoreElements()) {
129                 argsVector.addElement((String JavaDoc) token.nextElement());
130             }
131             args = new String JavaDoc[argsVector.size()];
132             argsVector.copyInto(args);
133         }
134     }
135
136     public String JavaDoc getArgsAsString() {
137         if(args == null || args.length == 0) {
138             return "";
139         }
140
141         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
142         for(int i = 0; i < args.length; i++) {
143             buf.append((i == 0 ? "" : " "));
144             buf.append(args[i]);
145         }
146         return buf.toString();
147     }
148
149     /**
150       * Takes a string in normal cron format, and turns it into
151       * a int array for easier processing.
152       * @param value string to parse
153       * @return an int array representing the parsed string
154       */

155     public int[] parseString(String JavaDoc value) {
156         int[] valuesArray = new int[] { -1 };
157
158         if(value != null && ! "".equals(value)) {
159             Vector valuesVector = new Vector();
160             StringTokenizer token = new StringTokenizer(value, ",");
161             while(token.hasMoreElements()) {
162                 String JavaDoc element = (String JavaDoc) token.nextElement();
163                 try {
164                     Integer JavaDoc intElement = new Integer JavaDoc(element.trim());
165                     valuesVector.addElement(intElement);
166                 } catch(NumberFormatException JavaDoc nfe) {
167                 }
168             }
169             valuesArray = new int[valuesVector.size()];
170             for(int i = 0; i < valuesVector.size(); i++) {
171                 valuesArray[i] = ((Integer JavaDoc) valuesVector.elementAt(i)).intValue();
172             }
173         }
174         return valuesArray;
175     }
176
177     /**
178       * Takes an int array and collapses it back to a string for use in
179       * storage.
180       * @param values an int array to join
181       * @return a string representing the int array
182       */

183     public String JavaDoc joinString(int[] values) {
184         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
185         if(values != null) {
186             for(int i = 0; i < values.length; i++) {
187                 buf.append((i == 0 ? "" : ","));
188                 buf.append(Integer.toString(values[i]));
189             }
190         }
191         return buf.toString();
192     }
193
194     public boolean isAll(int[] array) {
195         if(array != null) {
196             for(int i = 0; i < array.length; i++) {
197                 if(array[i] == -1) {
198                     return true;
199                 }
200             }
201         }
202         return false;
203     }
204
205     public Object JavaDoc clone() {
206         Object JavaDoc clone = null;
207         ScheduledTaskModel clonedTask = null;
208         try {
209             clone = super.clone();
210         } catch(CloneNotSupportedException JavaDoc e) {
211         }
212
213         clonedTask = (ScheduledTaskModel) clone;
214
215         if(clonedTask != null) {
216             if(id != null) {
217                 clonedTask.id = new Integer JavaDoc(id.intValue());
218             }
219
220             if(hours != null) {
221                 clonedTask.hours = new int[hours.length];
222                 for(int i = 0; i < hours.length; i++) {
223                     clonedTask.hours[i] = hours[i];
224                 }
225             }
226             if(mins != null) {
227                 clonedTask.mins = new int[mins.length];
228                 for(int i = 0; i < mins.length; i++) {
229                     clonedTask.mins[i] = mins[i];
230                 }
231             }
232             if(mdays != null) {
233                 clonedTask.mdays = new int[mdays.length];
234                 for(int i = 0; i < mdays.length; i++) {
235                     clonedTask.mdays[i] = mdays[i];
236                 }
237             }
238             if(months != null) {
239                 clonedTask.months = new int[months.length];
240                 for(int i = 0; i < months.length; i++) {
241                     clonedTask.months[i] = months[i];
242                 }
243             }
244             if(wdays != null) {
245                 clonedTask.wdays = new int[wdays.length];
246                 for(int i = 0; i < wdays.length; i++) {
247                     clonedTask.wdays[i] = wdays[i];
248                 }
249             }
250
251             if(className != null) {
252                 clonedTask.className = new String JavaDoc(className);
253             }
254             if(args != null) {
255                 clonedTask.args = new String JavaDoc[args.length];
256                 for(int i = 0; i < args.length; i++) {
257                     clonedTask.args[i] = args[i];
258                 }
259             }
260
261             if(lastRun != null) {
262                 clonedTask.lastRun = (Date) lastRun.clone();
263             }
264         }
265         return clonedTask;
266     }
267
268     public String JavaDoc toString() {
269         return "Id: " + getId() + " Class: " + getClassName();
270     }
271 }
272
Popular Tags