source: trunk/www/biomaterials/extracts/index.jsp @ 4895

Last change on this file since 4895 was 4895, checked in by Martin Svensson, 13 years ago

Refereces #1128 Added new formatter classes and tested them on complex properties of extracts. Export of other items' complex properties should be implemented next.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Id
File size: 16.1 KB
Line 
1<%-- $Id: index.jsp 4895 2009-04-20 06:38:02Z martin $
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 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.Extract"
33  import="net.sf.basedb.core.BioMaterialEvent"
34  import="net.sf.basedb.core.BioWell"
35  import="net.sf.basedb.core.LabeledExtract"
36  import="net.sf.basedb.core.Protocol"
37  import="net.sf.basedb.core.ItemQuery"
38  import="net.sf.basedb.core.ItemResultIterator"
39  import="net.sf.basedb.core.Permission"
40  import="net.sf.basedb.core.ItemContext"
41  import="net.sf.basedb.core.MultiPermissions"
42  import="net.sf.basedb.core.OwnedItem"
43  import="net.sf.basedb.core.PermissionDeniedException"
44  import="net.sf.basedb.core.ItemAlreadyExistsException"
45  import="net.sf.basedb.util.RemovableUtil"
46  import="net.sf.basedb.util.ShareableUtil"
47  import="net.sf.basedb.util.OwnableUtil"
48  import="net.sf.basedb.clients.web.Base"
49  import="net.sf.basedb.clients.web.WebException"
50  import="net.sf.basedb.util.Values"
51  import="net.sf.basedb.clients.web.util.HTML"
52  import="net.sf.basedb.util.formatter.ParentPropertyFormatter"
53  import="net.sf.basedb.util.formatter.PropertyFormatter"
54  import="net.sf.basedb.util.formatter.Formatter"
55  import="net.sf.basedb.util.formatter.WellCoordinateFormatter"
56  import="net.sf.basedb.clients.web.formatter.FormatterFactory"
57  import="java.util.Enumeration"
58  import="java.util.Set"
59  import="java.util.HashSet"
60  import="java.util.List"
61  import="java.util.ArrayList"
62  import="java.util.Collections"
63  import="java.util.Date"
64%>
65<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
66<%!
67  private static final ItemContext defaultContext = Base.createDefaultContext("name", "name,originalQuantity,remainingQuantity,labeledextracts,description");
68  private static final Item itemType = Item.EXTRACT;
69 
70  private static void registerExportFormatters(ItemContext cc)
71  {
72    cc.setObject("export.formatter.bioWell.row", new WellCoordinateFormatter(true));
73    cc.setObject("export.formatter.bioWell.column", new WellCoordinateFormatter(false));
74    cc.setObject("export.formatter.&children(name)", new PropertyFormatter("getLabeledExtracts"));
75    cc.setObject("export.formatter.&creationEvent.sourceBioMaterials(name)", new ParentPropertyFormatter("getCreationEvent.getSources", "getSample"));
76  }
77%>
78<%
79final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
80final String ID = sc.getId();
81final String cmd = request.getParameter("cmd");
82final String root = request.getContextPath()+"/";
83final String mode = request.getParameter("mode");
84final String callback = request.getParameter("callback");
85final String itemId = request.getParameter("item_id");
86final String listPage = "list_extracts.jsp?ID="+ID
87  +(mode == null ? "" : "&mode="+mode)
88  +(callback == null ? "" : "&callback="+callback)
89  +(itemId == null ? "" : "&item_id="+itemId);
90final String viewPage = "view_extract.jsp?ID="+ID;
91final String editPage = "edit_extract.jsp?ID="+ID;
92
93String forward = null;
94String redirect = null;
95String message = null;
96DbControl dc = null;
97
98try
99{
100  if (cmd == null || "List".equals(cmd))
101  {
102    // Display the list page without updatinging the current context
103    Base.getAndSetCurrentContext(sc, itemType, null, defaultContext, true);
104    redirect = listPage;
105  }
106  else if ("UpdateContext".equals(cmd))
107  {
108    // Display the list page after updating the current context from the request parameters
109    Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
110    redirect = listPage;
111  }
112  else if ("LoadContext".equals(cmd))
113  {
114    // Display the list page after loading a saved context
115    int contextId = Values.getInt(request.getParameter("context"));
116    Base.loadContext(sc, contextId, defaultContext);
117    redirect = listPage;
118  }
119
120  else if ("ViewItem".equals(cmd))
121  {
122    // Display the view page for a single item
123    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
124    forward = viewPage;
125  }
126  else if ("EditItem".equals(cmd))
127  {
128    // Display the edit page for a single item (should be opened in a popup)
129    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
130    redirect = editPage;
131  }
132  else if ("NewItem".equals(cmd))
133  {
134    // Display the edit page for a new item (should be opened in a popup)
135    if (!sc.hasPermission(Permission.CREATE, itemType))
136    {
137      throw new PermissionDeniedException(Permission.CREATE, itemType.toString());
138    }
139    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
140    cc.setId(0);
141    forward = editPage;
142  }
143  else if ("NewPooledItem".equals(cmd))
144  {
145    // Display the edit page for a new pooled item (should be opened in a popup)
146    if (!sc.hasPermission(Permission.CREATE, itemType))
147    {
148      throw new PermissionDeniedException(Permission.CREATE, itemType.toString());
149    }
150    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
151    cc.setId(0);
152    forward = editPage+"&pooled="+true;
153  }
154  else if ("UpdateItem".equals(cmd))
155  {
156    // Update the properties on an item (will close the popup)
157    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, null, defaultContext);
158    final int maxRecent = Base.getMaxRecent(sc);
159    dc = sc.newDbControl();
160    Extract extract = (Extract)cc.getObject("item");
161    if (extract == null)
162    {
163      extract = Extract.getNew(dc);
164      message = "Extract created";
165      dc.saveItem(extract);
166    }
167    else
168    {
169      dc.reattachItem(extract);
170      dc.reattachItem(extract.getCreationEvent());
171      message = "Extract updated";
172    }
173    extract.setName(Values.getStringOrNull(request.getParameter("name")));
174    extract.setDescription(Values.getStringOrNull(request.getParameter("description")));
175    extract.setExternalId(Values.getStringOrNull(request.getParameter("external_id")));
176    extract.setOriginalQuantity(Values.getFloat(request.getParameter("original_quantity"), null));
177   
178    BioMaterialEvent creationEvent = extract.getCreationEvent();
179    Formatter<Date> dateFormatter = FormatterFactory.getDateFormatter(sc);
180    creationEvent.setEventDate(dateFormatter.parseString(Values.getStringOrNull(request.getParameter("event_date"))));
181    int protocolId = Values.getInt(request.getParameter("protocol_id"), -1);
182    if (protocolId >= 0) // < 0 = denied or unchanged
183    {
184      Protocol pt = protocolId == 0 ? null : Protocol.getById(dc, protocolId);
185      creationEvent.setProtocol(pt);
186      if (pt != null) cc.setRecent(pt, maxRecent);
187    }
188   
189    int biowellId = Values.getInt(request.getParameter("biowell_id"), -1);
190    if (biowellId >= 0) // < 0 = denied or unchanged
191    {
192      BioWell bw = biowellId == 0 ? null : BioWell.getById(dc, biowellId);
193      if (bw != null && bw.getBioMaterial() != null)
194      {
195        throw new WebException("popup", "Biowell can not be used",
196            "The biowell [{1}] on bioplate {2} is already connected to another biomaterial", 
197            HTML.encodeTags(bw.getRow()+"," + bw.getColumn()), HTML.encodeTags(bw.getPlate().getName()));
198      }
199      extract.setBioWell(bw);
200      if (bw != null) cc.setRecent(bw.getPlate(), maxRecent);
201    }
202
203    // Parents tab
204    extract.setPooled(Values.getBoolean(request.getParameter("pooled")));
205    if (!extract.isPooled())
206    {
207      int sampleId = Values.getInt(request.getParameter("sample_id"), -1);
208      if (sampleId >= 0) // < 0 = denied or unchanged
209      {
210        Sample s = sampleId == 0 ? null : Sample.getById(dc, sampleId);
211        extract.setSample(s, s == null ? null : Values.getFloat(request.getParameter("used_from_sample"), null));
212        if (s != null) cc.setRecent(s, maxRecent);
213      }
214    }
215    else
216    {
217      String[] modifiedExtracts = Values.getString(request.getParameter("modifiedExtracts")).split(",");
218      for (int i = 0; i < modifiedExtracts.length; ++i)
219      {
220        int eId = Values.getInt(modifiedExtracts[i], -1);
221        if (eId != -1) 
222        {
223          Extract e = Extract.getById(dc, eId);
224          creationEvent.addSource(e, Values.getFloat(request.getParameter("E"+eId), null));
225        }
226      }
227      String[] removedExtracts = Values.getString(request.getParameter("removedExtracts")).split(",");
228      for (int i = 0; i < removedExtracts.length; ++i)
229      {
230        int eId = Values.getInt(removedExtracts[i], -1);
231        if (eId != -1) creationEvent.removeSource(Extract.getById(dc, eId));
232      }
233    }
234
235    // Annotations tab
236    Base.updateAnnotations(dc, extract, extract, request);
237    dc.commit();
238    cc.removeObject("item");
239  }
240  else if ("DeleteItem".equals(cmd))
241  {
242    // Delete a single item and then return to the view page
243    dc = sc.newDbControl();
244    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
245    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), true);
246    dc.commit();
247    redirect = viewPage;
248  }
249  else if ("DeleteItems".equals(cmd))
250  {
251    // Delete all selected items on the list page
252    dc = sc.newDbControl();
253    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
254    int numTotal = cc.getSelected().size();
255    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), true);
256    dc.commit();
257    if (numTotal != numRemoved)
258    {
259      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be deleted, because you have no DELETE permission";
260    }
261    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
262  }
263  else if ("RestoreItem".equals(cmd))
264  {
265    // Restore a single item and then return to the view page
266    dc = sc.newDbControl();
267    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
268    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), false);
269    dc.commit();
270    redirect = viewPage;
271  }
272  else if ("RestoreItems".equals(cmd))
273  {
274    // Restore all selected items on the list page
275    dc = sc.newDbControl();
276    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
277    int numTotal = cc.getSelected().size();
278    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), false);
279    dc.commit();
280    if (numTotal != numRemoved)
281    {
282      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be restored, because you have no WRITE permission";
283    }
284    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
285  }
286  else if ("ShareItem".equals(cmd))
287  {
288    // Display a popup window for sharing a single item
289    dc = sc.newDbControl();
290    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
291    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, Collections.singleton(cc.getId()));
292    dc.close();
293    cc.setObject("MultiPermissions", permissions);
294    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
295  }
296  else if ("ShareItems".equals(cmd))
297  {
298    // Display a popup window for sharing all selected items on the list page
299    dc = sc.newDbControl();
300    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
301    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, cc.getSelected());
302    dc.close();
303    cc.setObject("MultiPermissions", permissions);
304    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
305  }
306  else if ("SetOwnerOfItem".equals(cmd))
307  {
308    // Change owner of items selected on a list page
309    dc = sc.newDbControl();
310    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
311    OwnedItem item = (OwnedItem)itemType.getById(dc, cc.getId());
312    cc.setObject("OwnedItems", Collections.singleton(item));
313    redirect = "../../common/ownership/ownership.jsp?ID="+ID+"&item_type="+itemType.name();
314  }
315  else if ("SetOwnerOfItems".equals(cmd))
316  {
317    // Change owner of items selected on a list page
318    dc = sc.newDbControl();
319    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
320    Set<OwnedItem> items = new HashSet<OwnedItem>();
321    for (Integer id : cc.getSelected())
322    {
323      if (id != null) items.add((OwnedItem)itemType.getById(dc, id));
324    }
325    dc.close();
326    cc.setObject("OwnedItems", items);
327    redirect = "../../common/ownership/ownership.jsp?ID="+ID+"&item_type="+itemType.name();
328  }
329  else if ("ExportItems".equals(cmd))
330  {
331    // Run an export plugin in a list context
332    dc = sc.newDbControl();
333    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
334    final ItemQuery<Extract> query = Extract.getQuery();
335    cc.configureQuery(query, true);
336    registerExportFormatters(cc);
337    dc.close();
338    cc.setQuery(query);
339    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Export+extracts";   
340  }
341  else if ("ExportItem".equals(cmd))
342  {
343    // Run an export plugin in single-item context
344    dc = sc.newDbControl();
345    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
346    registerExportFormatters(cc);
347    dc.close();
348    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Export+extract";
349  }
350  else if ("ImportItems".equals(cmd))
351  {
352    // Run an import plugin in a list context
353    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
354    final ItemQuery<Extract> query = Extract.getQuery();
355    cc.configureQuery(query, true);
356    cc.setQuery(query);
357    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Import+extracts";
358  }
359  else if ("ImportItem".equals(cmd))
360  {
361    // Run an import plugin in single-item context
362    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
363    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Import+extract";
364  }
365  else if ("RunListPlugin".equals(cmd))
366  {
367    // Run another plugin in a list context
368    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
369    final ItemQuery<Extract> query = Extract.getQuery();
370    cc.configureQuery(query, true);
371    cc.setQuery(query);
372    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&main_type=OTHER&title=Run+plugin";
373  }
374  else if ("RunPlugin".equals(cmd))
375  {
376    // Run another plugin in single-item context
377    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
378    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&main_type=OTHER&title=Run+plugin";
379  }
380  else if ("CreateBioMaterialList".equals(cmd))
381  {
382    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
383    final ItemQuery<Extract> query = Extract.getQuery();
384    cc.configureQuery(query, true);
385    cc.setQuery(query);
386    redirect = "../lists/index.jsp?ID="+ID+"&cmd=NewItem&addBioMaterials=1&formId=extracts&fromContext=EXTRACT";
387  }
388  else
389  {
390    throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd);
391  }
392}
393finally
394{
395  if (dc != null) dc.close();
396}
397
398if (forward != null)
399{
400  pageContext.forward(forward);
401}
402else if (redirect != null)
403{
404  response.sendRedirect(redirect);
405}
406else if (message == null)
407{
408  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&wait=0");
409}
410else
411{
412  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&message="+HTML.urlEncode(message));
413}
414%>
415
Note: See TracBrowser for help on using the repository browser.