source: trunk/www/lims/arrayslides/index.jsp @ 5136

Last change on this file since 5136 was 5136, checked in by Nicklas Nordborg, 13 years ago

Merged patch release 2.13.1 to the trunk.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Id
File size: 14.6 KB
Line 
1<%@page import="net.sf.basedb.core.InvalidDataException"%>
2<%-- $Id: index.jsp 5136 2009-10-15 06:33:07Z nicklas $
3  ------------------------------------------------------------------
4  Copyright (C) 2005 Nicklas Nordborg
5  Copyright (C) 2006 Jari Häkkinen, Nicklas Nordborg, Martin Svensson
6
7  This file is part of BASE - BioArray Software Environment.
8  Available at http://base.thep.lu.se/
9
10  BASE is free software; you can redistribute it and/or
11  modify it under the terms of the GNU General Public License
12  as published by the Free Software Foundation; either version 3
13  of the License, or (at your option) any later version.
14
15  BASE is distributed in the hope that it will be useful,
16  but WITHOUT ANY WARRANTY; without even the implied warranty of
17  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  GNU General Public License for more details.
19
20  You should have received a copy of the GNU General Public License
21  along with BASE. If not, see <http://www.gnu.org/licenses/>.
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.Hybridization"
33  import="net.sf.basedb.core.ArraySlide"
34  import="net.sf.basedb.core.ArrayBatch"
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.core.HibernateUtil" 
45  import="net.sf.basedb.util.RemovableUtil"
46  import="net.sf.basedb.util.ShareableUtil"
47  import="net.sf.basedb.util.OwnableUtil"
48  import="net.sf.basedb.clients.web.Base"
49  import="net.sf.basedb.clients.web.WebException"
50  import="net.sf.basedb.util.Values"
51  import="net.sf.basedb.clients.web.util.HTML"
52  import="java.util.Enumeration"
53  import="java.util.Set"
54  import="java.util.HashSet"
55  import="java.util.TreeSet"
56  import="java.util.List"
57  import="java.util.ArrayList"
58  import="java.util.Collections"
59%>
60<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
61<%!
62  private static final ItemContext defaultContext = Base.createDefaultContext("name", "name,arrayDesign,hybridization,description");
63  private static final Item itemType = Item.ARRAYSLIDE;
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_slides.jsp?ID="+ID
74  +(mode == null ? "" : "&mode="+mode)
75  +(callback == null ? "" : "&callback="+callback)
76  +(itemId == null ? "" : "&item_id="+itemId);
77final String viewPage = "view_slide.jsp?ID="+ID;
78final String editPage = "edit_slide.jsp?ID="+ID;
79final String wizardPage = "create_wizard.jsp?ID="+ID+"&cmd=WizardStep1";
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 ("UpdateItem".equals(cmd))
132  {
133    // Update the properties on an item (will close the popup)
134    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, null, defaultContext);
135    final int maxRecent = Base.getMaxRecent(sc);
136    dc = sc.newDbControl();
137    ArraySlide slide = (ArraySlide)cc.getObject("item");
138    if (slide == null)
139    {
140      int batchId = Values.getInt(request.getParameter("arraybatch_id"), -1);
141      ArrayBatch batch = ArrayBatch.getById(dc, batchId);
142      slide = ArraySlide.getNew(dc, batch);
143      message = "Array slide created";
144      dc.saveItem(slide);
145      if (batch != null) cc.setRecent(batch, maxRecent);
146    }
147    else
148    {
149      dc.reattachItem(slide, false);
150      message = "Array slide updated";
151    }
152    slide.setName(Values.getStringOrNull(request.getParameter("name")));
153    String newBarcode = Values.getStringOrNull(request.getParameter("barcode"));
154    if (newBarcode != null && !newBarcode.equals(slide.getBarcode()))
155    {
156      if (ArraySlide.barcodeExists(dc, newBarcode))
157      {
158        throw new ItemAlreadyExistsException("ArraySlide[barcode=" + newBarcode + "]");
159      }
160    }
161    slide.setBarcode(newBarcode);
162    slide.setBatchIndex(Values.getInt(request.getParameter("batch_index")));
163    slide.setDestroyed(Values.getBoolean(request.getParameter("destroyed")));
164    slide.setDescription(Values.getStringOrNull(request.getParameter("description")));
165   
166    // Annotations tab
167    Base.updateAnnotations(dc, slide, slide, request);
168    dc.commit();
169    cc.removeObject("item");
170  }
171  else if ("NewItems".equals(cmd))
172  {
173//    Display the wizard page for new items wizard(should be open in a popup)
174    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
175    cc.setId(0);
176    forward = wizardPage;
177  }
178  else if ("CreateItems".equals(cmd))
179  {
180    //Finish the wizard and create the given quantity of Array slides
181    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, null, defaultContext);
182    final int maxRecent = Base.getMaxRecent(sc);
183    dc = sc.newDbControl();
184    int quantity = Values.getInt(request.getParameter("quantity"));
185    int batchId = Values.getInt(request.getParameter("arraybatch_id"));
186    int startAt = Values.getInt(request.getParameter("start_at"), 1);
187    String description = Values.getStringOrNull(request.getParameter("description"));
188    ArrayBatch batch = ArrayBatch.getById(dc, batchId);
189    if (batch != null) cc.setRecent(batch, maxRecent);
190    boolean caseInsensitive = HibernateUtil.getDbEngine().caseInsensitiveComparison();
191    Set<String> barcodes = caseInsensitive ? 
192      new TreeSet<String>(String.CASE_INSENSITIVE_ORDER) : new HashSet<String>();
193    for (int i = 0; i < quantity; ++i)
194    {
195      ArraySlide slide = ArraySlide.getNew(dc, batch);
196      dc.saveItem(slide);
197      slide.setName(Values.getStringOrNull(request.getParameter("name"+i)));
198      slide.setBatchIndex(startAt + i);
199     
200      String newBarcode = Values.getStringOrNull(request.getParameter("barcode" + i));
201      if (newBarcode != null)
202      {
203        if (barcodes.contains(newBarcode))
204        {
205          throw new InvalidDataException("Trying to create more than one slide with barcode=" + newBarcode);
206        }
207        else if (ArraySlide.barcodeExists(dc, newBarcode))
208        {
209          throw new ItemAlreadyExistsException("ArraySlide[barcode=" + newBarcode + "]");
210        }
211        barcodes.add(newBarcode);
212      }
213      slide.setBarcode(newBarcode);
214      slide.setDescription(description);
215    }
216    dc.commit();
217    message = quantity + " Array slides created";
218  }
219  else if ("DeleteItem".equals(cmd))
220  {
221    // Delete a single item and then return to the view page
222    dc = sc.newDbControl();
223    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
224    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), true);
225    dc.commit();
226    redirect = viewPage;
227  }
228  else if ("DeleteItems".equals(cmd))
229  {
230    // Delete all selected items on the list page
231    dc = sc.newDbControl();
232    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
233    int numTotal = cc.getSelected().size();
234    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), true);
235    dc.commit();
236    if (numTotal != numRemoved)
237    {
238      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be deleted, because you have no DELETE permission";
239    }
240    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
241  }
242  else if ("RestoreItem".equals(cmd))
243  {
244    // Restore a single item and then return to the view page
245    dc = sc.newDbControl();
246    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
247    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), false);
248    dc.commit();
249    redirect = viewPage;
250  }
251  else if ("RestoreItems".equals(cmd))
252  {
253    // Restore all selected items on the list page
254    dc = sc.newDbControl();
255    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
256    int numTotal = cc.getSelected().size();
257    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), false);
258    dc.commit();
259    if (numTotal != numRemoved)
260    {
261      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be restored, because you have no WRITE permission";
262    }
263    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
264  }
265  else if ("ShareItem".equals(cmd))
266  {
267    // Display a popup window for sharing a single item
268    dc = sc.newDbControl();
269    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
270    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, Collections.singleton(cc.getId()));
271    dc.close();
272    cc.setObject("MultiPermissions", permissions);
273    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
274  }
275  else if ("ShareItems".equals(cmd))
276  {
277    // Display a popup window for sharing all selected items on the list page
278    dc = sc.newDbControl();
279    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
280    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, cc.getSelected());
281    dc.close();
282    cc.setObject("MultiPermissions", permissions);
283    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
284  }
285  else if ("SetOwnerOfItem".equals(cmd))
286  {
287    // Change owner of items selected on a list page
288    dc = sc.newDbControl();
289    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
290    OwnedItem item = (OwnedItem)itemType.getById(dc, cc.getId());
291    cc.setObject("OwnedItems", Collections.singleton(item));
292    redirect = "../../common/ownership/ownership.jsp?ID="+ID+"&item_type="+itemType.name();
293  }
294  else if ("SetOwnerOfItems".equals(cmd))
295  {
296    // Change owner of items selected on a list page
297    dc = sc.newDbControl();
298    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
299    Set<OwnedItem> items = new HashSet<OwnedItem>();
300    for (Integer id : cc.getSelected())
301    {
302      if (id != null) items.add((OwnedItem)itemType.getById(dc, id));
303    }
304    dc.close();
305    cc.setObject("OwnedItems", items);
306    redirect = "../../common/ownership/ownership.jsp?ID="+ID+"&item_type="+itemType.name();
307  }
308  else if ("ExportItems".equals(cmd))
309  {
310    // Run an export plugin in a list context
311    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
312    final ItemQuery<ArraySlide> query = ArraySlide.getQuery();
313    cc.configureQuery(query, true);
314    cc.setQuery(query);
315    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Export+array+slides";
316  }
317  else if ("ExportItem".equals(cmd))
318  {
319    // Run an export plugin in single-item context
320    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
321    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Export+array+slide";
322  }
323  else if ("ImportItems".equals(cmd))
324  {
325    // Run an import plugin in a list context
326    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
327    final ItemQuery<ArraySlide> query = ArraySlide.getQuery();
328    cc.configureQuery(query, true);
329    cc.setQuery(query);
330    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Import+array+slides";
331  }
332  else if ("ImportItem".equals(cmd))
333  {
334    // Run an import plugin in single-item context
335    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
336    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Import+array+slide";
337  }
338  else if ("RunListPlugin".equals(cmd))
339  {
340    // Run another plugin in a list context
341    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
342    final ItemQuery<ArraySlide> query = ArraySlide.getQuery();
343    cc.configureQuery(query, true);
344    cc.setQuery(query);
345    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&main_type=OTHER&title=Run+plugin";
346  }
347  else if ("RunPlugin".equals(cmd))
348  {
349    // Run another plugin in single-item context
350    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
351    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&main_type=OTHER&title=Run+plugin";
352  }
353  else
354  {
355    throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd);
356  }
357}
358finally
359{
360  if (dc != null) dc.close();
361}
362
363if (forward != null)
364{
365  pageContext.forward(forward);
366}
367else if (redirect != null)
368{
369  response.sendRedirect(redirect);
370}
371else if (message == null)
372{
373  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&wait=0");
374}
375else
376{
377  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&message="+HTML.urlEncode(message));
378}
379%>
380
Note: See TracBrowser for help on using the repository browser.