View Javadoc

1   package com.sri.emo.wizard.selection.management;
2   
3   import com.jcorporate.expresso.core.controller.*;
4   import com.jcorporate.expresso.core.db.DBException;
5   import com.sri.emo.controller.SelectionWizardManager;
6   import com.sri.emo.dbobj.StepAttributes;
7   import com.sri.emo.dbobj.WizDefinition;
8   import com.sri.emo.dbobj.WizStep;
9   import com.sri.emo.wizard.expressoimpl.WizardController;
10  
11  import java.util.HashMap;
12  import java.util.Map;
13  
14  /***
15   * Abstract base class for the step handler.  Includes the ability to
16   * tell its derived classes whether we're editing or not.  This will affect
17   * the rendering of the pages.
18   *
19   * @author Michael Rimov
20   */
21  public abstract class AbstractStepHandler {
22  
23      /***
24       * Constant for indication that the handler is editing a step.
25       */
26      public static final Boolean EDIT_MODE = Boolean.TRUE;
27  
28      /***
29       * Constant for indication that the handler is adding a step.
30       */
31      public static final Boolean ADD_MODE = Boolean.FALSE;
32  
33      /***
34       * Instance of the step we're working with.  May be new or from
35       * a data source.
36       */
37      private WizStep theStep = null;
38  
39      /***
40       * Status variable for whether we're editing wizard step
41       * or not.  Most often will affect the view, but will affect some
42       * processing as well.
43       */
44      private Boolean editing = Boolean.FALSE;
45  
46      /***
47       * Constant in the session where attributes are stored.
48       */
49      public static final String ATTRIBUTE_SESSION_KEY = AbstractStepHandler.class + ".Attributes";
50  
51      /***
52       * Parameter for Reference URL
53       */
54      public static final String PARAM_RETURN_URL = "RefURL";
55  
56  
57      /***
58       * Constructor that takes a wizard id and a step id and forms its internal
59       * state, allowing for easy retrieval of database objects from subclasses.
60       *
61       * @param wizId  The Wizard ID (usually an integer)
62       * @param stepId The Step Id.  May be null if there is no step definition.
63       * @throws DBException upon error.
64       */
65      public AbstractStepHandler(final String wizId, final String stepId) throws DBException {
66          //Create a new step
67          WizStep step = new WizStep();
68          //If we have a new step
69          if (stepId == null || stepId.length() == 0) {
70              //Populate with defaults
71              step.setFieldsWithDefaults();
72              step.setWizId(wizId);
73              //Editing is false
74              setEditing(Boolean.FALSE);
75          } else {
76              //Else: Populate with data.
77              step.setId(stepId);
78              step.retrieve();
79  
80              assert wizId.equals(step.getWizId()):"Wizard Parameter Id must match corresponding step id";
81              //Set editing is true.
82              setEditing(Boolean.TRUE);
83          }
84          //Set the step attribute.
85          theStep = step;
86      }
87  
88      /***
89       * Returns true if we're in editing mode for a particular page.
90       *
91       * @return boolean true.
92       */
93      public boolean isEditing() {
94          return editing.booleanValue();
95      }
96  
97      /***
98       * Allows definition of whether the state is editing a current wizard.
99       *
100      * @param value Boolean
101      */
102     protected void setEditing(final Boolean value) {
103         assert value != null:"Must supply a boolean value";
104         editing = value;
105     }
106 
107     /***
108      * Retreieves the step based on the id given to us.
109      *
110      * @return WizStep constructed WizStep as either stored in the database
111      *         or with default values populated.
112      * @throws DBException upon error.
113      */
114     protected WizStep getStep() throws DBException {
115         return theStep;
116     }
117 
118     /***
119      * Function that delegates to the {@link StepParameterParser} class.
120      *
121      * @param request ExpressoRequest the ExpressoRequest object
122      * @param ec      ErrorCollection [in/out] the error collection that gets populated
123      *                with notes if the parameters are invalid.
124      * @return WizStep constructed and populated with available parameters.
125      * @throws DBException upon error
126      */
127     protected WizStep getStepFromParameters(final ExpressoRequest request, final ErrorCollection ec) throws
128             DBException {
129 
130         return new StepParameterParser(request).parseStep(ec);
131     }
132 
133     /***
134      * Retreives the definition associated with the step.
135      *
136      * @return WizDefinition wizard definition.
137      * @throws DBException upon error.
138      */
139     protected WizDefinition getDefinition() throws DBException {
140         return theStep.getWizardDefinition();
141     }
142 
143     /***
144      * Retrieve the title of the current step.
145      * where we extract the necessary parameters to build the title.
146      *
147      * @return String the step title.
148      * @throws DBException
149      */
150     protected String getStepTitle() throws DBException {
151         return "Step '" + getStepName() + "' of Wizard '" +
152                 getWizardTitle() + "'";
153     }
154 
155     /***
156      * Retrieves the wizard title.
157      *
158      * @return String
159      * @throws DBException
160      */
161     protected String getWizardTitle() throws DBException {
162         WizDefinition wizDef = getDefinition();
163         return wizDef.getWizName();
164     }
165 
166     /***
167      * Retrieve the step name
168      *
169      * @return String
170      * @throws DBException
171      */
172     protected String getStepName() throws DBException {
173         if (isEditing()) {
174             return getStep().getTitle();
175         } else {
176             return "[New Step]";
177         }
178     }
179 
180     /***
181      * Builds a cancel transition to allow easy exiting while in any
182      * State of editing the wizard step.
183      *
184      * @return Transition The cancel transition.
185      * @throws DBException upon database access error.
186      */
187     protected Transition buildCancelTransition() throws DBException {
188         Transition cancel = new Transition("cancel", "Cancel", SelectionWizardManager.class,
189                 SelectionWizardManager.STATE_PROMPT_EDIT);
190 
191         cancel.addParam(WizardController.WIZ_PARAMETER_ID, getStep().getWizardDefinition().getId());
192         return cancel;
193     }
194 
195     /***
196      * Transitions to another class, usually on error.
197      *
198      * @param controllerClass Class
199      * @param state           String
200      * @param request         ExpressoRequest
201      * @param response        ExpressoResponse
202      * @throws ControllerException
203      */
204     protected void transitionOnError(final Class controllerClass,
205                                      final String state,
206                                      final ExpressoRequest request,
207                                      final ExpressoResponse response) throws ControllerException {
208         Transition t = new Transition();
209         Map oldparams = request.getAllParameters();
210         t.setParams(oldparams);
211         t.setState(state);
212         t.setControllerObject(controllerClass);
213         t.executeTransition(request, response);
214     }
215 
216     /***
217      * Parses the appropriate attributes from the controller request and
218      * saves it to the session for 'save' retrieval.
219      *
220      * @param request ExpressoRequest the ExpressoRequest object
221      * @return Map the Map
222      * @throws ControllerException upon error.
223      */
224     protected Map parseStepAttributes(final ExpressoRequest request) throws ControllerException {
225         Map returnValue = new HashMap();
226         String val;
227 
228         val = request.getParameter(StepAttributes.ATTRIBUTE_INSTANCE_FIELD);
229         if (val != null && val.length() > 0) {
230             returnValue.put(StepAttributes.ATTRIBUTE_INSTANCE_FIELD, val);
231         }
232 
233         val = request.getParameter(StepAttributes.ATTRIBUTE_INSTANCE_ID);
234         if (val != null && val.length() > 0) {
235             returnValue.put(StepAttributes.ATTRIBUTE_INSTANCE_ID, val);
236         }
237 
238         val = request.getParameter(StepAttributes.ATTRIBUTE_MODEL);
239         if (val != null && val.length() > 0) {
240             returnValue.put(StepAttributes.ATTRIBUTE_MODEL, val);
241         }
242 
243         val = request.getParameter(StepAttributes.ATTRIBUTE_MODEL_FIELD);
244         if (val != null && val.length() > 0) {
245             returnValue.put(StepAttributes.ATTRIBUTE_MODEL_FIELD, val);
246         }
247 
248         val = request.getParameter(StepAttributes.ATTRIBUTE_TEXT_STYLE);
249         if (val != null && val.length() > 0) {
250             returnValue.put(StepAttributes.ATTRIBUTE_TEXT_STYLE, val);
251         }
252 
253         request.getSession().setPersistentAttribute(ATTRIBUTE_SESSION_KEY, returnValue);
254         return returnValue;
255     }
256 
257 
258 }