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

Last change on this file since 7605 was 7605, checked in by Nicklas Nordborg, 3 years ago

References #2151: Pre-compile all JSP pages before releases

Getting rid of a lot of unchecked warnings. Most of them are solved by changed the API in several BASE core clases to use implict casting of return types instead of explicit (this moved the 'unchecked' warning to the API method from the caller). Example:

// Before
ItemContext cc = ...
Hardware hardware = (Hardware)cc.getObject("item");
// After
Hardware hardware = cc.getObject("item");

In most cases both the old and new version of the code will work, but if the returned object is using a "type" parameter it will not compile:

// This will not compile!
Set<AnnotatedItem> items = (Set<AnnotatedItem>)cc.getObject("AnnotatedItems");
// But this will!
Set<AnnotatedItem> items = cc.getObject("AnnotatedItems");

Note that existing compiled code will still work, but that some changes may be needed when compiling agains BASE 3.15. The issues should be easy to solve (eg. remove an explict cast).

Extensions that uses JSP files that works in BASE 3.14 may stop working in BASE 3.15 since the compilation of the JSP files happens on the BASE server as they are accessed.

Another issues is that there are still a lot of unchecked warnings related to the JSON library. This build on regular Map and List API:s but has not specified generic type parameters so there is no way to get rid of those warnings without fixing the JSON library source code. The latest released version is from 2012 so it is not likely that this should happen unless we do it ourselves (the warnings are really annoying so maybe we should!).

File size: 9.6 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.OnSaveAction"
51  import="net.sf.basedb.clients.web.extensions.edit.OnSaveRenderer"
52  import="net.sf.basedb.clients.web.extensions.list.ListColumnExportRenderer"
53  import="net.sf.basedb.clients.web.extensions.list.ListColumnAction"
54  import="net.sf.basedb.clients.web.extensions.list.ListColumnUtil"
55  import="java.util.Set"
56  import="java.util.HashSet"
57  import="java.util.List"
58  import="java.util.ArrayList"
59  import="java.util.Collections"
60  import="java.util.Arrays"
61%>
62<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
63<%!
64  private static final ItemContext defaultContext = Base.createDefaultContext("name", "name,client,lastUsed,lastRemoteId,location,description");
65  private static final Item itemType = Item.USERDEVICE;
66%>
67<%
68final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
69final String ID = sc.getId();
70final String cmd = request.getParameter("cmd");
71final String root = request.getContextPath()+"/";
72final String mode = request.getParameter("mode");
73final String callback = request.getParameter("callback");
74final String itemId = request.getParameter("item_id");
75final String listPage = "list_devices.jsp?ID="+ID
76  +(mode == null ? "" : "&mode="+mode)
77  +(callback == null ? "" : "&callback="+callback)
78  +(itemId == null ? "" : "&item_id="+itemId);
79final String viewPage = "view_device.jsp?ID="+ID;
80final String editPage = "edit_device.jsp?ID="+ID;
81
82String forward = null;
83String redirect = null;
84String message = null;
85DbControl dc = null;
86
87try
88{
89  if (cmd == null || "List".equals(cmd))
90  {
91    // Display the list page without updatinging the current context
92    Base.getAndSetCurrentContext(sc, itemType, null, defaultContext, true);
93    redirect = listPage;
94  }
95  else if ("UpdateContext".equals(cmd))
96  {
97    // Display the list page after updating the current context from the request parameters
98    Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
99    redirect = listPage;
100  }
101  else if ("LoadContext".equals(cmd))
102  {
103    // Display the list page after loading a saved context
104    int contextId = Values.getInt(request.getParameter("context"));
105    Base.loadContext(sc, contextId, defaultContext);
106    redirect = listPage;
107  }
108
109  else if ("ViewItem".equals(cmd))
110  {
111    // Display the view page for a single item
112    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
113    forward = viewPage;
114  }
115  else if ("EditItem".equals(cmd))
116  {
117    // Display the edit page for a single item (should be opened in a popup)
118    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
119    redirect = editPage;
120  }
121  else if ("UpdateItem".equals(cmd))
122  {
123    // Update the properties on an item (will close the popup)
124    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, null, defaultContext);
125    final int maxRecent = Base.getMaxRecent(sc);
126    dc = sc.newDbControl();
127    UserDevice device = cc.getObject("item");
128    if (device == null)
129    {
130      throw new PermissionDeniedException(Permission.CREATE, "device");
131    }
132    else
133    {
134      dc.reattachItem(device, false);
135      message = "Device updated";
136    }
137    JspContext jspContext = ExtensionsControl.createContext(dc, pageContext, GuiContext.item(Item.USERDEVICE), device);
138    ExtensionsInvoker<OnSaveAction> invoker = EditUtil.useOnSaveExtensions(jspContext);
139    try
140    {
141      device.setName(Values.getStringOrNull(request.getParameter("name")));
142      device.setDescription(Values.getStringOrNull(request.getParameter("description")));
143     
144      // OnSave extensions
145      invoker.render(OnSaveRenderer.ON_SAVE);
146      dc.commit();
147      invoker.render(OnSaveRenderer.ON_COMMIT);
148    }
149    catch (Exception ex)
150    {
151      invoker.render(OnSaveRenderer.onRollback(ex));
152      throw ex;
153    }
154    finally
155    {
156      cc.removeObject("item");
157    }
158  }
159  else if ("DeleteItem".equals(cmd))
160  {
161    // Delete a single item and then return to the view page
162    dc = sc.newDbControl();
163    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
164    RemovableUtil.delete(dc, itemType, Collections.singleton(cc.getId()));
165    dc.commit();
166    redirect = listPage;
167  }
168  else if ("DeleteItems".equals(cmd))
169  {
170    // Delete all selected items on the list page
171    dc = sc.newDbControl();
172    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
173    int numTotal = cc.getSelected().size();
174    int numRemoved = RemovableUtil.delete(dc, itemType, cc.getSelected());
175    dc.commit();
176    if (numTotal != numRemoved)
177    {
178      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be deleted, because you have no DELETE permission";
179    }
180    redirect = listPage;
181  }
182  else if ("ExportItems".equals(cmd))
183  {
184    // Run an export plugin in a list context
185    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
186    dc = sc.newDbControl();
187    final User user = cc.getInclude().contains(Include.OTHERS) ?
188        null : User.getById(dc, sc.getLoggedInUserId());
189    final ItemQuery<UserDevice> query = UserDevice.getQuery(user);
190    cc.configureQuery(dc, query, true);
191    cc.setQuery(query);
192    JspContext jspContext = ExtensionsControl.createContext(dc, pageContext, GuiContext.list(itemType), null);
193    ExtensionsInvoker<ListColumnAction<UserDevice,?>> listInvoker = ListColumnUtil.useExtensions(jspContext);
194    listInvoker.render(new ListColumnExportRenderer<UserDevice>(cc));
195    dc.close();
196    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Export+devices";
197  }
198  else if ("ExportItem".equals(cmd))
199  {
200    // Run an export plugin in single-item context
201    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
202    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Export+device";
203  }
204  else if ("RunListPlugin".equals(cmd))
205  {
206    // Run another plugin in a list context
207    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
208    dc = sc.newDbControl();
209    final User user = cc.getInclude().contains(Include.OTHERS) ?
210        null : User.getById(dc, sc.getLoggedInUserId());
211    final ItemQuery<UserDevice> query = UserDevice.getQuery(user);
212    cc.configureQuery(dc, query, false);
213    dc.close();
214    cc.setQuery(query);
215    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&main_type=OTHER&title=Run+plugin";
216  }
217  else if ("RunPlugin".equals(cmd))
218  {
219    // Run another plugin in single-item context
220    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
221    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&main_type=OTHER&title=Run+plugin";
222  }
223  else
224  {
225    throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd);
226  }
227}
228finally
229{
230  if (dc != null) dc.close();
231}
232
233if (forward != null)
234{
235  sc.setSessionSetting("alert-message", message);
236  pageContext.forward(forward);
237}
238else if (redirect != null)
239{
240  sc.setSessionSetting("alert-message", message);
241  response.sendRedirect(redirect);
242}
243else if (message == null)
244{
245  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&wait=0");
246}
247else
248{
249  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&message="+HTML.urlEncode(message));
250}
251%>
252
Note: See TracBrowser for help on using the repository browser.