source: trunk/www/admin/roles/index.jsp @ 4906

Last change on this file since 4906 was 4906, checked in by Martin Svensson, 13 years ago

References #1128 More item-pages updated

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 11.5 KB
Line 
1<%-- $Id: index.jsp 4906 2009-04-24 13:53:14Z martin $
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.Role"
33  import="net.sf.basedb.core.RoleKey"
34  import="net.sf.basedb.core.User"
35  import="net.sf.basedb.core.ItemQuery"
36  import="net.sf.basedb.core.ItemResultList"
37  import="net.sf.basedb.core.Permission"
38  import="net.sf.basedb.core.ItemContext"
39  import="net.sf.basedb.core.MultiPermissions"
40  import="net.sf.basedb.core.PermissionDeniedException"
41  import="net.sf.basedb.core.ItemAlreadyExistsException"
42  import="net.sf.basedb.core.query.Expressions"
43  import="net.sf.basedb.core.query.Hql"
44  import="net.sf.basedb.core.query.Orders"
45  import="net.sf.basedb.core.query.Restrictions"
46  import="net.sf.basedb.util.RemovableUtil"
47  import="net.sf.basedb.util.formatter.NameableFormatter"
48  import="net.sf.basedb.clients.web.Base"
49  import="net.sf.basedb.clients.web.PermissionUtil"
50  import="net.sf.basedb.clients.web.WebException"
51  import="net.sf.basedb.util.Values"
52  import="net.sf.basedb.clients.web.util.HTML"
53  import="net.sf.basedb.clients.web.plugins.ItemQueryLoader"
54  import="java.util.Enumeration"
55  import="java.util.Set"
56  import="java.util.HashSet"
57  import="java.util.List"
58  import="java.util.ArrayList"
59  import="java.util.Collections"
60%>
61<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
62<%!
63  private static final ItemContext defaultContext = Base.createDefaultContext("name", "name,isDefault,description");
64  private static final Item itemType = Item.ROLE;
65 
66  private static void registerExportUtils(ItemContext cc)
67  {
68    // Register formatter
69    cc.setObject("export.formatter.&users(name)", new NameableFormatter());
70   
71    // Register dataloader
72    String roleParameter = "role";
73    ItemQuery<User> userQuery = User.getQuery();
74    userQuery.include(cc.getInclude());
75    userQuery.join(Hql.innerJoin("roles", "rle"));
76    userQuery.restrict(Restrictions.eq(Hql.alias("rle"), Expressions.parameter(roleParameter)));
77    userQuery.order(Orders.asc(Hql.property("name")));
78    cc.setObject("export.dataloader.&users(name)", new ItemQueryLoader(userQuery, roleParameter));
79  }
80%>
81<%
82final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
83final String ID = sc.getId();
84final String cmd = request.getParameter("cmd");
85final String root = request.getContextPath()+"/";
86final String mode = request.getParameter("mode");
87final String callback = request.getParameter("callback");
88final String itemId = request.getParameter("item_id");
89final String listPage = "list_roles.jsp?ID="+ID
90  +(mode == null ? "" : "&mode="+mode)
91  +(callback == null ? "" : "&callback="+callback)
92  +(itemId == null ? "" : "&item_id="+itemId);
93final String viewPage = "view_role.jsp?ID="+ID;
94final String editPage = "edit_role.jsp?ID="+ID;
95
96String forward = null;
97String redirect = null;
98String message = null;
99DbControl dc = null;
100
101try
102{
103  if (cmd == null || "List".equals(cmd))
104  {
105    // Display the list page without updatinging the current context
106    Base.getAndSetCurrentContext(sc, itemType, null, defaultContext, true);
107    redirect = listPage;
108  }
109  else if ("UpdateContext".equals(cmd))
110  {
111    // Display the list page after updating the current context from the request parameters
112    Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
113    redirect = listPage;
114  }
115  else if ("LoadContext".equals(cmd))
116  {
117    // Display the list page after loading a saved context
118    int contextId = Values.getInt(request.getParameter("context"));
119    Base.loadContext(sc, contextId, defaultContext);
120    redirect = listPage;
121  }
122
123  else if ("ViewItem".equals(cmd))
124  {
125    // Display the view page for a single item
126    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
127    forward = viewPage;
128  }
129  else if ("EditItem".equals(cmd))
130  {
131    // Display the edit page for a single item (should be opened in a popup)
132    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
133    redirect = editPage;
134  }
135  else if ("NewItem".equals(cmd))
136  {
137    // Display the edit page for a new item (should be opened in a popup)
138    if (!sc.hasPermission(Permission.CREATE, itemType))
139    {
140      throw new PermissionDeniedException(Permission.CREATE, itemType.toString());
141    }
142    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
143    cc.setId(0);
144    redirect = editPage;
145  }
146  else if ("UpdateItem".equals(cmd))
147  {
148    // Update the properties on an item (will close the popup)
149    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, null, defaultContext);
150    dc = sc.newDbControl();
151    Role role = (Role)cc.getObject("item");
152    if (role == null)
153    {
154      role = Role.getNew(dc);
155      message = "Role created";
156      dc.saveItem(role);
157    }
158    else
159    {
160      dc.reattachItem(role);
161      message = "Role updated";
162    }
163    role.setName(Values.getStringOrNull(request.getParameter("name")));
164    role.setDescription(Values.getStringOrNull(request.getParameter("description")));
165    role.setDefault(Values.getBoolean(request.getParameter("is_default"), false));
166   
167    ItemQuery<RoleKey> q = RoleKey.getQuery();
168    q.setCacheResult(true);
169    ItemResultList<RoleKey> roleKeys = q.list(dc);
170    for (RoleKey key : roleKeys)
171    {
172      String name = key.getItemType().name();
173      Set<Permission> permissions = PermissionUtil.getPermissions(Values.getInt(request.getParameter(name)));
174      key.setPermissions(role, permissions);
175    }
176   
177    String[] removeUsers = Values.getString(request.getParameter("removeUsers")).split(",");
178    for (int i = 0; i < removeUsers.length; ++i)
179    {
180      int userId = Values.getInt(removeUsers[i], -1);
181      if (userId != -1) role.removeUser(User.getById(dc, userId));
182    }
183   
184    String[] addUsers = Values.getString(request.getParameter("addUsers")).split(",");
185    for (int i = 0; i < addUsers.length; ++i)
186    {
187      int userId = Values.getInt(addUsers[i], -1);
188      if (userId != -1) role.addUser(User.getById(dc, userId));
189    }
190   
191    dc.commit();
192    cc.removeObject("item");
193  }
194  else if ("DeleteItem".equals(cmd))
195  {
196    // Delete a single item and then return to the view page
197    dc = sc.newDbControl();
198    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
199    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), true);
200    dc.commit();
201    redirect = viewPage;
202  }
203  else if ("DeleteItems".equals(cmd))
204  {
205    // Delete all selected items on the list page
206    dc = sc.newDbControl();
207    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
208    int numTotal = cc.getSelected().size();
209    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), true);
210    dc.commit();
211    if (numTotal != numRemoved)
212    {
213      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be deleted, because you have no DELETE permission";
214    }
215    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
216  }
217  else if ("RestoreItem".equals(cmd))
218  {
219    // Restore a single item and then return to the view page
220    dc = sc.newDbControl();
221    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
222    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), false);
223    dc.commit();
224    redirect = viewPage;
225  }
226  else if ("RestoreItems".equals(cmd))
227  {
228    // Restore all selected items on the list page
229    dc = sc.newDbControl();
230    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
231    int numTotal = cc.getSelected().size();
232    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), false);
233    dc.commit();
234    if (numTotal != numRemoved)
235    {
236      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be restored, because you have no WRITE permission";
237    }
238    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
239  }
240  else if ("ExportItems".equals(cmd))
241  {
242    // Run an export plugin in a list context
243    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
244    final ItemQuery<Role> query = Role.getQuery();
245    cc.configureQuery(query, true);
246    cc.setQuery(query);
247    registerExportUtils(cc);
248    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Export+roles";
249  }
250  else if ("ExportItem".equals(cmd))
251  {
252    // Run an export plugin in single-item context
253    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
254    registerExportUtils(cc);
255    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Export+role";
256  }
257  else if ("ImportItems".equals(cmd))
258  {
259    // Run an import plugin in a list context
260    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
261    final ItemQuery<Role> query = Role.getQuery();
262    cc.configureQuery(query, true);
263    cc.setQuery(query);
264    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Import+roles";
265  }
266  else if ("ImportItem".equals(cmd))
267  {
268    // Run an import plugin in single-item context
269    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
270    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Import+role";
271  }
272  else if ("RunListPlugin".equals(cmd))
273  {
274    // Run another plugin in a list context
275    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
276    final ItemQuery<Role> query = Role.getQuery();
277    cc.configureQuery(query, true);
278    cc.setQuery(query);
279    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&main_type=OTHER&title=Run+plugin";
280  }
281  else if ("RunPlugin".equals(cmd))
282  {
283    // Run another plugin in single-item context
284    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
285    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&main_type=OTHER&title=Run+plugin";
286  }
287  else
288  {
289    throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd);
290  }
291}
292finally
293{
294  if (dc != null) dc.close();
295}
296
297if (forward != null)
298{
299  pageContext.forward(forward);
300}
301else if (redirect != null)
302{
303  response.sendRedirect(redirect);
304}
305else if (message == null)
306{
307  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&wait=0");
308}
309else
310{
311  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&message="+HTML.urlEncode(message));
312}
313%>
314
Note: See TracBrowser for help on using the repository browser.