1 package com.sri.emo.wizard.wizardgateway;
2
3 import com.jcorporate.expresso.core.controller.*;
4 import com.jcorporate.expresso.core.db.DBException;
5 import com.sri.common.controller.StateHandler;
6 import com.sri.common.dbobj.ObjectNotFoundException;
7 import com.sri.emo.dbobj.WizDefinition;
8 import com.sri.emo.wizard.IWizardManager;
9 import com.sri.emo.wizard.WizDefinitionRepository;
10 import com.sri.emo.wizard.expressoimpl.WizardController;
11 import org.apache.log4j.Logger;
12
13 /***
14 * Edits a wizard. In reality it queries the Wizard Definition and redirects
15 * to the appropriate editing controller as specified in the definition.
16 *
17 * @author Michael Rimov
18 */
19 public class EditWizard extends WizardGatewayHandler implements StateHandler {
20
21 /***
22 * Name of the State for which this handler deals with.
23 */
24 public static final String STATE_NAME = "editWizard";
25
26
27 /***
28 * Friendly description of this handler.
29 */
30 public static final String STATE_DESCRIPTION = "Edit A Wizard";
31
32
33 /***
34 * Constructs this particular state handler.
35 *
36 * @param handlerOwner Controller the controller that is the parent of this
37 * state handler.
38 * @param myRepository WizDefinitionRepository the repository to use
39 * for data access methods.
40 */
41 public EditWizard(final Controller handlerOwner, final WizDefinitionRepository myRepository) {
42 super(handlerOwner, myRepository);
43 }
44
45
46 /***
47 * Called to handle the request.
48 *
49 * @param request ControllerRequest The Function's ControllerRequest
50 * object.
51 * @param response ControllerResponse The Function's ControllerResponse
52 * object.
53 * @throws DBException upon underlying database exception error.
54 * @throws ControllerException upon underlying ControllerException error.
55 */
56 public void handleRequest(final ExpressoRequest request, final ExpressoResponse response) throws DBException,
57 ControllerException {
58 int id = Integer.parseInt(request.getParameter(WizardController.WIZ_PARAMETER_ID));
59
60 WizDefinition definition = null;
61 try {
62 definition = getRepository().findById(id);
63 } catch (ObjectNotFoundException ex) {
64 Logger.getLogger(EditWizard.class).error("Unable to locate wizard by id", ex);
65 response.addError(
66 "Unable to locate wizard by id: " + id + " please contact your system administrator for assistance.");
67 Transition returnToPrompt = new Transition("prompt", "", this.getOwner().getClass(),
68 ListWizards.STATE_NAME);
69 returnToPrompt.redirectTransition(request, response);
70 return;
71 }
72
73
74 Controller targetController = definition.getManagingController();
75 if (!(targetController instanceof IWizardManager)) {
76 throw new ControllerException("Class: " + targetController.getClass().getName()
77 + " must implement interface: IWizardManager");
78 }
79
80 ((IWizardManager) targetController).edit(definition, request, response);
81
82 }
83 }