source: trunk/www/biomaterials/biosources/index.jsp @ 7703

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

Merged pre-3.15 changes to the trunk.

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