BrightSide Workbench Full Report + Source Code
DefaultEntity.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.entities;
19 
20 import java.util.Date;
21 import org.turro.string.ObjectString;
22 import org.jdom.Element;
23 import org.turro.elephant.context.IConstructor;
24 import org.turro.entities.Entities;
25 import org.turro.plugin.scheduler.ScheduledAction;
26 import org.turro.scheduler.motor.Constraints;
27 import org.turro.util.CompareUtil;
28 
33 public class DefaultEntity implements ScheduledEntity, Comparable<ScheduledEntity> {
34 
35  private String description, data, entityPath, notification;
36  private boolean active;
37  private Date startDate, endDate;
38  private Constraints constraints = new Constraints();
39 
40  public DefaultEntity(String entityPath) {
41  this.entityPath = entityPath;
42  }
43 
44  @Override
45  public boolean isActive() {
46  return active;
47  }
48 
49  @Override
50  public void setActive(boolean active) {
51  this.active = active;
52  }
53 
55  return constraints;
56  }
57 
58  @Override
59  public String getData() {
60  return data;
61  }
62 
63  @Override
64  public void setData(String data) {
65  this.data = data;
66  }
67 
68  @Override
69  public String getDescription() {
70  return description;
71  }
72 
73  @Override
74  public void setDescription(String value) {
75  description = value;
76  }
77 
78  @Override
79  public Date getEndDate() {
80  return endDate;
81  }
82 
83  @Override
84  public void setEndDate(Date endDate) {
85  this.endDate = endDate;
86  }
87 
88  @Override
89  public String getEntityPath() {
90  return entityPath;
91  }
92 
93  public void setEntityPath(String value) {
94  entityPath = value;
95  }
96 
97  @Override
98  public Date getStartDate() {
99  return startDate;
100  }
101 
102  @Override
103  public void setStartDate(Date startDate) {
104  this.startDate = startDate;
105  }
106 
107  @Override
108  public String getNotify() {
109  return notification;
110  }
111 
112  @Override
113  public void setNotify(String value) {
114  this.notification = value;
115  }
116 
117  @Override
118  public boolean shouldRun(Date now) {
119  if(active && (startDate == null || now.after(startDate)) &&
120  (endDate == null || now.before(endDate)) &&
121  constraints.isValid(now)) {
122  return constraints.needsRefresh();
123  }
124  return false;
125  }
126 
127  @Override
128  public Object doRun(IConstructor constructor, Date now) {
129  Object entity = Entities.getController(entityPath).getEntity();
130  //Object entity = CommandFactory.getEntity(DummyApplication.getInstance(constructor), entityPath);
131  if(entity instanceof ScheduledAction) {
132  return ((ScheduledAction) entity).doAction(now, data);
133  }
134  return null;
135  }
136 
137  @Override
138  public void readXML(Element root) {
139  String s = root.getAttributeValue("active");
140  active = "true".equals(s);
141  entityPath = root.getAttributeValue("path");
142  if(root.getAttributeValue("description") != null)
143  setDescription(root.getAttributeValue("description"));
144  if(root.getAttributeValue("notification") != null)
145  setDescription(root.getAttributeValue("notification"));
146  if(root.getAttributeValue("start-date") != null)
147  setStartDate((Date) ObjectString.parseString(root.getAttributeValue("start-date"), ObjectString.COMPRESSED_DATE_PATTERN, Date.class, false));
148  if(root.getAttributeValue("end-date") != null)
149  setEndDate((Date) ObjectString.parseString(root.getAttributeValue("end-date"), ObjectString.COMPRESSED_DATE_PATTERN, Date.class, false));
150  if(root.getAttributeValue("data") != null)
151  setData(root.getAttributeValue("data"));
152  constraints.readXML(root);
153  }
154 
155  @Override
156  public void writeXML(Element root) {
157  Element el = new Element("scheduled-entity");
158  el.setAttribute("path", getEntityPath());
159  if(getDescription() != null) el.setAttribute("description", getDescription());
160  if(getNotify() != null) el.setAttribute("notification", getNotify());
161  el.setAttribute("active", active ? "true" : "false");
162  if(getStartDate() != null) el.setAttribute("start-date", ObjectString.formatObject(getStartDate(), ObjectString.COMPRESSED_DATE_PATTERN, false));
163  if(getEndDate() != null) el.setAttribute("end-date", ObjectString.formatObject(getEndDate(), ObjectString.COMPRESSED_DATE_PATTERN, false));
164  if(getData() != null) el.setAttribute("data", getData());
165  root.addContent(el);
166  constraints.writeXML(el);
167  }
168 
169  @Override
170  public int compareTo(ScheduledEntity o) {
171  return CompareUtil.compare(entityPath, o.getEntityPath());
172  }
173 
174  @Override
175  public boolean equals(Object obj) {
176  if (obj == null) {
177  return false;
178  }
179  if (getClass() != obj.getClass()) {
180  return false;
181  }
182  final DefaultEntity other = (DefaultEntity) obj;
183  if ((this.entityPath == null) ? (other.entityPath != null) : !this.entityPath.equals(other.entityPath)) {
184  return false;
185  }
186  return true;
187  }
188 
189  @Override
190  public int hashCode() {
191  int hash = 3;
192  hash = 53 * hash + (this.entityPath != null ? this.entityPath.hashCode() : 0);
193  return hash;
194  }
195 
196 }
static IElephantEntity getController(String path)
Definition: Entities.java:78
Object doRun(IConstructor constructor, Date now)