KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jcorporate > expresso > services > controller > WizardFormStack


1 /* ====================================================================
2  * The Jcorporate Apache Style Software License, Version 1.2 05-07-2002
3  *
4  * Copyright (c) 1995-2002 Jcorporate Ltd. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright
11  * notice, this list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  * notice, this list of conditions and the following disclaimer in
15  * the documentation and/or other materials provided with the
16  * distribution.
17  *
18  * 3. The end-user documentation included with the redistribution,
19  * if any, must include the following acknowledgment:
20  * "This product includes software developed by Jcorporate Ltd.
21  * (http://www.jcorporate.com/)."
22  * Alternately, this acknowledgment may appear in the software itself,
23  * if and wherever such third-party acknowledgments normally appear.
24  *
25  * 4. "Jcorporate" and product names such as "Expresso" must
26  * not be used to endorse or promote products derived from this
27  * software without prior written permission. For written permission,
28  * please contact info@jcorporate.com.
29  *
30  * 5. Products derived from this software may not be called "Expresso",
31  * or other Jcorporate product names; nor may "Expresso" or other
32  * Jcorporate product names appear in their name, without prior
33  * written permission of Jcorporate Ltd.
34  *
35  * 6. No product derived from this software may compete in the same
36  * market space, i.e. framework, without prior written permission
37  * of Jcorporate Ltd. For written permission, please contact
38  * partners@jcorporate.com.
39  *
40  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
41  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
42  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
43  * DISCLAIMED. IN NO EVENT SHALL JCORPORATE LTD OR ITS CONTRIBUTORS
44  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
45  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
46  * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
47  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
48  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
49  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
50  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51  * SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This software consists of voluntary contributions made by many
55  * individuals on behalf of the Jcorporate Ltd. Contributions back
56  * to the project(s) are encouraged when you make modifications.
57  * Please send them to support@jcorporate.com. For more information
58  * on Jcorporate Ltd. and its products, please see
59  * <http://www.jcorporate.com/>.
60  *
61  * Portions of this software are based upon other open source
62  * products and are subject to their respective licenses.
63  */

64
65 package com.jcorporate.expresso.services.controller;
66
67 import com.jcorporate.expresso.core.controller.Controller;
68 import com.jcorporate.expresso.core.controller.ControllerException;
69 import com.jcorporate.expresso.core.controller.ControllerRequest;
70 import com.jcorporate.expresso.core.controller.ControllerResponse;
71 import com.jcorporate.expresso.core.controller.Transition;
72
73 import java.util.Stack JavaDoc;
74
75 /**
76  * Utility class for wizard type interfaces. Call processNewState() for each
77  * 'prompt' state. Ignore for each process state.
78  * <p>Typical Usage<br/>
79  * <code><pre>
80  * WizardFormStack.getInstance(request).processNewState(this, request,response);
81  * Transition backBtn = WizardFormStack.getInstance(request).getPreviousTransition();
82  * if (backBtn != null) {
83  * response.add(backBtn);
84  * }
85  * </pre></code>
86  * </p>
87  * <p/>
88  * The issues with using this class include: (1) The WizardFormStack does not
89  * work across multiple controllers. (2) You must not use a parameter named
90  * 'back' in your form since this is required to detect if you've gone backwards
91  * in the workflow.
92  * </p>
93  *
94  * @author Michael Rimov
95  * @version $Revision: 1.5 $ on $Date: 2004/11/17 20:48:17 $
96  * @since Expresso 5.1
97  */

98 public class WizardFormStack implements java.io.Serializable JavaDoc {
99
100     /**
101      * Name of the attribute that sits on the session for this current object
102      */

103     protected static final String JavaDoc MY_KEY = WizardFormStack.class.getName() + ".key";
104
105     /**
106      * String for the parameter that is included in URLs to let the WizardFormStack
107      * know that the current form was reached by clicking the 'back' parameter.
108      */

109     public static final String JavaDoc BACK_PARAM = "back";
110
111     /**
112      * The actual data structure used for this
113      */

114     protected static Stack JavaDoc formStack = new Stack JavaDoc();
115
116     /**
117      * A transition to the current page.
118      */

119     protected Transition current;
120
121     /**
122      * Default constructor. Do not call directly. Instead use getInstance()
123      * to get an instance of the WizardFormStack.
124      */

125     protected WizardFormStack() {
126     }
127
128     /**
129      * Way to get an instance of the wizard form stack
130      *
131      * @param request the <code>ControllerRequest</code> object
132      * @return an instance of WizardFormStack unique to your session.
133      * @throws ControllerException upon session error
134      */

135     public static synchronized WizardFormStack getInstance(ControllerRequest request) throws ControllerException {
136         WizardFormStack me = (WizardFormStack) request.getSession().getPersistentAttribute(MY_KEY);
137         if (me == null) {
138             me = new WizardFormStack();
139             request.getSession().setPersistentAttribute(MY_KEY, me);
140         }
141
142         return me;
143     }
144
145     /**
146      * Retrieve the Transition to use for your 'back button' The name of the
147      * transition is 'back' appropriately ;)
148      *
149      * @return Transition or null of there is no previous form
150      */

151     public synchronized Transition getPreviousTransition() {
152         if (formStack.size() > 0) {
153             return (Transition) formStack.peek();
154         } else {
155             return null;
156         }
157     }
158
159     /**
160      * Resets all the instances of this form. This will wipe out any instances,
161      * so be careful when calling this method.
162      */

163     public synchronized void reset() {
164         current = null;
165         formStack = new Stack JavaDoc();
166     }
167
168     /**
169      * Registers a new 'prompt' form with the wizard 'workflow'
170      *
171      * @param c the Controller. Usually pass 'this' in your state handler
172      * @param request The <code>ControllerRequest</code> object
173      * @param response The <code>ControllerResponse</code> object
174      * @throws ControllerException upon error creating the session.
175      */

176     public synchronized void processNewState(Controller c, ControllerRequest request, ControllerResponse response) throws ControllerException {
177         if ("true".equals(request.getParameter(BACK_PARAM))) {
178             if (formStack.size() == 0) {
179                 throw new IllegalStateException JavaDoc("There are no more previous states");
180             }
181             current = (Transition) formStack.pop();
182         } else {
183             if (current != null && current.getState().equals(request.getParameter(Controller.STATE_PARAM_KEY))) {
184                 //we've gotten back to the same state through an error probably
185
//don't push this value again.
186
return;
187             }
188
189             if (current != null) {
190                 formStack.push(current);
191             }
192
193             String JavaDoc curState = request.getParameter(Controller.STATE_PARAM_KEY);
194             if (curState == null) {
195                 curState = c.getInitialState();
196             }
197             Transition back = new Transition(curState, c);
198             back.addParam("back", "true");
199             back.setName("back");
200             current = back;
201         }
202     }
203
204
205     /**
206      * Pushes a new custom transition on the stack. This is normally not called, but
207      * is provided for maximum flexibility.
208      *
209      * @param t The transition that you want to be hit when the 'back' button is
210      * pressed.
211      * @param request the ControllerRequest object
212      * @param response the ControllerResponse object
213      * @throws ControllerException upon error
214      */

215     public synchronized void pushNewTransition(Transition t,
216                                                ControllerRequest request,
217                                                ControllerResponse response) throws ControllerException {
218
219         if (current != null) {
220             formStack.push(current);
221         }
222
223         t.addParam("back", "true");
224         t.setName("back");
225         current = t;
226     }
227
228
229 }
Popular Tags