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.completion.management;
11  
12  import com.jcorporate.expresso.core.controller.*;
13  import com.jcorporate.expresso.core.db.DBException;
14  import com.sri.common.controller.StateHandler;
15  import com.sri.common.dbobj.ObjectNotFoundException;
16  import com.sri.emo.wizard.completion.CompletionRepository;
17  import com.sri.emo.wizard.completion.model.CompletionBean;
18  import com.sri.emo.wizard.expressoimpl.WizardController;
19  
20  
21  /***
22   * @author Michael Rimov
23   */
24  public class PromptEditWizard implements StateHandler {
25  
26      /***
27       * The name of this state as referred to by the Expresso Controller
28       */
29      public static final String STATE_NAME = "promptEditWizard";
30  
31      /***
32       * The description of this state as referred to by the Expresso Controller.
33       */
34      public static final String STATE_DESCRIPTION = "Edit Completion Wizard";
35  
36      /***
37       * Parent controller backreference.
38       */
39      private final Controller parent;
40  
41      /***
42       * The completion session manager.
43       */
44      private final ICompletionBeanManager beanManager;
45  
46  
47      /***
48       * Interface to the internal repository.
49       */
50      private final CompletionRepository completionRepository;
51  
52  
53      /***
54       * Constructor.  Assembles the parent controller, the completion repository
55       * and the state bean manager.
56       *
57       * @param stateOwner       Controller
58       * @param repository       CompletionRepository
59       * @param stateBeanManager ICompletionBeanManager
60       */
61      public PromptEditWizard(final Controller stateOwner, final CompletionRepository repository,
62                              final ICompletionBeanManager stateBeanManager) {
63          parent = stateOwner;
64          beanManager = stateBeanManager;
65          completionRepository = repository;
66      }
67  
68      /***
69       * Called to handle the request.
70       *
71       * @param request  ControllerRequest The Function's ControllerRequest
72       *                 object.
73       * @param response ControllerResponse The Function's ControllerResponse
74       *                 object.
75       * @throws DBException         upon underlying database exception error.
76       * @throws ControllerException upon underlying ControllerException error.
77       */
78      public void handleRequest(final ExpressoRequest request, final ExpressoResponse response) throws DBException,
79              ControllerException {
80  
81          int wizardId = Integer.parseInt(request.getParameter(WizardController.WIZ_PARAMETER_ID));
82          if (wizardId < 0) {
83              throw new ControllerException("Illegal Paramter for wizard id: " + wizardId);
84          }
85  
86          try {
87              CompletionBean completionBean = completionRepository.findById(wizardId);
88              beanManager.setCompletionBean(request, completionBean);
89          } catch (ObjectNotFoundException ex) {
90              throw new ControllerException("Unable to locate wizard by id: " + wizardId
91                      + " it may have been deleted by someone else.");
92          }
93  
94          Transition redirect = new Transition("", "", parent.getClass(), PromptChooseNode.STATE_NAME);
95          redirect.redirectTransition(request, response);
96      }
97  }