View Javadoc

1   /* ===================================================================
2    * Copyright 2002-05 SRI International.
3    * Released under the MOZILLA PUBLIC LICENSE Version 1.1
4    * which can be obtained at http://www.mozilla.org/MPL/MPL-1.1.html
5    * This software is distributed on an "AS IS"
6    * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.
7    * See the License for the specific language governing rights and
8    * limitations under the License.
9    * =================================================================== */
10  package com.sri.emo.wizard.creation.management;
11  
12  import com.jcorporate.expresso.core.controller.Controller;
13  import com.jcorporate.expresso.core.controller.ControllerException;
14  import com.jcorporate.expresso.core.controller.ErrorCollection;
15  import com.jcorporate.expresso.core.controller.ExpressoRequest;
16  import com.jcorporate.expresso.core.controller.ExpressoResponse;
17  import com.jcorporate.expresso.core.controller.Transition;
18  import com.jcorporate.expresso.core.db.DBException;
19  import com.sri.common.controller.StateHandler;
20  import com.sri.emo.wizard.creation.model.CreationBean;
21  import com.sri.emo.wizard.creation.model.CreationPartsBean;
22  import com.sri.emo.wizard.creation.model.FieldCompletion;
23  
24  import java.util.Iterator;
25  import com.sri.emo.wizard.creation.model.*;
26  
27  /***
28   * @author Michael Rimov
29   */
30  public class DoChooseParts implements StateHandler {
31  
32      /***
33       * The name of this state as referred to by the Expresso Controller
34       */
35      public static final String STATE_NAME = "doChooseParts";
36  
37      /***
38       * The description of this state as referred to by the Expresso Controller.
39       */
40      public static final String STATE_DESCRIPTION = "Process Parts Choices";
41  
42      /***
43       * Parent controller backreference.
44       */
45      private final Controller owner;
46  
47      /***
48       * Delegating helper methods.
49       */
50      private final CreationBeanManager beanManager;
51  
52      public DoChooseParts(final Controller stateOwner) {
53          this.owner = stateOwner;
54          beanManager = new CreationBeanManager();
55      }
56  
57  
58      /***
59       * Called to handle the request.
60       *
61       * @param request  ControllerRequest The Function's ControllerRequest
62       *                 object.
63       * @param response ControllerResponse The Function's ControllerResponse
64       *                 object.
65       * @throws DBException         upon underlying database exception error.
66       * @throws ControllerException upon underlying ControllerException error.
67       */
68      public void handleRequest(final ExpressoRequest request, final ExpressoResponse response) throws DBException,
69              ControllerException {
70  
71          if (beanManager.handleSessionTimeout(request, response)) {
72              return;
73          }
74  
75          ErrorCollection ec = request.getErrorCollection();
76          if (ec == null) {
77              ec = new ErrorCollection();
78          }
79  
80          CreationBeans wizardBeans = beanManager.getActionForm(request);
81          /***
82           * @todo rich: make sure that this part works
83           */
84          CreationBean wizardBean = wizardBeans.getCurrentBean();
85          boolean foundAtLeastOneWizardSetting = false;
86          for (Iterator i = wizardBean.getCompletionParts().iterator(); i.hasNext();) {
87              final CreationPartsBean onePart = (CreationPartsBean) i.next();
88              String parameterValue = request.getParameter(onePart.getPart().getPartNum());
89              if (parameterValue == null || parameterValue.length() == 0) {
90                  parameterValue = FieldCompletion.NOT_INCLUDED.toString();
91              }
92  
93              if (parameterValue.equalsIgnoreCase(FieldCompletion.WIZARD.toString())) {
94                  onePart.setFieldCompletion(FieldCompletion.WIZARD);
95                  foundAtLeastOneWizardSetting = true;
96              } else if (parameterValue.equalsIgnoreCase(FieldCompletion.NOT_INCLUDED.toString())) {
97                  onePart.setFieldCompletion(FieldCompletion.NOT_INCLUDED);
98              } else {
99                  throw new ControllerException("Unknown radio position: " + parameterValue + " for part number: "
100                         + onePart.getPart().getPartNum());
101             }
102         }
103 
104         if (!foundAtLeastOneWizardSetting) {
105             ec.addError("Select at least one value that the wizard will complete, or cancel this wizard.");
106         }
107 
108         ec = beanManager.validate(request, ec);
109         if (ec.size() > 0) {
110             response.saveErrors(ec);
111             Transition prompt = new Transition(PromptChooseParts.STATE_NAME, owner);
112             prompt.executeTransition(request, response);
113             return;
114         }
115 
116         Transition redirect = new Transition(PromptChooseCriteria.STATE_NAME, owner);
117         redirect.redirectTransition(request, response);
118     }
119 }