source: trunk/www/admin/software/index.jsp @ 6334

Last change on this file since 6334 was 6334, checked in by olle, 9 years ago

Refs #1772. Support for annotatable Software items added.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 13.9 KB
Line 
1<%-- $Id: index.jsp 6334 2013-10-16 14:39:55Z olle $
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.Software"
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.SOFTWARE;
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_software.jsp?ID="+ID
77  +(mode == null ? "" : "&mode="+mode)
78  +(callback == null ? "" : "&callback="+callback)
79  +(itemId == null ? "" : "&item_id="+itemId);
80final String viewPage = "view_software.jsp?ID="+ID;
81final String editPage = "edit_software.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    Software software = (Software)cc.getObject("item");
140    if (software == null)
141    {
142      software = Software.getNew(dc);
143      message = "Software created";
144      dc.saveItem(software);
145    }
146    else
147    {
148      dc.reattachItem(software, false);
149      message = "Software updated";
150    }
151    JspContext jspContext = ExtensionsControl.createContext(dc, pageContext, GuiContext.item(Item.SOFTWARE), software);
152    ExtensionsInvoker invoker = EditUtil.useOnSaveExtensions(jspContext);
153    try
154    {
155      software.setName(Values.getStringOrNull(request.getParameter("name")));
156      software.setVersionString(Values.getStringOrNull(request.getParameter("version")));
157      software.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        software.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(software);
174        }
175        else
176        {
177          p.addDefaultItem(software);
178        }
179      }
180     
181      Base.updateAnnotations(dc, software, software, request);
182      // OnSave extensions
183      invoker.render(OnSaveRenderer.ON_SAVE);
184      dc.commit();
185      invoker.render(OnSaveRenderer.ON_COMMIT);
186    }
187    catch (Exception ex)
188    {
189      invoker.render(OnSaveRenderer.onRollback(ex));
190      throw ex;
191    }
192    finally
193    {
194      cc.removeObject("item");
195    }
196  }
197  else if ("DeleteItem".equals(cmd))
198  {
199    // Delete a single item and then return to the view page
200    dc = sc.newDbControl();
201    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
202    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), true);
203    dc.commit();
204    redirect = viewPage;
205  }
206  else if ("DeleteItems".equals(cmd))
207  {
208    // Delete all selected items on the list page
209    dc = sc.newDbControl();
210    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
211    int numTotal = cc.getSelected().size();
212    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), true);
213    dc.commit();
214    if (numTotal != numRemoved)
215    {
216      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be deleted, because you have no DELETE permission";
217    }
218    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
219  }
220  else if ("RestoreItem".equals(cmd))
221  {
222    // Restore a single item and then return to the view page
223    dc = sc.newDbControl();
224    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
225    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), false);
226    dc.commit();
227    redirect = viewPage;
228  }
229  else if ("RestoreItems".equals(cmd))
230  {
231    // Restore all selected items on the list page
232    dc = sc.newDbControl();
233    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
234    int numTotal = cc.getSelected().size();
235    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), false);
236    dc.commit();
237    if (numTotal != numRemoved)
238    {
239      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be restored, because you have no WRITE permission";
240    }
241    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
242  }
243  else if ("ShareItem".equals(cmd))
244  {
245    // Display a popup window for sharing a single item
246    dc = sc.newDbControl();
247    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
248    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, Collections.singleton(cc.getId()));
249    dc.close();
250    cc.setObject("MultiPermissions", permissions);
251    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
252  }
253  else if ("ShareItems".equals(cmd))
254  {
255    // Display a popup window for sharing all selected items on the list page
256    dc = sc.newDbControl();
257    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
258    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, cc.getSelected());
259    dc.close();
260    cc.setObject("MultiPermissions", permissions);
261    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
262  }
263  else if ("SetOwnerOfItem".equals(cmd))
264  {
265    // Change owner of items selected on a list page
266    dc = sc.newDbControl();
267    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
268    OwnedItem item = (OwnedItem)itemType.getById(dc, cc.getId());
269    cc.setObject("OwnedItems", Collections.singleton(item));
270    redirect = "../../common/ownership/ownership.jsp?ID="+ID+"&item_type="+itemType.name();
271  }
272  else if ("SetOwnerOfItems".equals(cmd))
273  {
274    // Change owner of items selected on a list page
275    dc = sc.newDbControl();
276    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
277    Set<OwnedItem> items = new HashSet<OwnedItem>();
278    for (Integer id : cc.getSelected())
279    {
280      if (id != null) items.add((OwnedItem)itemType.getById(dc, id));
281    }
282    dc.close();
283    cc.setObject("OwnedItems", items);
284    redirect = "../../common/ownership/ownership.jsp?ID="+ID+"&item_type="+itemType.name();
285  }
286  else if ("ExportItems".equals(cmd))
287  {
288    // Run an export plugin in a list context
289    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
290    final ItemQuery<Software> query = Software.getQuery();
291    dc = sc.newDbControl();
292    cc.configureQuery(dc, query, true);
293    cc.setQuery(query);
294    JspContext jspContext = ExtensionsControl.createContext(dc, pageContext, GuiContext.list(itemType), null);
295    ExtensionsInvoker listInvoker = ListColumnUtil.useExtensions(jspContext);
296    listInvoker.render(new ListColumnExportRenderer(cc));
297    dc.close();
298    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Export+raw+bioassays";
299  }
300  else if ("ExportItem".equals(cmd))
301  {
302    // Run an export plugin in single-item context
303    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
304    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Export+raw+bioassay";
305  }
306  else if ("ImportItems".equals(cmd))
307  {
308    // Run an import plugin in a list context
309    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
310    final ItemQuery<Software> query = Software.getQuery();
311    dc = sc.newDbControl();
312    cc.configureQuery(dc, query, true);
313    dc.close();
314    cc.setQuery(query);
315    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Import+raw+bioassays";
316  }
317  else if ("ImportItem".equals(cmd))
318  {
319    // Run an import plugin in single-item context
320    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
321    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Import+raw+bioassay";
322  }
323  else if ("RunListPlugin".equals(cmd))
324  {
325    // Run another plugin in a list context
326    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
327    final ItemQuery<Software> query = Software.getQuery();
328    dc = sc.newDbControl();
329    cc.configureQuery(dc, query, true);
330    dc.close();
331    cc.setQuery(query);
332    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&main_type=OTHER&title=Run+plugin";
333  }
334  else if ("RunPlugin".equals(cmd))
335  {
336    // Run another plugin in single-item context
337    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
338    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&main_type=OTHER&title=Run+plugin";
339  }
340  else
341  {
342    throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd);
343  }
344}
345finally
346{
347  if (dc != null) dc.close();
348}
349
350if (forward != null)
351{
352  sc.setSessionSetting("alert-message", message);
353  pageContext.forward(forward);
354}
355else if (redirect != null)
356{
357  sc.setSessionSetting("alert-message", message);
358  response.sendRedirect(redirect);
359}
360else if (message == null)
361{
362  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&wait=0");
363}
364else
365{
366  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&message="+HTML.urlEncode(message));
367}
368%>
369
Note: See TracBrowser for help on using the repository browser.