source: trunk/www/admin/groups/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: 12.3 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.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<%
91final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
92final String ID = sc.getId();
93final String cmd = request.getParameter("cmd");
94final String root = request.getContextPath()+"/";
95final String mode = request.getParameter("mode");
96final String callback = request.getParameter("callback");
97final String itemId = request.getParameter("item_id");
98final String listPage = "list_groups.jsp?ID="+ID
99  +(mode == null ? "" : "&mode="+mode)
100  +(callback == null ? "" : "&callback="+callback)
101  +(itemId == null ? "" : "&item_id="+itemId);
102final String viewPage = "view_group.jsp?ID="+ID;
103final String editPage = "edit_group.jsp?ID="+ID;
104
105String forward = null;
106String redirect = null;
107String message = null;
108DbControl dc = null;
109
110try
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);
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    int quotaId = Values.getInt(request.getParameter("quota_id"), -1);
176    if (quotaId >= 0) // < 0 = denied or unchanged
177    {
178      group.setQuota(quotaId == 0 ? null : Quota.getById(dc, quotaId));
179    }
180   
181    String[] removeUsers = Values.getString(request.getParameter("removeUsers")).split(",");
182    for (int i = 0; i < removeUsers.length; ++i)
183    {
184      int userId = Values.getInt(removeUsers[i], -1);
185      if (userId != -1) group.removeUser(User.getById(dc, userId));
186    }
187   
188    String[] addUsers = Values.getString(request.getParameter("addUsers")).split(",");
189    for (int i = 0; i < addUsers.length; ++i)
190    {
191      int userId = Values.getInt(addUsers[i], -1);
192      if (userId != -1) group.addUser(User.getById(dc, userId));
193    }
194
195    String[] removeGroups = Values.getString(request.getParameter("removeGroups")).split(",");
196    for (int i = 0; i < removeGroups.length; ++i)
197    {
198      int groupId = Values.getInt(removeGroups[i], -1);
199      if (groupId != -1) group.removeGroup(Group.getById(dc, groupId));
200    }
201   
202    String[] addGroups = Values.getString(request.getParameter("addGroups")).split(",");
203    for (int i = 0; i < addGroups.length; ++i)
204    {
205      int groupId = Values.getInt(addGroups[i], -1);
206      if (groupId != -1) group.addGroup(Group.getById(dc, groupId));
207    }
208    dc.commit();
209    cc.removeObject("item");
210  }
211  else if ("DeleteItem".equals(cmd))
212  {
213    // Delete a single item and then return to the view page
214    dc = sc.newDbControl();
215    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
216    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), true);
217    dc.commit();
218    redirect = viewPage;
219  }
220  else if ("DeleteItems".equals(cmd))
221  {
222    // Delete all selected items on the list page
223    dc = sc.newDbControl();
224    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
225    int numTotal = cc.getSelected().size();
226    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), true);
227    dc.commit();
228    if (numTotal != numRemoved)
229    {
230      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be deleted, because you have no DELETE permission";
231    }
232    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
233  }
234  else if ("RestoreItem".equals(cmd))
235  {
236    // Restore a single item and then return to the view page
237    dc = sc.newDbControl();
238    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
239    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), false);
240    dc.commit();
241    redirect = viewPage;
242  }
243  else if ("RestoreItems".equals(cmd))
244  {
245    // Restore all selected items on the list page
246    dc = sc.newDbControl();
247    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
248    int numTotal = cc.getSelected().size();
249    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), false);
250    dc.commit();
251    if (numTotal != numRemoved)
252    {
253      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be restored, because you have no WRITE permission";
254    }
255    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
256  }
257  else if ("ExportItems".equals(cmd))
258  {
259    // Run an export plugin in a list context
260    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
261    final ItemQuery<Group> query = Group.getQuery();
262    cc.configureQuery(query, true);
263    cc.setQuery(query);
264    registerExportUtils(cc);
265    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Export+groups";
266  }
267  else if ("ExportItem".equals(cmd))
268  {
269    // Run an export plugin in single-item context
270    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
271    registerExportUtils(cc);
272    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Export+group";
273  }
274  else if ("ImportItems".equals(cmd))
275  {
276    // Run an import plugin in a list context
277    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
278    final ItemQuery<Group> query = Group.getQuery();
279    cc.configureQuery(query, true);
280    cc.setQuery(query);
281    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Import+groups";
282  }
283  else if ("ImportItem".equals(cmd))
284  {
285    // Run an import plugin in single-item context
286    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
287    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Import+group";
288  }
289  else if ("RunListPlugin".equals(cmd))
290  {
291    // Run another plugin in a list context
292    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
293    final ItemQuery<Group> query = Group.getQuery();
294    cc.configureQuery(query, true);
295    cc.setQuery(query);
296    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&main_type=OTHER&title=Run+plugin";
297  }
298  else if ("RunPlugin".equals(cmd))
299  {
300    // Run another plugin in single-item context
301    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
302    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&main_type=OTHER&title=Run+plugin";
303  }
304  else
305  {
306    throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd);
307  }
308}
309finally
310{
311  if (dc != null) dc.close();
312}
313
314if (forward != null)
315{
316  pageContext.forward(forward);
317}
318else if (redirect != null)
319{
320  response.sendRedirect(redirect);
321}
322else if (message == null)
323{
324  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&wait=0");
325}
326else
327{
328  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&message="+HTML.urlEncode(message));
329}
330%>
331
Note: See TracBrowser for help on using the repository browser.