1 | <%-- $Id: index.jsp 5077 2009-08-26 12:03:37Z nicklas $ |
---|
2 | ------------------------------------------------------------------ |
---|
3 | Copyright (C) 2005 Nicklas Nordborg |
---|
4 | Copyright (C) 2006 Jari Häkkinen, Nicklas Nordborg, Martin Svensson |
---|
5 | Copyright (C) 2007 Nicklas Nordborg |
---|
6 | |
---|
7 | This file is part of BASE - BioArray Software Environment. |
---|
8 | Available at http://base.thep.lu.se/ |
---|
9 | |
---|
10 | BASE is free software; you can redistribute it and/or |
---|
11 | modify it under the terms of the GNU General Public License |
---|
12 | as published by the Free Software Foundation; either version 3 |
---|
13 | of the License, or (at your option) any later version. |
---|
14 | |
---|
15 | BASE is distributed in the hope that it will be useful, |
---|
16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
18 | GNU General Public License for more details. |
---|
19 | |
---|
20 | You should have received a copy of the GNU General Public License |
---|
21 | along with BASE. If not, see <http://www.gnu.org/licenses/>. |
---|
22 | ------------------------------------------------------------------ |
---|
23 | |
---|
24 | @author Nicklas |
---|
25 | @version 2.0 |
---|
26 | --%> |
---|
27 | <%@ page session="false" |
---|
28 | import="net.sf.basedb.core.SessionControl" |
---|
29 | import="net.sf.basedb.core.DbControl" |
---|
30 | import="net.sf.basedb.core.Item" |
---|
31 | import="net.sf.basedb.core.Include" |
---|
32 | import="net.sf.basedb.core.Group" |
---|
33 | import="net.sf.basedb.core.User" |
---|
34 | import="net.sf.basedb.core.Quota" |
---|
35 | import="net.sf.basedb.core.ItemQuery" |
---|
36 | import="net.sf.basedb.core.Permission" |
---|
37 | import="net.sf.basedb.core.ItemContext" |
---|
38 | import="net.sf.basedb.core.MultiPermissions" |
---|
39 | import="net.sf.basedb.core.PermissionDeniedException" |
---|
40 | import="net.sf.basedb.core.ItemAlreadyExistsException" |
---|
41 | import="net.sf.basedb.core.query.Expressions" |
---|
42 | import="net.sf.basedb.core.query.Hql" |
---|
43 | import="net.sf.basedb.core.query.Orders" |
---|
44 | import="net.sf.basedb.core.query.Restrictions" |
---|
45 | import="net.sf.basedb.util.RemovableUtil" |
---|
46 | import="net.sf.basedb.clients.web.Base" |
---|
47 | import="net.sf.basedb.clients.web.WebException" |
---|
48 | import="net.sf.basedb.util.Values" |
---|
49 | import="net.sf.basedb.util.formatter.NameableFormatter" |
---|
50 | import="net.sf.basedb.clients.web.util.HTML" |
---|
51 | import="net.sf.basedb.clients.web.plugins.ItemQueryLoader" |
---|
52 | import="java.util.Enumeration" |
---|
53 | import="java.util.Set" |
---|
54 | import="java.util.HashSet" |
---|
55 | import="java.util.List" |
---|
56 | import="java.util.ArrayList" |
---|
57 | import="java.util.Collections" |
---|
58 | %> |
---|
59 | <%@ taglib prefix="base" uri="/WEB-INF/base.tld" %> |
---|
60 | <%! |
---|
61 | private static final ItemContext defaultContext = Base.createDefaultContext("name", "name,isDefault,description"); |
---|
62 | private static final Item itemType = Item.GROUP; |
---|
63 | |
---|
64 | private static void registerExportUtils(ItemContext cc) |
---|
65 | { |
---|
66 | // Register formatters |
---|
67 | cc.setObject("export.formatter.&children(name)", new NameableFormatter()); |
---|
68 | cc.setObject("export.formatter.&users(name)", new NameableFormatter()); |
---|
69 | |
---|
70 | |
---|
71 | // Register dataloaders |
---|
72 | String groupParameter = "group"; |
---|
73 | // Groups |
---|
74 | ItemQuery<Group> groupQuery = Group.getQuery(); |
---|
75 | groupQuery.include(cc.getInclude()); |
---|
76 | groupQuery.join(Hql.innerJoin("parents", "p")); |
---|
77 | groupQuery.restrict(Restrictions.eq(Hql.alias("p"), Expressions.parameter(groupParameter))); |
---|
78 | groupQuery.order(Orders.asc(Hql.property("name"))); |
---|
79 | cc.setObject("export.dataloader.&children(name)", new ItemQueryLoader(groupQuery, groupParameter)); |
---|
80 | |
---|
81 | // Users |
---|
82 | ItemQuery<User> userQuery = User.getQuery(); |
---|
83 | userQuery.include(cc.getInclude()); |
---|
84 | userQuery.join(Hql.innerJoin("groups", "grp")); |
---|
85 | userQuery.restrict(Restrictions.eq(Hql.alias("grp"), Expressions.parameter(groupParameter))); |
---|
86 | userQuery.order(Orders.asc(Hql.property("name"))); |
---|
87 | cc.setObject("export.dataloader.&users(name)", new ItemQueryLoader(userQuery, groupParameter)); |
---|
88 | } |
---|
89 | %> |
---|
90 | <% |
---|
91 | final SessionControl sc = Base.getExistingSessionControl(pageContext, true); |
---|
92 | final String ID = sc.getId(); |
---|
93 | final String cmd = request.getParameter("cmd"); |
---|
94 | final String root = request.getContextPath()+"/"; |
---|
95 | final String mode = request.getParameter("mode"); |
---|
96 | final String callback = request.getParameter("callback"); |
---|
97 | final String itemId = request.getParameter("item_id"); |
---|
98 | final String listPage = "list_groups.jsp?ID="+ID |
---|
99 | +(mode == null ? "" : "&mode="+mode) |
---|
100 | +(callback == null ? "" : "&callback="+callback) |
---|
101 | +(itemId == null ? "" : "&item_id="+itemId); |
---|
102 | final String viewPage = "view_group.jsp?ID="+ID; |
---|
103 | final String editPage = "edit_group.jsp?ID="+ID; |
---|
104 | |
---|
105 | String forward = null; |
---|
106 | String redirect = null; |
---|
107 | String message = null; |
---|
108 | DbControl dc = null; |
---|
109 | |
---|
110 | try |
---|
111 | { |
---|
112 | if (cmd == null || "List".equals(cmd)) |
---|
113 | { |
---|
114 | // Display the list page without updatinging the current context |
---|
115 | Base.getAndSetCurrentContext(sc, itemType, null, defaultContext, true); |
---|
116 | redirect = listPage; |
---|
117 | } |
---|
118 | else if ("UpdateContext".equals(cmd)) |
---|
119 | { |
---|
120 | // Display the list page after updating the current context from the request parameters |
---|
121 | Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext); |
---|
122 | redirect = listPage; |
---|
123 | } |
---|
124 | else if ("LoadContext".equals(cmd)) |
---|
125 | { |
---|
126 | // Display the list page after loading a saved context |
---|
127 | int contextId = Values.getInt(request.getParameter("context")); |
---|
128 | Base.loadContext(sc, contextId, defaultContext); |
---|
129 | redirect = listPage; |
---|
130 | } |
---|
131 | |
---|
132 | else if ("ViewItem".equals(cmd)) |
---|
133 | { |
---|
134 | // Display the view page for a single item |
---|
135 | ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext); |
---|
136 | forward = viewPage; |
---|
137 | } |
---|
138 | else if ("EditItem".equals(cmd)) |
---|
139 | { |
---|
140 | // Display the edit page for a single item (should be opened in a popup) |
---|
141 | ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext); |
---|
142 | redirect = editPage; |
---|
143 | } |
---|
144 | else if ("NewItem".equals(cmd)) |
---|
145 | { |
---|
146 | // Display the edit page for a new item (should be opened in a popup) |
---|
147 | if (!sc.hasPermission(Permission.CREATE, itemType)) |
---|
148 | { |
---|
149 | throw new PermissionDeniedException(Permission.CREATE, itemType.toString()); |
---|
150 | } |
---|
151 | ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext); |
---|
152 | cc.setId(0); |
---|
153 | forward = editPage; |
---|
154 | } |
---|
155 | else if ("UpdateItem".equals(cmd)) |
---|
156 | { |
---|
157 | // Update the properties on an item (will close the popup) |
---|
158 | ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, null, defaultContext); |
---|
159 | dc = sc.newDbControl(); |
---|
160 | Group group = (Group)cc.getObject("item"); |
---|
161 | if (group == null) |
---|
162 | { |
---|
163 | group = Group.getNew(dc); |
---|
164 | message = "Group created"; |
---|
165 | dc.saveItem(group); |
---|
166 | } |
---|
167 | else |
---|
168 | { |
---|
169 | dc.reattachItem(group, false); |
---|
170 | message = "Group updated"; |
---|
171 | } |
---|
172 | group.setName(Values.getStringOrNull(request.getParameter("name"))); |
---|
173 | group.setDescription(Values.getStringOrNull(request.getParameter("description"))); |
---|
174 | group.setDefault(Values.getBoolean(request.getParameter("is_default"), false)); |
---|
175 | group.setHiddenMembers(Values.getBoolean(request.getParameter("hidden_members"), false)); |
---|
176 | int quotaId = Values.getInt(request.getParameter("quota_id"), -1); |
---|
177 | if (quotaId >= 0) // < 0 = denied or unchanged |
---|
178 | { |
---|
179 | group.setQuota(quotaId == 0 ? null : Quota.getById(dc, quotaId)); |
---|
180 | } |
---|
181 | |
---|
182 | String[] removeUsers = Values.getString(request.getParameter("removeUsers")).split(","); |
---|
183 | for (int i = 0; i < removeUsers.length; ++i) |
---|
184 | { |
---|
185 | int userId = Values.getInt(removeUsers[i], -1); |
---|
186 | if (userId != -1) group.removeUser(User.getById(dc, userId)); |
---|
187 | } |
---|
188 | |
---|
189 | String[] addUsers = Values.getString(request.getParameter("addUsers")).split(","); |
---|
190 | for (int i = 0; i < addUsers.length; ++i) |
---|
191 | { |
---|
192 | int userId = Values.getInt(addUsers[i], -1); |
---|
193 | if (userId != -1) group.addUser(User.getById(dc, userId)); |
---|
194 | } |
---|
195 | |
---|
196 | String[] removeGroups = Values.getString(request.getParameter("removeGroups")).split(","); |
---|
197 | for (int i = 0; i < removeGroups.length; ++i) |
---|
198 | { |
---|
199 | int groupId = Values.getInt(removeGroups[i], -1); |
---|
200 | if (groupId != -1) group.removeGroup(Group.getById(dc, groupId)); |
---|
201 | } |
---|
202 | |
---|
203 | String[] addGroups = Values.getString(request.getParameter("addGroups")).split(","); |
---|
204 | for (int i = 0; i < addGroups.length; ++i) |
---|
205 | { |
---|
206 | int groupId = Values.getInt(addGroups[i], -1); |
---|
207 | if (groupId != -1) group.addGroup(Group.getById(dc, groupId)); |
---|
208 | } |
---|
209 | dc.commit(); |
---|
210 | cc.removeObject("item"); |
---|
211 | } |
---|
212 | else if ("DeleteItem".equals(cmd)) |
---|
213 | { |
---|
214 | // Delete a single item and then return to the view page |
---|
215 | dc = sc.newDbControl(); |
---|
216 | ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext); |
---|
217 | RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), true); |
---|
218 | dc.commit(); |
---|
219 | redirect = viewPage; |
---|
220 | } |
---|
221 | else if ("DeleteItems".equals(cmd)) |
---|
222 | { |
---|
223 | // Delete all selected items on the list page |
---|
224 | dc = sc.newDbControl(); |
---|
225 | ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext); |
---|
226 | int numTotal = cc.getSelected().size(); |
---|
227 | int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), true); |
---|
228 | dc.commit(); |
---|
229 | if (numTotal != numRemoved) |
---|
230 | { |
---|
231 | message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be deleted, because you have no DELETE permission"; |
---|
232 | } |
---|
233 | redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : ""); |
---|
234 | } |
---|
235 | else if ("RestoreItem".equals(cmd)) |
---|
236 | { |
---|
237 | // Restore a single item and then return to the view page |
---|
238 | dc = sc.newDbControl(); |
---|
239 | ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext); |
---|
240 | RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), false); |
---|
241 | dc.commit(); |
---|
242 | redirect = viewPage; |
---|
243 | } |
---|
244 | else if ("RestoreItems".equals(cmd)) |
---|
245 | { |
---|
246 | // Restore all selected items on the list page |
---|
247 | dc = sc.newDbControl(); |
---|
248 | ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext); |
---|
249 | int numTotal = cc.getSelected().size(); |
---|
250 | int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), false); |
---|
251 | dc.commit(); |
---|
252 | if (numTotal != numRemoved) |
---|
253 | { |
---|
254 | message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be restored, because you have no WRITE permission"; |
---|
255 | } |
---|
256 | redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : ""); |
---|
257 | } |
---|
258 | else if ("ExportItems".equals(cmd)) |
---|
259 | { |
---|
260 | // Run an export plugin in a list context |
---|
261 | ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext); |
---|
262 | final ItemQuery<Group> query = Group.getQuery(); |
---|
263 | cc.configureQuery(query, true); |
---|
264 | cc.setQuery(query); |
---|
265 | registerExportUtils(cc); |
---|
266 | redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Export+groups"; |
---|
267 | } |
---|
268 | else if ("ExportItem".equals(cmd)) |
---|
269 | { |
---|
270 | // Run an export plugin in single-item context |
---|
271 | ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext); |
---|
272 | registerExportUtils(cc); |
---|
273 | redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Export+group"; |
---|
274 | } |
---|
275 | else if ("ImportItems".equals(cmd)) |
---|
276 | { |
---|
277 | // Run an import plugin in a list context |
---|
278 | ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext); |
---|
279 | final ItemQuery<Group> query = Group.getQuery(); |
---|
280 | cc.configureQuery(query, true); |
---|
281 | cc.setQuery(query); |
---|
282 | redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Import+groups"; |
---|
283 | } |
---|
284 | else if ("ImportItem".equals(cmd)) |
---|
285 | { |
---|
286 | // Run an import plugin in single-item context |
---|
287 | ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext); |
---|
288 | redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Import+group"; |
---|
289 | } |
---|
290 | else if ("RunListPlugin".equals(cmd)) |
---|
291 | { |
---|
292 | // Run another plugin in a list context |
---|
293 | ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext); |
---|
294 | final ItemQuery<Group> query = Group.getQuery(); |
---|
295 | cc.configureQuery(query, true); |
---|
296 | cc.setQuery(query); |
---|
297 | redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&main_type=OTHER&title=Run+plugin"; |
---|
298 | } |
---|
299 | else if ("RunPlugin".equals(cmd)) |
---|
300 | { |
---|
301 | // Run another plugin in single-item context |
---|
302 | ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext); |
---|
303 | redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&main_type=OTHER&title=Run+plugin"; |
---|
304 | } |
---|
305 | else |
---|
306 | { |
---|
307 | throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd); |
---|
308 | } |
---|
309 | } |
---|
310 | finally |
---|
311 | { |
---|
312 | if (dc != null) dc.close(); |
---|
313 | } |
---|
314 | |
---|
315 | if (forward != null) |
---|
316 | { |
---|
317 | pageContext.forward(forward); |
---|
318 | } |
---|
319 | else if (redirect != null) |
---|
320 | { |
---|
321 | response.sendRedirect(redirect); |
---|
322 | } |
---|
323 | else if (message == null) |
---|
324 | { |
---|
325 | response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&wait=0"); |
---|
326 | } |
---|
327 | else |
---|
328 | { |
---|
329 | response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&message="+HTML.urlEncode(message)); |
---|
330 | } |
---|
331 | %> |
---|
332 | |
---|