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:
009: import org.w3c.dom.Element;
010:
011: import java.io.PrintWriter;
012:
013: import java.util.ArrayList;
014: import java.util.List;
015:
016: /**
017: * @author <a href="mailto:plightbo@hotmail.com">Pat Lightbody</a>
018: */
019: public class ConditionalResultDescriptor extends ResultDescriptor {
020: //~ Instance fields ////////////////////////////////////////////////////////
021:
022: protected List conditions = new ArrayList();
023:
024: //~ Constructors ///////////////////////////////////////////////////////////
025:
026: /**
027: * @deprecated use {@link DescriptorFactory} instead
028: */
029: ConditionalResultDescriptor() {
030: }
031:
032: /**
033: * @deprecated use {@link DescriptorFactory} instead
034: */
035: ConditionalResultDescriptor(Element conditionalResult) {
036: init(conditionalResult);
037: }
038:
039: //~ Methods ////////////////////////////////////////////////////////////////
040:
041: public List getConditions() {
042: return conditions;
043: }
044:
045: public String getDestination() {
046: WorkflowDescriptor desc = null;
047: String sName = "";
048: AbstractDescriptor actionDesc = getParent().getParent();
049:
050: if (actionDesc != null) {
051: desc = (WorkflowDescriptor) actionDesc.getParent();
052: }
053:
054: if (join != 0) {
055: return "join #" + join;
056: } else if (split != 0) {
057: return "split #" + split;
058: } else {
059: if (desc != null) {
060: sName = desc.getStep(step).getName();
061: }
062:
063: return "step #" + step + " [" + sName + "]";
064: }
065: }
066:
067: public void validate() throws InvalidWorkflowDescriptorException {
068: super .validate();
069:
070: if (conditions.size() == 0) {
071: throw new InvalidWorkflowDescriptorException(
072: "Conditional result from "
073: + ((ActionDescriptor) getParent())
074: .getName() + " to "
075: + getDestination()
076: + " must have at least one condition");
077: }
078:
079: ValidationHelper.validate(conditions);
080: }
081:
082: public void writeXML(PrintWriter out, int indent) {
083: XMLUtil.printIndent(out, indent++);
084:
085: StringBuffer buf = new StringBuffer();
086: buf.append("<result");
087:
088: if (hasId()) {
089: buf.append(" id=\"").append(getId()).append('\"');
090: }
091:
092: if ((dueDate != null) && (dueDate.length() > 0)) {
093: buf.append(" due-date=\"").append(getDueDate())
094: .append('\"');
095: }
096:
097: buf.append(" old-status=\"").append(oldStatus).append('\"');
098:
099: if (join != 0) {
100: buf.append(" join=\"").append(join).append('\"');
101: } else if (split != 0) {
102: buf.append(" split=\"").append(split).append('\"');
103: } else {
104: buf.append(" status=\"").append(status).append('\"');
105: buf.append(" step=\"").append(step).append('\"');
106:
107: if ((owner != null) && (owner.length() > 0)) {
108: buf.append(" owner=\"").append(owner).append('\"');
109: }
110:
111: if ((displayName != null) && (displayName.length() > 0)) {
112: buf.append(" display-name=\"").append(displayName)
113: .append('\"');
114: }
115: }
116:
117: buf.append('>');
118: out.println(buf);
119:
120: for (int i = 0; i < conditions.size(); i++) {
121: ConditionsDescriptor condition = (ConditionsDescriptor) conditions
122: .get(i);
123: condition.writeXML(out, indent);
124: }
125:
126: if (validators.size() > 0) {
127: XMLUtil.printIndent(out, indent++);
128: out.println("<validators>");
129:
130: for (int i = 0; i < validators.size(); i++) {
131: ValidatorDescriptor validator = (ValidatorDescriptor) validators
132: .get(i);
133: validator.writeXML(out, indent);
134: }
135:
136: XMLUtil.printIndent(out, --indent);
137: out.println("</validators>");
138: }
139:
140: printPreFunctions(out, indent);
141: printPostFunctions(out, indent);
142: XMLUtil.printIndent(out, --indent);
143: out.println("</result>");
144: }
145:
146: protected void init(Element conditionalResult) {
147: super .init(conditionalResult);
148:
149: List conditionNodes = XMLUtil.getChildElements(
150: conditionalResult, "conditions");
151:
152: int length = conditionNodes.size();
153:
154: for (int i = 0; i < length; i++) {
155: Element condition = (Element) conditionNodes.get(i);
156: ConditionsDescriptor conditionDescriptor = DescriptorFactory
157: .getFactory().createConditionsDescriptor(condition);
158: conditionDescriptor.setParent(this);
159: this.conditions.add(conditionDescriptor);
160: }
161: }
162: }
|