source: trunk/www/biomaterials/labeledextracts/index.jsp @ 3005

Last change on this file since 3005 was 3005, checked in by Martin Svensson, 17 years ago

Fixes #455 Empty date field is not accepted at several places.

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