source: trunk/www/biomaterials/lists/index.jsp @ 4705

Last change on this file since 4705 was 4705, checked in by Nicklas Nordborg, 14 years ago

Fixes #1191: Biomaterial lists manager API and interface

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Id
File size: 14.1 KB
Line 
1<%-- $Id: index.jsp 4705 2008-12-16 13:29:12Z nicklas $
2  ------------------------------------------------------------------
3  Copyright (C) 2008 Nicklas Nordborg
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 3
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 BASE. If not, see <http://www.gnu.org/licenses/>.
20  ------------------------------------------------------------------
21
22  @author Nicklas
23  @version 2.10
24--%>
25<%@ page session="false"
26  import="net.sf.basedb.core.SessionControl"
27  import="net.sf.basedb.core.DbControl"
28  import="net.sf.basedb.core.Item"
29  import="net.sf.basedb.core.Include"
30  import="net.sf.basedb.core.BioMaterialList"
31  import="net.sf.basedb.core.ItemQuery"
32  import="net.sf.basedb.core.Permission"
33  import="net.sf.basedb.core.ItemContext"
34  import="net.sf.basedb.core.ItemQuery"
35  import="net.sf.basedb.core.MultiPermissions"
36  import="net.sf.basedb.core.OwnedItem"
37  import="net.sf.basedb.core.PermissionDeniedException"
38  import="net.sf.basedb.core.ItemAlreadyExistsException"
39  import="net.sf.basedb.util.RemovableUtil"
40  import="net.sf.basedb.util.ShareableUtil"
41  import="net.sf.basedb.util.OwnableUtil"
42  import="net.sf.basedb.clients.web.Base"
43  import="net.sf.basedb.clients.web.WebException"
44  import="net.sf.basedb.util.Values"
45  import="net.sf.basedb.clients.web.util.HTML"
46  import="java.util.Enumeration"
47  import="java.util.Set"
48  import="java.util.HashSet"
49  import="java.util.List"
50  import="java.util.ArrayList"
51  import="java.util.Collections"
52%>
53<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
54<%!
55  private static final ItemContext defaultContext = Base.createDefaultContext("name", "name,memberType,size,description");
56  private static final Item itemType = Item.BIOMATERIALLIST;
57%>
58<%
59final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
60final String ID = sc.getId();
61final String cmd = request.getParameter("cmd");
62final String root = request.getContextPath()+"/";
63final String mode = request.getParameter("mode");
64final String callback = request.getParameter("callback");
65final String itemId = request.getParameter("item_id");
66final String listPage = "list_lists.jsp?ID="+ID
67  +(mode == null ? "" : "&mode="+mode)
68  +(callback == null ? "" : "&callback="+callback)
69  +(itemId == null ? "" : "&item_id="+itemId);
70final String viewPage = "view_list.jsp?ID="+ID;
71final String editPage = "edit_list.jsp?ID="+ID;
72
73String forward = null;
74String redirect = null;
75String message = null;
76DbControl dc = null;
77
78try
79{
80  if (cmd == null || "List".equals(cmd))
81  {
82    // Display the list page without updatinging the current context
83    Base.getAndSetCurrentContext(sc, itemType, null, defaultContext, true);
84    redirect = listPage;
85  }
86  else if ("UpdateContext".equals(cmd))
87  {
88    // Display the list page after updating the current context from the request parameters
89    Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
90    redirect = listPage;
91  }
92  else if ("LoadContext".equals(cmd))
93  {
94    // Display the list page after loading a saved context
95    int contextId = Values.getInt(request.getParameter("context"));
96    Base.loadContext(sc, contextId, defaultContext);
97    redirect = listPage;
98  }
99
100  else if ("ViewItem".equals(cmd))
101  {
102    // Display the view page for a single item
103    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
104    forward = viewPage;
105  }
106  else if ("EditItem".equals(cmd))
107  {
108    // Display the edit page for a single item (should be opened in a popup)
109    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
110    redirect = editPage;
111  }
112  else if ("NewItem".equals(cmd))
113  {
114    // Display the edit page for a new item (should be opened in a popup)
115    if (!sc.hasPermission(Permission.CREATE, itemType))
116    {
117      throw new PermissionDeniedException(Permission.CREATE, itemType.toString());
118    }
119    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
120    cc.setId(0);
121    forward = editPage;
122  }
123  else if ("UpdateItem".equals(cmd))
124  {
125    // Update the properties on an item (will close the popup)
126    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, null, defaultContext);
127    dc = sc.newDbControl();
128    BioMaterialList list = (BioMaterialList)cc.getObject("item");
129    if (list == null)
130    {
131      Item memberType = Item.valueOf(request.getParameter("memberType"));
132      list = BioMaterialList.getNew(dc, memberType);
133      dc.saveItem(list);
134
135      // Add items from other lists?
136      String create = Values.getStringOrNull(request.getParameter("create"));
137      if (create != null)
138      {
139        List<BioMaterialList> lists = new ArrayList<BioMaterialList>(cc.getSelected().size());
140        for (int listId : cc.getSelected())
141        {
142          lists.add(BioMaterialList.getById(dc, listId));
143        }
144        if ("union".equals(create))
145        {
146          list.addUnion(lists);
147        }
148        else if ("intersection".equals(create))
149        {
150          list.addIntersection(lists);
151        }
152        else if ("count".equals(create))
153        {
154          int minCount = Values.getInt(request.getParameter("minCount"), 1);
155          list.addIfPresentIn(minCount, lists.size(), lists);
156        }
157      }
158      message = "Biomaterial list created with " + list.getSize() + " item(s)";
159    }
160    else
161    {
162      dc.reattachItem(list);
163      message = "List updated";
164    }
165    list.setName(Values.getStringOrNull(request.getParameter("name")));
166    list.setExternalId(Values.getStringOrNull(request.getParameter("external_id")));
167    list.setDescription(Values.getStringOrNull(request.getParameter("description")));
168
169    dc.commit();
170    cc.removeObject("item");
171  }
172  else if ("DeleteItem".equals(cmd))
173  {
174    // Delete a single item and then return to the view page
175    dc = sc.newDbControl();
176    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
177    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), true);
178    dc.commit();
179    redirect = viewPage;
180  }
181  else if ("DeleteItems".equals(cmd))
182  {
183    // Delete all selected items on the list page
184    dc = sc.newDbControl();
185    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
186    int numTotal = cc.getSelected().size();
187    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), true);
188    dc.commit();
189    if (numTotal != numRemoved)
190    {
191      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be deleted, because you have no DELETE permission";
192    }
193    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
194  }
195  else if ("RestoreItem".equals(cmd))
196  {
197    // Restore a single item and then return to the view page
198    dc = sc.newDbControl();
199    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
200    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), false);
201    dc.commit();
202    redirect = viewPage;
203  }
204  else if ("RestoreItems".equals(cmd))
205  {
206    // Restore all selected items on the list page
207    dc = sc.newDbControl();
208    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
209    int numTotal = cc.getSelected().size();
210    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), false);
211    dc.commit();
212    if (numTotal != numRemoved)
213    {
214      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be restored, because you have no WRITE permission";
215    }
216    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
217  }
218  else if ("ShareItem".equals(cmd))
219  {
220    // Display a popup window for sharing a single item
221    dc = sc.newDbControl();
222    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
223    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, Collections.singleton(cc.getId()));
224    dc.close();
225    cc.setObject("MultiPermissions", permissions);
226    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
227  }
228  else if ("ShareItems".equals(cmd))
229  {
230    // Display a popup window for sharing all selected items on the list page
231    dc = sc.newDbControl();
232    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
233    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, cc.getSelected());
234    dc.close();
235    cc.setObject("MultiPermissions", permissions);
236    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
237  }
238  else if ("SetOwnerOfItem".equals(cmd))
239  {
240    // Change owner of items selected on a list page
241    dc = sc.newDbControl();
242    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
243    OwnedItem item = (OwnedItem)itemType.getById(dc, cc.getId());
244    cc.setObject("OwnedItems", Collections.singleton(item));
245    redirect = "../../common/ownership/ownership.jsp?ID="+ID+"&item_type="+itemType.name();
246  }
247  else if ("SetOwnerOfItems".equals(cmd))
248  {
249    // Change owner of items selected on a list page
250    dc = sc.newDbControl();
251    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
252    Set<OwnedItem> items = new HashSet<OwnedItem>();
253    for (Integer id : cc.getSelected())
254    {
255      if (id != null) items.add((OwnedItem)itemType.getById(dc, id));
256    }
257    dc.close();
258    cc.setObject("OwnedItems", items);
259    redirect = "../../common/ownership/ownership.jsp?ID="+ID+"&item_type="+itemType.name();
260  }
261  else if ("ExportItems".equals(cmd))
262  {
263    // Run an export plugin in a list context
264    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
265    final ItemQuery<BioMaterialList> query = BioMaterialList.getQuery();
266    cc.configureQuery(query, true);
267    cc.setQuery(query);
268    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Export+biomaterial+lists";
269  }
270  else if ("ExportItem".equals(cmd))
271  {
272    // Run an export plugin in single-item context
273    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
274    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Export+label";
275  }
276  else if ("ImportItems".equals(cmd))
277  {
278    // Run an import plugin in a list context
279    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
280    final ItemQuery<BioMaterialList> query = BioMaterialList.getQuery();
281    cc.configureQuery(query, true);
282    cc.setQuery(query);
283    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Import+biomaterial+lists";
284  }
285  else if ("ImportItem".equals(cmd))
286  {
287    // Run an import plugin in single-item context
288    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
289    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Import+label";
290  }
291  else if ("RunListPlugin".equals(cmd))
292  {
293    // Run another plugin in a list context
294    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
295    final ItemQuery<BioMaterialList> query = BioMaterialList.getQuery();
296    cc.configureQuery(query, true);
297    cc.setQuery(query);
298    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&main_type=OTHER&title=Run+plugin";
299  }
300  else if ("RunPlugin".equals(cmd))
301  {
302    // Run another plugin in single-item context
303    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
304    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&main_type=OTHER&title=Run+plugin";
305  }
306  else if ("MergeItem".equals(cmd))
307  {
308    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, null, defaultContext);
309    dc = sc.newDbControl();
310    BioMaterialList bl = (BioMaterialList)cc.getObject("item");
311    dc.reattachItem(bl);
312   
313    String mergeType = request.getParameter("mergeType");
314    String sourceMerge = request.getParameter("sourceMerge");
315   
316    String[] sourceListIds = Values.getString(request.getParameter("sourceLists")).split(",");
317    List<BioMaterialList> sourceLists = new ArrayList<BioMaterialList>(sourceListIds.length);
318    for (int i = 0; i < sourceListIds.length; ++i)
319    {
320      int listId = Values.getInt(sourceListIds[i], -1);
321      if (listId != -1) sourceLists.add(BioMaterialList.getById(dc, listId));
322    }
323   
324    int numAdded = 0;
325    int numRemoved = 0;
326    if ("union".equals(mergeType))
327    {
328      if ("union".equals(sourceMerge))
329      {
330        numAdded = bl.addUnion(sourceLists);
331      }
332      else if ("intersection".equals(sourceMerge))
333      {
334        numAdded = bl.addIntersection(sourceLists);
335      }
336      message = numAdded + " items added to this list";
337    }
338    else if ("intersection".equals(mergeType))
339    {
340      if ("union".equals(sourceMerge))
341      {
342        numRemoved = bl.retainUnion(sourceLists);
343      }
344      else if ("intersection".equals(sourceMerge))
345      {
346        numRemoved = bl.retainIntersection(sourceLists);
347      }
348      message = numRemoved + " items removed from this list";
349    }
350    else if ("complement".equals(mergeType))
351    {
352      if ("union".equals(sourceMerge))
353      {
354        numRemoved = bl.removeUnion(sourceLists);
355      }
356      else if ("intersection".equals(sourceMerge))
357      {
358        numRemoved = bl.removeIntersection(sourceLists);
359      }
360      message = numRemoved + " items removed from this list";
361    }
362    dc.commit();
363  } 
364  else
365  {
366    throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd);
367  }
368}
369finally
370{
371  if (dc != null) dc.close();
372}
373
374if (forward != null)
375{
376  pageContext.forward(forward);
377}
378else if (redirect != null)
379{
380  response.sendRedirect(redirect);
381}
382else if (message == null)
383{
384  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&wait=0");
385}
386else
387{
388  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&message="+HTML.urlEncode(message));
389}
390%>
391
Note: See TracBrowser for help on using the repository browser.