source: trunk/www/admin/hardware/index.jsp @ 6192

Last change on this file since 6192 was 6192, checked in by Nicklas Nordborg, 10 years ago

References #1729 and #1730.

Refactored the code for displaying a popup message when loading a page. The message should now be stored as a session setting instead: sc.setSessionSetting("alert-message", message). The added benefit is that the message is only displayed once and is not re-displayed if the page is reloaded.

Removed onunload, onkeypress and attributes from the <base:body> tag. Added support for dynamic attributes instead.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 13.7 KB
Line 
1<%-- $Id: index.jsp 6192 2012-10-31 12:34:19Z nicklas $
2  ------------------------------------------------------------------
3  Copyright (C) 2005 Nicklas Nordborg
4  Copyright (C) 2006 Jari Häkkinen, Nicklas Nordborg, Martin Svensson
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.Include"
31  import="net.sf.basedb.core.Hardware"
32  import="net.sf.basedb.core.Project"
33  import="net.sf.basedb.core.ItemSubtype"
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.MultiPermissions"
38  import="net.sf.basedb.core.OwnedItem"
39  import="net.sf.basedb.core.PermissionDeniedException"
40  import="net.sf.basedb.core.ItemAlreadyExistsException"
41  import="net.sf.basedb.util.RemovableUtil"
42  import="net.sf.basedb.util.ShareableUtil"
43  import="net.sf.basedb.util.OwnableUtil"
44  import="net.sf.basedb.clients.web.Base"
45  import="net.sf.basedb.clients.web.WebException"
46  import="net.sf.basedb.util.Values"
47  import="net.sf.basedb.clients.web.util.HTML"
48  import="net.sf.basedb.core.plugin.GuiContext"
49  import="net.sf.basedb.util.extensions.ExtensionsInvoker"
50  import="net.sf.basedb.clients.web.extensions.ExtensionsControl"
51  import="net.sf.basedb.clients.web.extensions.JspContext"
52  import="net.sf.basedb.clients.web.extensions.edit.EditUtil"
53  import="net.sf.basedb.clients.web.extensions.edit.OnSaveRenderer"
54  import="net.sf.basedb.clients.web.extensions.list.ListColumnExportRenderer"
55  import="net.sf.basedb.clients.web.extensions.list.ListColumnUtil"
56  import="java.util.Enumeration"
57  import="java.util.Set"
58  import="java.util.HashSet"
59  import="java.util.List"
60  import="java.util.ArrayList"
61  import="java.util.Collections"
62%>
63<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
64<%!
65  private static final ItemContext defaultContext = Base.createDefaultContext("name", "name,itemSubtype,version,description");
66  private static final Item itemType = Item.HARDWARE;
67%>
68<%
69final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
70final String ID = sc.getId();
71final String cmd = request.getParameter("cmd");
72final String root = request.getContextPath()+"/";
73final String mode = request.getParameter("mode");
74final String callback = request.getParameter("callback");
75final String itemId = request.getParameter("item_id");
76final String listPage = "list_hardware.jsp?ID="+ID
77  +(mode == null ? "" : "&mode="+mode)
78  +(callback == null ? "" : "&callback="+callback)
79  +(itemId == null ? "" : "&item_id="+itemId);
80final String viewPage = "view_hardware.jsp?ID="+ID;
81final String editPage = "edit_hardware.jsp?ID="+ID;
82
83String forward = null;
84String redirect = null;
85String message = null;
86DbControl dc = null;
87
88try
89{
90  if (cmd == null || "List".equals(cmd))
91  {
92    // Display the list page without updatinging the current context
93    Base.getAndSetCurrentContext(sc, itemType, null, defaultContext, true);
94    redirect = listPage;
95  }
96  else if ("UpdateContext".equals(cmd))
97  {
98    // Display the list page after updating the current context from the request parameters
99    Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
100    redirect = listPage;
101  }
102  else if ("LoadContext".equals(cmd))
103  {
104    // Display the list page after loading a saved context
105    int contextId = Values.getInt(request.getParameter("context"));
106    Base.loadContext(sc, contextId, defaultContext);
107    redirect = listPage;
108  }
109
110  else if ("ViewItem".equals(cmd))
111  {
112    // Display the view page for a single item
113    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
114    forward = viewPage;
115  }
116  else if ("EditItem".equals(cmd))
117  {
118    // Display the edit page for a single item (should be opened in a popup)
119    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
120    redirect = editPage;
121  }
122  else if ("NewItem".equals(cmd))
123  {
124    // Display the edit page for a new item (should be opened in a popup)
125    if (!sc.hasPermission(Permission.CREATE, itemType))
126    {
127      throw new PermissionDeniedException(Permission.CREATE, itemType.toString());
128    }
129    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
130    cc.setId(0);
131    forward = editPage;
132  }
133  else if ("UpdateItem".equals(cmd))
134  {
135    // Update the properties on an item (will close the popup)
136    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, null, defaultContext);
137    final int maxRecent = Base.getMaxRecent(sc);
138    dc = sc.newDbControl();
139    Hardware hardware = (Hardware)cc.getObject("item");
140    if (hardware == null)
141    {
142      hardware = Hardware.getNew(dc);
143      message = "Hardware created";
144      dc.saveItem(hardware);
145    }
146    else
147    {
148      dc.reattachItem(hardware, false);
149      message = "Hardware updated";
150    }
151    JspContext jspContext = ExtensionsControl.createContext(dc, pageContext, GuiContext.item(Item.HARDWARE), hardware);
152    ExtensionsInvoker invoker = EditUtil.useOnSaveExtensions(jspContext);
153    try
154    {
155      hardware.setName(Values.getStringOrNull(request.getParameter("name")));
156      hardware.setVersionString(Values.getStringOrNull(request.getParameter("version")));
157      hardware.setDescription(Values.getStringOrNull(request.getParameter("description")));
158
159      int subtypeId = Values.getInt(request.getParameter("subtype_id"), -1);
160      if (subtypeId >= 0) // < 0 = denied or unchanged
161      {
162        ItemSubtype subtype = subtypeId == 0 ? null : ItemSubtype.getById(dc, subtypeId);
163        hardware.setItemSubtype(subtype);
164        if (subtype != null) cc.setRecent(subtype, maxRecent);
165      }
166     
167      // Project default
168      if (Values.getBoolean(request.getParameter("setAsProjectDefault")))
169      {
170        Project p = Project.getById(dc, sc.getActiveProjectId());
171        if (Values.getBoolean(request.getParameter("replaceExistingDefault")))
172        {
173          p.replaceDefaultItem(hardware);
174        }
175        else
176        {
177          p.addDefaultItem(hardware);
178        }
179      }
180     
181      // OnSave extensions
182      invoker.render(OnSaveRenderer.ON_SAVE);
183      dc.commit();
184      invoker.render(OnSaveRenderer.ON_COMMIT);
185    }
186    catch (Exception ex)
187    {
188      invoker.render(OnSaveRenderer.onRollback(ex));
189      throw ex;
190    }
191    finally
192    {
193      cc.removeObject("item");
194    }
195  }
196  else if ("DeleteItem".equals(cmd))
197  {
198    // Delete a single item and then return to the view page
199    dc = sc.newDbControl();
200    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
201    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), true);
202    dc.commit();
203    redirect = viewPage;
204  }
205  else if ("DeleteItems".equals(cmd))
206  {
207    // Delete all selected items on the list page
208    dc = sc.newDbControl();
209    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
210    int numTotal = cc.getSelected().size();
211    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), true);
212    dc.commit();
213    if (numTotal != numRemoved)
214    {
215      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be deleted, because you have no DELETE permission";
216    }
217    redirect = listPage;
218  }
219  else if ("RestoreItem".equals(cmd))
220  {
221    // Restore a single item and then return to the view page
222    dc = sc.newDbControl();
223    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
224    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), false);
225    dc.commit();
226    redirect = viewPage;
227  }
228  else if ("RestoreItems".equals(cmd))
229  {
230    // Restore all selected items on the list page
231    dc = sc.newDbControl();
232    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
233    int numTotal = cc.getSelected().size();
234    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), false);
235    dc.commit();
236    if (numTotal != numRemoved)
237    {
238      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be restored, because you have no WRITE permission";
239    }
240    redirect = listPage;
241  }
242  else if ("ShareItem".equals(cmd))
243  {
244    // Display a popup window for sharing a single item
245    dc = sc.newDbControl();
246    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
247    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, Collections.singleton(cc.getId()));
248    dc.close();
249    cc.setObject("MultiPermissions", permissions);
250    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
251  }
252  else if ("ShareItems".equals(cmd))
253  {
254    // Display a popup window for sharing all selected items on the list page
255    dc = sc.newDbControl();
256    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
257    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, cc.getSelected());
258    dc.close();
259    cc.setObject("MultiPermissions", permissions);
260    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
261  }
262  else if ("SetOwnerOfItem".equals(cmd))
263  {
264    // Change owner of items selected on a list page
265    dc = sc.newDbControl();
266    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
267    OwnedItem item = (OwnedItem)itemType.getById(dc, cc.getId());
268    cc.setObject("OwnedItems", Collections.singleton(item));
269    redirect = "../../common/ownership/ownership.jsp?ID="+ID+"&item_type="+itemType.name();
270  }
271  else if ("SetOwnerOfItems".equals(cmd))
272  {
273    // Change owner of items selected on a list page
274    dc = sc.newDbControl();
275    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
276    Set<OwnedItem> items = new HashSet<OwnedItem>();
277    for (Integer id : cc.getSelected())
278    {
279      if (id != null) items.add((OwnedItem)itemType.getById(dc, id));
280    }
281    dc.close();
282    cc.setObject("OwnedItems", items);
283    redirect = "../../common/ownership/ownership.jsp?ID="+ID+"&item_type="+itemType.name();
284  }
285  else if ("ExportItems".equals(cmd))
286  {
287    // Run an export plugin in a list context
288    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
289    final ItemQuery<Hardware> query = Hardware.getQuery();
290    dc = sc.newDbControl();
291    cc.configureQuery(dc, query, true);
292    cc.setQuery(query);
293    JspContext jspContext = ExtensionsControl.createContext(dc, pageContext, GuiContext.list(itemType), null);
294    ExtensionsInvoker listInvoker = ListColumnUtil.useExtensions(jspContext);
295    listInvoker.render(new ListColumnExportRenderer(cc));
296    dc.close();
297    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Export+raw+bioassays";
298  }
299  else if ("ExportItem".equals(cmd))
300  {
301    // Run an export plugin in single-item context
302    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
303    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Export+raw+bioassay";
304  }
305  else if ("ImportItems".equals(cmd))
306  {
307    // Run an import plugin in a list context
308    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
309    final ItemQuery<Hardware> query = Hardware.getQuery();
310    dc = sc.newDbControl();
311    cc.configureQuery(dc, query, true);
312    dc.close();
313    cc.setQuery(query);
314    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Import+raw+bioassays";
315  }
316  else if ("ImportItem".equals(cmd))
317  {
318    // Run an import plugin in single-item context
319    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
320    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Import+raw+bioassay";
321  }
322  else if ("RunListPlugin".equals(cmd))
323  {
324    // Run another plugin in a list context
325    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
326    final ItemQuery<Hardware> query = Hardware.getQuery();
327    dc = sc.newDbControl();
328    cc.configureQuery(dc, query, true);
329    dc.close();
330    cc.setQuery(query);
331    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&main_type=OTHER&title=Run+plugin";
332  }
333  else if ("RunPlugin".equals(cmd))
334  {
335    // Run another plugin in single-item context
336    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
337    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&main_type=OTHER&title=Run+plugin";
338  }
339  else
340  {
341    throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd);
342  }
343}
344finally
345{
346  if (dc != null) dc.close();
347}
348
349if (forward != null)
350{
351  sc.setSessionSetting("alert-message", message);
352  pageContext.forward(forward);
353}
354else if (redirect != null)
355{
356  sc.setSessionSetting("alert-message", message);
357  response.sendRedirect(redirect);
358}
359else if (message == null)
360{
361  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&wait=0");
362}
363else
364{
365  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&message="+HTML.urlEncode(message));
366}
367%>
368
Note: See TracBrowser for help on using the repository browser.