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.jcorporate.expresso.core.dbobj.RowSecuredDBObject;
6 import com.sri.common.controller.StateHandler;
7 import com.sri.emo.controller.PermissionController;
8 import com.sri.emo.dbobj.Node;
9 import com.sri.emo.dbobj.WizDefinition;
10 import com.sri.emo.wizard.WizDefinitionRepository;
11 import com.sri.emo.wizard.expressoimpl.WizardController;
12
13 import java.util.Iterator;
14 import java.util.List;
15
16 /***
17 * Lists all wizards available in the system that the person has 'read' permisison
18 * for. Also provides the appropriate links to manage/edit/delete the wizard.
19 *
20 * @author Michael Rimov
21 */
22 public class ListWizards extends WizardGatewayHandler implements StateHandler {
23
24 /***
25 * The state name for listing the wizards.
26 */
27 public static final String STATE_NAME = "listWizards";
28
29 /***
30 * The state description.
31 */
32 public static final String STATE_DESCRIPTION = "List all Wizards";
33
34 /***
35 * Constructs this particular state handler.
36 *
37 * @param handlerOwner Controller the controller that is the parent of this
38 * state handler.
39 * @param myRepository WizDefinitionRepository the repository to use
40 * for data access methods.
41 */
42 public ListWizards(final Controller handlerOwner, final WizDefinitionRepository myRepository) {
43 super(handlerOwner, myRepository);
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
59 if (this.getRepository().canRequesterAdd()) {
60 Transition addWizard = new Transition("add", "add wizard",
61 getOwner().getClass(), PromptAddWizard.STATE_NAME);
62 response.add(addWizard);
63 }
64
65
66 Block wizardList = new Block("WizardList");
67 wizardList.setDescription("Wizard");
68 response.add(wizardList);
69
70
71 Node node = new Node();
72 if (!node.canRequesterAdd()) {
73 wizardList.setAttribute("readonly", "readonly");
74 } else {
75 List results = this.getRepository().listAll(WizDefinition.FLD_NAME);
76 for (Iterator iterat = results.iterator(); iterat.hasNext();) {
77 WizDefinition oneDef = (WizDefinition) iterat.next();
78
79 Block oneRow = new Block("oneRow");
80 wizardList.add(oneRow);
81 if (oneDef.canRequesterWrite()) {
82 Transition delete = new Transition("delete", "delete", getOwner().getClass(),
83 PromptDeleteWizard.STATE_NAME);
84 delete.addParam(WizardController.WIZ_PARAMETER_ID, oneDef.getId());
85 oneRow.add(delete);
86
87 Transition edit = new Transition("edit", "edit", getOwner().getClass(), EditWizard.STATE_NAME);
88 edit.addParam(WizardController.WIZ_PARAMETER_ID, oneDef.getId());
89 oneRow.add(edit);
90 }
91
92
93 Transition run = getRunTransition(oneDef.getId());
94 oneRow.add(run);
95 oneRow.add(new Output("name", oneDef.getWizName()));
96 oneRow.add(new Output("summary", oneDef.getSummary()));
97
98 if (oneDef.canRequesterAdministrate()) {
99 oneRow.add(getPermissionManagerTransition(oneDef));
100 }
101 }
102 }
103 }
104
105 /***
106 * Retrieve the permission transition for the row secured dbobject.
107 *
108 * @param obj RowSecuredDBObject the object to get the transition for.
109 * @return Transition the transition for permissions.
110 */
111 public Transition getPermissionManagerTransition(final RowSecuredDBObject obj) {
112 Transition trans = new Transition("permit", PermissionController.class, PermissionController.PROMPT_EDIT_PERMS);
113 trans.addParam(PermissionController.KEY_FIELD_VALUES, obj.getKey());
114 trans.addParam(PermissionController.OBJ_TYPE, obj.getClass().getName());
115 return trans;
116 }
117
118 /***
119 * Builds a Transition for "Run a Wizard".
120 *
121 * @param id String the wizard id.
122 * @return Transition
123 */
124 private Transition getRunTransition(final String id) {
125 Transition run = new Transition("run", getOwner().getClass(), RunWizard.STATE_NAME);
126 run.addParam(WizardController.WIZ_PARAMETER_ID, id);
127 return run;
128 }
129 }