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.core.dbobj.ValidValue;
6 import com.sri.common.controller.StateHandler;
7 import com.sri.emo.dbobj.*;
8
9 import java.util.ArrayList;
10
11 /***
12 * This state prompts for the model that should be used in locating the attribute
13 * to use for building the menus.
14 *
15 * @author Michael Rimov
16 */
17 public class PromptSelectModel extends AbstractStepHandler implements StateHandler {
18
19 /***
20 * Id of the input parameter on the form.
21 */
22 public static final String PARAM_MODEL_ID = StepAttributes.ATTRIBUTE_MODEL;
23
24 /***
25 * Constant for access to this state.
26 */
27 public static final String STATE_NAME = "promptSelectModel";
28
29 /***
30 * Constant for description of this state.
31 */
32 public static final String STATE_DESCRIPTION = "Select Step Model";
33
34
35 /***
36 * Constructor that takes a wizard id and a step id and forms its internal
37 * state, allowing for easy retrieval of database objects from subclasses.
38 *
39 * @param wizId The Wizard ID (usually an integer)
40 * @param stepId The Step Id. May be null if there is no step definition.
41 * @throws DBException upon error.
42 */
43 public PromptSelectModel(final String wizId, final String stepId) throws DBException {
44 super(wizId, stepId);
45 }
46
47 /***
48 * Called to handle the request.
49 *
50 * @param request ExpressoRequest The Function's ExpressoRequest
51 * object.
52 * @param response ExpressoResponse The Function's ExpressoResponse
53 * object.
54 * @throws DBException upon underlying database exception error.
55 * @throws ControllerException upon underlying ControllerException error.
56 */
57 public void handleRequest(final ExpressoRequest request, final ExpressoResponse response) throws DBException,
58 ControllerException {
59
60
61 ErrorCollection ec = request.getErrorCollection();
62 if (ec == null) {
63 ec = new ErrorCollection();
64 }
65
66 getStepFromParameters(request, ec);
67 if (ec.getErrorCount() > 0) {
68 response.saveErrors(ec);
69 }
70
71 String title = getStepTitle();
72 response.setTitle("Select Model for: " + title);
73 response.add(new Output("pageTitle", title));
74
75
76 NodeType[] allTypes = NodeType.getAllTypes();
77 ArrayList typesWithAttributes = new ArrayList();
78 for (int j = 0; j < allTypes.length; j++) {
79 NodeType aType = allTypes[j];
80 Part[] parts = PartsFactory.getParts(aType.getEntityName());
81 for (int k = 0; k < parts.length; k++) {
82 Part part = parts[k];
83 if (part.hasPicklist()) {
84 ValidValue vv = new ValidValue(aType.getId(), aType.getEntityDescription());
85 typesWithAttributes.add(vv);
86 break;
87 }
88 }
89 }
90
91 if (typesWithAttributes.size() == 0) {
92 response.addError(
93 "No node types were found that have menu-driven attributes. Please use the Back button and choose a different type of object.");
94 } else {
95 Input i = new Input(StepAttributes.ATTRIBUTE_MODEL, "Data Model");
96 i.setValidValues(typesWithAttributes);
97 i.setDefaultValue(request.getParameter("DefaultModel"));
98 response.add(i);
99 }
100
101 Transition next = new Transition("next",
102 "Save and next: Choose menu field",
103 WizardStepController.class, PromptModelField.STATE_NAME);
104
105 next.addParam(WizStep.FLD_WIZID,
106 request.getParameter(WizStep.FLD_WIZID));
107 next.addParam(WizStep.FLD_STEP_TYPE, request.getParameter(WizStep.FLD_STEP_TYPE));
108 if (isEditing()) {
109 next.addParam(WizStep.FLD_ID, getStep().getId());
110 }
111 response.add(next);
112
113 response.add(buildCancelTransition());
114
115 }
116 }