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

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

References #1589: Delete deprecated classes and methods

Removed deprecated methods and classes from the core API.

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