View Javadoc

1   /* ===================================================================
2    * Copyright 2002-04 SRI International.
3    * Released under the MOZILLA PUBLIC LICENSE Version 1.1
4    * which can be obtained at http://www.mozilla.org/MPL/MPL-1.1.html
5    *
6    * This program is distributed in the hope that it will be useful, but
7    * WITHOUT ANY WARRANTY; without even the implied warranty of
8    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
9    * See the MOZILLA PUBLIC LICENSE for more details.
10   * =================================================================== */
11  
12  package com.sri.emo.wizard.selection;
13  
14  import com.jcorporate.expresso.core.controller.*;
15  import com.jcorporate.expresso.core.db.DBException;
16  import com.sri.emo.EmoSchema;
17  import com.sri.emo.controller.AddNodeAction;
18  import com.sri.emo.dbobj.Node;
19  import com.sri.emo.wizard.Wizard;
20  import com.sri.emo.wizard.WizardRepository;
21  import com.sri.emo.wizard.expressoimpl.ExpressoLink;
22  import com.sri.emo.wizard.expressoimpl.WizardController;
23  import com.sri.emo.wizard.wizardgateway.ListWizards;
24  import com.sri.emo.wizard.wizardgateway.WizardGatewayController;
25  
26  import java.util.Map;
27  
28  
29  /***
30   * Derived controller for Wizard steps in EMO.  Provides unique construction
31   * and finishing capabilities as well as the final saving of the EMO wizard.
32   *
33   * @author Michael Rimov
34   */
35  
36  public class WizardAction extends WizardController {
37  
38      /***
39  	 * 
40  	 */
41  	private static final long serialVersionUID = 1L;
42  
43  
44  	/***
45       * Constant for state "Prompt Template Name"
46       */
47      public static final String STATE_NAME_TEMPLATE = "promptTemplateName";
48  
49  
50      /***
51       * Constant for state "Clone Template"
52       */
53      public static final String STATE_CLONE_TEMPLATE = "cloneTemplate";
54  
55      /***
56       * No wizard error message constant.
57       */
58      public static final String NO_WIZ_ERROR_MESSAGE = "No wizard was found. Please navigate via the links in header or footer or back button.";
59  
60      /***
61       * Default constructor.  Adds two more states to the typical
62       * Wizard Controller:  prompt template name and clone template.
63       */
64      public WizardAction() {
65          State s = new State(STATE_NAME_TEMPLATE, "Prompt Template Name");
66          s.addRequiredParameter("nodeId");
67          addState(s);
68  
69          s = new State(STATE_CLONE_TEMPLATE, "Clone Template");
70          s.addRequiredParameter("templateName");
71          s.addRequiredParameter("nodeId");
72          addState(s);
73  
74          setSchema(EmoSchema.class.getName());
75      }
76  
77      /***
78       * Override to provide backlink to list wizards page in EMO.
79       *
80       * @return Transition.
81       */
82      protected Transition getCancelTransition() {
83          return new Transition("cancel", "cancel", WizardGatewayController.class, ListWizards.STATE_NAME);
84      }
85  
86      /***
87       * Override of WizardController to provide for the unique capabilities
88       * and requirements of an ExpressoAwareWizardRepository as defined in
89       * the Schema.
90       *
91       * @param request The <code>ExpressoRequest</code>Object
92       * @return Wizard instance
93       * @throws com.sri.emo.wizard.WizardException
94       *          upon
95       *          wizard related construction error
96       * @throws com.jcorporate.expresso.core.controller.ControllerException
97       *          upon
98       *          parameter retrieval error.
99       */
100     protected Wizard newWizard(final ExpressoRequest request) throws com.sri.emo.
101             wizard.WizardException,
102             com.jcorporate.expresso.core.controller.ControllerException {
103         Integer id = new Integer(request.getParameter(WIZ_PARAMETER_ID));
104         WizardRepository repository = ((EmoSchema) getSchemaInstance()).
105                 getWizardRepository(request.getDataContext());
106 
107         Wizard returnValue = repository.find(id);
108         storeWizInSession(request, returnValue, id);
109 
110         return returnValue;
111     }
112 
113     private void addViewTrans(final Node n, final ExpressoResponse response) throws DBException, ControllerException {
114         Transition view = AddNodeAction.getViewTrans(n.getNodeId());
115         view.setLabel(n.getNodeTitle());
116         response.add(view);
117     }
118 
119     private void addNoWizError(final ExpressoResponse response) throws ControllerException {
120         ErrorCollection ec = new ErrorCollection();
121         ec.addError(NO_WIZ_ERROR_MESSAGE);
122         response.saveErrors(ec);
123         response.setStyle("home");
124     }
125 
126     /***
127      * Prompts for the name of the template to clone as defined by
128      * the decision matrix.
129      *
130      * @param request  the <code>ExpressoRequest</code> object
131      * @param response the <code>ExpressoResponse</code> object.
132      * @throws ControllerException    upon error
133      * @throws NonHandleableException upon fatal error
134      */
135     protected void runPromptTemplateNameState(final ExpressoRequest request, final ExpressoResponse response)
136             throws ControllerException, NonHandleableException {
137 
138         try {
139             //Grab the current wizard.
140             Wizard wiz = getCurrentWizard(request);
141             if (wiz == null) {
142                 addNoWizError(response);
143                 return;
144             }
145 
146             String nodeIdString = request.getParameter("nodeId");
147             Node node = new Node();
148             node.setNodeId(nodeIdString);
149             node.retrieve();
150 //            Input nodeIdHiddenInput = new Input("nodeId");
151 //            nodeIdHiddenInput.setAttribute(Input.ATTRIBUTE_HIDDEN, "Y");
152 //            nodeIdHiddenInput.setDefaultValue(node.getNodeId());
153 //            response.add(nodeIdHiddenInput);
154 
155             addViewTrans(node, response);
156 
157             Input templateName = new Input("templateName",
158                     "Please enter a name for your template.");
159             templateName.setDefaultValue(response);
160             response.add(templateName);
161             response.add(getCancelTransition());
162 
163             Transition previous = ((ExpressoLink) wiz.getCurrentPage().
164                     getMetadata().getPrevious()).getTransition();
165             previous.addParam(WizardController.WIZ_PARAMETER_ID,
166                     request.getParameter(WizardController.WIZ_PARAMETER_ID));
167             previous.addParam(WizardController.WIZ_DATA_ID,
168                     wiz.getCurrentPage().getId().toString());
169             response.add(previous);
170 
171             Transition next = new Transition("Save and next: Customize your template",
172                     getClass(), STATE_CLONE_TEMPLATE);
173             next.setName("Next");
174             next.addParam(WizardController.WIZ_PARAMETER_ID,
175                     request.getParameter(WIZ_PARAMETER_ID));
176             next.addParam("nodeId", nodeIdString);
177             response.add(next);
178         } catch (DBException ex) {
179             throw new ControllerException("Error in database access "
180                     + "for Node object", ex);
181         }
182 
183     }
184 
185     protected void addOnFinishWizardParameters(final ExpressoRequest request,
186                                                final ExpressoResponse response,
187                                                final Map parameterMap) {
188         parameterMap.put("response", response);
189         parameterMap.put("request", request);
190     }
191 
192 
193     /***
194      * Template method.  In this case, the wizard proper performs a redirect.
195      *
196      * @param request  ExpressoRequest Expresso Request object
197      * @param response ExpressoResponse ExpressoResponse object
198      * @throws ControllerException Never
199      */
200     protected void afterFinishState(final ExpressoRequest request, final ExpressoResponse response) throws
201             ControllerException {
202     }
203 
204 }