BrightSide Workbench Full Report + Source Code
elephant-scheduler/src/main/java/org/turro/scheduler/motor/DefaultTask.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.scheduler.motor;
19 
20 import java.time.Duration;
21 import java.util.Date;
22 import java.util.concurrent.ScheduledFuture;
23 import org.turro.string.ObjectString;
24 import org.jdom.Element;
25 import org.turro.elephant.context.IConstructor;
26 import org.turro.elephant.impl.abstracts.AbstractImplementation;
27 
32 @Deprecated
33 public abstract class DefaultTask extends AbstractImplementation implements ScheduledTask {
34 
35  private ScheduledFuture<?> future;
36  private String description, implementation, data;
37  private boolean active;
38  private Date startDate, endDate, lastExecuted;
39  private Constraints constraints = new Constraints();
40 
41  @Override
42  public boolean isSystem() {
43  return false;
44  }
45 
46  @Override
47  public boolean isDone() {
48  return future == null ? true : future.isDone();
49  }
50 
51  @Override
52  public boolean isActive() {
53  return active;
54  }
55 
56  @Override
57  public void setActive(boolean active) {
58  this.active = active;
59  }
60 
62  return constraints;
63  }
64 
65  @Override
67  return constructor;
68  }
69 
70  @Override
71  public String getData() {
72  return data;
73  }
74 
75  @Override
76  public void setData(String data) {
77  this.data = data;
78  }
79 
80  @Override
81  public String getDescription() {
82  return description;
83  }
84 
85  @Override
86  public void setDescription(String value) {
87  description = value;
88  }
89 
90  @Override
91  public Date getEndDate() {
92  return endDate;
93  }
94 
95  @Override
96  public void setEndDate(Date endDate) {
97  this.endDate = endDate;
98  }
99 
100  @Override
101  public String getImplementation() {
102  return implementation;
103  }
104 
105  @Override
106  public void setImplementation(String value) {
107  implementation = value;
108  }
109 
110  @Override
111  public Date getStartDate() {
112  return startDate;
113  }
114 
115  @Override
116  public void setStartDate(Date startDate) {
117  this.startDate = startDate;
118  }
119 
120  public void setLastExecuted(Date lastExecuted) {
121  this.lastExecuted = lastExecuted;
122  }
123 
124  @Override
125  public boolean shouldRun(Date now) {
126  if(enoughTimeLapse(now) &&
127  (startDate == null || now.after(startDate)) &&
128  (endDate == null || now.before(endDate)) &&
129  constraints.isValid(now)) {
130  return constraints.needsRefresh();
131  }
132  return false;
133  }
134 
135  @Override
136  public void doRun(Motor motor) {
137  lastExecuted = new Date();
138  //future = motor.runTask(this);
139  }
140 
141  @Override
142  public void stop() {
143  if(future != null && !future.isDone()) {
144  future.cancel(true);
145  }
146  }
147 
148  @Override
149  public void readXML(Element root) {
150  setImplementation(root.getAttributeValue("implementation"));
151  String s = root.getAttributeValue("active");
152  active = "true".equals(s);
153  if(root.getAttributeValue("description") != null)
154  setDescription(root.getAttributeValue("description"));
155  if(root.getAttributeValue("start-date") != null)
156  setStartDate((Date) ObjectString.parseString(root.getAttributeValue("start-date"), ObjectString.COMPRESSED_DATE_PATTERN, Date.class, false));
157  if(root.getAttributeValue("end-date") != null)
158  setEndDate((Date) ObjectString.parseString(root.getAttributeValue("end-date"), ObjectString.COMPRESSED_DATE_PATTERN, Date.class, false));
159  if(root.getAttributeValue("data") != null)
160  setData(root.getAttributeValue("data"));
161  constraints.readXML(root);
162  }
163 
164  @Override
165  public void writeXML(Element root) {
166  Element el = new Element("scheduled-task");
167  el.setAttribute("name", getName());
168  el.setAttribute("implementation", getImplementation());
169  if(getDescription() != null) el.setAttribute("description", getDescription());
170  el.setAttribute("active", active ? "true" : "false");
171  if(getStartDate() != null) el.setAttribute("start-date", ObjectString.formatObject(getStartDate(), ObjectString.COMPRESSED_DATE_PATTERN, false));
172  if(getEndDate() != null) el.setAttribute("end-date", ObjectString.formatObject(getEndDate(), ObjectString.COMPRESSED_DATE_PATTERN, false));
173  if(getData() != null) el.setAttribute("data", getData());
174  root.addContent(el);
175  constraints.writeXML(el);
176  }
177 
178  @Override
179  public abstract String getName();
180  @Override
181  public abstract void run();
182  @Override
183  public abstract String getDataLabel();
184 
185  private boolean enoughTimeLapse(Date now) {
186  return lastExecuted == null || (Duration.between(lastExecuted.toInstant(), now.toInstant()).getSeconds() > (60 * 5));
187  }
188 
189 }