KickJava   Java API By Example, From Geeks To Geeks.

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


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
9 import org.w3c.dom.Element JavaDoc;
10
11 import java.io.PrintWriter JavaDoc;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.List JavaDoc;
15
16
17 /**
18  * @author <a HREF="mailto:plightbo@hotmail.com">Pat Lightbody</a>
19  * @version $Revision: 1.6 $
20  */

21 public class ConditionalResultDescriptor extends ResultDescriptor {
22     //~ Instance fields ////////////////////////////////////////////////////////
23

24     protected List JavaDoc conditions = new ArrayList JavaDoc();
25
26     //~ Constructors ///////////////////////////////////////////////////////////
27

28     public ConditionalResultDescriptor() {
29     }
30
31     public ConditionalResultDescriptor(Element JavaDoc conditionalResult) {
32         init(conditionalResult);
33     }
34
35     //~ Methods ////////////////////////////////////////////////////////////////
36

37     public List JavaDoc getConditions() {
38         return conditions;
39     }
40
41     public void validate() throws InvalidWorkflowDescriptorException {
42         super.validate();
43
44         if (conditions.size() == 0) {
45             throw new InvalidWorkflowDescriptorException("Conditional result from " + ((ActionDescriptor) getParent()).getName() + " to " + getDestination() + " must have at least one condition");
46         }
47
48         ValidationHelper.validate(conditions);
49     }
50
51     public void writeXML(PrintWriter JavaDoc out, int indent) {
52         XMLUtil.printIndent(out, indent++);
53
54         StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
55         buf.append("<result");
56
57         if (hasId()) {
58             buf.append(" id=\"").append(getId()).append("\"");
59         }
60
61         buf.append(" old-status=\"").append(oldStatus).append("\"");
62
63         if (join != 0) {
64             buf.append(" join=\"").append(join).append("\"");
65         } else if (split != 0) {
66             buf.append(" split=\"").append(split).append("\"");
67         } else {
68             buf.append(" status=\"").append(status).append("\"");
69             buf.append(" step=\"").append(step).append("\"");
70
71             if ((owner != null) && (owner.length() > 0)) {
72                 buf.append(" owner=\"").append(owner).append("\"");
73             }
74         }
75
76         buf.append(">");
77         out.println(buf);
78
79         for (int i = 0; i < conditions.size(); i++) {
80             ConditionsDescriptor condition = (ConditionsDescriptor) conditions.get(i);
81             condition.writeXML(out, indent);
82         }
83
84         if (validators.size() > 0) {
85             XMLUtil.printIndent(out, indent++);
86             out.println("<validators>");
87
88             for (int i = 0; i < validators.size(); i++) {
89                 ValidatorDescriptor validator = (ValidatorDescriptor) validators.get(i);
90                 validator.writeXML(out, indent);
91             }
92
93             XMLUtil.printIndent(out, --indent);
94             out.println("</validators>");
95         }
96
97         printPreFunctions(out, indent);
98         printPostFunctions(out, indent);
99         XMLUtil.printIndent(out, --indent);
100         out.println("</result>");
101     }
102
103     protected void init(Element JavaDoc conditionalResult) {
104         super.init(conditionalResult);
105
106         List JavaDoc conditionNodes = XMLUtil.getChildElements(conditionalResult, "conditions");
107
108         int length = conditionNodes.size();
109
110         for (int i = 0; i < length; i++) {
111             Element JavaDoc condition = (Element JavaDoc) conditionNodes.get(i);
112             ConditionsDescriptor conditionDescriptor = new ConditionsDescriptor(condition);
113             conditionDescriptor.setParent(this);
114             this.conditions.add(conditionDescriptor);
115         }
116     }
117
118     private String JavaDoc getDestination() {
119         if (join != 0) {
120             return "join #" + join;
121         } else if (split != 0) {
122             return "split #" + split;
123         } else {
124             return "step #" + step;
125         }
126     }
127 }
128
Popular Tags