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

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

Clear hidden 'exclude' filter that was set by popups

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Id
File size: 12.9 KB
Line 
1<%-- $Id: index.jsp 2811 2006-10-26 10:05:23Z 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.clients.web.Base"
46  import="net.sf.basedb.clients.web.WebException"
47  import="net.sf.basedb.util.Values"
48  import="net.sf.basedb.clients.web.util.HTML"
49  import="java.util.Enumeration"
50  import="java.util.Set"
51  import="java.util.HashSet"
52  import="java.util.List"
53  import="java.util.ArrayList"
54  import="java.util.Collections"
55%>
56<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
57<%!
58  private static final ItemContext defaultContext = Base.createDefaultContext("name", "name,originalQuantity,remainingQuantity,extracts,description");
59  private static final Item itemType = Item.SAMPLE;
60%>
61<%
62final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
63final String ID = sc.getId();
64final String cmd = request.getParameter("cmd");
65final String root = request.getContextPath()+"/";
66final String mode = request.getParameter("mode");
67final String callback = request.getParameter("callback");
68final String itemId = request.getParameter("item_id");
69final String listPage = "list_samples.jsp?ID="+ID
70  +(mode == null ? "" : "&mode="+mode)
71  +(callback == null ? "" : "&callback="+callback)
72  +(itemId == null ? "" : "&item_id="+itemId);
73final String viewPage = "view_sample.jsp?ID="+ID;
74final String editPage = "edit_sample.jsp?ID="+ID;
75
76String forward = null;
77String redirect = null;
78String message = null;
79DbControl dc = null;
80
81try
82{
83  if (cmd == null || "List".equals(cmd))
84  {
85    // Display the list page without updatinging the current context
86    Base.getAndSetCurrentContext(sc, itemType, null, defaultContext, true);
87    redirect = listPage;
88  }
89  else if ("UpdateContext".equals(cmd))
90  {
91    // Display the list page after updating the current context from the request parameters
92    Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
93    redirect = listPage;
94  }
95  else if ("LoadContext".equals(cmd))
96  {
97    // Display the list page after loading a saved context
98    int contextId = Values.getInt(request.getParameter("context"));
99    Base.loadContext(sc, contextId, defaultContext);
100    redirect = listPage;
101  }
102
103  else if ("ViewItem".equals(cmd))
104  {
105    // Display the view page for a single item
106    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
107    forward = viewPage;
108  }
109  else if ("EditItem".equals(cmd))
110  {
111    // Display the edit page for a single item (should be opened in a popup)
112    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
113    redirect = editPage;
114  }
115  else if ("NewItem".equals(cmd))
116  {
117    // Display the edit page for a new item (should be opened in a popup)
118    if (!sc.hasPermission(Permission.CREATE, itemType))
119    {
120      throw new PermissionDeniedException(Permission.CREATE, itemType.toString());
121    }
122    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
123    cc.setId(0);
124    forward = editPage;
125  }
126  else if ("NewPooledItem".equals(cmd))
127  {
128    // Display the edit page for a new pooled item (should be opened in a popup)
129    if (!sc.hasPermission(Permission.CREATE, itemType))
130    {
131      throw new PermissionDeniedException(Permission.CREATE, itemType.toString());
132    }
133    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
134    cc.setId(0);
135    forward = editPage+"&pooled="+true;
136  }
137  else if ("UpdateItem".equals(cmd))
138  {
139    // Update the properties on an item (will close the popup)
140    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, null, defaultContext);
141    final int maxRecent = Base.getMaxRecent(sc);
142    dc = sc.newDbControl();
143    Sample sample = (Sample)cc.getObject("item");
144    if (sample == null)
145    {
146      sample = Sample.getNew(dc);
147      message = "Sample created";
148      dc.saveItem(sample);
149    }
150    else
151    {
152      dc.reattachItem(sample);
153      dc.reattachItem(sample.getCreationEvent());
154      message = "Sample updated";
155    }
156    sample.setName(Values.getStringOrNull(request.getParameter("name")));
157    sample.setDescription(Values.getStringOrNull(request.getParameter("description")));
158    sample.setExternalId(Values.getStringOrNull(request.getParameter("external_id")));
159    sample.setOriginalQuantity(Values.getFloat(request.getParameter("original_quantity"), null));
160   
161    BioMaterialEvent creationEvent = sample.getCreationEvent();
162    creationEvent.setEventDate(Values.getDate(request.getParameter("event_date")));
163    int protocolId = Values.getInt(request.getParameter("protocol_id"), -1);
164    if (protocolId >= 0) // < 0 = denied or unchanged
165    {
166      Protocol pt = protocolId == 0 ? null : Protocol.getById(dc, protocolId);
167      creationEvent.setProtocol(pt);
168      if (pt != null) cc.setRecent(pt, maxRecent);
169    }
170
171    // Parents tab
172    sample.setPooled(Values.getBoolean(request.getParameter("pooled")));
173    if (!sample.isPooled())
174    {
175      int bioSourceId = Values.getInt(request.getParameter("biosource_id"), -1);
176      if (bioSourceId >= 0) // < 0 = denied or unchanged
177      {
178        BioSource bs = bioSourceId == 0 ? null : BioSource.getById(dc, bioSourceId);
179        sample.setBioSource(bs);
180        if (bs != null) cc.setRecent(bs, maxRecent);
181      }
182    }
183    else
184    {
185      String[] modifiedSamples = Values.getString(request.getParameter("modifiedSamples")).split(",");
186      for (int i = 0; i < modifiedSamples.length; ++i)
187      {
188        int sId = Values.getInt(modifiedSamples[i], -1);
189        if (sId != -1) 
190        {
191          Sample s = Sample.getById(dc, sId);
192          creationEvent.addSource(s, Values.getFloat(request.getParameter("S"+sId), null));
193        }
194      }
195      String[] removedSamples = Values.getString(request.getParameter("removedSamples")).split(",");
196      for (int i = 0; i < removedSamples.length; ++i)
197      {
198        int sId = Values.getInt(removedSamples[i], -1);
199        if (sId != -1) creationEvent.removeSource(Sample.getById(dc, sId));
200      }
201    }
202
203    // Annotations tab
204    Base.updateAnnotations(dc, sample, sample, request);
205    dc.commit();
206    cc.removeObject("item");
207  }
208  else if ("DeleteItem".equals(cmd))
209  {
210    // Delete a single item and then return to the view page
211    dc = sc.newDbControl();
212    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
213    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), true);
214    dc.commit();
215    redirect = viewPage;
216  }
217  else if ("DeleteItems".equals(cmd))
218  {
219    // Delete all selected items on the list page
220    dc = sc.newDbControl();
221    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
222    int numTotal = cc.getSelected().size();
223    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), true);
224    dc.commit();
225    if (numTotal != numRemoved)
226    {
227      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be deleted, because you have no DELETE permission";
228    }
229    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
230  }
231  else if ("RestoreItem".equals(cmd))
232  {
233    // Restore a single item and then return to the view page
234    dc = sc.newDbControl();
235    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
236    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), false);
237    dc.commit();
238    redirect = viewPage;
239  }
240  else if ("RestoreItems".equals(cmd))
241  {
242    // Restore all selected items on the list page
243    dc = sc.newDbControl();
244    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
245    int numTotal = cc.getSelected().size();
246    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), false);
247    dc.commit();
248    if (numTotal != numRemoved)
249    {
250      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be restored, because you have no WRITE permission";
251    }
252    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
253  }
254  else if ("ShareItem".equals(cmd))
255  {
256    // Display a popup window for sharing a single item
257    dc = sc.newDbControl();
258    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
259    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, Collections.singleton(cc.getId()));
260    dc.close();
261    cc.setObject("MultiPermissions", permissions);
262    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
263  }
264  else if ("ShareItems".equals(cmd))
265  {
266    // Display a popup window for sharing all selected items on the list page
267    dc = sc.newDbControl();
268    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
269    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, cc.getSelected());
270    dc.close();
271    cc.setObject("MultiPermissions", permissions);
272    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
273  }
274  else if ("ExportItems".equals(cmd))
275  {
276    // Run an export plugin in a list context
277    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
278    final ItemQuery<Sample> query = Sample.getQuery();
279    cc.configureQuery(query, true);
280    cc.setQuery(query);
281    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Export+samples";
282  }
283  else if ("ExportItem".equals(cmd))
284  {
285    // Run an export plugin in single-item context
286    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
287    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Export+sample";
288  }
289  else if ("ImportItems".equals(cmd))
290  {
291    // Run an import plugin in a list context
292    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
293    final ItemQuery<Sample> query = Sample.getQuery();
294    cc.configureQuery(query, true);
295    cc.setQuery(query);
296    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Import+sample";
297  }
298  else if ("ImportItem".equals(cmd))
299  {
300    // Run an import plugin in single-item context
301    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
302    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Import+sample";
303  }
304  else if ("RunListPlugin".equals(cmd))
305  {
306    // Run another plugin in a list context
307    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
308    final ItemQuery<Sample> query = Sample.getQuery();
309    cc.configureQuery(query, true);
310    cc.setQuery(query);
311    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&main_type=OTHER&title=Run+plugin";
312  }
313  else if ("RunPlugin".equals(cmd))
314  {
315    // Run another plugin in single-item context
316    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
317    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&main_type=OTHER&title=Run+plugin";
318  }
319  else
320  {
321    throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd);
322  }
323}
324finally
325{
326  if (dc != null) dc.close();
327}
328
329if (forward != null)
330{
331  pageContext.forward(forward);
332}
333else if (redirect != null)
334{
335  response.sendRedirect(redirect);
336}
337else if (message == null)
338{
339  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&wait=0");
340}
341else
342{
343  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&message="+HTML.urlEncode(message));
344}
345%>
346
Note: See TracBrowser for help on using the repository browser.