001    /*
002     * Copyright 1999-2001,2004 The Apache Software Foundation.
003     * 
004     * Licensed under the Apache License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     *      http://www.apache.org/licenses/LICENSE-2.0
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */ 
016    
017    package org.apache.commons.workflow.web;
018    
019    
020    import java.io.IOException;
021    import java.util.EmptyStackException;
022    import javax.servlet.RequestDispatcher;
023    import javax.servlet.ServletException;
024    import javax.servlet.ServletRequest;
025    import javax.servlet.ServletResponse;
026    import org.apache.commons.workflow.Context;
027    import org.apache.commons.workflow.Step;
028    import org.apache.commons.workflow.StepException;
029    import org.apache.commons.workflow.base.BaseStep;
030    
031    
032    /**
033     * <p>Unconditionally transfer control to the step that is identified by
034     * a request parameter with the specified name.</p>
035     *
036     * <p>Supported Attributes:</p>
037     * <ul>
038     * <li><strong>step</strong> - Name of a request parameter, included on the
039     *     current request, that contains the identifier of the Step (within the
040     *     current Activity) to which control should be transferred.  If not
041     *     specified, a request parameter named <code>step</code> is used.</li>
042     * </ul>
043     *
044     * @version $Revision: 155475 $ $Date: 2005-02-26 13:31:11 +0000 (Sat, 26 Feb 2005) $
045     * @author Craig R. McClanahan
046     */
047    
048    public class GotoStep extends BaseStep {
049    
050    
051        // ----------------------------------------------------------= Constructors
052    
053    
054        /**
055         * Construct a default instance of this Step.
056         */
057        public GotoStep() {
058    
059            super();
060    
061        }
062    
063    
064        /**
065         * Construct an instance of this Step with the specified identifier.
066         *
067         * @param id Step identifier
068         */
069        public GotoStep(String id) {
070    
071            super();
072            setId(id);
073    
074        }
075    
076    
077        /**
078         * Construct a fully configured instance of this Step.
079         *
080         * @param id Step identifier
081         * @param step Request parameter name containing our step identifier
082         */
083        public GotoStep(String id, String step) {
084    
085            super();
086            setId(id);
087            setStep(step);
088    
089        }
090    
091    
092        // ------------------------------------------------------------- Properties
093    
094    
095        /**
096         * The request parameter containing the identifier of the Step to which
097         * control should be transferred.
098         */
099        protected String step = "step";
100    
101        public String getStep() {
102            return (this.step);
103        }
104    
105        public void setStep(String step) {
106            this.step = step;
107        }
108    
109    
110        // --------------------------------------------------------- Public Methods
111    
112    
113        /**
114         * Perform the executable actions related to this Step, in the context of
115         * the specified Context.
116         *
117         * @param context The Context that is tracking our execution state
118         *
119         * @exception StepException if a processing error has occurred
120         */
121        public void execute(Context context) throws StepException {
122    
123            // Make sure our executing Context is a WebContext
124            if (!(context instanceof WebContext))
125                throw new StepException("Execution context is not a WebContext",
126                                        this);
127            WebContext webContext = (WebContext) context;
128    
129            // Locate the step identifier to which we will transfer control
130            ServletRequest request = webContext.getServletRequest();
131            String id = request.getParameter(step);
132            if (id == null)
133                throw new StepException("No request parameter '" + step + "'",
134                                        this);
135    
136    
137            // Locate the step to which we will transfer control
138            Step next = getOwner().findStep(id);
139            if (next == null)
140                throw new StepException("Cannot find step '" + id + "'", this);
141    
142            // Tell our Context to transfer control
143            context.setNextStep(next);
144    
145        }
146    
147    
148        /**
149         * Render a string representation of this Step.
150         */
151        public String toString() {
152    
153            StringBuffer sb = new StringBuffer("<web:goto");
154            if (getId() != null) {
155                sb.append(" id=\"");
156                sb.append(getId());
157                sb.append("\"");
158            }
159            sb.append(" step=\"");
160            sb.append(getStep());
161            sb.append("\"/>");
162            return (sb.toString());
163    
164        }
165    
166    
167    }