1 package com.sri.emo.controller;
2
3 import com.jcorporate.expresso.core.controller.ControllerException;
4 import com.jcorporate.expresso.core.controller.ExpressoRequest;
5 import com.jcorporate.expresso.core.controller.ExpressoResponse;
6 import com.jcorporate.expresso.core.controller.Output;
7 import com.jcorporate.expresso.core.db.DBException;
8 import com.jcorporate.expresso.core.registry.RequestRegistry;
9 import com.jcorporate.expresso.services.dbobj.Setup;
10 import com.sri.common.controller.StateHandler;
11 import com.sri.common.taglib.TreeNode;
12 import com.sri.emo.EmoSchema;
13 import com.sri.emo.dbobj.Node;
14
15
16 /***
17 * This is a state handler that renders the node into a javascript-based
18 * tree menu.
19 *
20 * @author Michael Rimov
21 */
22 public class ViewNodeAsTree implements StateHandler {
23
24 /***
25 * The node to render.
26 */
27 private final Node renderNode;
28
29 /***
30 * The attribute in the request where the tree view data structure is
31 * stored. This is picked up by the TreeMenuTag for actual rendering.
32 */
33 public static final String TREE_VIEW_ATTRIBUTE = "TreeView";
34
35 /***
36 * Parameter for expand all comand.
37 */
38 public static final String PARAM_EXPAND_ALL = "ExpandAll";
39
40 /***
41 * Parameter for collapse all command.
42 */
43 public static final String PARAM_COLLAPSE_ALL = "CollapseAll";
44
45 /***
46 * Constructor that takes the node to render as a parameter.
47 *
48 * @param nodeToRender Node the node to render, must not be null.
49 */
50 public ViewNodeAsTree(Node nodeToRender) {
51
52 assert nodeToRender != null;
53
54 renderNode = nodeToRender;
55 }
56
57 /***
58 * Called to handle the request.
59 *
60 * @param request ExpressoRequest The Function's ExpressoRequest
61 * object.
62 * @param response ExpressoResponse The Function's ExpressoResponse
63 * object.
64 * @throws DBException upon underlying database exception error.
65 * @throws ControllerException upon underlying ControllerException error.
66 */
67 public void handleRequest(ExpressoRequest request, ExpressoResponse response) throws DBException,
68 ControllerException {
69 try {
70 assert request != null;
71 assert response != null;
72
73 response.add(new Output(Node.NODE_TITLE, renderNode.getNodeTitle()));
74
75
76
77 TreeViewFactory factory = new TreeViewFactory(renderNode,
78 response,
79 Integer.parseInt(Setup.getValueRequired(RequestRegistry.getDataContext(),
80 EmoSchema.class.getName(),
81 EmoSchema.TREEVIEW_NESTING_LIMIT)));
82
83
84 TreeNode treeNode = factory.buildTree();
85
86
87 if (request.getParameter(PARAM_EXPAND_ALL) != null) {
88 treeNode.expandAllFolders();
89 } else if (request.getParameter(PARAM_COLLAPSE_ALL) != null) {
90 treeNode.collapseAllFolders();
91 } else {
92 treeNode.setSelected(true);
93 }
94
95
96 request.getSession().setAttribute(TREE_VIEW_ATTRIBUTE, treeNode);
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143 } catch (Exception dbe) {
144 throw new ControllerException(dbe);
145 }
146
147 }
148 }