KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensymphony > workflow > loader > ResultDescriptor


1 /*
2  * Copyright (c) 2002-2003 by OpenSymphony
3  * All rights reserved.
4  */

5 package com.opensymphony.workflow.loader;
6
7 import com.opensymphony.workflow.InvalidWorkflowDescriptorException;
8 import com.opensymphony.workflow.util.Validatable;
9
10 import org.w3c.dom.Element JavaDoc;
11
12 import java.io.PrintWriter JavaDoc;
13
14 import java.util.ArrayList JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.List JavaDoc;
17
18
19 /**
20  * @author <a HREF="mailto:plightbo@hotmail.com">Pat Lightbody</a>
21  * @version $Revision: 1.7 $
22  */

23 public class ResultDescriptor extends AbstractDescriptor implements Validatable {
24     //~ Instance fields ////////////////////////////////////////////////////////
25

26     protected List JavaDoc postFunctions = new ArrayList JavaDoc();
27     protected List JavaDoc preFunctions = new ArrayList JavaDoc();
28     protected List JavaDoc validators = new ArrayList JavaDoc();
29     protected String JavaDoc dueDate;
30     protected String JavaDoc oldStatus;
31     protected String JavaDoc owner;
32     protected String JavaDoc status;
33     protected boolean hasStep = false;
34     protected int join;
35     protected int split;
36     protected int step = 0;
37
38     //~ Constructors ///////////////////////////////////////////////////////////
39

40     public ResultDescriptor() {
41     }
42
43     public ResultDescriptor(Element JavaDoc result) {
44         init(result);
45     }
46
47     //~ Methods ////////////////////////////////////////////////////////////////
48

49     public String JavaDoc getDueDate() {
50         return dueDate;
51     }
52
53     public void setJoin(int join) {
54         this.join = join;
55     }
56
57     public int getJoin() {
58         return join;
59     }
60
61     public void setOldStatus(String JavaDoc oldStatus) {
62         this.oldStatus = oldStatus;
63     }
64
65     public String JavaDoc getOldStatus() {
66         return oldStatus;
67     }
68
69     public void setOwner(String JavaDoc owner) {
70         this.owner = owner;
71     }
72
73     public String JavaDoc getOwner() {
74         return owner;
75     }
76
77     public List JavaDoc getPostFunctions() {
78         return postFunctions;
79     }
80
81     public List JavaDoc getPreFunctions() {
82         return preFunctions;
83     }
84
85     public void setSplit(int split) {
86         this.split = split;
87     }
88
89     public int getSplit() {
90         return split;
91     }
92
93     public void setStatus(String JavaDoc status) {
94         this.status = status;
95     }
96
97     public String JavaDoc getStatus() {
98         return status;
99     }
100
101     public void setStep(int step) {
102         this.step = step;
103         hasStep = true;
104     }
105
106     public int getStep() {
107         return step;
108     }
109
110     public List JavaDoc getValidators() {
111         return validators;
112     }
113
114     public void validate() throws InvalidWorkflowDescriptorException {
115         ValidationHelper.validate(preFunctions);
116         ValidationHelper.validate(postFunctions);
117         ValidationHelper.validate(validators);
118
119         //if it's not a split or a join, then we require a next step
120
if ((split == 0) && (join == 0)) {
121             StringBuffer JavaDoc error = new StringBuffer JavaDoc("Result ");
122
123             if (getId() > 0) {
124                 error.append("#").append(getId());
125             }
126
127             error.append(" is not a split or join, and has no ");
128
129             if (!hasStep) {
130                 throw new InvalidWorkflowDescriptorException(error.append("next step").toString());
131             }
132
133             if ((status == null) || (status.length() == 0)) {
134                 throw new InvalidWorkflowDescriptorException(error.append("status").toString());
135             }
136         }
137
138         //taken out for now
139
//if ((split != 0) && ((join != 0) || (oldStatus.length() > 0) || (step != 0) || (status.length() > 0) || (owner.length() != 0))) {
140
// throw new InvalidWorkflowDescriptorException("Result " + id + " has a split attribute, so should not any other attributes");
141
//} else if ((join != 0) && ((split != 0) || (oldStatus.length() > 0) || (step != 0) || (status.length() > 0) || (owner.length() != 0))) {
142
// throw new InvalidWorkflowDescriptorException("Result has a join attribute, should thus not any other attributes");
143
//} else if ((oldStatus.length() == 0) || (step == 0) || (status.length() == 0)) {
144
// throw new InvalidWorkflowDescriptorException("old-status, step, status and owner attributes are required if no split or join");
145
//}
146
}
147
148     public void writeXML(PrintWriter JavaDoc out, int indent) {
149         XMLUtil.printIndent(out, indent++);
150
151         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
152         buf.append("<unconditional-result");
153
154         if (hasId()) {
155             buf.append(" id=\"").append(getId()).append("\"");
156         }
157
158         buf.append(" old-status=\"").append(oldStatus).append("\"");
159
160         if (join != 0) {
161             buf.append(" join=\"").append(join).append("\"");
162         } else if (split != 0) {
163             buf.append(" split=\"").append(split).append("\"");
164         } else {
165             buf.append(" status=\"").append(status).append("\"");
166             buf.append(" step=\"").append(step).append("\"");
167
168             if ((owner != null) && (owner.length() > 0)) {
169                 buf.append(" owner=\"").append(owner).append("\"");
170             }
171         }
172
173         if ((preFunctions.size() == 0) && (postFunctions.size() == 0)) {
174             buf.append("/>");
175             out.println(buf.toString());
176         } else {
177             buf.append(">");
178             out.println(buf.toString());
179             printPreFunctions(out, indent);
180             printPostFunctions(out, indent);
181             XMLUtil.printIndent(out, --indent);
182             out.println("</unconditional-result>");
183         }
184     }
185
186     protected void init(Element JavaDoc result) {
187         oldStatus = result.getAttribute("old-status");
188         status = result.getAttribute("status");
189
190         try {
191             setId(Integer.parseInt(result.getAttribute("id")));
192         } catch (NumberFormatException JavaDoc e) {
193         }
194
195         dueDate = result.getAttribute("due-date");
196
197         try {
198             split = Integer.parseInt(result.getAttribute("split"));
199         } catch (Exception JavaDoc ex) {
200         }
201
202         try {
203             join = Integer.parseInt(result.getAttribute("join"));
204         } catch (Exception JavaDoc ex) {
205         }
206
207         try {
208             step = Integer.parseInt(result.getAttribute("step"));
209             hasStep = true;
210         } catch (Exception JavaDoc ex) {
211         }
212
213         owner = result.getAttribute("owner");
214
215         // set up validators -- OPTIONAL
216
Element JavaDoc v = XMLUtil.getChildElement(result, "validators");
217
218         if (v != null) {
219             List JavaDoc validators = XMLUtil.getChildElements(v, "validator");
220
221             for (int k = 0; k < validators.size(); k++) {
222                 Element JavaDoc validator = (Element JavaDoc) validators.get(k);
223                 ValidatorDescriptor validatorDescriptor = new ValidatorDescriptor(validator);
224                 validatorDescriptor.setParent(this);
225                 this.validators.add(validatorDescriptor);
226             }
227         }
228
229         // set up pre-functions -- OPTIONAL
230
Element JavaDoc pre = XMLUtil.getChildElement(result, "pre-functions");
231
232         if (pre != null) {
233             List JavaDoc preFunctions = XMLUtil.getChildElements(pre, "function");
234
235             for (int k = 0; k < preFunctions.size(); k++) {
236                 Element JavaDoc preFunction = (Element JavaDoc) preFunctions.get(k);
237                 FunctionDescriptor functionDescriptor = new FunctionDescriptor(preFunction);
238                 functionDescriptor.setParent(this);
239                 this.preFunctions.add(functionDescriptor);
240             }
241         }
242
243         // set up post-functions - OPTIONAL
244
Element JavaDoc post = XMLUtil.getChildElement(result, "post-functions");
245
246         if (post != null) {
247             List JavaDoc postFunctions = XMLUtil.getChildElements(post, "function");
248
249             for (int k = 0; k < postFunctions.size(); k++) {
250                 Element JavaDoc postFunction = (Element JavaDoc) postFunctions.get(k);
251                 FunctionDescriptor functionDescriptor = new FunctionDescriptor(postFunction);
252                 functionDescriptor.setParent(this);
253                 this.postFunctions.add(functionDescriptor);
254             }
255         }
256     }
257
258     protected void printPostFunctions(PrintWriter JavaDoc out, int indent) {
259         if (postFunctions.size() > 0) {
260             XMLUtil.printIndent(out, indent++);
261             out.println("<post-functions>");
262
263             Iterator JavaDoc iter = postFunctions.iterator();
264
265             while (iter.hasNext()) {
266                 FunctionDescriptor function = (FunctionDescriptor) iter.next();
267                 function.writeXML(out, indent);
268             }
269
270             XMLUtil.printIndent(out, --indent);
271             out.println("</post-functions>");
272         }
273     }
274
275     protected void printPreFunctions(PrintWriter JavaDoc out, int indent) {
276         if (preFunctions.size() > 0) {
277             XMLUtil.printIndent(out, indent++);
278             out.println("<pre-functions>");
279
280             Iterator JavaDoc iter = preFunctions.iterator();
281
282             while (iter.hasNext()) {
283                 FunctionDescriptor function = (FunctionDescriptor) iter.next();
284                 function.writeXML(out, indent);
285             }
286
287             XMLUtil.printIndent(out, --indent);
288             out.println("</pre-functions>");
289         }
290     }
291 }
292
Popular Tags