001: /*
002: * Copyright (c) 2002-2003 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.workflow.loader;
006:
007: import com.opensymphony.workflow.InvalidWorkflowDescriptorException;
008: import com.opensymphony.workflow.util.Validatable;
009:
010: import org.w3c.dom.Element;
011:
012: import java.io.PrintWriter;
013:
014: import java.util.ArrayList;
015: import java.util.Iterator;
016: import java.util.List;
017:
018: /**
019: * @author <a href="mailto:plightbo@hotmail.com">Pat Lightbody</a>
020: */
021: public class ResultDescriptor extends AbstractDescriptor implements
022: Validatable {
023: //~ Instance fields ////////////////////////////////////////////////////////
024:
025: protected List postFunctions = new ArrayList();
026: protected List preFunctions = new ArrayList();
027: protected List validators = new ArrayList();
028: protected String displayName;
029: protected String dueDate;
030: protected String oldStatus;
031: protected String owner;
032: protected String status;
033: protected boolean hasStep = false;
034: protected int join;
035: protected int split;
036: protected int step = 0;
037:
038: //~ Constructors ///////////////////////////////////////////////////////////
039:
040: /**
041: * @deprecated use {@link DescriptorFactory} instead
042: */
043: ResultDescriptor() {
044: }
045:
046: /**
047: * @deprecated use {@link DescriptorFactory} instead
048: */
049: ResultDescriptor(Element result) {
050: init(result);
051: }
052:
053: //~ Methods ////////////////////////////////////////////////////////////////
054:
055: public void setDisplayName(String displayName) {
056: if (getParent() instanceof ActionDescriptor) {
057: if (((ActionDescriptor) getParent()).getName().equals(
058: displayName)) {
059: this .displayName = null; // if displayName==parentAction.displayName, reset displayName
060:
061: return;
062: }
063: }
064:
065: this .displayName = displayName;
066: }
067:
068: public String getDisplayName() {
069: return displayName;
070: }
071:
072: public String getDueDate() {
073: return dueDate;
074: }
075:
076: public void setJoin(int join) {
077: this .join = join;
078: }
079:
080: public int getJoin() {
081: return join;
082: }
083:
084: public void setOldStatus(String oldStatus) {
085: this .oldStatus = oldStatus;
086: }
087:
088: public String getOldStatus() {
089: return oldStatus;
090: }
091:
092: public void setOwner(String owner) {
093: this .owner = owner;
094: }
095:
096: public String getOwner() {
097: return owner;
098: }
099:
100: public List getPostFunctions() {
101: return postFunctions;
102: }
103:
104: public List getPreFunctions() {
105: return preFunctions;
106: }
107:
108: public void setSplit(int split) {
109: this .split = split;
110: }
111:
112: public int getSplit() {
113: return split;
114: }
115:
116: public void setStatus(String status) {
117: this .status = status;
118: }
119:
120: public String getStatus() {
121: return status;
122: }
123:
124: public void setStep(int step) {
125: this .step = step;
126: hasStep = true;
127: }
128:
129: public int getStep() {
130: return step;
131: }
132:
133: public List getValidators() {
134: return validators;
135: }
136:
137: public void validate() throws InvalidWorkflowDescriptorException {
138: ValidationHelper.validate(preFunctions);
139: ValidationHelper.validate(postFunctions);
140: ValidationHelper.validate(validators);
141:
142: //if it's not a split or a join or a finish, then we require a next step
143: if ((split == 0)
144: && (join == 0)
145: && !(getParent() instanceof ActionDescriptor && ((ActionDescriptor) getParent())
146: .isFinish())) {
147: StringBuffer error = new StringBuffer("Result ");
148:
149: if (getId() > 0) {
150: error.append("#").append(getId());
151: }
152:
153: error.append(" is not a split or join, and has no ");
154:
155: if (!hasStep) {
156: throw new InvalidWorkflowDescriptorException(error
157: .append("next step").toString());
158: }
159:
160: if ((status == null) || (status.length() == 0)) {
161: throw new InvalidWorkflowDescriptorException(error
162: .append("status").toString());
163: }
164: }
165:
166: //taken out for now
167: //if ((split != 0) && ((join != 0) || (oldStatus.length() > 0) || (step != 0) || (status.length() > 0) || (owner.length() != 0))) {
168: // throw new InvalidWorkflowDescriptorException("Result " + id + " has a split attribute, so should not any other attributes");
169: //} else if ((join != 0) && ((split != 0) || (oldStatus.length() > 0) || (step != 0) || (status.length() > 0) || (owner.length() != 0))) {
170: // throw new InvalidWorkflowDescriptorException("Result has a join attribute, should thus not any other attributes");
171: //} else if ((oldStatus.length() == 0) || (step == 0) || (status.length() == 0)) {
172: // throw new InvalidWorkflowDescriptorException("old-status, step, status and owner attributes are required if no split or join");
173: //}
174: }
175:
176: public void writeXML(PrintWriter out, int indent) {
177: XMLUtil.printIndent(out, indent++);
178:
179: StringBuffer buf = new StringBuffer();
180: buf.append("<unconditional-result");
181:
182: if (hasId()) {
183: buf.append(" id=\"").append(getId()).append("\"");
184: }
185:
186: if ((dueDate != null) && (dueDate.length() > 0)) {
187: buf.append(" due-date=\"").append(getDueDate())
188: .append("\"");
189: }
190:
191: buf.append(" old-status=\"").append(oldStatus).append("\"");
192:
193: if (join != 0) {
194: buf.append(" join=\"").append(join).append("\"");
195: } else if (split != 0) {
196: buf.append(" split=\"").append(split).append("\"");
197: } else {
198: buf.append(" status=\"").append(status).append("\"");
199: buf.append(" step=\"").append(step).append("\"");
200:
201: if ((owner != null) && (owner.length() > 0)) {
202: buf.append(" owner=\"").append(owner).append("\"");
203: }
204:
205: if ((displayName != null) && (displayName.length() > 0)) {
206: buf.append(" display-name=\"").append(displayName)
207: .append("\"");
208: }
209: }
210:
211: if ((preFunctions.size() == 0) && (postFunctions.size() == 0)) {
212: buf.append("/>");
213: out.println(buf.toString());
214: } else {
215: buf.append(">");
216: out.println(buf.toString());
217: printPreFunctions(out, indent);
218: printPostFunctions(out, indent);
219: XMLUtil.printIndent(out, --indent);
220: out.println("</unconditional-result>");
221: }
222: }
223:
224: protected void init(Element result) {
225: oldStatus = result.getAttribute("old-status");
226: status = result.getAttribute("status");
227:
228: try {
229: setId(Integer.parseInt(result.getAttribute("id")));
230: } catch (NumberFormatException e) {
231: }
232:
233: dueDate = result.getAttribute("due-date");
234:
235: try {
236: split = Integer.parseInt(result.getAttribute("split"));
237: } catch (Exception ex) {
238: }
239:
240: try {
241: join = Integer.parseInt(result.getAttribute("join"));
242: } catch (Exception ex) {
243: }
244:
245: try {
246: step = Integer.parseInt(result.getAttribute("step"));
247: hasStep = true;
248: } catch (Exception ex) {
249: }
250:
251: owner = result.getAttribute("owner");
252: displayName = result.getAttribute("display-name");
253:
254: // set up validators -- OPTIONAL
255: Element v = XMLUtil.getChildElement(result, "validators");
256:
257: if (v != null) {
258: List validators = XMLUtil.getChildElements(v, "validator");
259:
260: for (int k = 0; k < validators.size(); k++) {
261: Element validator = (Element) validators.get(k);
262: ValidatorDescriptor validatorDescriptor = DescriptorFactory
263: .getFactory().createValidatorDescriptor(
264: validator);
265: validatorDescriptor.setParent(this );
266: this .validators.add(validatorDescriptor);
267: }
268: }
269:
270: // set up pre-functions -- OPTIONAL
271: Element pre = XMLUtil.getChildElement(result, "pre-functions");
272:
273: if (pre != null) {
274: List preFunctions = XMLUtil.getChildElements(pre,
275: "function");
276:
277: for (int k = 0; k < preFunctions.size(); k++) {
278: Element preFunction = (Element) preFunctions.get(k);
279: FunctionDescriptor functionDescriptor = DescriptorFactory
280: .getFactory().createFunctionDescriptor(
281: preFunction);
282: functionDescriptor.setParent(this );
283: this .preFunctions.add(functionDescriptor);
284: }
285: }
286:
287: // set up post-functions - OPTIONAL
288: Element post = XMLUtil
289: .getChildElement(result, "post-functions");
290:
291: if (post != null) {
292: List postFunctions = XMLUtil.getChildElements(post,
293: "function");
294:
295: for (int k = 0; k < postFunctions.size(); k++) {
296: Element postFunction = (Element) postFunctions.get(k);
297: FunctionDescriptor functionDescriptor = DescriptorFactory
298: .getFactory().createFunctionDescriptor(
299: postFunction);
300: functionDescriptor.setParent(this );
301: this .postFunctions.add(functionDescriptor);
302: }
303: }
304: }
305:
306: protected void printPostFunctions(PrintWriter out, int indent) {
307: if (postFunctions.size() > 0) {
308: XMLUtil.printIndent(out, indent++);
309: out.println("<post-functions>");
310:
311: Iterator iter = postFunctions.iterator();
312:
313: while (iter.hasNext()) {
314: FunctionDescriptor function = (FunctionDescriptor) iter
315: .next();
316: function.writeXML(out, indent);
317: }
318:
319: XMLUtil.printIndent(out, --indent);
320: out.println("</post-functions>");
321: }
322: }
323:
324: protected void printPreFunctions(PrintWriter out, int indent) {
325: if (preFunctions.size() > 0) {
326: XMLUtil.printIndent(out, indent++);
327: out.println("<pre-functions>");
328:
329: Iterator iter = preFunctions.iterator();
330:
331: while (iter.hasNext()) {
332: FunctionDescriptor function = (FunctionDescriptor) iter
333: .next();
334: function.writeXML(out, indent);
335: }
336:
337: XMLUtil.printIndent(out, --indent);
338: out.println("</pre-functions>");
339: }
340: }
341: }
|