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.PickList;
8   import com.sri.emo.dbobj.StepAttributes;
9   import com.sri.emo.dbobj.WizStep;
10  
11  import java.util.ArrayList;
12  import java.util.List;
13  
14  /***
15   * Prompts for a picklist id to use.
16   *
17   * @author Michael Rimov
18   */
19  public class PromptPicklist extends EditFinalState implements StateHandler {
20  
21      /***
22       * Constant for access to this state.
23       */
24      public static final String STATE_NAME = "promptPicklist";
25  
26      /***
27       * Constant for description of this state.
28       */
29      public static final String STATE_DESCRIPTION = "Select Step Custom Picklist";
30  
31  
32      /***
33       * Constructor that takes a wizard id and a step id and forms its internal
34       * state, allowing for easy retrieval of database objects from subclasses.
35       *
36       * @param wizId  The Wizard ID (usually an integer)
37       * @param stepId The Step Id.  May be null if there is no step definition.
38       * @throws DBException upon error.
39       */
40      public PromptPicklist(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  ControllerRequest The Function's ControllerRequest
48       *                 object.
49       * @param response ControllerResponse The Function's ControllerResponse
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)
55              throws DBException, ControllerException {
56  
57          String title = "Define Custom Picklist Entry for " + getStepTitle();
58          response.setTitle(title);
59          response.add(new Output("pageTitle", title));
60  
61          ErrorCollection ec = request.getErrorCollection();
62          if (ec == null) {
63              ec = new ErrorCollection();
64          }
65  
66          WizStep step = getStepFromParameters(request, ec);
67          if (ec.getErrorCount() > 0) {
68              response.saveErrors(ec);
69          }
70  
71          //Render the common items
72          Block commonDef = renderCommonControls(step, request, response);
73          response.add(commonDef);
74  
75  
76          addStepTypeLabel(response, step);
77          if (isEditing()) {
78              Transition edit = buildEditTypeTransition(step);
79              response.add(edit);
80          }
81  
82          //Add 'save' transition
83          Transition save = buildSaveAndReturnTransition(step);
84          response.add(save);
85  
86          //Add the Cancel Transition.
87          response.add(buildCancelTransition());
88  
89          //Add the custom picklist Input
90          Input picklistInput = new Input(StepAttributes.ATTRIBUTE_PICKLIST_ID, "Custom Picklist ID");
91          String picklistValue = response.getFormCache(StepAttributes.ATTRIBUTE_PICKLIST_ID);
92          if (picklistValue == null || picklistValue.length() == 0) {
93              picklistValue = step.getStepAttribute(StepAttributes.ATTRIBUTE_PICKLIST_ID);
94              picklistInput.setDefaultValue(picklistValue);
95          }
96  
97          //Add the sample dropdown menu
98          if (isEditing() && picklistValue != null && picklistValue.length() > 0) {
99              PickList pl = new PickList();
100             pl.setPickAttribType(picklistValue);
101             List results = pl.searchAndRetrieveList(PickList.ORDER_NUM);
102             if (results.size() == 0) {
103                 ec.addError("Couldn't find any picklist items for id of: " + picklistValue);
104             } else {
105                 List sampleMenu = new ArrayList(results.size());
106                 for (int i = 0; i < results.size(); i++) {
107                     PickList onepick = (PickList) results.get(i);
108                     sampleMenu.add(new ValidValue("", onepick.getDisplayString()));
109                 }
110 
111                 //@todo TOTAL HACK TO FIX: Only attribute that will take a vector :)
112                 picklistInput.setValidValues(sampleMenu);
113 
114             }
115         }
116 
117         response.add(picklistInput);
118     }
119 }