source: trunk/www/views/physicalbioassays/index.jsp @ 6314

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

References #1729 and #1730. Several edit dialogs in the 'View' menu. Expression builder, which required changes to the 'Validate' function which is now done on the server side instead of in the browser (due to the use of eval() on user input).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Id
File size: 17.9 KB
Line 
1<%-- $Id: index.jsp 6314 2013-09-02 12:47:53Z nicklas $
2  ------------------------------------------------------------------
3  Copyright (C) 2005 Nicklas Nordborg, Gregory Vincic
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.PhysicalBioAssay"
32  import="net.sf.basedb.core.ArraySlide"
33  import="net.sf.basedb.core.BioMaterialEvent"
34  import="net.sf.basedb.core.BioMaterialEventSource"
35  import="net.sf.basedb.core.Extract"
36  import="net.sf.basedb.core.Protocol"
37  import="net.sf.basedb.core.Hardware"
38  import="net.sf.basedb.core.ItemSubtype"
39  import="net.sf.basedb.core.DerivedBioAssay"
40  import="net.sf.basedb.core.ItemQuery"
41  import="net.sf.basedb.core.ItemResultIterator"
42  import="net.sf.basedb.core.Permission"
43  import="net.sf.basedb.core.ItemContext"
44  import="net.sf.basedb.core.MultiPermissions"
45  import="net.sf.basedb.core.OwnedItem"
46  import="net.sf.basedb.core.PermissionDeniedException"
47  import="net.sf.basedb.core.ItemAlreadyExistsException"
48  import="net.sf.basedb.core.query.Hql"
49  import="net.sf.basedb.core.query.Orders"
50  import="net.sf.basedb.core.query.Restrictions"
51  import="net.sf.basedb.core.query.Expressions"
52  import="net.sf.basedb.util.RemovableUtil"
53  import="net.sf.basedb.util.ShareableUtil"
54  import="net.sf.basedb.util.OwnableUtil"
55  import="net.sf.basedb.clients.web.Base"
56  import="net.sf.basedb.clients.web.WebException"
57  import="net.sf.basedb.util.Values"
58  import="net.sf.basedb.clients.web.util.HTML"
59  import="net.sf.basedb.util.formatter.Formatter"
60  import="net.sf.basedb.util.formatter.NameableFormatter"
61  import="net.sf.basedb.clients.web.formatter.FormatterFactory"
62  import="net.sf.basedb.clients.web.plugins.ItemQueryLoader"
63  import="net.sf.basedb.clients.web.plugins.BioMaterialEventSourceFormatter"
64  import="net.sf.basedb.clients.web.plugins.ParentBioMaterialEventSourceLoaderFromBioAssay"
65  import="net.sf.basedb.core.plugin.GuiContext"
66  import="net.sf.basedb.util.extensions.ExtensionsInvoker"
67  import="net.sf.basedb.clients.web.extensions.ExtensionsControl"
68  import="net.sf.basedb.clients.web.extensions.JspContext"
69  import="net.sf.basedb.clients.web.extensions.edit.EditUtil"
70  import="net.sf.basedb.clients.web.extensions.edit.OnSaveRenderer"
71  import="net.sf.basedb.clients.web.extensions.list.ListColumnExportRenderer"
72  import="net.sf.basedb.clients.web.extensions.list.ListColumnUtil"
73  import="java.util.Enumeration"
74  import="java.util.Set"
75  import="java.util.HashSet"
76  import="java.util.List"
77  import="java.util.ArrayList"
78  import="java.util.Collections"
79  import="java.util.Date"
80%>
81<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
82<%!
83  private static final ItemContext defaultContext = Base.createDefaultContext("name", "name,itemSubtype,extracts,arraySlide,description");
84  private static final Item itemType = Item.PHYSICALBIOASSAY;
85 
86  private static void registerExportUtils(ItemContext cc)
87  {
88    // Register formatters
89    cc.setObject("export.formatter.&creationEvent.sources(bioMaterial.name)", new BioMaterialEventSourceFormatter());
90    cc.setObject("export.formatter.&rootDerivedBioAssays(name)", new NameableFormatter());
91   
92    // Register dataloaders
93    String bioassayParameter = "bioassay";
94    cc.setObject("export.dataloader.&creationEvent.sources(bioMaterial.name)", new ParentBioMaterialEventSourceLoaderFromBioAssay());
95
96    ItemQuery<DerivedBioAssay> dbasQuery = DerivedBioAssay.getQuery();
97    dbasQuery.include(cc.getInclude());
98    dbasQuery.join(Hql.innerJoin("physicalBioAssays", "pba"));
99    dbasQuery.restrict(Restrictions.eq(Hql.alias("pba"), Expressions.parameter(bioassayParameter)));
100    dbasQuery.restrict(Restrictions.eq(Hql.property("root"), Expressions.bool(true)));
101    dbasQuery.order(Orders.asc(Hql.property("name")));
102    cc.setObject("export.dataloader.&rootDerivedBioAssays(name)", new ItemQueryLoader(dbasQuery, bioassayParameter));
103  }
104%>
105<%
106final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
107final String ID = sc.getId();
108final String cmd = request.getParameter("cmd");
109final String root = request.getContextPath()+"/";
110final String mode = request.getParameter("mode");
111final String callback = request.getParameter("callback");
112final String itemId = request.getParameter("item_id");
113final String listPage = "list_bioassays.jsp?ID="+ID
114  +(mode == null ? "" : "&mode="+mode)
115  +(callback == null ? "" : "&callback="+callback)
116  +(itemId == null ? "" : "&item_id="+itemId);
117final String viewPage = "view_bioassay.jsp?ID="+ID;
118final String editPage = "edit_bioassay.jsp?ID="+ID;
119
120String forward = null;
121String redirect = null;
122String message = null;
123DbControl dc = null;
124
125try
126{
127  if (cmd == null || "List".equals(cmd))
128  {
129    // Display the list page without updatinging the current context
130    Base.getAndSetCurrentContext(sc, itemType, null, defaultContext, true);
131    redirect = listPage;
132  }
133  else if ("UpdateContext".equals(cmd))
134  {
135    // Display the list page after updating the current context from the request parameters
136    Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
137    redirect = listPage;
138  }
139  else if ("LoadContext".equals(cmd))
140  {
141    // Display the list page after loading a saved context
142    int contextId = Values.getInt(request.getParameter("context"));
143    Base.loadContext(sc, contextId, defaultContext);
144    redirect = listPage;
145  }
146
147  else if ("ViewItem".equals(cmd))
148  {
149    // Display the view page for a single item
150    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
151    forward = viewPage;
152  }
153  else if ("EditItem".equals(cmd))
154  {
155    // Display the edit page for a single item (should be opened in a popup)
156    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
157    redirect = editPage;
158  }
159  else if ("NewItem".equals(cmd))
160  {
161    // Display the edit page for a new item (should be opened in a popup)
162    if (!sc.hasPermission(Permission.CREATE, itemType))
163    {
164      throw new PermissionDeniedException(Permission.CREATE, itemType.toString());
165    }
166    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
167    cc.setId(0);
168    forward = editPage;
169  }
170  else if ("UpdateItem".equals(cmd))
171  {
172    // Update the properties on an item (will close the popup)
173    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, null, defaultContext);
174    final int maxRecent = Base.getMaxRecent(sc);
175    dc = sc.newDbControl();
176    PhysicalBioAssay pba = (PhysicalBioAssay)cc.getObject("item");
177    BioMaterialEvent creationEvent = null;
178    if (pba == null)
179    {
180      pba = PhysicalBioAssay.getNew(dc);
181      creationEvent = pba.getCreationEvent();
182      message = "Physical bioassay created";
183      dc.saveItem(pba);
184    }
185    else
186    {
187      creationEvent = pba.getCreationEvent();
188      dc.reattachItem(pba, false);
189      dc.reattachItem(creationEvent, false);
190      message = "Physical bioassay updated";
191    }
192    JspContext jspContext = ExtensionsControl.createContext(dc, pageContext, GuiContext.item(Item.PHYSICALBIOASSAY), pba);
193    ExtensionsInvoker invoker = EditUtil.useOnSaveExtensions(jspContext);
194    try
195    {
196      pba.setName(Values.getStringOrNull(request.getParameter("name")));
197      pba.setDescription(Values.getStringOrNull(request.getParameter("description")));
198      pba.setSize(Values.getInt(request.getParameter("size"), 1));
199
200      int subtypeId = Values.getInt(request.getParameter("subtype_id"), -1);
201      ItemSubtype subtype = null;
202      if (subtypeId >= 0) // < 0 = denied or unchanged
203      {
204        if (subtypeId > 0) subtype = ItemSubtype.getById(dc, subtypeId);
205        pba.setItemSubtype(subtype);
206        if (subtype != null) cc.setRecent(subtype, maxRecent);
207      }
208     
209      int arraySlideId = Values.getInt(request.getParameter("arrayslide_id"), -1);
210      if (arraySlideId >= 0) // < 0 = denied or unchanged
211      {
212        ArraySlide slide = arraySlideId == 0 ? null : ArraySlide.getById(dc, arraySlideId);
213        pba.setArraySlide(slide);
214      }
215      Formatter<Date> dateFormatter = FormatterFactory.getDateFormatter(sc);
216      creationEvent.setEventDate(dateFormatter.parseString(Values.getStringOrNull(request.getParameter("event_date"))));
217      int protocolId = Values.getInt(request.getParameter("protocol_id"), -1);
218      if (protocolId >= 0) // < 0 = denied or unchanged
219      {
220        Protocol pt = protocolId == 0 ? null : Protocol.getById(dc, protocolId);
221        creationEvent.setProtocol(pt);
222        if (pt != null) cc.setRecent(pt, subtype, maxRecent);
223      }
224     
225      //Hardware
226      int hardwareId = Values.getInt(request.getParameter("hardware_id"), -1);
227      if (hardwareId >= 0) // < 0 denied or unchanged
228      {
229        Hardware hw = hardwareId == 0 ? null : Hardware.getById(dc, hardwareId);
230        creationEvent.setHardware(hw);
231        if (hw != null) cc.setRecent(hw, subtype, maxRecent);
232      }
233     
234      // Extracts
235      String[] modifiedExtracts = Values.getString(request.getParameter("+EXTRACT")).split(",");
236      for (int i = 0; i < modifiedExtracts.length; ++i)
237      {
238        int extractId = Values.getInt(modifiedExtracts[i], -1);
239        if (extractId != -1) 
240        {
241          Extract e = Extract.getById(dc, extractId);
242          String[] extra = request.getParameter("EXTRACT."+extractId).split(":");
243          Float usedQuantity = Values.getFloat(extra[0], null);
244          int position = extra.length > 1 ? Values.getInt(extra[1], 1) : 1;
245          BioMaterialEventSource evtSrc = creationEvent.addSource(e);
246          evtSrc.setUsedQuantity(usedQuantity);
247          evtSrc.setPosition(position);
248        }
249      }
250      String[] removedExtracts = Values.getString(request.getParameter("-EXTRACT")).split(",");
251      for (int i = 0; i < removedExtracts.length; ++i)
252      {
253        int extractId = Values.getInt(removedExtracts[i], -1);
254        if (extractId != -1) creationEvent.removeSource(Extract.getById(dc, extractId));
255      }
256     
257      // Annotations tab
258      Base.updateAnnotations(dc, pba, pba, request);
259     
260      // OnSave extensions
261      invoker.render(OnSaveRenderer.ON_SAVE);
262      dc.commit();
263      invoker.render(OnSaveRenderer.ON_COMMIT);
264    }
265    catch (Exception ex)
266    {
267      invoker.render(OnSaveRenderer.onRollback(ex));
268      throw ex;
269    }
270    finally
271    {
272      cc.removeObject("item");
273    }
274  }
275  else if ("DeleteItem".equals(cmd))
276  {
277    // Delete a single item and then return to the view page
278    dc = sc.newDbControl();
279    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
280    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), true);
281    dc.commit();
282    redirect = viewPage;
283  }
284  else if ("DeleteItems".equals(cmd))
285  {
286    // Delete all selected items on the list page
287    dc = sc.newDbControl();
288    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
289    int numTotal = cc.getSelected().size();
290    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), true);
291    dc.commit();
292    if (numTotal != numRemoved)
293    {
294      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be deleted, because you have no DELETE permission";
295    }
296    redirect = listPage;
297  }
298  else if ("RestoreItem".equals(cmd))
299  {
300    // Restore a single item and then return to the view page
301    dc = sc.newDbControl();
302    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
303    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), false);
304    dc.commit();
305    redirect = viewPage;
306  }
307  else if ("RestoreItems".equals(cmd))
308  {
309    // Restore all selected items on the list page
310    dc = sc.newDbControl();
311    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
312    int numTotal = cc.getSelected().size();
313    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), false);
314    dc.commit();
315    if (numTotal != numRemoved)
316    {
317      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be restored, because you have no WRITE permission";
318    }
319    redirect = listPage;
320  }
321  else if ("ShareItem".equals(cmd))
322  {
323    // Display a popup window for sharing a single item
324    dc = sc.newDbControl();
325    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
326    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, Collections.singleton(cc.getId()));
327    dc.close();
328    cc.setObject("MultiPermissions", permissions);
329    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
330  }
331  else if ("ShareItems".equals(cmd))
332  {
333    // Display a popup window for sharing all selected items on the list page
334    dc = sc.newDbControl();
335    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
336    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, cc.getSelected());
337    dc.close();
338    cc.setObject("MultiPermissions", permissions);
339    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
340  }
341  else if ("SetOwnerOfItem".equals(cmd))
342  {
343    // Change owner of items selected on a list page
344    dc = sc.newDbControl();
345    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
346    OwnedItem item = (OwnedItem)itemType.getById(dc, cc.getId());
347    cc.setObject("OwnedItems", Collections.singleton(item));
348    redirect = "../../common/ownership/ownership.jsp?ID="+ID+"&item_type="+itemType.name();
349  }
350  else if ("SetOwnerOfItems".equals(cmd))
351  {
352    // Change owner of items selected on a list page
353    dc = sc.newDbControl();
354    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
355    Set<OwnedItem> items = new HashSet<OwnedItem>();
356    for (Integer id : cc.getSelected())
357    {
358      if (id != null) items.add((OwnedItem)itemType.getById(dc, id));
359    }
360    dc.close();
361    cc.setObject("OwnedItems", items);
362    redirect = "../../common/ownership/ownership.jsp?ID="+ID+"&item_type="+itemType.name();
363  }
364  else if ("ExportItems".equals(cmd))
365  {
366    // Run an export plugin in a list context
367    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
368    final ItemQuery<PhysicalBioAssay> query = PhysicalBioAssay.getQuery();
369    dc = sc.newDbControl();
370    cc.configureQuery(dc, query, true);
371    cc.setQuery(query);
372    registerExportUtils(cc);
373    JspContext jspContext = ExtensionsControl.createContext(dc, pageContext, GuiContext.list(itemType), null);
374    ExtensionsInvoker listInvoker = ListColumnUtil.useExtensions(jspContext);
375    listInvoker.render(new ListColumnExportRenderer(cc));
376    dc.close();
377    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Export+physical+bioassays";
378  }
379  else if ("ExportItem".equals(cmd))
380  {
381    // Run an export plugin in single-item context
382    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
383    registerExportUtils(cc);
384    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Export+physical+bioassay";
385  }
386  else if ("ImportItems".equals(cmd))
387  {
388    // Run an import plugin in a list context
389    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
390    final ItemQuery<PhysicalBioAssay> query = PhysicalBioAssay.getQuery();
391    dc = sc.newDbControl();
392    cc.configureQuery(dc, query, true);
393    dc.close();
394    cc.setQuery(query);
395    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Import+physical+bioassays";
396  }
397  else if ("ImportItem".equals(cmd))
398  {
399    // Run an import plugin in single-item context
400    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
401    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Import+physical+bioassay";
402  }
403  else if ("RunListPlugin".equals(cmd))
404  {
405    // Run another plugin in a list context
406    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
407    final ItemQuery<PhysicalBioAssay> query = PhysicalBioAssay.getQuery();
408    dc = sc.newDbControl();
409    cc.configureQuery(dc, query, true);
410    dc.close();
411    cc.setQuery(query);
412    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&main_type=OTHER&title=Run+plugin";
413  }
414  else if ("RunPlugin".equals(cmd))
415  {
416    // Run another plugin in single-item context
417    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
418    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&main_type=OTHER&title=Run+plugin";
419  }
420  else if ("NewMergedDerivedBioAssay".equals(cmd))
421  {
422    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
423    redirect = "../derivedbioassays/index.jsp?ID="+ID+"&cmd=NewItem&useParents=PHYISCALBIOASSAY";
424  }
425  else
426  {
427    throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd);
428  }
429}
430finally
431{
432  if (dc != null) dc.close();
433}
434
435if (forward != null)
436{
437  sc.setSessionSetting("alert-message", message);
438  pageContext.forward(forward);
439}
440else if (redirect != null)
441{
442  sc.setSessionSetting("alert-message", message);
443  response.sendRedirect(redirect);
444}
445else if (message == null)
446{
447  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&wait=0");
448}
449else
450{
451  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&message="+HTML.urlEncode(message));
452}
453%>
454
Note: See TracBrowser for help on using the repository browser.