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.common.controller.StateHandler;
6 import com.sri.emo.dbobj.WizStep;
7
8
9 /***
10 * Handles the 'choose step type' state. Creates a series of inputs
11 * for radio buttons and instructions as well as button for the next step.
12 *
13 * @author Michael Rimov
14 * @todo Refactor to use the WizStep ValidValue capabilities instead of homegrown arrays.
15 */
16 public class PromptStepType extends AbstractStepHandler implements StateHandler {
17
18
19 /***
20 * Constant for steps that have a per-model picklist.
21 */
22 public static final int MODEL_PICKLIST = 1;
23
24 /***
25 * Constant for steps that have a per-instance picklist.
26 */
27 public static final int INSTANCE_PICKLIST = 2;
28
29 /***
30 * Constant for steps that have a custom picklist.
31 */
32 public static final int CUSTOM_PICKLIST = 3;
33
34 /***
35 * Constant for steps that have custom text entry.
36 */
37 public static final int TEXT_ENTRY = 4;
38
39 /***
40 * Constant for steps that have custom instructions only.
41 */
42 public static final int INSTRUCTIONS_ONLY = 5;
43
44 /***
45 * Constant for access to this state.
46 */
47 public static final String STATE_NAME = "promptSelectStepType";
48
49 /***
50 * Constant for description of this state.
51 */
52 public static final String STATE_DESCRIPTION = "Select Step Type";
53
54
55 /***
56 * Array of menu items that occur for the step type. Package protected
57 * to allow testing against the known values.
58 */
59 String[][] menuitems;
60
61
62 /***
63 * Default Constructor that takes a step and an (optional) step id.
64 *
65 * @param wizId The Wizard Id.
66 * @param stepId The Step Id.
67 * @throws DBException if superclass throws exception.
68 */
69 public PromptStepType(final String wizId, final String stepId) throws DBException {
70 super(wizId, stepId);
71
72 menuitems = new String[][]{
73 {INSTANCE_PICKLIST + "", "Picklist from attribute of instance",
74 "e.g.: a TMV, or a menu created from values of attribute \"Version\" of movie instance \"BladeRunner\""},
75
76 {TEXT_ENTRY + "", "Text entry", "e.g. a field for user to enter the length of a movie"},
77 {INSTRUCTIONS_ONLY + "", "No input; instructions only", ""},
78 {MODEL_PICKLIST + "", "Picklist from attribute of model",
79 "e.g. the menu for Film Board Rating in the model \"Movie\""},
80 };
81
82 }
83
84 /***
85 * Displays the 'prompt step type' state.
86 *
87 * @param request ControllerRequest The Function's ControllerRequest
88 * object.
89 * @param response ControllerResponse The Function's ControllerResponse
90 * object.
91 * @throws DBException upon underlying database exception error.
92 * @throws ControllerException upon underlying ControllerException error.
93 */
94 public void handleRequest(final ExpressoRequest request, final ExpressoResponse response)
95 throws DBException, ControllerException {
96
97 response.setTitle(STATE_DESCRIPTION);
98
99
100 WizStep step = getStep();
101
102
103 String currentStepType = response.getFormCache(WizStep.FLD_STEP_TYPE);
104 if (currentStepType == null || currentStepType.length() == 0) {
105 currentStepType = step.getStepType() + "";
106 }
107
108
109 addInputs(currentStepType, response);
110
111
112 Transition post = new Transition("next", "Save and next: Step Attributes", WizardStepController.class,
113 WizardStepController.STATE_DO_SELECT_STEP_TYPE);
114
115 post.addParam(WizStep.FLD_WIZID, request.getParameter(WizStep.FLD_WIZID));
116
117 if (isEditing()) {
118 post.addParam(WizStep.FLD_ID, step.getId());
119 }
120
121 response.add(post);
122
123 response.add(buildCancelTransition());
124
125 }
126
127 /***
128 * Adds the inputs to the step.
129 *
130 * @param currentStepType String the current value defined for the step.
131 * @param response ControllerResponse what we populate with inputs.
132 * @throws ControllerException upon <code>ControllerResponse.add(Input)</code> error.
133 */
134 protected void addInputs(final String currentStepType, final ExpressoResponse response) throws ControllerException {
135
136
137
138 for (int i = 0; i < menuitems.length; i++) {
139 Input input = new Input();
140 input.setName("Option" + i);
141 input.setAttribute(Input.ATTRIBUTE_RADIO, "Y");
142 input.setAttribute("StepTypeId", menuitems[i][0]);
143 if (currentStepType.equals(menuitems[i][0])) {
144 input.setDefaultValue("Y");
145 }
146
147 input.setLabel(menuitems[i][1]);
148 input.setAttribute("Explanation", menuitems[i][2]);
149 response.add(input);
150 }
151 }
152 }