source: trunk/www/views/devices/index.jsp @ 7452

Last change on this file since 7452 was 7452, checked in by Nicklas Nordborg, 4 years ago

References #2097: Implement support for device verification

Write email sending error to log file.

File size: 9.4 KB
Line 
1<%-- $Id: index.jsp 6192 2012-10-31 12:34:19Z nicklas $
2  ------------------------------------------------------------------
3  Copyright (C) 2006 Jari Häkkinen, Nicklas Nordborg, Martin Svensson
4  Copyright (C) 2007 Nicklas Nordborg
5
6  This file is part of BASE - BioArray Software Environment.
7  Available at http://base.thep.lu.se/
8
9  BASE is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License
11  as published by the Free Software Foundation; either version 3
12  of the License, or (at your option) any later version.
13
14  BASE is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  GNU General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with BASE. If not, see <http://www.gnu.org/licenses/>.
21  ------------------------------------------------------------------
22
23  @author Nicklas
24  @version 2.0
25--%>
26<%@ page pageEncoding="UTF-8" session="false"
27  import="net.sf.basedb.core.SessionControl"
28  import="net.sf.basedb.core.DbControl"
29  import="net.sf.basedb.core.Item"
30  import="net.sf.basedb.core.Type"
31  import="net.sf.basedb.core.Include"
32  import="net.sf.basedb.core.User"
33  import="net.sf.basedb.core.UserDevice"
34  import="net.sf.basedb.core.ItemQuery"
35  import="net.sf.basedb.core.Permission"
36  import="net.sf.basedb.core.ItemContext"
37  import="net.sf.basedb.core.ItemQuery"
38  import="net.sf.basedb.core.PermissionDeniedException"
39  import="net.sf.basedb.core.ItemAlreadyExistsException"
40  import="net.sf.basedb.util.RemovableUtil"
41  import="net.sf.basedb.clients.web.Base"
42  import="net.sf.basedb.clients.web.WebException"
43  import="net.sf.basedb.util.Values"
44  import="net.sf.basedb.clients.web.util.HTML"
45  import="net.sf.basedb.core.plugin.GuiContext"
46  import="net.sf.basedb.util.extensions.ExtensionsInvoker"
47  import="net.sf.basedb.clients.web.extensions.ExtensionsControl"
48  import="net.sf.basedb.clients.web.extensions.JspContext"
49  import="net.sf.basedb.clients.web.extensions.edit.EditUtil"
50  import="net.sf.basedb.clients.web.extensions.edit.OnSaveRenderer"
51  import="net.sf.basedb.clients.web.extensions.list.ListColumnExportRenderer"
52  import="net.sf.basedb.clients.web.extensions.list.ListColumnUtil"
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  import="java.util.Arrays"
59%>
60<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
61<%!
62  private static final ItemContext defaultContext = Base.createDefaultContext("name", "name,client,lastUsed,lastRemoteId,location,description");
63  private static final Item itemType = Item.USERDEVICE;
64%>
65<%
66final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
67final String ID = sc.getId();
68final String cmd = request.getParameter("cmd");
69final String root = request.getContextPath()+"/";
70final String mode = request.getParameter("mode");
71final String callback = request.getParameter("callback");
72final String itemId = request.getParameter("item_id");
73final String listPage = "list_devices.jsp?ID="+ID
74  +(mode == null ? "" : "&mode="+mode)
75  +(callback == null ? "" : "&callback="+callback)
76  +(itemId == null ? "" : "&item_id="+itemId);
77final String viewPage = "view_device.jsp?ID="+ID;
78final String editPage = "edit_device.jsp?ID="+ID;
79
80String forward = null;
81String redirect = null;
82String message = null;
83DbControl dc = null;
84
85try
86{
87  if (cmd == null || "List".equals(cmd))
88  {
89    // Display the list page without updatinging the current context
90    Base.getAndSetCurrentContext(sc, itemType, null, defaultContext, true);
91    redirect = listPage;
92  }
93  else if ("UpdateContext".equals(cmd))
94  {
95    // Display the list page after updating the current context from the request parameters
96    Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
97    redirect = listPage;
98  }
99  else if ("LoadContext".equals(cmd))
100  {
101    // Display the list page after loading a saved context
102    int contextId = Values.getInt(request.getParameter("context"));
103    Base.loadContext(sc, contextId, defaultContext);
104    redirect = listPage;
105  }
106
107  else if ("ViewItem".equals(cmd))
108  {
109    // Display the view page for a single item
110    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
111    forward = viewPage;
112  }
113  else if ("EditItem".equals(cmd))
114  {
115    // Display the edit page for a single item (should be opened in a popup)
116    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
117    redirect = editPage;
118  }
119  else if ("UpdateItem".equals(cmd))
120  {
121    // Update the properties on an item (will close the popup)
122    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, null, defaultContext);
123    final int maxRecent = Base.getMaxRecent(sc);
124    dc = sc.newDbControl();
125    UserDevice device = (UserDevice)cc.getObject("item");
126    if (device == null)
127    {
128      throw new PermissionDeniedException(Permission.CREATE, "device");
129    }
130    else
131    {
132      dc.reattachItem(device, false);
133      message = "Device updated";
134    }
135    JspContext jspContext = ExtensionsControl.createContext(dc, pageContext, GuiContext.item(Item.USERDEVICE), device);
136    ExtensionsInvoker invoker = EditUtil.useOnSaveExtensions(jspContext);
137    try
138    {
139      device.setName(Values.getStringOrNull(request.getParameter("name")));
140      device.setDescription(Values.getStringOrNull(request.getParameter("description")));
141     
142      // OnSave extensions
143      invoker.render(OnSaveRenderer.ON_SAVE);
144      dc.commit();
145      invoker.render(OnSaveRenderer.ON_COMMIT);
146    }
147    catch (Exception ex)
148    {
149      invoker.render(OnSaveRenderer.onRollback(ex));
150      throw ex;
151    }
152    finally
153    {
154      cc.removeObject("item");
155    }
156  }
157  else if ("DeleteItem".equals(cmd))
158  {
159    // Delete a single item and then return to the view page
160    dc = sc.newDbControl();
161    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
162    RemovableUtil.delete(dc, itemType, Collections.singleton(cc.getId()));
163    dc.commit();
164    redirect = listPage;
165  }
166  else if ("DeleteItems".equals(cmd))
167  {
168    // Delete all selected items on the list page
169    dc = sc.newDbControl();
170    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
171    int numTotal = cc.getSelected().size();
172    int numRemoved = RemovableUtil.delete(dc, itemType, cc.getSelected());
173    dc.commit();
174    if (numTotal != numRemoved)
175    {
176      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be deleted, because you have no DELETE permission";
177    }
178    redirect = listPage;
179  }
180  else if ("ExportItems".equals(cmd))
181  {
182    // Run an export plugin in a list context
183    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
184    dc = sc.newDbControl();
185    final User user = cc.getInclude().contains(Include.OTHERS) ?
186        null : User.getById(dc, sc.getLoggedInUserId());
187    final ItemQuery<UserDevice> query = UserDevice.getQuery(user);
188    cc.configureQuery(dc, query, true);
189    cc.setQuery(query);
190    JspContext jspContext = ExtensionsControl.createContext(dc, pageContext, GuiContext.list(itemType), null);
191    ExtensionsInvoker listInvoker = ListColumnUtil.useExtensions(jspContext);
192    listInvoker.render(new ListColumnExportRenderer(cc));
193    dc.close();
194    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Export+devices";
195  }
196  else if ("ExportItem".equals(cmd))
197  {
198    // Run an export plugin in single-item context
199    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
200    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Export+device";
201  }
202  else if ("RunListPlugin".equals(cmd))
203  {
204    // Run another plugin in a list context
205    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
206    dc = sc.newDbControl();
207    final User user = cc.getInclude().contains(Include.OTHERS) ?
208        null : User.getById(dc, sc.getLoggedInUserId());
209    final ItemQuery<UserDevice> query = UserDevice.getQuery(user);
210    cc.configureQuery(dc, query, false);
211    dc.close();
212    cc.setQuery(query);
213    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&main_type=OTHER&title=Run+plugin";
214  }
215  else if ("RunPlugin".equals(cmd))
216  {
217    // Run another plugin in single-item context
218    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
219    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&main_type=OTHER&title=Run+plugin";
220  }
221  else
222  {
223    throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd);
224  }
225}
226finally
227{
228  if (dc != null) dc.close();
229}
230
231if (forward != null)
232{
233  sc.setSessionSetting("alert-message", message);
234  pageContext.forward(forward);
235}
236else if (redirect != null)
237{
238  sc.setSessionSetting("alert-message", message);
239  response.sendRedirect(redirect);
240}
241else if (message == null)
242{
243  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&wait=0");
244}
245else
246{
247  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&message="+HTML.urlEncode(message));
248}
249%>
250
Note: See TracBrowser for help on using the repository browser.