source: trunk/www/views/derivedbioassays/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: 19.7 KB
Line 
1<%-- $Id $
2  ------------------------------------------------------------------
3  Copyright (C) 2011 Nicklas Nordborg
4
5  This file is part of BASE - BioArray Software Environment.
6  Available at http://base.thep.lu.se/
7
8  This file is part of BASE.
9
10  BASE is free software; you can redistribute it and/or
11  modify it under the terms of the GNU General Public License
12  as published by the Free Software Foundation; either version 3
13  of the License, or (at your option) any later version.
14
15  BASE is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU General Public License for more details.
19
20  You should have received a copy of the GNU General Public License
21  along with BASE. If not, see <http://www.gnu.org/licenses/>.
22  ------------------------------------------------------------------
23
24--%>
25<%@ page pageEncoding="UTF-8" session="false"
26  import="net.sf.basedb.core.SessionControl"
27  import="net.sf.basedb.core.DbControl"
28  import="net.sf.basedb.core.DerivedBioAssay"
29  import="net.sf.basedb.core.PhysicalBioAssay"
30  import="net.sf.basedb.core.RawBioAssay"
31  import="net.sf.basedb.core.ItemSubtype"
32  import="net.sf.basedb.core.Extract"
33  import="net.sf.basedb.core.Protocol"
34  import="net.sf.basedb.core.Software"
35  import="net.sf.basedb.core.Hardware"
36  import="net.sf.basedb.core.Item"
37  import="net.sf.basedb.core.Include"
38  import="net.sf.basedb.core.User"
39  import="net.sf.basedb.core.OwnedItem"
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.PluginDefinition"
44  import="net.sf.basedb.core.ItemContext"
45  import="net.sf.basedb.core.MultiPermissions"
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.IncludeExcludeFilter"
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.core.plugin.GuiContext"
60  import="net.sf.basedb.util.extensions.ExtensionsInvoker"
61  import="net.sf.basedb.util.formatter.NameableFormatter"
62  import="net.sf.basedb.clients.web.extensions.ExtensionsControl"
63  import="net.sf.basedb.clients.web.extensions.JspContext"
64  import="net.sf.basedb.clients.web.extensions.edit.EditUtil"
65  import="net.sf.basedb.clients.web.extensions.edit.OnSaveRenderer"
66  import="net.sf.basedb.clients.web.extensions.list.ListColumnExportRenderer"
67  import="net.sf.basedb.clients.web.extensions.list.ListColumnUtil"
68  import="net.sf.basedb.clients.web.plugins.ItemQueryLoader"
69  import="java.util.Enumeration"
70  import="java.util.Set"
71  import="java.util.HashSet"
72  import="java.util.List"
73  import="java.util.LinkedList"
74  import="java.util.Collections"
75  import="java.util.Arrays"
76%>
77<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
78<%!
79  private static final ItemContext defaultContext = Base.createDefaultContext("name", "name,itemSubtype,physicalBioAssay,extract,parent,description");
80  private static final Item itemType = Item.DERIVEDBIOASSAY;
81 
82  private static void registerExportUtils(ItemContext cc)
83  {
84    // Register formatters
85    cc.setObject("export.formatter.&physicalBioAssays(name)", new NameableFormatter());
86    cc.setObject("export.formatter.&parents(name)", new NameableFormatter());
87    cc.setObject("export.formatter.&children(name)", new NameableFormatter());
88    cc.setObject("export.formatter.&rawBioAssays(name)", new NameableFormatter());
89   
90    // Register dataloaders
91    String bioassayParameter = "bioassay";
92    // Physical bioassays
93    ItemQuery<PhysicalBioAssay> physicalQuery = PhysicalBioAssay.getQuery();
94    physicalQuery.include(cc.getInclude());
95    physicalQuery.join(Hql.innerJoin("derivedBioAssays", "dba"));
96    physicalQuery.restrict(Restrictions.eq(Hql.alias("dba"), Expressions.parameter(bioassayParameter)));
97    physicalQuery.order(Orders.asc(Hql.property("name")));
98    cc.setObject("export.dataloader.&physicalBioAssays(name)", new ItemQueryLoader(physicalQuery, bioassayParameter));
99
100    // Parent bioassays
101    ItemQuery<DerivedBioAssay> parentQuery = DerivedBioAssay.getQuery();
102    parentQuery.include(cc.getInclude());
103    parentQuery.join(Hql.innerJoin("children", "c"));
104    parentQuery.restrict(Restrictions.eq(Hql.alias("c"), Expressions.parameter(bioassayParameter)));
105    parentQuery.order(Orders.asc(Hql.property("name")));
106    cc.setObject("export.dataloader.&parents(name)", new ItemQueryLoader(parentQuery, bioassayParameter));
107
108    // Child bioassays
109    ItemQuery<DerivedBioAssay> childQuery = DerivedBioAssay.getQuery();
110    childQuery.include(cc.getInclude());
111    childQuery.join(Hql.innerJoin("parents", "p"));
112    childQuery.restrict(Restrictions.eq(Hql.alias("p"), Expressions.parameter(bioassayParameter)));
113    childQuery.order(Orders.asc(Hql.property("name")));
114    cc.setObject("export.dataloader.&children(name)", new ItemQueryLoader(childQuery, bioassayParameter));
115   
116    // Child raw bioassays
117    ItemQuery<RawBioAssay> rawQuery = RawBioAssay.getQuery();
118    rawQuery.include(cc.getInclude());
119    rawQuery.restrict(Restrictions.eq(Hql.property("parentBioAssay"), Expressions.parameter(bioassayParameter)));
120    rawQuery.order(Orders.asc(Hql.property("name")));
121    cc.setObject("export.dataloader.&rawBioAssays(name)", new ItemQueryLoader(rawQuery, bioassayParameter));
122
123  }
124
125%>
126<%
127final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
128final String ID = sc.getId();
129final String cmd = request.getParameter("cmd");
130final String root = request.getContextPath()+"/";
131final String mode = request.getParameter("mode");
132final String callback = request.getParameter("callback");
133final String itemId = request.getParameter("item_id");
134final String listPage = "list_bioassays.jsp?ID="+ID+"&item_id="+itemId
135  +(mode == null ? "" : "&mode="+mode)
136  +(callback == null ? "" : "&callback="+callback)
137  +(itemId == null ? "" : "&item_id="+itemId);
138final String viewPage = "view_bioassay.jsp?ID="+ID;
139final String editPage = "edit_bioassay.jsp?ID="+ID;
140
141String forward = null;
142String redirect = null;
143String message = null;
144DbControl dc = null;
145
146try
147{
148  if (cmd == null || "List".equals(cmd))
149  {
150    // Display the list page without updatinging the current context
151    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, null, defaultContext, true);
152    redirect = listPage;
153  }
154  else if ("UpdateContext".equals(cmd))
155  {
156    // Display the list page after updating the current context from the request parameters
157    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
158    redirect = listPage;
159  }
160  else if ("LoadContext".equals(cmd))
161  {
162    // Display the list page after loading a saved context
163    int contextId = Values.getInt(request.getParameter("context"));
164    Base.loadContext(sc, contextId, defaultContext);
165    redirect = listPage;
166  }
167  else if ("ViewItem".equals(cmd))
168  {
169    // Display the view page for a single item
170    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
171    forward = viewPage;
172  }
173  else if ("EditItem".equals(cmd))
174  {
175    // Display the edit page for a single item (should be opened in a popup)
176    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
177    forward = editPage;
178  }
179  else if ("NewItem".equals(cmd))
180  {
181    // Display the edit page for a new item (should be opened in a popup)
182    if (!sc.hasPermission(Permission.CREATE, itemType))
183    {
184      throw new PermissionDeniedException(Permission.CREATE, itemType.toString());
185    }
186    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
187    cc.setId(0);
188    forward = editPage;
189  }
190  else if ("UpdateItem".equals(cmd))
191  {
192    // Update the properties on an item (will close the popup)
193    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, null, defaultContext);
194    final int maxRecent = Base.getMaxRecent(sc);
195    dc = sc.newDbControl();
196   
197    DerivedBioAssay bas = (DerivedBioAssay)cc.getObject("item");
198    if (bas == null)
199    {
200      bas = DerivedBioAssay.getNew(dc, Values.getBoolean(request.getParameter("isRoot")), null);
201      dc.saveItem(bas);
202      message = "Derived bioassay created";
203    }
204    else
205    {
206      dc.reattachItem(bas, false);
207      message = "Derived bioassay updated";
208    }
209
210    JspContext jspContext = ExtensionsControl.createContext(dc, pageContext, GuiContext.item(itemType), bas);
211    ExtensionsInvoker invoker = EditUtil.useOnSaveExtensions(jspContext);
212    try
213    {
214      bas.setName(Values.getStringOrNull(request.getParameter("name")));
215      bas.setDescription(Values.getStringOrNull(request.getParameter("description")));
216 
217      int subtypeId = Values.getInt(request.getParameter("subtype_id"), -1);
218      ItemSubtype subtype = null;
219      if (subtypeId >= 0) // < 0 = denied or unchanged
220      {
221        subtype = subtypeId == 0 ? null : ItemSubtype.getById(dc, subtypeId);
222        bas.setItemSubtype(subtype);
223        if (subtype != null) cc.setRecent(subtype, maxRecent);
224      }
225     
226      int extractId = Values.getInt(request.getParameter("extract_id"), -1);
227      if (extractId >= 0) // < 0 = denied or unchanged
228      {
229        Extract extract = extractId == 0 ? null : Extract.getById(dc, extractId);
230        bas.setExtract(extract);
231      }
232
233      int protocolId = Values.getInt(request.getParameter("protocol_id"), -1);
234      if (protocolId >= 0) // < 0 = denied or unchanged
235      {
236        Protocol pt = protocolId == 0 ? null : Protocol.getById(dc, protocolId);
237        bas.setProtocol(pt);
238        if (pt != null) cc.setRecent(pt, subtype, maxRecent);
239      }
240     
241      int hardwareId = Values.getInt(request.getParameter("hardware_id"), -1);
242      if (hardwareId >= 0) // < 0 denied or unchanged
243      {
244        Hardware hw = hardwareId == 0 ? null : Hardware.getById(dc, hardwareId);
245        bas.setHardware(hw);
246        if (hw != null) cc.setRecent(hw, subtype, maxRecent);
247      }
248
249      int softwareId = Values.getInt(request.getParameter("software_id"), -1);
250      if (softwareId >= 0) // < 0 denied or unchanged
251      {
252        Software sw = softwareId == 0 ? null : Software.getById(dc, softwareId);
253        bas.setSoftware(sw);
254        if (sw != null) cc.setRecent(sw, subtype, maxRecent);
255      }
256     
257      if (bas.isRoot())
258      {
259        // A root derived bioassay set may have physical bioassays as parents
260        String[] removePhysicalBioAssays = Values.getString(request.getParameter("-PHYSICALBIOASSAY")).split(",");
261        for (int i = 0; i < removePhysicalBioAssays.length; ++i)
262        {
263          int pbaId = Values.getInt(removePhysicalBioAssays[i], -1);
264          if (pbaId != -1) bas.removePhysicalBioAssay(PhysicalBioAssay.getById(dc, pbaId));
265        }
266       
267        String[] addPhysicalBioAssays = Values.getString(request.getParameter("+PHYSICALBIOASSAY")).split(",");
268        for (int i = 0; i < addPhysicalBioAssays.length; ++i)
269        {
270          int pbaId = Values.getInt(addPhysicalBioAssays[i], -1);
271          if (pbaId != -1) bas.addPhysicalBioAssay(PhysicalBioAssay.getById(dc, pbaId));
272        }
273      }
274      else
275      {
276        // A non-root derived bioassay set may have other derived bioassays as parents
277        String[] removeParents = Values.getString(request.getParameter("-DERIVEDBIOASSAY")).split(",");
278        for (int i = 0; i < removeParents.length; ++i)
279        {
280          int parentId = Values.getInt(removeParents[i], -1);
281          if (parentId != -1) bas.removeParent(DerivedBioAssay.getById(dc, parentId));
282        }
283       
284        String[] addParents = Values.getString(request.getParameter("+DERIVEDBIOASSAY")).split(",");
285        for (int i = 0; i < addParents.length; ++i)
286        {
287          int parentId = Values.getInt(addParents[i], -1);
288          if (parentId != -1) bas.addParent(DerivedBioAssay.getById(dc, parentId));
289        }
290      }
291     
292      // Data files tab
293      boolean validate = Values.getBoolean(request.getParameter("datafiles.validate"));
294      Base.updateFiles(dc, bas, request, validate, cc, maxRecent);
295     
296      // Annotations tab
297      Base.updateAnnotations(dc, bas, bas, request);
298       
299      // OnSave extensions
300      invoker.render(OnSaveRenderer.ON_SAVE);
301      dc.commit();
302      invoker.render(OnSaveRenderer.ON_COMMIT);
303    }
304    catch (Exception ex)
305    {
306      invoker.render(OnSaveRenderer.onRollback(ex));
307      throw ex;
308    }
309    finally
310    {
311      cc.removeObject("item");
312    }
313  }
314  else if ("DeleteItem".equals(cmd))
315  {
316    // Delete a single item and then return to the view page
317    dc = sc.newDbControl();
318    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
319    RemovableUtil.removeRecursively(dc, itemType, Collections.singleton(cc.getId()), true);
320    dc.commit();
321    redirect = viewPage;
322  }
323  else if ("DeleteItems".equals(cmd))
324  {
325    // Delete all selected items on the list page
326    dc = sc.newDbControl();
327    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
328    int numTotal = cc.getSelected().size();
329    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), true);
330    dc.commit();
331    if (numTotal != numRemoved)
332    {
333      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be deleted, because you have no DELETE permission";
334    }
335    redirect = listPage;
336  }
337  else if ("RestoreItem".equals(cmd))
338  {
339    // Restore a single item and then return to the view page
340    dc = sc.newDbControl();
341    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
342    RemovableUtil.removeRecursively(dc, itemType, Collections.singleton(cc.getId()), false);
343    dc.commit();
344    redirect = viewPage;
345  }
346  else if ("RestoreItems".equals(cmd))
347  {
348    // Restore all selected items on the list page
349    dc = sc.newDbControl();
350    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
351    int numTotal = cc.getSelected().size();
352    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), false);
353    dc.commit();
354    if (numTotal != numRemoved)
355    {
356      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be restored, because you have no WRITE permission";
357    }
358    redirect = listPage;
359  }
360  else if ("ShareItem".equals(cmd))
361  {
362    // Display a popup window for sharing a single item
363    dc = sc.newDbControl();
364    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
365    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, Collections.singleton(cc.getId()));
366    dc.close();
367    cc.setObject("MultiPermissions", permissions);
368    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
369  }
370  else if ("ShareItems".equals(cmd))
371  {
372    // Display a popup window for sharing all selected items on the list page
373    dc = sc.newDbControl();
374    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
375    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, cc.getSelected());
376    dc.close();
377    cc.setObject("MultiPermissions", permissions);
378    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
379  }
380  else if ("SetOwnerOfItem".equals(cmd))
381  {
382    // Change owner of items selected on a list page
383    dc = sc.newDbControl();
384    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
385    OwnedItem item = (OwnedItem)itemType.getById(dc, cc.getId());
386    cc.setObject("OwnedItems", Collections.singleton(item));
387    redirect = "../../common/ownership/ownership.jsp?ID="+ID+"&item_type="+itemType.name();
388  }
389  else if ("SetOwnerOfItems".equals(cmd))
390  {
391    // Change owner of items selected on a list page
392    dc = sc.newDbControl();
393    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
394    Set<OwnedItem> items = new HashSet<OwnedItem>();
395    for (Integer id : cc.getSelected())
396    {
397      if (id != null) items.add((OwnedItem)itemType.getById(dc, id));
398    }
399    dc.close();
400    cc.setObject("OwnedItems", items);
401    redirect = "../../common/ownership/ownership.jsp?ID="+ID+"&item_type="+itemType.name();
402  }
403  else if ("ExportItems".equals(cmd))
404  {
405    // Run an export plugin in a list context
406    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
407    final ItemQuery<DerivedBioAssay> query = DerivedBioAssay.getQuery();
408    dc = sc.newDbControl();
409    cc.configureQuery(dc, query, true);
410    cc.setQuery(query);
411    registerExportUtils(cc);
412    JspContext jspContext = ExtensionsControl.createContext(dc, pageContext, GuiContext.list(itemType), null);
413    ExtensionsInvoker listInvoker = ListColumnUtil.useExtensions(jspContext);
414    listInvoker.render(new ListColumnExportRenderer(cc));
415    dc.close();
416    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Export+bioassay+sets";
417  }
418  else if ("ExportItem".equals(cmd))
419  {
420    // Run an export plugin in single-item context
421    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
422    registerExportUtils(cc);
423    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Export+bioassay+set";
424  }
425  else if ("ImportItems".equals(cmd))
426  {
427    // Run an import plugin in a list context
428    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
429    final ItemQuery<DerivedBioAssay> query = DerivedBioAssay.getQuery();
430    dc = sc.newDbControl();
431    cc.configureQuery(dc, query, true);
432    dc.close();
433    cc.setQuery(query);
434    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Import+bioassay+sets";
435  }
436  else if ("ImportItem".equals(cmd))
437  {
438    // Run an import plugin in single-item context
439    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
440    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Import+bioassay+set";
441  }
442  else if ("RunListPlugin".equals(cmd))
443  {
444    // Run another plugin in a list context
445    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
446    final ItemQuery<DerivedBioAssay> query = DerivedBioAssay.getQuery();
447    dc = sc.newDbControl();
448    cc.configureQuery(dc, query, true);
449    dc.close();
450    cc.setQuery(query);
451    redirect = "../../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&main_type=OTHER&title=Run+plugin";
452  }
453  else if ("RunPlugin".equals(cmd))
454  {
455    // Run a plugin in single-item context
456    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
457    redirect = "../../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&main_type=OTHER&title=Run+plugin";
458  }
459  else if ("RunAnalysisPlugin".equals(cmd))
460  {
461    // Run an analysis plugin in single-item context
462    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
463    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&main_type=ANALYZE&title=Run+analysis+plugin";
464  }
465  else if ("NewMergedDerivedBioAssay".equals(cmd))
466  {
467    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
468    cc.setId(0);
469    forward = editPage + "&useParents=DERIVEDBIOASSAY";
470  }
471  else
472  {
473    throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd);
474  }
475}
476finally
477{
478  if (dc != null) dc.close();
479}
480
481if (forward != null)
482{
483  sc.setSessionSetting("alert-message", message);
484  pageContext.forward(forward);
485}
486else if (redirect != null)
487{
488  sc.setSessionSetting("alert-message", message);
489  response.sendRedirect(redirect);
490}
491else if (message == null)
492{
493  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&wait=0");
494}
495else
496{
497  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&message="+HTML.urlEncode(message));
498}
499%>
500
Note: See TracBrowser for help on using the repository browser.