KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > SOFA > SOFAnet > Repository > OutputTrigger


1 /*
2  * OutputTrigger.java
3  *
4  * Created on 27. listopad 2003, 22:35
5  */

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 JavaDoc;
15
16 /**
17  * Representation of the output trigger.
18  * One item of persistent storage.
19  * <p>
20  * Output triggers are actually not "triggers", they are more filters. They specifies
21  * which nodes can download wich bundles from the node. The pull request must pass at least one Output Trigger.
22  * <p>
23  * The output trigger contains:
24  * <ul>
25  * <li>text description of the trigger
26  * <li>filter that specifies the bundles on which the trigger should be used
27  * <li>filter of nodes that must be passed to use the trigger
28  * <li>time validity
29  * <li>(optional) ID and Rule of contract that is connected with the input trigger
30  * </ul>
31  *
32  * @author Ladislav Sobr
33  */

34 public class OutputTrigger extends XMLStorageItem implements StorageItem, Serializable, Cloneable JavaDoc
35 {
36   private String JavaDoc description;
37   private boolean autoDelete;
38   private BundleNameFilter bundleFilter; //can be null
39
private NodeNameFilter nodeFilter; //can be null
40
private Date validFrom; //can be null
41
private Date validTo; //can be null
42
private boolean isContract;
43   private String JavaDoc contractID;
44   private String JavaDoc contractRule;
45   
46   
47   
48   /** Creates a new instance of OutputTrigger */
49   public OutputTrigger(String JavaDoc 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 JavaDoc clone()
62   {
63     OutputTrigger clone = null;
64     try
65     {
66       clone = (OutputTrigger)super.clone();
67     }
68     catch (CloneNotSupportedException JavaDoc e)
69     {
70       throw new InternalError JavaDoc();
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   /**
84    * Resets content of the item.
85    */

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   /**
100    * Loads the item from the XML DOM tree.
101    * <p>
102    * XML format of storage item:
103    * <p>
104    * <pre>
105    * &lt;output_trigger&gt;
106    * OutputTrigger XML format
107    * &lt;/output_trigger&gt;
108    * </pre>
109    * <p>
110    * XML format:
111    * <p>
112    * <pre>
113    * &lt;description&gt;string&lt;/description&gt;
114    * ?&lt;bundle_filter&gt;
115    * BundleNameFilter XML format
116    * &lt;/bundle_filter&gt;
117    * ?&lt;node_filter&gt;
118    * NodeNameFilter XML format
119    * &lt;/node_filter&gt;
120    * ?&lt;validity ?from="date" ?to="date" ?autodelete="boolean"/&gt;
121    * ?&lt;contract cid="string" rule="string"/&gt;
122    * </pre>
123    *
124    */

125   public void loadFromXML(Element element) throws SAXException JavaDoc
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 JavaDoc 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 JavaDoc from = el.getAttribute("from");
157           String JavaDoc to = el.getAttribute("to");
158           String JavaDoc 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 JavaDoc("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 JavaDoc("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   /**
202    * Saves the item to XML DOM tree.
203    */

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 JavaDoc 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() //can be null
265
{
266     return validFrom;
267   }
268   
269   public Date getValidTo() //can be null
270
{
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 JavaDoc getContractID()
286   {
287     return contractID;
288   }
289   
290   public String JavaDoc getContractRule()
291   {
292     return contractRule;
293   }
294
295   public void setDescription(String JavaDoc 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) //can be null
316
{
317     this.validFrom = validFrom;
318   }
319   
320   public void setValidTo(Date validTo) //can be null
321
{
322     this.validTo = validTo;
323   }
324   
325   public void setContract(String JavaDoc cid, String JavaDoc 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