source: trunk/www/views/trashcan/index.jsp @ 3550

Last change on this file since 3550 was 3550, checked in by Martin Svensson, 16 years ago

Fixes #611 Improvements of Trashcan. Changed confusing button text/icon.
Browser is now redirected to an item's view page when the item has been restored from trashcan->view_item.jsp

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Id
File size: 8.7 KB
Line 
1<%-- $Id: index.jsp 3550 2007-07-05 11:18:28Z martin $
2  ------------------------------------------------------------------
3  Copyright (C) 2006 Nicklas Nordborg
4
5  This file is part of BASE - BioArray Software Environment.
6  Available at http://base.thep.lu.se/
7
8  BASE is free software; you can redistribute it and/or modify it
9  under the terms of the GNU General Public License as published by
10  the Free Software Foundation; either version 2 of the License, or
11  (at your option) any later version.
12
13  BASE is distributed in the hope that it will be useful, but
14  WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
21  02111-1307, USA. 
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.ItemContext"
32  import="net.sf.basedb.core.BasicItem"
33  import="net.sf.basedb.core.Removable"
34  import="net.sf.basedb.core.Permission"
35  import="net.sf.basedb.core.Trashcan"
36  import="net.sf.basedb.core.ItemInUseException"
37  import="net.sf.basedb.core.PermissionDeniedException"
38  import="net.sf.basedb.clients.web.Base"
39  import="net.sf.basedb.clients.web.WebException"
40  import="net.sf.basedb.util.Values"
41  import="net.sf.basedb.clients.web.util.HTML"
42  import="java.util.Enumeration"
43  import="java.util.Set"
44  import="java.util.HashSet"
45  import="java.util.List"
46  import="java.util.LinkedList"
47  import="java.util.Iterator"
48%>
49<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
50<%!
51  private static final ItemContext defaultContext = Base.createDefaultContext("", "type,name");
52  private static final Item itemType = Item.SYSTEM;
53  private static final String subContext = "trashcan";
54%>
55<%
56final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
57final String ID = sc.getId();
58final String cmd = request.getParameter("cmd");
59final String root = request.getContextPath()+"/";
60
61final String listPage = "list_trash.jsp?ID="+ID;
62final String viewPage = "view_item.jsp?ID="+ID;
63
64String forward = null;
65String redirect = null;
66String message = null;
67DbControl dc = null;
68
69try
70{
71  if (cmd == null || "List".equals(cmd))
72  {
73    // Display the list page without updatinging the current context
74    Base.getAndSetCurrentContext(sc, itemType, subContext, null, defaultContext);
75    redirect = listPage;
76  }
77  else if ("UpdateContext".equals(cmd))
78  {
79    // Display the list page after updating the current context from the request parameters
80    Base.getAndSetCurrentContext(sc, itemType, subContext, pageContext, defaultContext);
81    redirect = listPage;
82  }
83  else if ("LoadContext".equals(cmd))
84  {
85    // Display the list page after loading a saved context
86    int contextId = Values.getInt(request.getParameter("context"));
87    Base.loadContext(sc, contextId, defaultContext);
88    redirect = listPage;
89  }
90  else if ("DeleteItemsPermanently".equals(cmd))
91  {
92    // Delete all selected items on the list page
93    Enumeration<String> names = (Enumeration<String>)request.getParameterNames();
94    List<Removable> items = new LinkedList<Removable>();
95    dc = sc.newDbControl();
96    while (names.hasMoreElements())
97    {
98      String name = names.nextElement();
99      if (name.startsWith("item:"))
100      {
101        Item itemType = Item.valueOf(name.substring(5));
102        Integer[] ids = Values.getInt(request.getParameterValues(name));
103        for (int itemId : ids)
104        {
105          try
106          {
107            items.add((Removable)itemType.getById(dc, itemId));
108          }
109          catch (Throwable t)
110          {}
111        }
112      }
113    }
114    dc.close();
115   
116    int numTotal = items.size();
117    int numRemoved = Trashcan.delete(sc, items, false);
118
119    if (numTotal != numRemoved)
120    {
121      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be deleted, because you have no DELETE permission, or the item is used by other items";
122    }
123    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
124  }
125  else if ("DeleteItems".equals(cmd))
126  {
127    // Mark selected items as removed
128    dc = sc.newDbControl();
129    int numTotal = 0;
130    int numDeleted = 0;
131
132    Enumeration<String> names = (Enumeration<String>)request.getParameterNames();
133    while (names.hasMoreElements())
134    {
135      String name = names.nextElement();
136      if (name.startsWith("item:"))
137      {
138        Item itemType = Item.valueOf(name.substring(5));
139        Integer[] ids = Values.getInt(request.getParameterValues(name));
140        for (int itemId : ids)
141        {
142          numTotal++;
143          try
144          {
145            BasicItem item = itemType.getById(dc, itemId);
146            Removable r = (Removable)item;
147            r.setRemoved(true);
148            numDeleted++;
149          }
150          catch (PermissionDeniedException ex)
151          {}
152        }
153      }
154    }
155    dc.commit();
156    if (numTotal != numDeleted)
157    {
158      message = (numDeleted == 0 ? "No" : "Only "+numDeleted+" of "+numTotal) + " items could be deleted, because you have no DELETE permission";
159    }
160    Item itemType = Item.valueOf(request.getParameter("item_type"));
161    int itemId = Values.getInt(request.getParameter("item_id"));
162   
163    redirect = viewPage+"&item_type=" + itemType.name() + "&item_id="+itemId
164      +(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
165  }
166  else if ("RestoreItems".equals(cmd))
167  {
168    // Restore all selected items on the list or view page
169    dc = sc.newDbControl();
170    int numTotal = 0;
171    int numRestored = 0;
172
173    Enumeration<String> names = (Enumeration<String>)request.getParameterNames();
174    while (names.hasMoreElements())
175    {
176      String name = names.nextElement();
177      if (name.startsWith("item:"))
178      {
179        Item itemType = Item.valueOf(name.substring(5));
180        Integer[] ids = Values.getInt(request.getParameterValues(name));
181        for (int itemId : ids)
182        {
183          numTotal++;
184          try
185          {
186            BasicItem item = itemType.getById(dc, itemId);
187            Removable r = (Removable)item;
188            r.setRemoved(false);
189            numRestored++;
190          }
191          catch (PermissionDeniedException ex)
192          {}
193        }
194      }
195    }
196    dc.commit();
197   
198    if (numTotal != numRestored)
199    {
200      message = (numRestored == 0 ? "No" : "Only "+numRestored+" of "+numTotal) + " items could be restored, because you have no WRITE permission";
201    }
202
203    if (request.getParameter("item_type") != null)
204    {
205      Item itemType = Item.valueOf(request.getParameter("item_type"));
206      int itemId = Values.getInt(request.getParameter("item_id"));
207      redirect = viewPage + "&item_type=" + itemType.name() + "&item_id="+itemId;
208    }
209    else
210    {
211      redirect = listPage;
212    }
213    redirect += (message != null ? "&popmessage="+HTML.urlEncode(message) : "");
214  }
215  else if ("RestoreItem".equals(cmd))
216  {
217    // Restore the currently viewed item
218    dc = sc.newDbControl();
219    Item itemType = Item.valueOf(request.getParameter("item_type"));
220    int itemId = Values.getInt(request.getParameter("item_id"));
221    String itemsIndexPage = request.getParameter("itemsIndexPage");
222    BasicItem item = itemType.getById(dc, itemId);
223    Removable r = (Removable)item;
224    r.setRemoved(false);
225    dc.commit();
226    redirect = itemsIndexPage + "?ID=" + ID + "&cmd=ViewItem&item_id=" + itemId;
227  }
228  else if ("DeleteItem".equals(cmd))
229  {
230    // Delete the currently view item
231    dc = sc.newDbControl();
232    Item itemType = Item.valueOf(request.getParameter("item_type"));
233    int itemId = Values.getInt(request.getParameter("item_id"));
234    BasicItem item = itemType.getById(dc, itemId);
235    dc.deleteItem(item);
236    dc.commit();
237    redirect = listPage;
238  }
239  else if ("DeleteAllPermanently".equals(cmd))
240  {
241    dc = sc.newDbControl();
242    List<Removable> items = Trashcan.getItems(dc, null, 0, 0);
243    dc.close();
244    int numTotal = items.size();
245    int numRemoved = Trashcan.delete(sc, items, false);
246   
247    if (numTotal != numRemoved)
248    {
249      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be deleted, because you have no DELETE permission, or the item is used by other items";
250    }
251    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
252  }
253  else if ("ViewUsingItems".equals(cmd))
254  {
255    redirect = viewPage + "&item_type=" + request.getParameter("item_type") + "&item_id="+request.getParameter("item_id");
256  }
257  else
258  {
259    throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd);
260  }
261}
262finally
263{
264  if (dc != null) dc.close();
265}
266
267if (forward != null)
268{
269  pageContext.forward(forward);
270}
271else if (redirect != null)
272{
273  response.sendRedirect(redirect);
274}
275else if (message == null)
276{
277  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&wait=0");
278}
279else
280{
281  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&message="+HTML.urlEncode(message));
282}
283%>
284
Note: See TracBrowser for help on using the repository browser.