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 org.apache.log4j.Logger;
22  import com.sri.emo.wizard.creation.model.*;
23  
24  /***
25   * @author Michael Rimov
26   * @version 1.0
27   */
28  public class DoChooseNodeStep implements StateHandler {
29  
30      /***
31       * The name of this state as referred to by the Expresso Controller
32       */
33      public static final String STATE_NAME = "DoChooseNode";
34  
35      /***
36       * The description of this state as referred to by the Expresso Controller.
37       */
38      public static final String STATE_DESCRIPTION = "Handle Choose Node";
39  
40      /***
41       * Parent controller backreference.
42       */
43      private final Controller owner;
44  
45      /***
46       * Delegating helper methods.
47       */
48      private final CreationBeanManager beanManager;
49  
50      /***
51       * Constructor that takes the owner controller as a paramter.
52       *
53       * @param parent Controller the owning controller.
54       */
55      public DoChooseNodeStep(Controller parent) {
56          owner = parent;
57          beanManager = new CreationBeanManager();
58      }
59  
60      /***
61       * Called to handle the request.
62       *
63       * @param request  ControllerRequest The Function's ControllerRequest
64       *                 object.
65       * @param response ControllerResponse The Function's ControllerResponse
66       *                 object.
67       * @throws DBException         upon underlying database exception error.
68       * @throws ControllerException upon underlying ControllerException error.
69       */
70      public void handleRequest(final ExpressoRequest request, final ExpressoResponse response) throws DBException,
71              ControllerException {
72  
73          ErrorCollection ec = request.getErrorCollection();
74          if (ec == null) {
75              ec = new ErrorCollection();
76          }
77  
78          /***
79           * @todo rich: make sure that we only need to do this for the very first node!
80           */
81          System.out.println("getting the FIRST creation bean!!!");
82          CreationBeans beans = beanManager.createOrRetrieveActionForm(request);
83          CreationBean bean = beans.getCreationBean(0);
84  
85          String value = request.getParameter("targetId");
86          if (value != null && value.length() > 0) {
87              try {
88                  bean.setTargetId(Integer.parseInt(value));
89                  bean.setDynamicTarget(true);
90              } catch (NumberFormatException ex1) {
91                  ec.addError("Field 'Target Id' must be a valid integer.");
92              }
93          }
94  
95          value = request.getParameter("Title");
96          if (value != null && value.length() > 0) {
97              beans.setWizardTitle(value);
98          }
99  
100         beans.setSummary(request.getParameter("Summary"));
101 
102         String wizardClass = request.getParameter("WizardClass");
103         if (wizardClass == null || wizardClass.length() == 0) {
104             ec.addError("You need to specify a Wizard Class.");
105         } else {
106             try {
107                 beans.setWizardClass(wizardClass);
108             } catch (ClassNotFoundException ex2) {
109                 Logger.getLogger(DoChooseNodeStep.class).error("Error setting wizard class.", ex2);
110                 ec.addError("The wizard class: " + wizardClass + " appears to be invalid.  "
111                         + "Contact your system administrator if this is in error. (The entire error has been logged)");
112             }
113         }
114 
115         ec = beanManager.validate(request, ec);
116         if (ec.size() > 0) {
117             response.saveErrors(ec);
118             Transition prompt = new Transition(PromptChooseNode.STATE_NAME, owner);
119             prompt.executeTransition(request, response);
120             return;
121         }
122 
123         Transition redirect = new Transition(PromptChooseParts.STATE_NAME, owner);
124         redirect.redirectTransition(request, response);
125     }
126 }