001: /*
002: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
003: * Distributed under the terms of either:
004: * - the common development and distribution license (CDDL), v1.0; or
005: * - the GNU Lesser General Public License, v2.1 or later
006: * $Id: Task.java 3714 2007-04-08 02:57:38Z gbevin $
007: */
008: package com.uwyn.rife.scheduler;
009:
010: import com.uwyn.rife.config.RifeConfig;
011: import com.uwyn.rife.scheduler.exceptions.FrequencyException;
012: import com.uwyn.rife.scheduler.exceptions.SchedulerException;
013: import com.uwyn.rife.site.Validation;
014: import com.uwyn.rife.site.ValidationError;
015: import com.uwyn.rife.site.ValidationRule;
016: import com.uwyn.rife.site.ValidationRuleNotEmpty;
017: import com.uwyn.rife.site.ValidationRuleNotNull;
018: import com.uwyn.rife.tools.Localization;
019: import java.util.Calendar;
020: import java.util.Date;
021:
022: public class Task extends Validation implements Cloneable {
023: private int mId = -1;
024: private String mType = null;
025: private long mPlanned = 0;
026: private Frequency mFrequency = null;
027: private boolean mBusy = false;
028:
029: private TaskManager mTaskManager = null;
030:
031: public Task() {
032: }
033:
034: protected void activateValidation() {
035: addRule(new ValidationRuleNotNull("type"));
036: addRule(new ValidationRuleNotEmpty("planned"));
037: addRule(new InvalidPlanned());
038: }
039:
040: public void setTaskManager(TaskManager taskManager) {
041: mTaskManager = taskManager;
042: }
043:
044: public TaskManager getTaskManager() {
045: return mTaskManager;
046: }
047:
048: public String getTaskoptionValue(String name)
049: throws SchedulerException {
050: if (null == name)
051: throw new IllegalArgumentException("name can't be null.");
052: if (0 == name.length())
053: throw new IllegalArgumentException("name can't be empty.");
054:
055: if (null == mTaskManager) {
056: return null;
057: }
058:
059: Scheduler scheduler = mTaskManager.getScheduler();
060: if (null == scheduler) {
061: return null;
062: }
063:
064: TaskoptionManager taskoption_manager = scheduler
065: .getTaskoptionManager();
066: if (null == taskoption_manager) {
067: return null;
068: }
069:
070: Taskoption taskoption = taskoption_manager.getTaskoption(
071: getId(), name);
072: if (null == taskoption) {
073: return null;
074: }
075:
076: return taskoption.getValue();
077: }
078:
079: public long getNextDate() throws FrequencyException {
080: // lower towards the minute, remove seconds and milliseconds
081: Calendar current_calendar = Calendar.getInstance(
082: RifeConfig.Tools.getDefaultTimeZone(), Localization
083: .getLocale());
084: current_calendar.set(Calendar.SECOND, 0);
085: current_calendar.set(Calendar.MILLISECOND, 0);
086: long current_time = current_calendar.getTimeInMillis();
087: if (mPlanned <= current_time) {
088: return getNextDate(current_time);
089: }
090:
091: return -1;
092: }
093:
094: public long getNextDate(long start) throws FrequencyException {
095: if (null == mFrequency) {
096: return -1;
097: } else {
098: return mFrequency.getNextDate(start);
099: }
100: }
101:
102: public void setId(int id) {
103: mId = id;
104: }
105:
106: public int getId() {
107: return mId;
108: }
109:
110: public void setType(String type) {
111: mType = type;
112: }
113:
114: public String getType() {
115: return mType;
116: }
117:
118: public void setPlanned(Date planned) {
119: setPlanned(planned.getTime());
120: }
121:
122: public void setPlanned(long planned) {
123: // lower towards the minute, remove seconds and milliseconds
124: Calendar planned_calendar = Calendar.getInstance(
125: RifeConfig.Tools.getDefaultTimeZone(), Localization
126: .getLocale());
127: planned_calendar.setTimeInMillis(planned);
128: planned_calendar.set(Calendar.SECOND, 0);
129: planned_calendar.set(Calendar.MILLISECOND, 0);
130:
131: mPlanned = planned_calendar.getTimeInMillis();
132: }
133:
134: public long getPlanned() {
135: return mPlanned;
136: }
137:
138: public void setFrequency(String frequency)
139: throws FrequencyException {
140: if (null == frequency) {
141: mFrequency = null;
142: } else {
143: mFrequency = new Frequency(frequency);
144: }
145: }
146:
147: public String getFrequency() {
148: if (null == mFrequency) {
149: return null;
150: }
151: return mFrequency.getFrequency();
152: }
153:
154: public void setBusy(boolean busy) {
155: mBusy = busy;
156: }
157:
158: public boolean isBusy() {
159: return mBusy;
160: }
161:
162: public Task clone() throws CloneNotSupportedException {
163: return (Task) super .clone();
164: }
165:
166: public boolean equals(Object object) {
167: Task other_task = (Task) object;
168:
169: if (null != object
170: && other_task.getId() == getId()
171: && other_task.getType().equals(getType())
172: && other_task.getPlanned() == getPlanned()
173: && ((null == other_task.getFrequency() && null == getFrequency()) || (other_task
174: .getFrequency() != null && other_task
175: .getFrequency().equals(getFrequency())))
176: && other_task.getTaskManager() == getTaskManager()) {
177: return true;
178: }
179:
180: return false;
181: }
182:
183: public class InvalidPlanned implements ValidationRule {
184: public boolean validate() {
185: if (0 == mPlanned) {
186: return true;
187: }
188:
189: Calendar current_calendar = Calendar.getInstance(
190: RifeConfig.Tools.getDefaultTimeZone(), Localization
191: .getLocale());
192: current_calendar.set(Calendar.SECOND, 0);
193: current_calendar.set(Calendar.MILLISECOND, 0);
194: return mPlanned >= current_calendar.getTimeInMillis();
195: }
196:
197: public String getSubject() {
198: return "planned";
199: }
200:
201: public ValidationError getError() {
202: return new ValidationError.INVALID(getSubject());
203: }
204:
205: public Object getBean() {
206: return null;
207: }
208:
209: public <T extends ValidationRule> T setBean(Object bean) {
210: return (T) this ;
211: }
212:
213: public Object clone() {
214: return this;
215: }
216: }
217: }
|