1 6 7 package SOFA.SOFAnet.Repository; 8 9 import java.io.*; 10 import java.util.*; 11 import java.text.*; 12 import SOFA.Util.XML; 13 import org.w3c.dom.*; 14 import org.xml.sax.SAXException ; 15 16 34 public class OutputTrigger extends XMLStorageItem implements StorageItem, Serializable, Cloneable 35 { 36 private String description; 37 private boolean autoDelete; 38 private BundleNameFilter bundleFilter; private NodeNameFilter nodeFilter; private Date validFrom; private Date validTo; private boolean isContract; 43 private String contractID; 44 private String contractRule; 45 46 47 48 49 public OutputTrigger(String name, File file) 50 { 51 super(name, file, "output_trigger"); 52 reset(); 53 } 54 55 public OutputTrigger() 56 { 57 super("", null, "output_trigger"); 58 reset(); 59 } 60 61 public Object clone() 62 { 63 OutputTrigger clone = null; 64 try 65 { 66 clone = (OutputTrigger)super.clone(); 67 } 68 catch (CloneNotSupportedException e) 69 { 70 throw new InternalError (); 71 } 72 if (bundleFilter != null) clone.bundleFilter = (BundleNameFilter)bundleFilter.clone(); 73 if (nodeFilter != null) clone.nodeFilter = (NodeNameFilter)nodeFilter.clone(); 74 return clone; 75 } 76 77 78 public boolean toBeDeleted() 79 { 80 return validTo != null && autoDelete && validTo.before(Calendar.getInstance().getTime()); 81 } 82 83 86 protected void reset() 87 { 88 bundleFilter = null; 89 nodeFilter = null; 90 description = ""; 91 validFrom = null; 92 validTo = null; 93 autoDelete = false; 94 isContract = false; 95 contractID = ""; 96 contractRule = ""; 97 } 98 99 125 public void loadFromXML(Element element) throws SAXException 126 { 127 reset(); 128 NodeList nl = element.getChildNodes(); 129 for (int i = 0; i < nl.getLength(); i++) 130 { 131 Node node = nl.item(i); 132 if (node.getNodeType() == Node.ELEMENT_NODE) 133 { 134 Element el = (Element)node; 135 String nodeName = node.getNodeName(); 136 137 if (nodeName.compareTo("description") == 0) 138 { 139 description = XML.getTextContent(el); 140 } 141 else 142 if (nodeName.compareTo("bundle_filter") == 0) 143 { 144 bundleFilter = new BundleNameFilter(); 145 bundleFilter.loadFromXML(el); 146 } 147 else 148 if (nodeName.compareTo("node_filter") == 0) 149 { 150 nodeFilter = new NodeNameFilter(); 151 nodeFilter.loadFromXML(el); 152 } 153 else 154 if (nodeName.compareTo("validity") == 0) 155 { 156 String from = el.getAttribute("from"); 157 String to = el.getAttribute("to"); 158 String ad = el.getAttribute("autodelete"); 159 DateFormat dateFormat = DateFormat.getInstance(); 160 if (from.length() > 0) 161 { 162 try 163 { 164 validFrom = dateFormat.parse(from); 165 } 166 catch (ParseException e) 167 { 168 throw new SAXException ("Cannot parse date/time format of 'from' attribute of 'validity' tag", e); 169 } 170 } 171 172 if (to.length() > 0) 173 { 174 try 175 { 176 validTo = dateFormat.parse(to); 177 } 178 catch (ParseException e) 179 { 180 throw new SAXException ("Cannot parse date/time format of 'to' attribute of 'validity' tag", e); 181 } 182 } 183 184 if (ad.length() > 0) 185 { 186 ad = ad.trim().toLowerCase(); 187 if (ad.compareTo("1") == 0 || ad.compareTo("true") == 0) autoDelete = true; 188 } 189 } 190 else 191 if (nodeName.compareTo("contract") == 0) 192 { 193 isContract = true; 194 contractID = el.getAttribute("cid"); 195 contractRule = el.getAttribute("rule"); 196 } 197 } 198 } 199 } 200 201 204 public void saveToXML(Element element) 205 { 206 Document doc = element.getOwnerDocument(); 207 Element el; 208 209 XML.newTextElement(element, "description", description); 210 211 if (bundleFilter != null) 212 { 213 el = doc.createElement("bundle_filter"); 214 bundleFilter.saveToXML(el); 215 element.appendChild(el); 216 } 217 218 if (nodeFilter != null) 219 { 220 el = doc.createElement("node_filter"); 221 nodeFilter.saveToXML(el); 222 element.appendChild(el); 223 } 224 225 if (validFrom != null || validTo != null || autoDelete) 226 { 227 DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM); 228 el = doc.createElement("validity"); 229 if (validFrom != null) el.setAttribute("from", dateFormat.format(validFrom)); 230 if (validTo != null) el.setAttribute("to", dateFormat.format(validTo)); 231 if (autoDelete) el.setAttribute("autodelete", "true"); 232 element.appendChild(el); 233 } 234 235 if (isContract) 236 { 237 el = doc.createElement("contract"); 238 el.setAttribute("cid", contractID); 239 if (contractRule.length() > 0) el.setAttribute("rule", contractRule); 240 element.appendChild(el); 241 } 242 } 243 244 public String getDescription() 245 { 246 return description; 247 } 248 249 public boolean getAutoDelete() 250 { 251 return autoDelete; 252 } 253 254 public BundleNameFilter getBundleFilter() 255 { 256 return bundleFilter; 257 } 258 259 public NodeNameFilter getNodeFilter() 260 { 261 return nodeFilter; 262 } 263 264 public Date getValidFrom() { 266 return validFrom; 267 } 268 269 public Date getValidTo() { 271 return validTo; 272 } 273 274 public boolean isValid() 275 { 276 Date now = Calendar.getInstance().getTime(); 277 return (validFrom == null || !validFrom.after(now)) && (validTo == null || !validTo.before(now)); 278 } 279 280 public boolean isContract() 281 { 282 return isContract; 283 } 284 285 public String getContractID() 286 { 287 return contractID; 288 } 289 290 public String getContractRule() 291 { 292 return contractRule; 293 } 294 295 public void setDescription(String description) 296 { 297 this.description = description; 298 } 299 300 public void setAutoDelete(boolean autoDelete) 301 { 302 this.autoDelete = autoDelete; 303 } 304 305 public void setBundleFilter(BundleNameFilter bundleFilter) 306 { 307 this.bundleFilter = bundleFilter; 308 } 309 310 public void setNodeFilter(NodeNameFilter nodeFilter) 311 { 312 this.nodeFilter = nodeFilter; 313 } 314 315 public void setValidFrom(Date validFrom) { 317 this.validFrom = validFrom; 318 } 319 320 public void setValidTo(Date validTo) { 322 this.validTo = validTo; 323 } 324 325 public void setContract(String cid, String rule) 326 { 327 if (cid == null && rule == null) 328 { 329 contractID = ""; 330 contractRule = ""; 331 isContract = false; 332 } 333 else 334 { 335 contractID = cid; 336 contractRule = rule; 337 isContract = true; 338 } 339 } 340 341 } 342 | Popular Tags |