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.core.dbobj.ValidValue;
6   import com.sri.common.controller.StateHandler;
7   import com.sri.emo.dbobj.Node;
8   import com.sri.emo.dbobj.Part;
9   import com.sri.emo.dbobj.StepAttributes;
10  import com.sri.emo.dbobj.WizStep;
11  
12  import java.util.ArrayList;
13  import java.util.List;
14  
15  /***
16   * Prompts the Node Attribute to use for the picklist.
17   *
18   * @author Michael Rimov
19   */
20  public class PromptNodeAttribute extends EditFinalState implements StateHandler {
21  
22      /***
23       * Constant for access to this state.
24       */
25      public static final String STATE_NAME = "promptNodeAttribute";
26  
27      /***
28       * Constant for description of this state.
29       */
30      public static final String STATE_DESCRIPTION = "Choose Attribute of Instance";
31  
32      /***
33       * Constructor that takes a wizard id and step id to handle the state.
34       *
35       * @param wizId  String the wizard id.
36       * @param stepId String the wizard's step id (may be null for new steps)
37       * @throws DBException upon error.
38       */
39      public PromptNodeAttribute(final String wizId, final String stepId) throws DBException {
40          super(wizId, stepId);
41      }
42  
43      /***
44       * Called to handle the request.
45       *
46       * @param request  ExpressoRequest The Function's ExpressoRequest
47       *                 object.
48       * @param response ExpressoResponse The Function's ExpressoResponse
49       *                 object.
50       * @throws DBException         upon underlying database exception error.
51       * @throws ControllerException upon underlying ControllerException error.
52       */
53      public void handleRequest(final ExpressoRequest request, final ExpressoResponse response)
54              throws DBException, ControllerException {
55          ErrorCollection ec = request.getErrorCollection();
56          if (ec == null) {
57              ec = new ErrorCollection();
58          }
59  
60          WizStep step = getStepFromParameters(request, ec);
61  
62          if (ec.getErrorCount() > 0) {
63              response.saveErrors(ec);
64          }
65  
66          if (isNeedTransitionOnError(ec, response)) {
67              transitionOnError(WizardStepController.class, PromptNodeInstance.STATE_NAME, request, response);
68              return;
69          }
70  
71          String title = "Select Attribute for " + getStepTitle();
72          response.setTitle(title);
73  
74          //Render the common items
75          Block commonDef = renderCommonControls(step, request, response);
76          response.add(commonDef);
77  
78          //Add the per-instance fields available
79          Input attributes = new Input();
80          attributes.setName(StepAttributes.ATTRIBUTE_INSTANCE_FIELD);
81          attributes.setDefaultValue(step.getStepAttribute(StepAttributes.ATTRIBUTE_INSTANCE_FIELD));
82          commonDef.add(attributes);
83  
84          addStepTypeLabel(response, step);
85          if (isEditing()) {
86              Transition edit = buildEditTypeTransition(step);
87              response.add(edit);
88          }
89          //Add 'save' transition
90          Transition save = buildSaveAndReturnTransition(step);
91          save.addParam(StepAttributes.ATTRIBUTE_INSTANCE_ID,
92                  step.getStepAttribute(StepAttributes.ATTRIBUTE_INSTANCE_ID));
93          response.add(save);
94  
95          //Add the Cancel Transition.
96          response.add(buildCancelTransition());
97  
98          //Set the node instance name
99          Output instance = new Output();
100         instance.setName(StepAttributes.ATTRIBUTE_INSTANCE_ID);
101         Node node = new Node();
102         node.setField(Node.NODE_ID, step.getStepAttribute(StepAttributes.ATTRIBUTE_INSTANCE_ID));
103         try {
104             node.retrieve();
105         } catch (DBException ex) {
106 //            ec.addError("Could not find node of id: " + step.getStepAttribute(StepAttributes.ATTRIBUTE_INSTANCE_ID));
107 //            response.saveErrors(ec);
108             transitionOnError(WizardStepController.class, PromptNodeInstance.STATE_NAME, request, response);
109             return;
110         }
111 
112         instance.setContent(node.getNodeTitle());
113         response.add(instance);
114 
115         //Add the 'instance' Label
116         if (isEditing()) {
117             Transition edit = new Transition("editInstance", "Instance", WizardStepController.class,
118                     PromptNodeInstance.STATE_NAME);
119             edit.addParam(WizStep.FLD_STEP_TYPE, step.getStepType() + "");
120             edit.addParam(WizStep.FLD_WIZID, step.getWizId());
121             response.add(edit);
122         }
123         //Add the 'Model' Label
124         Output model = new Output();
125         model.setName(StepAttributes.ATTRIBUTE_MODEL);
126         model.setContent(node.getNodeType());
127         response.add(model);
128 
129         //Build the dropdown list.
130         List vv = new ArrayList();
131 
132         Part[] allParts = node.getParts();
133         if (allParts.length == 0) {
134             response.addError("No parts defined for this node type.");
135         } else {
136             for (int i = 0; i < allParts.length; i++) {
137                 Part aPart = allParts[i];
138                 if (aPart.isOwnedAttribute() && aPart.isMultipleAllowed()) {
139                     vv.add(new ValidValue(aPart.getId(), aPart.getPartLabel()));
140                 }
141             }
142 
143             if (vv.size() == 0) {
144                 response.addError("We were unable to locate any parts in the model that allowed"
145                         + " multiple attributes.  Please check the model documentation");
146             }
147         }
148 
149         attributes.setValidValues(vv);
150         response.add(attributes);
151     }
152 }