www/views/items/index.jsp

Code
Comments
Other
Rev Date Author Line
2902 14 Nov 06 nicklas 1 <%-- $Id$
2902 14 Nov 06 nicklas 2   ------------------------------------------------------------------
2902 14 Nov 06 nicklas 3   Copyright (C) 2006 Nicklas Nordborg
2902 14 Nov 06 nicklas 4
2902 14 Nov 06 nicklas 5   This file is part of BASE - BioArray Software Environment.
2902 14 Nov 06 nicklas 6   Available at http://base.thep.lu.se/
2902 14 Nov 06 nicklas 7
2902 14 Nov 06 nicklas 8   BASE is free software; you can redistribute it and/or modify it
2902 14 Nov 06 nicklas 9   under the terms of the GNU General Public License as published by
4476 05 Sep 08 jari 10   the Free Software Foundation; either version 3 of the License, or
2902 14 Nov 06 nicklas 11   (at your option) any later version.
2902 14 Nov 06 nicklas 12
2902 14 Nov 06 nicklas 13   BASE is distributed in the hope that it will be useful, but
2902 14 Nov 06 nicklas 14   WITHOUT ANY WARRANTY; without even the implied warranty of
2902 14 Nov 06 nicklas 15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
2902 14 Nov 06 nicklas 16   General Public License for more details.
2902 14 Nov 06 nicklas 17
2902 14 Nov 06 nicklas 18   You should have received a copy of the GNU General Public License
4511 11 Sep 08 jari 19   along with BASE. If not, see <http://www.gnu.org/licenses/>.
2902 14 Nov 06 nicklas 20   ------------------------------------------------------------------
2902 14 Nov 06 nicklas 21
2902 14 Nov 06 nicklas 22   @author Nicklas
2902 14 Nov 06 nicklas 23   @version 2.0
2902 14 Nov 06 nicklas 24 --%>
5426 24 Sep 10 nicklas 25 <%@ page pageEncoding="UTF-8" session="false"
2902 14 Nov 06 nicklas 26   import="net.sf.basedb.core.SessionControl"
2902 14 Nov 06 nicklas 27   import="net.sf.basedb.core.DbControl"
2902 14 Nov 06 nicklas 28   import="net.sf.basedb.core.Item"
2902 14 Nov 06 nicklas 29   import="net.sf.basedb.core.ItemContext"
2902 14 Nov 06 nicklas 30   import="net.sf.basedb.core.BasicItem"
2902 14 Nov 06 nicklas 31   import="net.sf.basedb.core.SharedItem"
4588 16 Oct 08 martin 32   import="net.sf.basedb.core.OwnedItem"
2902 14 Nov 06 nicklas 33   import="net.sf.basedb.core.Removable"
2902 14 Nov 06 nicklas 34   import="net.sf.basedb.core.Permission"
2902 14 Nov 06 nicklas 35   import="net.sf.basedb.core.User"
2902 14 Nov 06 nicklas 36   import="net.sf.basedb.core.MultiPermissions"
2902 14 Nov 06 nicklas 37   import="net.sf.basedb.core.PermissionDeniedException"
2902 14 Nov 06 nicklas 38   import="net.sf.basedb.clients.web.Base"
2902 14 Nov 06 nicklas 39   import="net.sf.basedb.clients.web.WebException"
2902 14 Nov 06 nicklas 40   import="net.sf.basedb.util.Values"
2902 14 Nov 06 nicklas 41   import="net.sf.basedb.clients.web.util.HTML"
2902 14 Nov 06 nicklas 42   import="java.util.Enumeration"
2902 14 Nov 06 nicklas 43   import="java.util.Set"
2902 14 Nov 06 nicklas 44   import="java.util.HashSet"
2902 14 Nov 06 nicklas 45   import="java.util.List"
2902 14 Nov 06 nicklas 46   import="java.util.Iterator"
2902 14 Nov 06 nicklas 47   import="java.util.Collections"
2902 14 Nov 06 nicklas 48 %>
2902 14 Nov 06 nicklas 49 <%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
2902 14 Nov 06 nicklas 50 <%!
2902 14 Nov 06 nicklas 51   private static final ItemContext defaultContext = Base.createDefaultContext("", "type,name");
2902 14 Nov 06 nicklas 52   private static final Item itemType = Item.SYSTEM;
2902 14 Nov 06 nicklas 53   private static final String subContext = "allitems";
2902 14 Nov 06 nicklas 54 %>
2902 14 Nov 06 nicklas 55 <%
2902 14 Nov 06 nicklas 56 final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
2902 14 Nov 06 nicklas 57 final String ID = sc.getId();
2902 14 Nov 06 nicklas 58 final String cmd = request.getParameter("cmd");
2902 14 Nov 06 nicklas 59 final String root = request.getContextPath()+"/";
2902 14 Nov 06 nicklas 60
2902 14 Nov 06 nicklas 61 final String listPage = "list_items.jsp?ID="+ID;
2902 14 Nov 06 nicklas 62
2902 14 Nov 06 nicklas 63 String forward = null;
2902 14 Nov 06 nicklas 64 String redirect = null;
2902 14 Nov 06 nicklas 65 String message = null;
2902 14 Nov 06 nicklas 66 DbControl dc = null;
2902 14 Nov 06 nicklas 67
2902 14 Nov 06 nicklas 68 try
2902 14 Nov 06 nicklas 69 {
2902 14 Nov 06 nicklas 70   if (cmd == null || "List".equals(cmd))
2902 14 Nov 06 nicklas 71   {
2902 14 Nov 06 nicklas 72     // Display the list page without updatinging the current context
2902 14 Nov 06 nicklas 73     Base.getAndSetCurrentContext(sc, itemType, subContext, null, defaultContext);
2902 14 Nov 06 nicklas 74     redirect = listPage;
2902 14 Nov 06 nicklas 75   }
2902 14 Nov 06 nicklas 76   else if ("UpdateContext".equals(cmd))
2902 14 Nov 06 nicklas 77   {
2902 14 Nov 06 nicklas 78     // Display the list page after updating the current context from the request parameters
2902 14 Nov 06 nicklas 79     Base.getAndSetCurrentContext(sc, itemType, subContext, pageContext, defaultContext);
2902 14 Nov 06 nicklas 80     redirect = listPage;
2902 14 Nov 06 nicklas 81   }
2902 14 Nov 06 nicklas 82   else if ("LoadContext".equals(cmd))
2902 14 Nov 06 nicklas 83   {
2902 14 Nov 06 nicklas 84     // Display the list page after loading a saved context
2902 14 Nov 06 nicklas 85     int contextId = Values.getInt(request.getParameter("context"));
2902 14 Nov 06 nicklas 86     Base.loadContext(sc, contextId, defaultContext);
2902 14 Nov 06 nicklas 87     redirect = listPage;
2902 14 Nov 06 nicklas 88   }
2902 14 Nov 06 nicklas 89   else if ("DeleteItems".equals(cmd))
2902 14 Nov 06 nicklas 90   {
2902 14 Nov 06 nicklas 91     // Mark selected items as removed
7954 12 May 21 nicklas 92     dc = sc.newDbControl(":Delete items");
2902 14 Nov 06 nicklas 93     int numTotal = 0;
2902 14 Nov 06 nicklas 94     int numDeleted = 0;
2902 14 Nov 06 nicklas 95
7601 22 Feb 19 nicklas 96     Enumeration<String> names = request.getParameterNames();
2902 14 Nov 06 nicklas 97     while (names.hasMoreElements())
2902 14 Nov 06 nicklas 98     {
2902 14 Nov 06 nicklas 99       String name = names.nextElement();
2902 14 Nov 06 nicklas 100       if (name.startsWith("item:"))
2902 14 Nov 06 nicklas 101       {
2902 14 Nov 06 nicklas 102         Item itemType = Item.valueOf(name.substring(5));
2902 14 Nov 06 nicklas 103         Integer[] ids = Values.getInt(request.getParameterValues(name));
2902 14 Nov 06 nicklas 104         for (int itemId : ids)
2902 14 Nov 06 nicklas 105         {
2902 14 Nov 06 nicklas 106           numTotal++;
2902 14 Nov 06 nicklas 107           try
2902 14 Nov 06 nicklas 108           {
2902 14 Nov 06 nicklas 109             BasicItem item = itemType.getById(dc, itemId);
2902 14 Nov 06 nicklas 110             if (item instanceof Removable)
2902 14 Nov 06 nicklas 111             {
2902 14 Nov 06 nicklas 112               Removable r = (Removable)item;
2902 14 Nov 06 nicklas 113               r.setRemoved(true);
2902 14 Nov 06 nicklas 114               numDeleted++;
2902 14 Nov 06 nicklas 115             }
2902 14 Nov 06 nicklas 116           }
2902 14 Nov 06 nicklas 117           catch (PermissionDeniedException ex)
2902 14 Nov 06 nicklas 118           {}
2902 14 Nov 06 nicklas 119         }
2902 14 Nov 06 nicklas 120       }
2902 14 Nov 06 nicklas 121     }
2902 14 Nov 06 nicklas 122     dc.commit();
2902 14 Nov 06 nicklas 123     if (numTotal != numDeleted)
2902 14 Nov 06 nicklas 124     {
2902 14 Nov 06 nicklas 125       message = (numDeleted == 0 ? "No" : "Only "+numDeleted+" of "+numTotal) + " items could be deleted, because you have no DELETE permission";
2902 14 Nov 06 nicklas 126     }
2902 14 Nov 06 nicklas 127     
6192 31 Oct 12 nicklas 128     redirect = listPage;
2902 14 Nov 06 nicklas 129   }
2902 14 Nov 06 nicklas 130   else if ("RestoreItems".equals(cmd))
2902 14 Nov 06 nicklas 131   {
2902 14 Nov 06 nicklas 132     // Restore all selected items on the list
7954 12 May 21 nicklas 133     dc = sc.newDbControl(":Retore items");
2902 14 Nov 06 nicklas 134     int numTotal = 0;
2902 14 Nov 06 nicklas 135     int numRestored = 0;
2902 14 Nov 06 nicklas 136
7601 22 Feb 19 nicklas 137     Enumeration<String> names = request.getParameterNames();
2902 14 Nov 06 nicklas 138     while (names.hasMoreElements())
2902 14 Nov 06 nicklas 139     {
2902 14 Nov 06 nicklas 140       String name = names.nextElement();
2902 14 Nov 06 nicklas 141       if (name.startsWith("item:"))
2902 14 Nov 06 nicklas 142       {
2902 14 Nov 06 nicklas 143         Item itemType = Item.valueOf(name.substring(5));
2902 14 Nov 06 nicklas 144         Integer[] ids = Values.getInt(request.getParameterValues(name));
2902 14 Nov 06 nicklas 145         for (int itemId : ids)
2902 14 Nov 06 nicklas 146         {
2902 14 Nov 06 nicklas 147           numTotal++;
2902 14 Nov 06 nicklas 148           try
2902 14 Nov 06 nicklas 149           {
2902 14 Nov 06 nicklas 150             BasicItem item = itemType.getById(dc, itemId);
2902 14 Nov 06 nicklas 151             if (item instanceof Removable)
2902 14 Nov 06 nicklas 152             {
2902 14 Nov 06 nicklas 153               Removable r = (Removable)item;
2902 14 Nov 06 nicklas 154               r.setRemoved(false);
2902 14 Nov 06 nicklas 155               numRestored++;
2902 14 Nov 06 nicklas 156             }
2902 14 Nov 06 nicklas 157           }
2902 14 Nov 06 nicklas 158           catch (PermissionDeniedException ex)
2902 14 Nov 06 nicklas 159           {}
2902 14 Nov 06 nicklas 160         }
2902 14 Nov 06 nicklas 161       }
2902 14 Nov 06 nicklas 162     }
2902 14 Nov 06 nicklas 163     dc.commit();
2902 14 Nov 06 nicklas 164     
2902 14 Nov 06 nicklas 165     if (numTotal != numRestored)
2902 14 Nov 06 nicklas 166     {
2902 14 Nov 06 nicklas 167       message = (numRestored == 0 ? "No" : "Only "+numRestored+" of "+numTotal) + " items could be restored, because you have no WRITE permission";
2902 14 Nov 06 nicklas 168     }
2902 14 Nov 06 nicklas 169
2902 14 Nov 06 nicklas 170     redirect = listPage;
2902 14 Nov 06 nicklas 171   }
2902 14 Nov 06 nicklas 172   else if ("ShareItems".equals(cmd))
2902 14 Nov 06 nicklas 173   {
2902 14 Nov 06 nicklas 174     // Share all selected items on the list page
7954 12 May 21 nicklas 175     dc = sc.newDbControl(":Share items");
2902 14 Nov 06 nicklas 176     ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, subContext, pageContext, defaultContext);
2902 14 Nov 06 nicklas 177
2902 14 Nov 06 nicklas 178     Set<SharedItem> ss = new HashSet<SharedItem>();
7601 22 Feb 19 nicklas 179     Enumeration<String> names = request.getParameterNames();
2902 14 Nov 06 nicklas 180     while (names.hasMoreElements())
2902 14 Nov 06 nicklas 181     {
2902 14 Nov 06 nicklas 182       String name = names.nextElement();
2902 14 Nov 06 nicklas 183       if (name.startsWith("item:"))
2902 14 Nov 06 nicklas 184       {
2902 14 Nov 06 nicklas 185         Item itemType = Item.valueOf(name.substring(5));
2902 14 Nov 06 nicklas 186         if (SharedItem.class.isAssignableFrom(itemType.getItemClass()))
2902 14 Nov 06 nicklas 187         {
2902 14 Nov 06 nicklas 188           Integer[] ids = Values.getInt(request.getParameterValues(name));
2902 14 Nov 06 nicklas 189           for (int itemId : ids)
2902 14 Nov 06 nicklas 190           {
2902 14 Nov 06 nicklas 191             ss.add((SharedItem)itemType.getById(dc, itemId));
2902 14 Nov 06 nicklas 192           }
2902 14 Nov 06 nicklas 193         }
2902 14 Nov 06 nicklas 194       }
2902 14 Nov 06 nicklas 195     }
2902 14 Nov 06 nicklas 196     if (ss.size() == 0)
2902 14 Nov 06 nicklas 197     {
2902 14 Nov 06 nicklas 198       throw new WebException("popup", "No shareable items selected",
2902 14 Nov 06 nicklas 199         "The items you selected can't be shared.");
2902 14 Nov 06 nicklas 200     }
2902 14 Nov 06 nicklas 201     cc.setObject("MultiPermissions", new MultiPermissions(ss));
2902 14 Nov 06 nicklas 202     dc.close();
2902 14 Nov 06 nicklas 203     redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name()+"&subcontext="+subContext;
2902 14 Nov 06 nicklas 204   }
2902 14 Nov 06 nicklas 205   else if ("ShareItem".equals(cmd))
2902 14 Nov 06 nicklas 206   {
2902 14 Nov 06 nicklas 207     // Display a popup window for sharing a single item
2902 14 Nov 06 nicklas 208     ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, subContext, pageContext, defaultContext);
7954 12 May 21 nicklas 209     dc = sc.newDbControl(":Share item");
2902 14 Nov 06 nicklas 210     Item type = Item.valueOf(request.getParameter("item_type"));
2902 14 Nov 06 nicklas 211     int itemId = Values.getInt(request.getParameter("item_id"));
2902 14 Nov 06 nicklas 212     MultiPermissions permissions = 
2902 14 Nov 06 nicklas 213       new MultiPermissions(Collections.singleton((SharedItem)type.getById(dc, itemId)));
2902 14 Nov 06 nicklas 214     dc.close();
2902 14 Nov 06 nicklas 215     cc.setObject("MultiPermissions", permissions);
2902 14 Nov 06 nicklas 216     redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name()+"&subcontext="+subContext;
2902 14 Nov 06 nicklas 217   }
4588 16 Oct 08 martin 218   else if ("SetOwnerOfItems".equals(cmd))
4588 16 Oct 08 martin 219   {
4588 16 Oct 08 martin 220     // Change owner of items selected on a list page
7954 12 May 21 nicklas 221     dc = sc.newDbControl(":Set owner of items");
4588 16 Oct 08 martin 222     ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
4588 16 Oct 08 martin 223     Set<OwnedItem> items = new HashSet<OwnedItem>();
7601 22 Feb 19 nicklas 224     Enumeration<String> names = request.getParameterNames();
4588 16 Oct 08 martin 225     //User user = User.getById(dc, sc.getLoggedInUserId());
4588 16 Oct 08 martin 226     while (names.hasMoreElements())
4588 16 Oct 08 martin 227     {
4588 16 Oct 08 martin 228       String name = names.nextElement();
4588 16 Oct 08 martin 229       if (name.startsWith("item:"))
4588 16 Oct 08 martin 230       {
4588 16 Oct 08 martin 231         Item itemType = Item.valueOf(name.substring(5));
4588 16 Oct 08 martin 232         Integer[] ids = Values.getInt(request.getParameterValues(name));
4588 16 Oct 08 martin 233         for (int itemId : ids)
4588 16 Oct 08 martin 234         {
4588 16 Oct 08 martin 235           BasicItem item = itemType.getById(dc, itemId);
4588 16 Oct 08 martin 236           if (item instanceof OwnedItem && item.hasPermission(Permission.SET_OWNER))
4588 16 Oct 08 martin 237           {
4588 16 Oct 08 martin 238             items.add((OwnedItem)item);
4588 16 Oct 08 martin 239           }
4588 16 Oct 08 martin 240         }
4588 16 Oct 08 martin 241       }
4588 16 Oct 08 martin 242     }
4588 16 Oct 08 martin 243     dc.close();
4588 16 Oct 08 martin 244     cc.setObject("OwnedItems", items);
4588 16 Oct 08 martin 245     redirect = "../../common/ownership/ownership.jsp?ID="+ID+"&item_type="+itemType.name();
4588 16 Oct 08 martin 246   }
2902 14 Nov 06 nicklas 247   else
2902 14 Nov 06 nicklas 248   {
2902 14 Nov 06 nicklas 249     throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd);
2902 14 Nov 06 nicklas 250   }
2902 14 Nov 06 nicklas 251 }
2902 14 Nov 06 nicklas 252 finally
2902 14 Nov 06 nicklas 253 {
2902 14 Nov 06 nicklas 254   if (dc != null) dc.close();
2902 14 Nov 06 nicklas 255 }
2902 14 Nov 06 nicklas 256
2902 14 Nov 06 nicklas 257 if (forward != null)
2902 14 Nov 06 nicklas 258 {
6192 31 Oct 12 nicklas 259   sc.setSessionSetting("alert-message", message);
2902 14 Nov 06 nicklas 260   pageContext.forward(forward);
2902 14 Nov 06 nicklas 261 }
2902 14 Nov 06 nicklas 262 else if (redirect != null)
2902 14 Nov 06 nicklas 263 {
6192 31 Oct 12 nicklas 264   sc.setSessionSetting("alert-message", message);
2902 14 Nov 06 nicklas 265   response.sendRedirect(redirect);
2902 14 Nov 06 nicklas 266 }
2902 14 Nov 06 nicklas 267 else if (message == null)
2902 14 Nov 06 nicklas 268 {
2902 14 Nov 06 nicklas 269   response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&wait=0");
2902 14 Nov 06 nicklas 270 }
2902 14 Nov 06 nicklas 271 else
2902 14 Nov 06 nicklas 272 {
2902 14 Nov 06 nicklas 273   response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&message="+HTML.urlEncode(message));
2902 14 Nov 06 nicklas 274 }
2902 14 Nov 06 nicklas 275 %>
2902 14 Nov 06 nicklas 276