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

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

References #1128 Undoing changeset [4895]

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