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

Last change on this file since 2917 was 2917, checked in by Nicklas Nordborg, 17 years ago

References #348: Take ownership of items

Implemented on all biomaterial pages

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Id
File size: 13.9 KB
Line 
1<%-- $Id: index.jsp 2917 2006-11-15 10:28:36Z nicklas $
2  ------------------------------------------------------------------
3  Copyright (C) Authors contributing to this file.
4
5  This file is part of BASE - BioArray Software Environment.
6  Available at http://base.thep.lu.se/
7
8  BASE is free software; you can redistribute it and/or
9  modify it under the terms of the GNU General Public License
10  as published by the Free Software Foundation; either version 2
11  of the License, or (at your option) any later version.
12
13  BASE is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  GNU General Public License for more details.
17
18  You should have received a copy of the GNU General Public License
19  along with this program; if not, write to the Free Software
20  Foundation, Inc., 59 Temple Place - Suite 330,
21  Boston, MA  02111-1307, USA.
22  ------------------------------------------------------------------
23
24  @author Nicklas
25  @version 2.0
26--%>
27<%@ page
28  import="net.sf.basedb.core.SessionControl"
29  import="net.sf.basedb.core.DbControl"
30  import="net.sf.basedb.core.Item"
31  import="net.sf.basedb.core.Include"
32  import="net.sf.basedb.core.Sample"
33  import="net.sf.basedb.core.BioSource"
34  import="net.sf.basedb.core.BioMaterialEvent"
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.PermissionDeniedException"
42  import="net.sf.basedb.core.ItemAlreadyExistsException"
43  import="net.sf.basedb.util.RemovableUtil"
44  import="net.sf.basedb.util.ShareableUtil"
45  import="net.sf.basedb.util.OwnableUtil"
46  import="net.sf.basedb.clients.web.Base"
47  import="net.sf.basedb.clients.web.WebException"
48  import="net.sf.basedb.util.Values"
49  import="net.sf.basedb.clients.web.util.HTML"
50  import="java.util.Enumeration"
51  import="java.util.Set"
52  import="java.util.HashSet"
53  import="java.util.List"
54  import="java.util.ArrayList"
55  import="java.util.Collections"
56%>
57<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
58<%!
59  private static final ItemContext defaultContext = Base.createDefaultContext("name", "name,originalQuantity,remainingQuantity,extracts,description");
60  private static final Item itemType = Item.SAMPLE;
61%>
62<%
63final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
64final String ID = sc.getId();
65final String cmd = request.getParameter("cmd");
66final String root = request.getContextPath()+"/";
67final String mode = request.getParameter("mode");
68final String callback = request.getParameter("callback");
69final String itemId = request.getParameter("item_id");
70final String listPage = "list_samples.jsp?ID="+ID
71  +(mode == null ? "" : "&mode="+mode)
72  +(callback == null ? "" : "&callback="+callback)
73  +(itemId == null ? "" : "&item_id="+itemId);
74final String viewPage = "view_sample.jsp?ID="+ID;
75final String editPage = "edit_sample.jsp?ID="+ID;
76
77String forward = null;
78String redirect = null;
79String message = null;
80DbControl dc = null;
81
82try
83{
84  if (cmd == null || "List".equals(cmd))
85  {
86    // Display the list page without updatinging the current context
87    Base.getAndSetCurrentContext(sc, itemType, null, defaultContext, true);
88    redirect = listPage;
89  }
90  else if ("UpdateContext".equals(cmd))
91  {
92    // Display the list page after updating the current context from the request parameters
93    Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
94    redirect = listPage;
95  }
96  else if ("LoadContext".equals(cmd))
97  {
98    // Display the list page after loading a saved context
99    int contextId = Values.getInt(request.getParameter("context"));
100    Base.loadContext(sc, contextId, defaultContext);
101    redirect = listPage;
102  }
103
104  else if ("ViewItem".equals(cmd))
105  {
106    // Display the view page for a single item
107    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
108    forward = viewPage;
109  }
110  else if ("EditItem".equals(cmd))
111  {
112    // Display the edit page for a single item (should be opened in a popup)
113    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
114    redirect = editPage;
115  }
116  else if ("NewItem".equals(cmd))
117  {
118    // Display the edit page for a new item (should be opened in a popup)
119    if (!sc.hasPermission(Permission.CREATE, itemType))
120    {
121      throw new PermissionDeniedException(Permission.CREATE, itemType.toString());
122    }
123    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
124    cc.setId(0);
125    forward = editPage;
126  }
127  else if ("NewPooledItem".equals(cmd))
128  {
129    // Display the edit page for a new pooled 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+"&pooled="+true;
137  }
138  else if ("UpdateItem".equals(cmd))
139  {
140    // Update the properties on an item (will close the popup)
141    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, null, defaultContext);
142    final int maxRecent = Base.getMaxRecent(sc);
143    dc = sc.newDbControl();
144    Sample sample = (Sample)cc.getObject("item");
145    if (sample == null)
146    {
147      sample = Sample.getNew(dc);
148      message = "Sample created";
149      dc.saveItem(sample);
150    }
151    else
152    {
153      dc.reattachItem(sample);
154      dc.reattachItem(sample.getCreationEvent());
155      message = "Sample updated";
156    }
157    sample.setName(Values.getStringOrNull(request.getParameter("name")));
158    sample.setDescription(Values.getStringOrNull(request.getParameter("description")));
159    sample.setExternalId(Values.getStringOrNull(request.getParameter("external_id")));
160    sample.setOriginalQuantity(Values.getFloat(request.getParameter("original_quantity"), null));
161   
162    BioMaterialEvent creationEvent = sample.getCreationEvent();
163    creationEvent.setEventDate(Values.getDate(request.getParameter("event_date")));
164    int protocolId = Values.getInt(request.getParameter("protocol_id"), -1);
165    if (protocolId >= 0) // < 0 = denied or unchanged
166    {
167      Protocol pt = protocolId == 0 ? null : Protocol.getById(dc, protocolId);
168      creationEvent.setProtocol(pt);
169      if (pt != null) cc.setRecent(pt, maxRecent);
170    }
171
172    // Parents tab
173    sample.setPooled(Values.getBoolean(request.getParameter("pooled")));
174    if (!sample.isPooled())
175    {
176      int bioSourceId = Values.getInt(request.getParameter("biosource_id"), -1);
177      if (bioSourceId >= 0) // < 0 = denied or unchanged
178      {
179        BioSource bs = bioSourceId == 0 ? null : BioSource.getById(dc, bioSourceId);
180        sample.setBioSource(bs);
181        if (bs != null) cc.setRecent(bs, maxRecent);
182      }
183    }
184    else
185    {
186      String[] modifiedSamples = Values.getString(request.getParameter("modifiedSamples")).split(",");
187      for (int i = 0; i < modifiedSamples.length; ++i)
188      {
189        int sId = Values.getInt(modifiedSamples[i], -1);
190        if (sId != -1) 
191        {
192          Sample s = Sample.getById(dc, sId);
193          creationEvent.addSource(s, Values.getFloat(request.getParameter("S"+sId), null));
194        }
195      }
196      String[] removedSamples = Values.getString(request.getParameter("removedSamples")).split(",");
197      for (int i = 0; i < removedSamples.length; ++i)
198      {
199        int sId = Values.getInt(removedSamples[i], -1);
200        if (sId != -1) creationEvent.removeSource(Sample.getById(dc, sId));
201      }
202    }
203
204    // Annotations tab
205    Base.updateAnnotations(dc, sample, sample, request);
206    dc.commit();
207    cc.removeObject("item");
208  }
209  else if ("DeleteItem".equals(cmd))
210  {
211    // Delete a single item and then return to the view page
212    dc = sc.newDbControl();
213    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
214    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), true);
215    dc.commit();
216    redirect = viewPage;
217  }
218  else if ("DeleteItems".equals(cmd))
219  {
220    // Delete all selected items on the list page
221    dc = sc.newDbControl();
222    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
223    int numTotal = cc.getSelected().size();
224    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), true);
225    dc.commit();
226    if (numTotal != numRemoved)
227    {
228      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be deleted, because you have no DELETE permission";
229    }
230    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
231  }
232  else if ("RestoreItem".equals(cmd))
233  {
234    // Restore a single item and then return to the view page
235    dc = sc.newDbControl();
236    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
237    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), false);
238    dc.commit();
239    redirect = viewPage;
240  }
241  else if ("RestoreItems".equals(cmd))
242  {
243    // Restore all selected items on the list page
244    dc = sc.newDbControl();
245    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
246    int numTotal = cc.getSelected().size();
247    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), false);
248    dc.commit();
249    if (numTotal != numRemoved)
250    {
251      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be restored, because you have no WRITE permission";
252    }
253    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
254  }
255  else if ("ShareItem".equals(cmd))
256  {
257    // Display a popup window for sharing a single item
258    dc = sc.newDbControl();
259    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
260    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, Collections.singleton(cc.getId()));
261    dc.close();
262    cc.setObject("MultiPermissions", permissions);
263    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
264  }
265  else if ("ShareItems".equals(cmd))
266  {
267    // Display a popup window for sharing all selected items on the list page
268    dc = sc.newDbControl();
269    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
270    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, cc.getSelected());
271    dc.close();
272    cc.setObject("MultiPermissions", permissions);
273    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
274  }
275  else if ("TakeOwnershipOfItem".equals(cmd))
276  {
277    // Take ownership a single item and then return to the view page
278    dc = sc.newDbControl();
279    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
280    OwnableUtil.setOwner(dc, itemType, Collections.singleton(cc.getId()), null);
281    dc.commit();
282    redirect = viewPage;
283  }
284  else if ("TakeOwnershipOfItems".equals(cmd))
285  {
286    // Take ownership all selected items on the list page
287    dc = sc.newDbControl();
288    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
289    int numTotal = cc.getSelected().size();
290    int numOwned = OwnableUtil.setOwner(dc, itemType, cc.getSelected(), null);
291    dc.commit();
292    if (numTotal != numOwned)
293    {
294      message = (numOwned == 0 ? "No" : "Only "+numOwned+" of "+numTotal) + " items could be taken, because you have no SET_OWNER permission";
295    }
296    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
297  }
298  else if ("ExportItems".equals(cmd))
299  {
300    // Run an export plugin in a list context
301    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
302    final ItemQuery<Sample> query = Sample.getQuery();
303    cc.configureQuery(query, true);
304    cc.setQuery(query);
305    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Export+samples";
306  }
307  else if ("ExportItem".equals(cmd))
308  {
309    // Run an export plugin in single-item context
310    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
311    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Export+sample";
312  }
313  else if ("ImportItems".equals(cmd))
314  {
315    // Run an import plugin in a list context
316    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
317    final ItemQuery<Sample> query = Sample.getQuery();
318    cc.configureQuery(query, true);
319    cc.setQuery(query);
320    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Import+sample";
321  }
322  else if ("ImportItem".equals(cmd))
323  {
324    // Run an import plugin in single-item context
325    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
326    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Import+sample";
327  }
328  else if ("RunListPlugin".equals(cmd))
329  {
330    // Run another plugin in a list context
331    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
332    final ItemQuery<Sample> query = Sample.getQuery();
333    cc.configureQuery(query, true);
334    cc.setQuery(query);
335    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&main_type=OTHER&title=Run+plugin";
336  }
337  else if ("RunPlugin".equals(cmd))
338  {
339    // Run another plugin in single-item context
340    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
341    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&main_type=OTHER&title=Run+plugin";
342  }
343  else
344  {
345    throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd);
346  }
347}
348finally
349{
350  if (dc != null) dc.close();
351}
352
353if (forward != null)
354{
355  pageContext.forward(forward);
356}
357else if (redirect != null)
358{
359  response.sendRedirect(redirect);
360}
361else if (message == null)
362{
363  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&wait=0");
364}
365else
366{
367  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&message="+HTML.urlEncode(message));
368}
369%>
370
Note: See TracBrowser for help on using the repository browser.