001: package com.openedit.modules.scheduler;
002:
003: import java.util.TimerTask;
004:
005: import com.openedit.config.Configuration;
006:
007: public class Action {
008:
009: protected long fieldPeriod;
010: protected String fieldFormattedPeriod;
011: protected long fieldDelay;
012: protected String fieldFormattedDelay;
013: protected String fieldPath;
014: protected String fieldUserName;
015: protected boolean fieldStarted;
016: protected TimerTask fieldTimerTask;
017: protected Configuration fieldConfig;
018:
019: public Action() {
020: setDefaults();
021: }
022:
023: public Action(String inPath, long inDelay, long inPeriod) {
024: setPath(inPath);
025: setDelay(inDelay);
026: setPeriod(inPeriod);
027: }
028:
029: Action(String inPath, String inDelay, String inPeriod) {
030: setDefaults();
031: setPath(inPath);
032: if (inDelay != null && inDelay.length() > 0) {
033: setDelay(inDelay);
034: }
035: if (inPeriod != null && inPeriod.length() > 0) {
036: setPeriod(inPeriod);
037: }
038: }
039:
040: private void setDefaults() {
041: setPeriod(12 * 60 * 60 * 1000); //defaults to runing every 12 hours
042: setDelay(60000L); //defaults to one minute
043: }
044:
045: public long getDelay() {
046: return fieldDelay;
047: }
048:
049: public String getFormattedDelay() {
050: return fieldFormattedDelay;
051: }
052:
053: public void setDelay(long inDelay) {
054: fieldDelay = inDelay;
055: }
056:
057: public void setDelay(String inDelay) {
058: fieldDelay = parse(inDelay);
059: fieldFormattedDelay = inDelay;
060: }
061:
062: public String getPath() {
063: return fieldPath;
064: }
065:
066: public String getName() {
067: return getPath();
068: }
069:
070: public void setPath(String inPath) {
071: fieldPath = inPath;
072: }
073:
074: public long getPeriod() {
075: return fieldPeriod;
076: }
077:
078: public void setPeriod(long inPeriod) {
079: fieldPeriod = inPeriod;
080: }
081:
082: public void setPeriod(String inPeriod) {
083: fieldPeriod = parse(inPeriod);
084: fieldFormattedPeriod = inPeriod;
085: }
086:
087: public String getFormattedPeriod() {
088: return fieldFormattedPeriod;
089: }
090:
091: public boolean isStarted() {
092: return fieldStarted;
093: }
094:
095: public void setStarted(boolean inStarted) {
096: fieldStarted = inStarted;
097: }
098:
099: /**
100: * @param inPeriodString
101: * @return
102: */
103: private long parse(String inPeriodString) {
104: if (inPeriodString == null) {
105: return 0;
106: }
107:
108: inPeriodString = inPeriodString.trim().toLowerCase();
109:
110: if (inPeriodString.endsWith("d")) {
111: long days = Long.parseLong(inPeriodString.substring(0,
112: inPeriodString.length() - 1));
113: long period = days * 24 * 60L * 60L * 1000L;
114: return period;
115: } else if (inPeriodString.endsWith("h")) {
116: long hours = Long.parseLong(inPeriodString.substring(0,
117: inPeriodString.length() - 1));
118: long period = hours * 60L * 60L * 1000L;
119: return period;
120: } else if (inPeriodString.endsWith("m")) {
121: long min = Long.parseLong(inPeriodString.substring(0,
122: inPeriodString.length() - 1));
123: long period = min * 60L * 1000L;
124: return period;
125: } else if (inPeriodString.endsWith("s")) {
126: long sec = Long.parseLong(inPeriodString.substring(0,
127: inPeriodString.length() - 1));
128: long period = sec * 1000L;
129: return period;
130: } else {
131: long period = Long.parseLong(inPeriodString);
132: return period;
133: }
134: }
135:
136: public TimerTask getTimerTask() {
137: return fieldTimerTask;
138: }
139:
140: public void setTimerTask(TimerTask inTimerTask) {
141: fieldTimerTask = inTimerTask;
142: }
143:
144: public void stop() {
145: if (getTimerTask() != null) {
146: getTimerTask().cancel();
147: }
148: }
149:
150: public String getUserName() {
151: return fieldUserName;
152: }
153:
154: public void setUserName(String inUserName) {
155: fieldUserName = inUserName;
156: }
157:
158: public Configuration getConfig() {
159: return fieldConfig;
160: }
161:
162: public void setConfig(Configuration inConfig) {
163: fieldConfig = inConfig;
164: }
165:
166: }
|