KickJava   Java API By Example, From Geeks To Geeks.

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


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 import org.w3c.dom.Node JavaDoc;
12 import org.w3c.dom.NodeList JavaDoc;
13
14 import java.io.PrintWriter JavaDoc;
15
16 import java.util.*;
17
18
19 /**
20  * @author <a HREF="mailto:plightbo@hotmail.com">Pat Lightbody</a>
21  * @version $Revision: 1.17 $
22  */

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

26     protected List JavaDoc actions = new ArrayList();
27
28     /**
29      * this list maintained internally to allow for proper xml serialization.
30      * All common-action elements in the XML file are expanded into ActionDescriptors
31      * and are available via getActions()
32      */

33     protected List JavaDoc commonActions = new ArrayList();
34     protected List JavaDoc permissions = new ArrayList();
35     protected List JavaDoc postFunctions = new ArrayList();
36     protected List JavaDoc preFunctions = new ArrayList();
37     protected Map metaAttributes = new HashMap();
38     protected String JavaDoc name;
39     protected boolean hasActions = false;
40
41     //~ Constructors ///////////////////////////////////////////////////////////
42

43     public StepDescriptor() {
44     }
45
46     public StepDescriptor(Element JavaDoc step) {
47         init(step);
48     }
49
50     /** sets parent */
51     public StepDescriptor(Element JavaDoc step, AbstractDescriptor parent) {
52         setParent(parent);
53         init(step);
54     }
55
56     //~ Methods ////////////////////////////////////////////////////////////////
57

58     public ActionDescriptor getAction(int id) {
59         for (Iterator iterator = actions.iterator(); iterator.hasNext();) {
60             ActionDescriptor action = (ActionDescriptor) iterator.next();
61
62             if (action.getId() == id) {
63                 return action;
64             }
65         }
66
67         return null;
68     }
69
70     /**
71      * Get a List of {@link ActionDescriptor}s for this step
72      */

73     public List JavaDoc getActions() {
74         return actions;
75     }
76
77     public void setMetaAttributes(Map metaAttributes) {
78         this.metaAttributes = metaAttributes;
79     }
80
81     public Map getMetaAttributes() {
82         return metaAttributes;
83     }
84
85     public void setName(String JavaDoc name) {
86         this.name = name;
87     }
88
89     public String JavaDoc getName() {
90         return name;
91     }
92
93     /**
94      * Get a List of {@link PermissionDescriptor}s for this step
95      */

96     public List JavaDoc getPermissions() {
97         return permissions;
98     }
99
100     public void setPostFunctions(List JavaDoc postFunctions) {
101         this.postFunctions = postFunctions;
102     }
103
104     public List JavaDoc getPostFunctions() {
105         return postFunctions;
106     }
107
108     public void setPreFunctions(List JavaDoc preFunctions) {
109         this.preFunctions = preFunctions;
110     }
111
112     public List JavaDoc getPreFunctions() {
113         return preFunctions;
114     }
115
116     public boolean resultsInJoin(int join) {
117         for (Iterator iterator = actions.iterator(); iterator.hasNext();) {
118             ActionDescriptor actionDescriptor = (ActionDescriptor) iterator.next();
119
120             if (actionDescriptor.getUnconditionalResult().getJoin() == join) {
121                 return true;
122             }
123
124             List JavaDoc results = actionDescriptor.getConditionalResults();
125
126             for (Iterator iterator2 = results.iterator(); iterator2.hasNext();) {
127                 ConditionalResultDescriptor resultDescriptor = (ConditionalResultDescriptor) iterator2.next();
128
129                 if (resultDescriptor.getJoin() == join) {
130                     return true;
131                 }
132             }
133         }
134
135         return false;
136     }
137
138     public void validate() throws InvalidWorkflowDescriptorException {
139         if ((commonActions.size() == 0) && (actions.size() == 0) && hasActions) {
140             throw new InvalidWorkflowDescriptorException("Step '" + name + "' actions element must contain at least one action or common-action");
141         }
142
143         if (getId() == -1) {
144             throw new InvalidWorkflowDescriptorException("Cannot use a step ID of -1 as it is a reserved value");
145         }
146
147         ValidationHelper.validate(actions);
148         ValidationHelper.validate(permissions);
149         ValidationHelper.validate(preFunctions);
150         ValidationHelper.validate(postFunctions);
151
152         Iterator iter = commonActions.iterator();
153
154         while (iter.hasNext()) {
155             Object JavaDoc o = iter.next();
156
157             try {
158                 Integer JavaDoc actionId = new Integer JavaDoc(o.toString());
159                 ActionDescriptor commonActionReference = (ActionDescriptor) ((WorkflowDescriptor) getParent()).getCommonActions().get(actionId);
160
161                 if (commonActionReference == null) {
162                     throw new InvalidWorkflowDescriptorException("Common action " + actionId + " specified in step " + getName() + " does not exist");
163                 }
164             } catch (NumberFormatException JavaDoc ex) {
165                 throw new InvalidWorkflowDescriptorException("Common action " + o + " is not a valid action ID");
166             }
167         }
168     }
169
170     public void writeXML(PrintWriter JavaDoc out, int indent) {
171         XMLUtil.printIndent(out, indent++);
172         out.print("<step id=\"" + getId() + "\"");
173
174         if ((name != null) && (name.length() > 0)) {
175             out.print(" name=\"" + name + "\"");
176         }
177
178         out.println(">");
179
180         Iterator iter = metaAttributes.entrySet().iterator();
181
182         while (iter.hasNext()) {
183             Map.Entry entry = (Map.Entry) iter.next();
184             XMLUtil.printIndent(out, indent);
185             out.print("<meta name=\"");
186             out.print(entry.getKey());
187             out.print("\">");
188             out.print(entry.getValue());
189             out.println("</meta>");
190         }
191
192         if (preFunctions.size() > 0) {
193             XMLUtil.printIndent(out, indent++);
194             out.println("<pre-functions>");
195
196             for (int i = 0; i < preFunctions.size(); i++) {
197                 FunctionDescriptor function = (FunctionDescriptor) preFunctions.get(i);
198                 function.writeXML(out, indent);
199             }
200
201             XMLUtil.printIndent(out, --indent);
202             out.println("</pre-functions>");
203         }
204
205         if (permissions.size() > 0) {
206             XMLUtil.printIndent(out, indent++);
207             out.println("<external-permissions>");
208
209             for (int i = 0; i < permissions.size(); i++) {
210                 PermissionDescriptor permission = (PermissionDescriptor) permissions.get(i);
211                 permission.writeXML(out, indent);
212             }
213
214             XMLUtil.printIndent(out, --indent);
215             out.println("</external-permissions>");
216         }
217
218         if ((actions.size() > 0) || (commonActions.size() > 0)) {
219             XMLUtil.printIndent(out, indent++);
220             out.println("<actions>");
221
222             // special serialization common-action elements
223
for (int i = 0; i < commonActions.size(); i++) {
224                 out.println("<common-action id=\"" + commonActions.get(i) + "\" />");
225             }
226
227             for (int i = 0; i < actions.size(); i++) {
228                 ActionDescriptor action = (ActionDescriptor) actions.get(i);
229                 action.writeXML(out, indent);
230             }
231
232             XMLUtil.printIndent(out, --indent);
233             out.println("</actions>");
234         }
235
236         if (postFunctions.size() > 0) {
237             XMLUtil.printIndent(out, indent++);
238             out.println("<post-functions>");
239
240             for (int i = 0; i < postFunctions.size(); i++) {
241                 FunctionDescriptor function = (FunctionDescriptor) postFunctions.get(i);
242                 function.writeXML(out, indent);
243             }
244
245             XMLUtil.printIndent(out, --indent);
246             out.println("</post-functions>");
247         }
248
249         XMLUtil.printIndent(out, --indent);
250         out.println("</step>");
251     }
252
253     protected void init(Element JavaDoc step) {
254         try {
255             setId(Integer.parseInt(step.getAttribute("id")));
256         } catch (Exception JavaDoc ex) {
257             throw new IllegalArgumentException JavaDoc("Invalid step id value " + step.getAttribute("id"));
258         }
259
260         name = step.getAttribute("name");
261
262         NodeList JavaDoc children = step.getChildNodes();
263
264         for (int i = 0; i < children.getLength(); i++) {
265             Node JavaDoc child = (Node JavaDoc) children.item(i);
266
267             if (child.getNodeName().equals("meta")) {
268                 Element JavaDoc meta = (Element JavaDoc) child;
269                 String JavaDoc value = XMLUtil.getText(meta);
270                 this.metaAttributes.put(meta.getAttribute("name"), value);
271             }
272         }
273
274         // set up pre-functions - OPTIONAL
275
Element JavaDoc pre = XMLUtil.getChildElement(step, "pre-functions");
276
277         if (pre != null) {
278             List JavaDoc preFunctions = XMLUtil.getChildElements(pre, "function");
279
280             for (int k = 0; k < preFunctions.size(); k++) {
281                 Element JavaDoc preFunction = (Element JavaDoc) preFunctions.get(k);
282                 FunctionDescriptor functionDescriptor = new FunctionDescriptor(preFunction);
283                 functionDescriptor.setParent(this);
284                 this.preFunctions.add(functionDescriptor);
285             }
286         }
287
288         // set up permissions - OPTIONAL
289
Element JavaDoc p = XMLUtil.getChildElement(step, "external-permissions");
290
291         if (p != null) {
292             List JavaDoc permissions = XMLUtil.getChildElements(p, "permission");
293
294             for (int i = 0; i < permissions.size(); i++) {
295                 Element JavaDoc permission = (Element JavaDoc) permissions.get(i);
296                 PermissionDescriptor permissionDescriptor = new PermissionDescriptor(permission);
297                 permissionDescriptor.setParent(this);
298                 this.permissions.add(permissionDescriptor);
299             }
300         }
301
302         // set up actions - OPTIONAL
303
Element JavaDoc a = XMLUtil.getChildElement(step, "actions");
304
305         if (a != null) {
306             hasActions = true;
307
308             List JavaDoc actions = XMLUtil.getChildElements(a, "action");
309
310             for (int i = 0; i < actions.size(); i++) {
311                 Element JavaDoc action = (Element JavaDoc) actions.get(i);
312                 ActionDescriptor actionDescriptor = new ActionDescriptor(action);
313                 actionDescriptor.setParent(this);
314                 this.actions.add(actionDescriptor);
315             }
316
317             // look for common-action elements
318
List JavaDoc commonActions = XMLUtil.getChildElements(a, "common-action");
319
320             for (int i = 0; i < commonActions.size(); i++) {
321                 Element JavaDoc commonAction = (Element JavaDoc) commonActions.get(i);
322
323                 WorkflowDescriptor workflowDescriptor = (WorkflowDescriptor) (getParent());
324
325                 try {
326                     Integer JavaDoc actionId = new Integer JavaDoc(commonAction.getAttribute("id"));
327
328                     ActionDescriptor commonActionReference = (ActionDescriptor) workflowDescriptor.getCommonActions().get(actionId);
329
330                     if (commonActionReference != null) {
331                         this.actions.add(commonActionReference);
332                     }
333
334                     this.commonActions.add(actionId);
335                 } catch (Exception JavaDoc ex) {
336                     //log.warn("Invalid common actionId:" + ex);
337
}
338             }
339         }
340
341         // set up post-functions - OPTIONAL
342
Element JavaDoc post = XMLUtil.getChildElement(step, "post-functions");
343
344         if (post != null) {
345             List JavaDoc postFunctions = XMLUtil.getChildElements(post, "function");
346
347             for (int k = 0; k < postFunctions.size(); k++) {
348                 Element JavaDoc postFunction = (Element JavaDoc) postFunctions.get(k);
349                 FunctionDescriptor functionDescriptor = new FunctionDescriptor(postFunction);
350                 functionDescriptor.setParent(this);
351                 this.postFunctions.add(functionDescriptor);
352             }
353         }
354     }
355 }
356
Popular Tags