source: trunk/www/biomaterials/samples/index.jsp @ 5645

Last change on this file since 5645 was 5645, checked in by Nicklas Nordborg, 11 years ago

References #1597: Subtypes of items

Added subtype functionality for sample list, view and edit dialog. Show the inversely related subtypes on the subtype view page.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Id
File size: 19.7 KB
Line 
1<%-- $Id: index.jsp 5645 2011-05-27 07:48:13Z 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.Sample"
32  import="net.sf.basedb.core.BioSource"
33  import="net.sf.basedb.core.BioMaterialEvent"
34  import="net.sf.basedb.core.BioPlateEventType"
35  import="net.sf.basedb.core.BioWell"
36  import="net.sf.basedb.core.ItemSubtype"
37  import="net.sf.basedb.core.Extract"
38  import="net.sf.basedb.core.Protocol"
39  import="net.sf.basedb.core.ItemQuery"
40  import="net.sf.basedb.core.ItemResultIterator"
41  import="net.sf.basedb.core.Permission"
42  import="net.sf.basedb.core.ItemContext"
43  import="net.sf.basedb.core.MultiPermissions"
44  import="net.sf.basedb.core.OwnedItem"
45  import="net.sf.basedb.core.PermissionDeniedException"
46  import="net.sf.basedb.core.ItemAlreadyExistsException"
47  import="net.sf.basedb.core.query.Hql"
48  import="net.sf.basedb.core.query.Orders"
49  import="net.sf.basedb.core.query.Restrictions"
50  import="net.sf.basedb.core.query.Expressions"
51  import="net.sf.basedb.util.RemovableUtil"
52  import="net.sf.basedb.util.ShareableUtil"
53  import="net.sf.basedb.util.OwnableUtil"
54  import="net.sf.basedb.clients.web.Base"
55  import="net.sf.basedb.clients.web.WebException"
56  import="net.sf.basedb.util.Values"
57  import="net.sf.basedb.clients.web.plugins.ParentBioMaterialLoader"
58  import="net.sf.basedb.clients.web.plugins.ItemQueryLoader"
59  import="net.sf.basedb.clients.web.util.HTML"
60  import="net.sf.basedb.util.formatter.Formatter"
61  import="net.sf.basedb.util.formatter.WellCoordinateFormatter"
62  import="net.sf.basedb.util.formatter.NameableFormatter"
63  import="net.sf.basedb.clients.web.formatter.FormatterFactory"
64
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
72  import="java.util.Enumeration"
73  import="java.util.Set"
74  import="java.util.HashSet"
75  import="java.util.List"
76  import="java.util.ArrayList"
77  import="java.util.Collections"
78  import="java.util.Date"
79%>
80<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
81<%!
82  private static final ItemContext defaultContext = Base.createDefaultContext("name", "name,itemSubtype,originalQuantity,remainingQuantity,extracts,description");
83  private static final Item itemType = Item.SAMPLE;
84 
85  private static void registerExportUtils(ItemContext cc)
86  {
87    // Register formatters
88    cc.setObject("export.formatter.bioWell.row", new WellCoordinateFormatter(true));
89    cc.setObject("export.formatter.bioWell.column", new WellCoordinateFormatter(false));
90    cc.setObject("export.formatter.&creationEvent.sourceBioMaterials(name)", new NameableFormatter());
91    cc.setObject("export.formatter.&sourceEvents(event.bioMaterial.name)", new NameableFormatter());
92    cc.setObject("export.formatter.&children(name)", new NameableFormatter());
93   
94    // Register dataloaders
95    String sampleParameter = "sample";
96
97    // Child extracts
98    ItemQuery<Extract> extractQuery = Extract.getQuery();
99    extractQuery.include(cc.getInclude());
100    extractQuery.restrict(Restrictions.eq(Hql.property("parent"), Expressions.parameter(sampleParameter)));
101    extractQuery.order(Orders.asc(Hql.property("name")));
102    cc.setObject("export.dataloader.&children(name)", new ItemQueryLoader(extractQuery, sampleParameter));
103
104    // Child samples
105    ItemQuery<Sample> childSamplesQuery = Sample.getQuery();
106    childSamplesQuery.join(Hql.innerJoin("creationEvent", "ce"));
107    childSamplesQuery.join(Hql.innerJoin("ce", "sources", "src"));
108    childSamplesQuery.restrict(Restrictions.eq(Hql.index("src", null), Expressions.parameter(sampleParameter)));
109    childSamplesQuery.order(Orders.asc(Hql.property("name")));
110    childSamplesQuery.include(cc.getInclude());
111    cc.setObject("export.dataloader.&sourceEvents(event.bioMaterial.name)", new ItemQueryLoader(childSamplesQuery, sampleParameter));
112   
113    // Parent samples
114    final ItemQuery<Sample> parentSamplesQuery = Sample.getQuery();
115    parentSamplesQuery.join(Hql.innerJoin("sourceEvents", "srcevt"));
116    parentSamplesQuery.join(Hql.innerJoin("srcevt", "event", "evt"));
117    parentSamplesQuery.restrict(Restrictions.eq(Hql.property("evt", "bioMaterial"),  Expressions.parameter(sampleParameter)));
118    parentSamplesQuery.order(Orders.asc(Hql.property("name")));
119    parentSamplesQuery.include(cc.getInclude());
120    cc.setObject("export.dataloader.&creationEvent.sourceBioMaterials(name)", new ItemQueryLoader(parentSamplesQuery, sampleParameter));
121  } 
122%>
123<%
124final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
125final String ID = sc.getId();
126final String cmd = request.getParameter("cmd");
127final String root = request.getContextPath()+"/";
128final String mode = request.getParameter("mode");
129final String callback = request.getParameter("callback");
130final String itemId = request.getParameter("item_id");
131final String listPage = "list_samples.jsp?ID="+ID
132  +(mode == null ? "" : "&mode="+mode)
133  +(callback == null ? "" : "&callback="+callback)
134  +(itemId == null ? "" : "&item_id="+itemId);
135final String viewPage = "view_sample.jsp?ID="+ID;
136final String editPage = "edit_sample.jsp?ID="+ID;
137
138String forward = null;
139String redirect = null;
140String message = null;
141DbControl dc = null;
142
143try
144{
145  if (cmd == null || "List".equals(cmd))
146  {
147    // Display the list page without updatinging the current context
148    Base.getAndSetCurrentContext(sc, itemType, null, defaultContext, true);
149    redirect = listPage;
150  }
151  else if ("UpdateContext".equals(cmd))
152  {
153    // Display the list page after updating the current context from the request parameters
154    Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
155    redirect = listPage;
156  }
157  else if ("LoadContext".equals(cmd))
158  {
159    // Display the list page after loading a saved context
160    int contextId = Values.getInt(request.getParameter("context"));
161    Base.loadContext(sc, contextId, defaultContext);
162    redirect = listPage;
163  }
164
165  else if ("ViewItem".equals(cmd))
166  {
167    // Display the view page for a single item
168    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
169    forward = viewPage;
170  }
171  else if ("EditItem".equals(cmd))
172  {
173    // Display the edit page for a single item (should be opened in a popup)
174    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
175    redirect = editPage;
176  }
177  else if ("NewItem".equals(cmd))
178  {
179    // Display the edit page for a new item (should be opened in a popup)
180    if (!sc.hasPermission(Permission.CREATE, itemType))
181    {
182      throw new PermissionDeniedException(Permission.CREATE, itemType.toString());
183    }
184    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
185    cc.setId(0);
186    forward = editPage;
187  }
188  else if ("NewPooledItem".equals(cmd))
189  {
190    // Display the edit page for a new pooled item (should be opened in a popup)
191    if (!sc.hasPermission(Permission.CREATE, itemType))
192    {
193      throw new PermissionDeniedException(Permission.CREATE, itemType.toString());
194    }
195    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
196    cc.setId(0);
197    forward = editPage+"&pooled="+true;
198  }
199  else if ("UpdateItem".equals(cmd))
200  {
201    // Update the properties on an item (will close the popup)
202    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, null, defaultContext);
203    final int maxRecent = Base.getMaxRecent(sc);
204    dc = sc.newDbControl();
205    Sample sample = (Sample)cc.getObject("item");
206    if (sample == null)
207    {
208      sample = Sample.getNew(dc);
209      message = "Sample created";
210      dc.saveItem(sample);
211    }
212    else
213    {
214      dc.reattachItem(sample, false);
215      dc.reattachItem(sample.getCreationEvent(), false);
216      message = "Sample updated";
217    }
218
219    // Create extension context
220    JspContext jspContext = ExtensionsControl.createContext(dc, pageContext, GuiContext.item(Item.SAMPLE), sample);
221    ExtensionsInvoker invoker = EditUtil.useOnSaveExtensions(jspContext);
222    try
223    {
224      sample.setName(Values.getStringOrNull(request.getParameter("name")));
225      sample.setDescription(Values.getStringOrNull(request.getParameter("description")));
226      sample.setExternalId(Values.getStringOrNull(request.getParameter("external_id")));
227      sample.setOriginalQuantity(Values.getFloat(request.getParameter("original_quantity"), null));
228     
229      BioMaterialEvent creationEvent = sample.getCreationEvent();
230      if (creationEvent.hasPermission(Permission.WRITE))
231      {
232        Formatter<Date> dateFormatter = FormatterFactory.getDateFormatter(sc);
233        creationEvent.setEventDate(dateFormatter.parseString(Values.getStringOrNull(request.getParameter("event_date"))));
234        int protocolId = Values.getInt(request.getParameter("protocol_id"), -1);
235        if (protocolId >= 0) // < 0 = denied or unchanged
236        {
237          Protocol pt = protocolId == 0 ? null : Protocol.getById(dc, protocolId);
238          creationEvent.setProtocol(pt);
239          if (pt != null) cc.setRecent(pt, maxRecent);
240        }
241      }
242     
243      int subtypeId = Values.getInt(request.getParameter("subtype_id"), -1);
244      if (subtypeId >= 0) // < 0 = denied or unchanged
245      {
246        ItemSubtype subtype = subtypeId == 0 ? null : ItemSubtype.getById(dc, subtypeId);
247        sample.setItemSubtype(subtype);
248        if (subtype != null) cc.setRecent(subtype, maxRecent);
249      }
250     
251      int biowellId = Values.getInt(request.getParameter("biowell_id"), -1);
252      if (biowellId >= 0) // < 0 = denied or unchanged
253      {
254        BioWell bw = biowellId == 0 ? null : BioWell.getById(dc, biowellId);
255        if (bw != null && bw.getBioMaterial() != null)
256        {
257          throw new WebException("popup", "Biowell can not be used",
258              "The biowell [{1}] on bioplate {2} is already connected to another biomaterial", 
259              HTML.encodeTags(bw.getRow()+"," + bw.getColumn()), HTML.encodeTags(bw.getPlate().getName()));
260        }
261        sample.setBioWell(bw);
262        if (bw != null) cc.setRecent(bw.getPlate(), maxRecent);
263      }
264 
265      // Parents tab
266      sample.setPooled(Values.getBoolean(request.getParameter("pooled")));
267      if (!sample.isPooled())
268      {
269        int bioSourceId = Values.getInt(request.getParameter("biosource_id"), -1);
270        if (bioSourceId >= 0) // < 0 = denied or unchanged
271        {
272          BioSource bs = bioSourceId == 0 ? null : BioSource.getById(dc, bioSourceId);
273          sample.setBioSource(bs);
274          if (bs != null) cc.setRecent(bs, maxRecent);
275        }
276      }
277      else
278      {
279        String[] modifiedSamples = Values.getString(request.getParameter("modifiedSamples")).split(",");
280        for (int i = 0; i < modifiedSamples.length; ++i)
281        {
282          int sId = Values.getInt(modifiedSamples[i], -1);
283          if (sId != -1) 
284          {
285            Sample s = Sample.getById(dc, sId);
286            creationEvent.addSource(s, Values.getFloat(request.getParameter("S"+sId), null));
287          }
288        }
289        String[] removedSamples = Values.getString(request.getParameter("removedSamples")).split(",");
290        for (int i = 0; i < removedSamples.length; ++i)
291        {
292          int sId = Values.getInt(removedSamples[i], -1);
293          if (sId != -1) creationEvent.removeSource(Sample.getById(dc, sId));
294        }
295      }
296 
297      // Annotations tab
298      Base.updateAnnotations(dc, sample, sample, request);
299     
300      // OnSave extensions
301      invoker.render(OnSaveRenderer.ON_SAVE);
302      dc.commit();
303      invoker.render(OnSaveRenderer.ON_COMMIT);
304    }
305    catch (Exception ex)
306    {
307      invoker.render(OnSaveRenderer.onRollback(ex));
308      throw ex;
309    }
310    finally
311    {
312      cc.removeObject("item");
313    }
314  }
315  else if ("DeleteItem".equals(cmd))
316  {
317    // Delete a single item and then return to the view page
318    dc = sc.newDbControl();
319    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
320    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), true);
321    dc.commit();
322    redirect = viewPage;
323  }
324  else if ("DeleteItems".equals(cmd))
325  {
326    // Delete all selected items on the list page
327    dc = sc.newDbControl();
328    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
329    int numTotal = cc.getSelected().size();
330    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), true);
331    dc.commit();
332    if (numTotal != numRemoved)
333    {
334      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be deleted, because you have no DELETE permission";
335    }
336    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
337  }
338  else if ("RestoreItem".equals(cmd))
339  {
340    // Restore a single item and then return to the view page
341    dc = sc.newDbControl();
342    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
343    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), false);
344    dc.commit();
345    redirect = viewPage;
346  }
347  else if ("RestoreItems".equals(cmd))
348  {
349    // Restore all selected items on the list page
350    dc = sc.newDbControl();
351    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
352    int numTotal = cc.getSelected().size();
353    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), false);
354    dc.commit();
355    if (numTotal != numRemoved)
356    {
357      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be restored, because you have no WRITE permission";
358    }
359    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
360  }
361  else if ("ShareItem".equals(cmd))
362  {
363    // Display a popup window for sharing a single item
364    dc = sc.newDbControl();
365    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
366    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, Collections.singleton(cc.getId()));
367    dc.close();
368    cc.setObject("MultiPermissions", permissions);
369    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
370  }
371  else if ("ShareItems".equals(cmd))
372  {
373    // Display a popup window for sharing all selected items on the list page
374    dc = sc.newDbControl();
375    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
376    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, cc.getSelected());
377    dc.close();
378    cc.setObject("MultiPermissions", permissions);
379    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
380  }
381  else if ("SetOwnerOfItem".equals(cmd))
382  {
383    // Change owner of items selected on a list page
384    dc = sc.newDbControl();
385    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
386    OwnedItem item = (OwnedItem)itemType.getById(dc, cc.getId());
387    cc.setObject("OwnedItems", Collections.singleton(item));
388    redirect = "../../common/ownership/ownership.jsp?ID="+ID+"&item_type="+itemType.name();
389  }
390  else if ("SetOwnerOfItems".equals(cmd))
391  {
392    // Change owner of items selected on a list page
393    dc = sc.newDbControl();
394    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
395    Set<OwnedItem> items = new HashSet<OwnedItem>();
396    for (Integer id : cc.getSelected())
397    {
398      if (id != null) items.add((OwnedItem)itemType.getById(dc, id));
399    }
400    dc.close();
401    cc.setObject("OwnedItems", items);
402    redirect = "../../common/ownership/ownership.jsp?ID="+ID+"&item_type="+itemType.name();
403  }
404  else if ("ExportItems".equals(cmd))
405  {
406    // Run an export plugin in a list context
407    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
408    final ItemQuery<Sample> query = Sample.getQuery();
409    dc = sc.newDbControl();
410    cc.configureQuery(dc, query, true);
411    dc.close();
412    registerExportUtils(cc);
413    cc.setQuery(query);
414    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Export+samples";
415  }
416  else if ("ExportItem".equals(cmd))
417  {
418    // Run an export plugin in single-item context
419    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
420    registerExportUtils(cc);
421    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Export+sample";
422  }
423  else if ("ImportItems".equals(cmd))
424  {
425    // Run an import plugin in a list context
426    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
427    final ItemQuery<Sample> query = Sample.getQuery();
428    dc = sc.newDbControl();
429    cc.configureQuery(dc, query, true);
430    dc.close();
431    cc.setQuery(query);
432    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Import+sample";
433  }
434  else if ("ImportItem".equals(cmd))
435  {
436    // Run an import plugin in single-item context
437    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
438    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Import+sample";
439  }
440  else if ("RunListPlugin".equals(cmd))
441  {
442    // Run another plugin in a list context
443    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
444    final ItemQuery<Sample> query = Sample.getQuery();
445    dc = sc.newDbControl();
446    cc.configureQuery(dc, query, true);
447    dc.close();
448    cc.setQuery(query);
449    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&main_type=OTHER&title=Run+plugin";
450  }
451  else if ("RunPlugin".equals(cmd))
452  {
453    // Run another plugin in single-item context
454    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
455    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&main_type=OTHER&title=Run+plugin";
456  }
457  else if ("CreateBioMaterialList".equals(cmd))
458  {
459    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
460    final ItemQuery<Sample> query = Sample.getQuery();
461    dc = sc.newDbControl();
462    cc.configureQuery(dc, query, true);
463    dc.close();
464    cc.setQuery(query);
465    redirect = "../lists/index.jsp?ID="+ID+"&cmd=NewItem&addBioMaterials=1&formId=samples&fromContext=SAMPLE";
466  }
467  else if ("PlaceOnPlate".equals(cmd))
468  {
469    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
470    final ItemQuery<Sample> query = Sample.getQuery();
471    dc = sc.newDbControl();
472    cc.configureQuery(dc, query, true);
473    dc.close();
474    cc.filterOnSelectedItems(query);
475    sc.getCurrentContext(Item.BIOPLATEEVENT, BioPlateEventType.PLACE_ON_PLATE).setQuery(query);
476    redirect = "../wizards/place_on_plate.jsp?ID="+ID+"&itemType=SAMPLE";
477  }
478  else
479  {
480    throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd);
481  }
482}
483finally
484{
485  if (dc != null) dc.close();
486}
487
488if (forward != null)
489{
490  pageContext.forward(forward);
491}
492else if (redirect != null)
493{
494  response.sendRedirect(redirect);
495}
496else if (message == null)
497{
498  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&wait=0");
499}
500else
501{
502  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&message="+HTML.urlEncode(message));
503}
504%>
505
Note: See TracBrowser for help on using the repository browser.