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

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

Added session="false" to all jsp pages so we no longer generate unneeded cookies

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