/*
 * Copyright (c) 2002-2003 by OpenSymphony
 * All rights reserved.
 */
package com.opensymphony.workflow.loader;

import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import java.io.PrintWriter;

import java.util.ArrayList;
import java.util.List;


/**
 * @author <a href="mailto:plightbo@hotmail.com">Pat Lightbody</a>
 * @version $Revision: 1.2 $
 */
public class ConditionalResultDescriptor extends ResultDescriptor {
    //~ Instance fields ////////////////////////////////////////////////////////

    protected List conditions = new ArrayList();
    protected String conditionType;

    //~ Constructors ///////////////////////////////////////////////////////////

    public ConditionalResultDescriptor() {
    }

    public ConditionalResultDescriptor(Element conditionalResult) {
        init(conditionalResult);
    }

    //~ Methods ////////////////////////////////////////////////////////////////

    public void setConditionType(String conditionType) {
        this.conditionType = conditionType;
    }

    public String getConditionType() {
        return conditionType;
    }

    public List getConditions() {
        return conditions;
    }

    public void writeXML(PrintWriter out, int indent) {
        XMLUtil.printIndent(out, indent++);

        StringBuffer buf = new StringBuffer();
        buf.append("<result");

        if (hasId()) {
            buf.append(" id=\"").append(getId()).append("\"");
        }

        buf.append(" old-status=\"").append(oldStatus).append("\"");

        if (join != 0) {
            buf.append(" join=\"").append(join).append("\"");
        } else if (split != 0) {
            buf.append(" split=\"").append(split).append("\"");
        } else {
            buf.append(" status=\"").append(status).append("\"");
            buf.append(" step=\"").append(step).append("\"");

            if ((owner != null) && (owner.length() > 0)) {
                buf.append(" owner=\"").append(owner).append("\"");
            }
        }

        buf.append(">");
        out.println(buf);
        XMLUtil.printIndent(out, indent++);
        out.println("<conditions type=\"" + conditionType + "\">");

        for (int i = 0; i < conditions.size(); i++) {
            ConditionDescriptor condition = (ConditionDescriptor) conditions.get(i);
            condition.writeXML(out, indent);
        }

        XMLUtil.printIndent(out, --indent);
        out.println("</conditions>");

        if (validators.size() > 0) {
            XMLUtil.printIndent(out, indent++);
            out.println("<validators>");

            for (int i = 0; i < validators.size(); i++) {
                ValidatorDescriptor validator = (ValidatorDescriptor) validators.get(i);
                validator.writeXML(out, indent);
            }

            XMLUtil.printIndent(out, --indent);
            out.println("</validators>");
        }

        printPreFunctions(out, indent);
        printPostFunctions(out, indent);
        XMLUtil.printIndent(out, --indent);
        out.println("</result>");
    }

    protected void init(Element conditionalResult) {
        super.init(conditionalResult);

        Element conditions = XMLUtil.getChildElement(conditionalResult, "conditions");
        conditionType = conditions.getAttribute("type");

        NodeList conditionNodes = conditions.getElementsByTagName("condition");
        int length = conditionNodes.getLength();

        for (int i = 0; i < length; i++) {
            Element condition = (Element) conditionNodes.item(i);
            ConditionDescriptor conditionDescriptor = new ConditionDescriptor(condition);
            conditionDescriptor.setParent(this);
            this.conditions.add(conditionDescriptor);
        }
    }
}

@@ -106,24 +106,94 @@
 ow.loader;%0A%0A
+import com.opensymphony.workflow.InvalidWorkflowDescriptorException;%0A%0A
 import org.w
@@ -416,17 +416,17 @@
 sion: 1.
-2
+3
  $%0A */%0Ap
@@ -1226,32 +1226,382 @@
 ditions;%0A    %7D%0A%0A
+    public void validate() throws InvalidWorkflowDescriptorException %7B%0A        super.validate();%0A%0A        if (conditions.size() == 0) %7B%0A            throw new InvalidWorkflowDescriptorException(%22Conditional result from %22 + ((ActionDescriptor) getParent()).getName() + %22 to %22 + getDestination() + %22 must have at least one condition%22);%0A        %7D%0A    %7D%0A%0A
     public void 
@@ -4145,11 +4145,251 @@
  %7D%0A    %7D
+%0A%0A    private String getDestination() %7B%0A        if (join != 0) %7B%0A            return %22join #%22 + join;%0A        %7D else if (split != 0) %7B%0A            return %22split #%22 + split;%0A        %7D else %7B%0A            return %22step #%22 + step;%0A        %7D%0A    %7D
 %0A%7D%0A

Add explicit validation for conditions, so we have a nicer error message in the designer