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

Last change on this file since 3655 was 3655, checked in by Nicklas Nordborg, 16 years ago

Fixes #720: Progress reporter when emptying trashcan

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Id
File size: 9.0 KB
Line 
1<%-- $Id: index.jsp 3655 2007-08-10 12:22:57Z nicklas $
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.core.SimpleProgressReporter"
39  import="net.sf.basedb.clients.web.Base"
40  import="net.sf.basedb.clients.web.WebException"
41  import="net.sf.basedb.util.Values"
42  import="net.sf.basedb.clients.web.util.HTML"
43  import="java.util.Enumeration"
44  import="java.util.Set"
45  import="java.util.HashSet"
46  import="java.util.List"
47  import="java.util.LinkedList"
48  import="java.util.Iterator"
49%>
50<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
51<%!
52  private static final ItemContext defaultContext = Base.createDefaultContext("", "type,name");
53  private static final Item itemType = Item.SYSTEM;
54  private static final String subContext = "trashcan";
55%>
56<%
57final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
58final String ID = sc.getId();
59final String cmd = request.getParameter("cmd");
60final String root = request.getContextPath()+"/";
61
62final String listPage = "list_trash.jsp?ID="+ID;
63final String viewPage = "view_item.jsp?ID="+ID;
64
65String forward = null;
66String redirect = null;
67String message = null;
68DbControl dc = null;
69
70try
71{
72  if (cmd == null || "List".equals(cmd))
73  {
74    // Display the list page without updatinging the current context
75    Base.getAndSetCurrentContext(sc, itemType, subContext, null, defaultContext);
76    redirect = listPage;
77  }
78  else if ("UpdateContext".equals(cmd))
79  {
80    // Display the list page after updating the current context from the request parameters
81    Base.getAndSetCurrentContext(sc, itemType, subContext, pageContext, defaultContext);
82    redirect = listPage;
83  }
84  else if ("LoadContext".equals(cmd))
85  {
86    // Display the list page after loading a saved context
87    int contextId = Values.getInt(request.getParameter("context"));
88    Base.loadContext(sc, contextId, defaultContext);
89    redirect = listPage;
90  }
91  else if ("DeleteItemsPermanently".equals(cmd))
92  {
93    // Delete all selected items on the list page
94    Enumeration<String> names = (Enumeration<String>)request.getParameterNames();
95    List<Removable> items = new LinkedList<Removable>();
96    dc = sc.newDbControl();
97    while (names.hasMoreElements())
98    {
99      String name = names.nextElement();
100      if (name.startsWith("item:"))
101      {
102        Item itemType = Item.valueOf(name.substring(5));
103        Integer[] ids = Values.getInt(request.getParameterValues(name));
104        for (int itemId : ids)
105        {
106          try
107          {
108            items.add((Removable)itemType.getById(dc, itemId));
109          }
110          catch (Throwable t)
111          {}
112        }
113      }
114    }
115    dc.close();
116   
117    int numTotal = items.size();
118    SimpleProgressReporter progress = new SimpleProgressReporter(null);
119    sc.setSessionSetting("progress.trashcan", progress);
120    int numRemoved = Trashcan.delete(sc, items, false, progress);
121
122    if (numTotal != numRemoved)
123    {
124      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";
125    }
126    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
127  }
128  else if ("DeleteItems".equals(cmd))
129  {
130    // Mark selected items as removed
131    dc = sc.newDbControl();
132    int numTotal = 0;
133    int numDeleted = 0;
134
135    Enumeration<String> names = (Enumeration<String>)request.getParameterNames();
136    while (names.hasMoreElements())
137    {
138      String name = names.nextElement();
139      if (name.startsWith("item:"))
140      {
141        Item itemType = Item.valueOf(name.substring(5));
142        Integer[] ids = Values.getInt(request.getParameterValues(name));
143        for (int itemId : ids)
144        {
145          numTotal++;
146          try
147          {
148            BasicItem item = itemType.getById(dc, itemId);
149            Removable r = (Removable)item;
150            r.setRemoved(true);
151            numDeleted++;
152          }
153          catch (PermissionDeniedException ex)
154          {}
155        }
156      }
157    }
158    dc.commit();
159    if (numTotal != numDeleted)
160    {
161      message = (numDeleted == 0 ? "No" : "Only "+numDeleted+" of "+numTotal) + " items could be deleted, because you have no DELETE permission";
162    }
163    Item itemType = Item.valueOf(request.getParameter("item_type"));
164    int itemId = Values.getInt(request.getParameter("item_id"));
165   
166    redirect = viewPage+"&item_type=" + itemType.name() + "&item_id="+itemId
167      +(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
168  }
169  else if ("RestoreItems".equals(cmd))
170  {
171    // Restore all selected items on the list or view page
172    dc = sc.newDbControl();
173    int numTotal = 0;
174    int numRestored = 0;
175
176    Enumeration<String> names = (Enumeration<String>)request.getParameterNames();
177    while (names.hasMoreElements())
178    {
179      String name = names.nextElement();
180      if (name.startsWith("item:"))
181      {
182        Item itemType = Item.valueOf(name.substring(5));
183        Integer[] ids = Values.getInt(request.getParameterValues(name));
184        for (int itemId : ids)
185        {
186          numTotal++;
187          try
188          {
189            BasicItem item = itemType.getById(dc, itemId);
190            Removable r = (Removable)item;
191            r.setRemoved(false);
192            numRestored++;
193          }
194          catch (PermissionDeniedException ex)
195          {}
196        }
197      }
198    }
199    dc.commit();
200   
201    if (numTotal != numRestored)
202    {
203      message = (numRestored == 0 ? "No" : "Only "+numRestored+" of "+numTotal) + " items could be restored, because you have no WRITE permission";
204    }
205
206    if (request.getParameter("item_type") != null)
207    {
208      Item itemType = Item.valueOf(request.getParameter("item_type"));
209      int itemId = Values.getInt(request.getParameter("item_id"));
210      redirect = viewPage + "&item_type=" + itemType.name() + "&item_id="+itemId;
211    }
212    else
213    {
214      redirect = listPage;
215    }
216    redirect += (message != null ? "&popmessage="+HTML.urlEncode(message) : "");
217  }
218  else if ("RestoreItem".equals(cmd))
219  {
220    // Restore the currently viewed item
221    dc = sc.newDbControl();
222    Item itemType = Item.valueOf(request.getParameter("item_type"));
223    int itemId = Values.getInt(request.getParameter("item_id"));
224    String itemsIndexPage = request.getParameter("itemsIndexPage");
225    BasicItem item = itemType.getById(dc, itemId);
226    Removable r = (Removable)item;
227    r.setRemoved(false);
228    dc.commit();
229    redirect = itemsIndexPage + "?ID=" + ID + "&cmd=ViewItem&item_id=" + itemId;
230  }
231  else if ("DeleteItem".equals(cmd))
232  {
233    // Delete the currently view item
234    dc = sc.newDbControl();
235    Item itemType = Item.valueOf(request.getParameter("item_type"));
236    int itemId = Values.getInt(request.getParameter("item_id"));
237    BasicItem item = itemType.getById(dc, itemId);
238    dc.deleteItem(item);
239    dc.commit();
240    redirect = listPage;
241  }
242  else if ("DeleteAllPermanently".equals(cmd))
243  {
244    dc = sc.newDbControl();
245    List<Removable> items = Trashcan.getItems(dc, null, 0, 0);
246    dc.close();
247    int numTotal = items.size();
248    SimpleProgressReporter progress = new SimpleProgressReporter(null);
249    sc.setSessionSetting("progress.trashcan", progress);
250    int numRemoved = Trashcan.delete(sc, items, false, progress);
251   
252    if (numTotal != numRemoved)
253    {
254      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";
255    }
256    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
257  }
258  else if ("ViewUsingItems".equals(cmd))
259  {
260    redirect = viewPage + "&item_type=" + request.getParameter("item_type") + "&item_id="+request.getParameter("item_id");
261  }
262  else
263  {
264    throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd);
265  }
266}
267finally
268{
269  if (dc != null) dc.close();
270}
271
272if (forward != null)
273{
274  pageContext.forward(forward);
275}
276else if (redirect != null)
277{
278  response.sendRedirect(redirect);
279}
280else if (message == null)
281{
282  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&wait=0");
283}
284else
285{
286  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&message="+HTML.urlEncode(message));
287}
288%>
289
Note: See TracBrowser for help on using the repository browser.