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.jcorporate.expresso.services.controller.ui.DefaultAutoElement;
6   import com.sri.emo.dbobj.StepAttributes;
7   import com.sri.emo.dbobj.WizStep;
8   
9   import java.util.HashMap;
10  import java.util.Map;
11  
12  /***
13   * Combines common controls into a step editor.
14   *
15   * @author Michael Rimov
16   */
17  public class EditFinalState extends AbstractStepHandler {
18  
19      /***
20       * Constant for title field.
21       */
22      public static final String PARAM_TITLE = "Title";
23  
24      /***
25       * Constant for directive field.
26       */
27      public static final String PARAM_DIRECTIVE = "Directive";
28  
29      /***
30       * Constant for helptext field.
31       */
32      public static final String PARAM_HELPTEXT = "HelpText";
33  
34  
35      /***
36       * Name of the save transition.
37       */
38      public static final String TRANSITION_SAVE = "save";
39  
40      /***
41       * Name of the edit transition.
42       */
43      public static final String TRANSITION_EDIT = "edit";
44  
45      /***
46       * Constant in the session where attributes are stored.
47       */
48      public static final String ATTRIBUTE_SESSION_KEY = EditFinalState.class + ".Attributes";
49  
50      /***
51       * Constant for where attributes are stored in the Request context.
52       */
53      public static final String REQUEST_ATTRIBUTES = "ObjectAttributes";
54  
55      /***
56       * Default constructor.
57       *
58       * @param wizId  The wizard id.
59       * @param stepId The Step Id.
60       * @throws DBException upon error.
61       */
62      public EditFinalState(final String wizId, final String stepId) throws DBException {
63          super(wizId, stepId);
64      }
65  
66      /***
67       * Renders the controls that are common to each of the 'last definition'
68       * steps.  These are <tt>Title</tt> <tt>Helptext</tt> <tt>Directive</tt>
69       *
70       * @param stepDefinition WizStep
71       * @param request        ExpressoRequest
72       * @param response       ExpressoResponse
73       * @return block instance containing input variables.
74       * @throws ControllerException upon Input rendering error.
75       * @throws DBException         upon DataObject.getField() error.
76       */
77      protected Block renderCommonControls(final WizStep stepDefinition,
78                                           final ExpressoRequest request,
79                                           final ExpressoResponse response) throws ControllerException, DBException {
80  
81          Block block = DefaultAutoElement.getAutoControllerElement().createDBObjectBlock(request,
82                  response, stepDefinition);
83  
84          // use raw text for helptext
85          Input input = new Input(WizStep.FLD_HELPTEXT);
86          input.setDefaultValue(stepDefinition.getHelpTextRaw());
87          block.add(input);
88          block.setName("wizstep");
89  
90          return block;
91      }
92  
93      /***
94       * Returns true if we didn't try to save and we got an error collection.  Then
95       * we should jump back once in the workflow.
96       *
97       * @param ec       ErrorCollection to examine if we need to transition
98       * @param response ExpressoResponse the ExpressoResponse object.
99       * @return boolean true if we need to transition to some other prompt state.
100      */
101     protected boolean isNeedTransitionOnError(final ErrorCollection ec, final ExpressoResponse response) {
102         if (ec.getErrorCount() > 0) {
103             return (!(WizardStepController.STATE_DO_ADD.equals(response.getRequestedState())
104                     || WizardStepController.STATE_DO_EDIT.equals(response.getRequestedState())));
105 
106         } else {
107             //No Errors!
108             return false;
109         }
110     }
111 
112     /***
113      * Parses the appropriate attributes from the controller request and
114      * saves it to the session for 'save' retrieval.
115      *
116      * @param request ExpressoRequest the ExpressoRequest object
117      * @return Map the Map
118      * @throws ControllerException upon error.
119      */
120     protected Map buildAndStoreStepAttributes(final ExpressoRequest request) throws ControllerException {
121         Map returnValue = new HashMap();
122         String val;
123 
124         val = request.getParameter(StepAttributes.ATTRIBUTE_INSTANCE_FIELD);
125         if (val != null && val.length() > 0) {
126             returnValue.put(StepAttributes.ATTRIBUTE_INSTANCE_FIELD, val);
127         }
128 
129         val = request.getParameter(StepAttributes.ATTRIBUTE_INSTANCE_ID);
130         if (val != null && val.length() > 0) {
131             returnValue.put(StepAttributes.ATTRIBUTE_INSTANCE_ID, val);
132         }
133 
134         val = request.getParameter(StepAttributes.ATTRIBUTE_MODEL);
135         if (val != null && val.length() > 0) {
136             returnValue.put(StepAttributes.ATTRIBUTE_MODEL, val);
137         }
138 
139         val = request.getParameter(StepAttributes.ATTRIBUTE_MODEL_FIELD);
140         if (val != null && val.length() > 0) {
141             returnValue.put(StepAttributes.ATTRIBUTE_MODEL_FIELD, val);
142         }
143 
144         val = request.getParameter(StepAttributes.ATTRIBUTE_TEXT_STYLE);
145         if (val != null && val.length() > 0) {
146             returnValue.put(StepAttributes.ATTRIBUTE_TEXT_STYLE, val);
147         }
148 
149         request.getSession().setPersistentAttribute(ATTRIBUTE_SESSION_KEY, returnValue);
150         return returnValue;
151     }
152 
153     /***
154      * Constructs the 'save and return' transition.  Sub classes will need
155      * to add their own parameters to this transition because some edit
156      * pages have gone through different workflows and will have additional
157      * data attached to them.  All WizStep fields to be modified should
158      * be sent with this parameter.
159      *
160      * @param step WizStep
161      * @return Transition that might be needed to have extra parameters
162      *         if needed from other steps.
163      * @throws DBException upon getField() error.
164      */
165     protected Transition buildSaveAndReturnTransition(final WizStep step) throws DBException {
166 
167         Transition t = new Transition();
168         t.setName(TRANSITION_SAVE);
169         t.setLabel("Save and return to main wizard page");
170         t.setControllerObject(WizardStepController.class);
171 
172         t.addParam(WizStep.FLD_STEP_TYPE, step.getType() + "");
173         t.addParam(WizStep.FLD_WIZID, step.getWizId());
174 
175         if (isEditing()) {
176             t.setState(WizardStepController.STATE_DO_EDIT);
177             t.addParam(WizStep.FLD_ID, step.getId());
178         } else {
179             t.setState(WizardStepController.STATE_DO_ADD);
180         }
181 
182         return t;
183     }
184 
185 
186     /***
187      * Constructs a transition that determines the 'step type'.
188      *
189      * @param stepDefinition WizStep
190      * @return Transition
191      * @throws DBException         upon DBObject.getField() Error
192      * @throws ControllerException upon response.add() error.
193      */
194     protected Transition buildEditTypeTransition(final WizStep stepDefinition) throws ControllerException, DBException {
195         //Get the type of step we're editing.
196         stepDefinition.getType();
197 
198         //Create the edit link to return
199         Transition t = new Transition();
200         t.setLabel("Edit");
201         t.setName(TRANSITION_EDIT);
202         t.setControllerObject(WizardStepController.class);
203         t.addParam(WizStep.FLD_STEP_TYPE, stepDefinition.getStepType() + "");
204         t.addParam(WizStep.FLD_WIZID, stepDefinition.getWizId());
205         t.setState(PromptStepType.STATE_NAME);
206 
207         assert stepDefinition.getId().length() != 0
208                 : "There should be an id defined for wizstep for edit link building";
209         t.addParam(WizStep.FLD_ID, stepDefinition.getId());
210 
211         return t;
212     }
213 
214     /***
215      * Creates output for the text label of what type of step we're dealing
216      * with. Saves the output to the name &quot;stepType&quot;
217      *
218      * @param response ExpressoResponse the response to add the output to.
219      * @param step     WizStep the step we're working with.
220      * @throws DBException         upon database error.
221      * @throws ControllerException upon controller error.
222      */
223     protected void addStepTypeLabel(final ExpressoResponse response, final WizStep step) throws DBException,
224             ControllerException {
225         //Create an output for the text
226         response.add(new Output("stepType", step.getValidValueDescrip(WizStep.FLD_STEP_TYPE)));
227     }
228 }