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.Node;
7 import com.sri.emo.dbobj.StepAttributes;
8 import com.sri.emo.dbobj.WizStep;
9 import org.apache.log4j.Logger;
10
11 /***
12 * Prompts For The Node Instance to use for per-node steps.
13 *
14 * @author Michael Rimov
15 */
16 public class PromptNodeInstance extends AbstractStepHandler implements StateHandler {
17
18 /***
19 * Logs any specific errors that we catch and handle.
20 */
21 private static final Logger LOG = Logger.getLogger(PromptNodeInstance.class);
22
23 /***
24 * Constant for access to this state.
25 */
26 public static final String STATE_NAME = "promptNodeInstance";
27
28 /***
29 * Constant for description of this state.
30 */
31 public static final String STATE_DESCRIPTION = "Choose Instance";
32
33 /***
34 * Constructor that takes a wizard id and step id to handle the state.
35 *
36 * @param wizId String the wizard id.
37 * @param stepId String the wizard's step id (may be null for new steps)
38 * @throws DBException upon error.
39 */
40 public PromptNodeInstance(final String wizId, final String stepId) throws DBException {
41 super(wizId, stepId);
42 }
43
44 /***
45 * Called to handle the request.
46 *
47 * @param request ExpressoRequest The Function's ExpressoRequest
48 * object.
49 * @param response ExpressoResponse The Function's ExpressoResponse
50 * object.
51 * @throws DBException upon underlying database exception error.
52 * @throws ControllerException upon underlying ControllerException error.
53 */
54 public void handleRequest(final ExpressoRequest request, final ExpressoResponse response) throws DBException,
55 ControllerException {
56 ErrorCollection ec = request.getErrorCollection();
57 if (ec == null) {
58 ec = new ErrorCollection();
59 }
60
61 WizStep ws = getStepFromParameters(request, ec);
62 if (ec.getErrorCount() > 0) {
63 response.saveErrors(ec);
64 }
65
66 String title = "Choose an Instance (a \"Node\") for " + getStepTitle();
67 response.setTitle(title);
68
69 String nodeId = response.getFormCache(StepAttributes.ATTRIBUTE_INSTANCE_ID);
70 Input input = new Input(StepAttributes.ATTRIBUTE_INSTANCE_ID, "Node ID");
71 input.setName(StepAttributes.ATTRIBUTE_INSTANCE_ID);
72 if (response.getFormCache(StepAttributes.ATTRIBUTE_INSTANCE_ID).length() == 0) {
73 input.setDefaultValue(ws.getStepAttribute(StepAttributes.ATTRIBUTE_INSTANCE_ID));
74 } else {
75 input.setDefaultValue(response);
76 Node node = new Node();
77 node.setNodeId(nodeId);
78 try {
79 if (node.find()) {
80 input.setAttribute("Title", "(ID " + node.getNodeId() + " is \"" + node.getNodeTitle() + "\" )");
81 }
82 } catch (DBException ex) {
83 LOG.warn("Error finding node id: " + nodeId, ex);
84
85 }
86 }
87
88 response.add(input);
89
90 Transition next = new Transition("next",
91 "Save and next: Choose menu field",
92 WizardStepController.class, PromptNodeAttribute.STATE_NAME);
93
94 next.addParam(WizStep.FLD_WIZID,
95 request.getParameter(WizStep.FLD_WIZID));
96 next.addParam(WizStep.FLD_STEP_TYPE, request.getParameter(WizStep.FLD_STEP_TYPE));
97 if (isEditing()) {
98 next.addParam(WizStep.FLD_ID, getStep().getId());
99 next.addParam(StepAttributes.ATTRIBUTE_INSTANCE_ID, nodeId);
100 }
101 response.add(next);
102
103 response.add(buildCancelTransition());
104 }
105 }