source: trunk/www/biomaterials/labels/index.jsp @ 2917

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

References #348: Take ownership of items

Implemented on all biomaterial pages

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Id
File size: 11.5 KB
Line 
1<%-- $Id: index.jsp 2917 2006-11-15 10:28:36Z 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.Label"
33  import="net.sf.basedb.core.ItemQuery"
34  import="net.sf.basedb.core.Permission"
35  import="net.sf.basedb.core.ItemContext"
36  import="net.sf.basedb.core.ItemQuery"
37  import="net.sf.basedb.core.MultiPermissions"
38  import="net.sf.basedb.core.PermissionDeniedException"
39  import="net.sf.basedb.core.ItemAlreadyExistsException"
40  import="net.sf.basedb.util.RemovableUtil"
41  import="net.sf.basedb.util.ShareableUtil"
42  import="net.sf.basedb.util.OwnableUtil"
43  import="net.sf.basedb.clients.web.Base"
44  import="net.sf.basedb.clients.web.WebException"
45  import="net.sf.basedb.util.Values"
46  import="net.sf.basedb.clients.web.util.HTML"
47  import="java.util.Enumeration"
48  import="java.util.Set"
49  import="java.util.HashSet"
50  import="java.util.List"
51  import="java.util.ArrayList"
52  import="java.util.Collections"
53%>
54<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
55<%!
56  private static final ItemContext defaultContext = Base.createDefaultContext("name", "name,description");
57  private static final Item itemType = Item.LABEL;
58%>
59<%
60final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
61final String ID = sc.getId();
62final String cmd = request.getParameter("cmd");
63final String root = request.getContextPath()+"/";
64final String mode = request.getParameter("mode");
65final String callback = request.getParameter("callback");
66final String itemId = request.getParameter("item_id");
67final String listPage = "list_labels.jsp?ID="+ID
68  +(mode == null ? "" : "&mode="+mode)
69  +(callback == null ? "" : "&callback="+callback)
70  +(itemId == null ? "" : "&item_id="+itemId);
71final String viewPage = "view_label.jsp?ID="+ID;
72final String editPage = "edit_label.jsp?ID="+ID;
73
74String forward = null;
75String redirect = null;
76String message = null;
77DbControl dc = null;
78
79try
80{
81  if (cmd == null || "List".equals(cmd))
82  {
83    // Display the list page without updatinging the current context
84    Base.getAndSetCurrentContext(sc, itemType, null, defaultContext, true);
85    redirect = listPage;
86  }
87  else if ("UpdateContext".equals(cmd))
88  {
89    // Display the list page after updating the current context from the request parameters
90    Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
91    redirect = listPage;
92  }
93  else if ("LoadContext".equals(cmd))
94  {
95    // Display the list page after loading a saved context
96    int contextId = Values.getInt(request.getParameter("context"));
97    Base.loadContext(sc, contextId, defaultContext);
98    redirect = listPage;
99  }
100
101  else if ("ViewItem".equals(cmd))
102  {
103    // Display the view page for a single item
104    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
105    forward = viewPage;
106  }
107  else if ("EditItem".equals(cmd))
108  {
109    // Display the edit page for a single item (should be opened in a popup)
110    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
111    redirect = editPage;
112  }
113  else if ("NewItem".equals(cmd))
114  {
115    // Display the edit page for a new item (should be opened in a popup)
116    if (!sc.hasPermission(Permission.CREATE, itemType))
117    {
118      throw new PermissionDeniedException(Permission.CREATE, itemType.toString());
119    }
120    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
121    cc.setId(0);
122    redirect = editPage;
123  }
124  else if ("UpdateItem".equals(cmd))
125  {
126    // Update the properties on an item (will close the popup)
127    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, null, defaultContext);
128    dc = sc.newDbControl();
129    Label label = (Label)cc.getObject("item");
130    if (label == null)
131    {
132      label = Label.getNew(dc);
133      message = "Label created";
134      dc.saveItem(label);
135    }
136    else
137    {
138      dc.reattachItem(label);
139      message = "Label updated";
140    }
141    label.setName(Values.getStringOrNull(request.getParameter("name")));
142    label.setDescription(Values.getStringOrNull(request.getParameter("description")));
143
144    dc.commit();
145    cc.removeObject("item");
146  }
147  else if ("DeleteItem".equals(cmd))
148  {
149    // Delete a single item and then return to the view page
150    dc = sc.newDbControl();
151    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
152    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), true);
153    dc.commit();
154    redirect = viewPage;
155  }
156  else if ("DeleteItems".equals(cmd))
157  {
158    // Delete all selected items on the list page
159    dc = sc.newDbControl();
160    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
161    int numTotal = cc.getSelected().size();
162    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), true);
163    dc.commit();
164    if (numTotal != numRemoved)
165    {
166      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be deleted, because you have no DELETE permission";
167    }
168    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
169  }
170  else if ("RestoreItem".equals(cmd))
171  {
172    // Restore a single item and then return to the view page
173    dc = sc.newDbControl();
174    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
175    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), false);
176    dc.commit();
177    redirect = viewPage;
178  }
179  else if ("RestoreItems".equals(cmd))
180  {
181    // Restore all selected items on the list page
182    dc = sc.newDbControl();
183    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
184    int numTotal = cc.getSelected().size();
185    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), false);
186    dc.commit();
187    if (numTotal != numRemoved)
188    {
189      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be restored, because you have no WRITE permission";
190    }
191    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
192  }
193  else if ("ShareItem".equals(cmd))
194  {
195    // Display a popup window for sharing a single item
196    dc = sc.newDbControl();
197    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
198    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, Collections.singleton(cc.getId()));
199    dc.close();
200    cc.setObject("MultiPermissions", permissions);
201    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
202  }
203  else if ("ShareItems".equals(cmd))
204  {
205    // Display a popup window for sharing all selected items on the list page
206    dc = sc.newDbControl();
207    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
208    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, cc.getSelected());
209    dc.close();
210    cc.setObject("MultiPermissions", permissions);
211    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
212  }
213  else if ("TakeOwnershipOfItem".equals(cmd))
214  {
215    // Take ownership a single item and then return to the view page
216    dc = sc.newDbControl();
217    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
218    OwnableUtil.setOwner(dc, itemType, Collections.singleton(cc.getId()), null);
219    dc.commit();
220    redirect = viewPage;
221  }
222  else if ("TakeOwnershipOfItems".equals(cmd))
223  {
224    // Take ownership 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 numOwned = OwnableUtil.setOwner(dc, itemType, cc.getSelected(), null);
229    dc.commit();
230    if (numTotal != numOwned)
231    {
232      message = (numOwned == 0 ? "No" : "Only "+numOwned+" of "+numTotal) + " items could be taken, because you have no SET_OWNER permission";
233    }
234    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
235  }
236  else if ("ExportItems".equals(cmd))
237  {
238    // Run an export plugin in a list context
239    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
240    final ItemQuery<Label> query = Label.getQuery();
241    cc.configureQuery(query, true);
242    cc.setQuery(query);
243    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Export+labels";
244  }
245  else if ("ExportItem".equals(cmd))
246  {
247    // Run an export plugin in single-item context
248    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
249    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Export+label";
250  }
251  else if ("ImportItems".equals(cmd))
252  {
253    // Run an import plugin in a list context
254    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
255    final ItemQuery<Label> query = Label.getQuery();
256    cc.configureQuery(query, true);
257    cc.setQuery(query);
258    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Import+labels";
259  }
260  else if ("ImportItem".equals(cmd))
261  {
262    // Run an import plugin in single-item context
263    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
264    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Import+label";
265  }
266  else if ("RunListPlugin".equals(cmd))
267  {
268    // Run another plugin in a list context
269    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
270    final ItemQuery<Label> query = Label.getQuery();
271    cc.configureQuery(query, true);
272    cc.setQuery(query);
273    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&main_type=OTHER&title=Run+plugin";
274  }
275  else if ("RunPlugin".equals(cmd))
276  {
277    // Run another plugin in single-item context
278    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
279    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&main_type=OTHER&title=Run+plugin";
280  }
281  else
282  {
283    throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd);
284  }
285}
286finally
287{
288  if (dc != null) dc.close();
289}
290
291if (forward != null)
292{
293  pageContext.forward(forward);
294}
295else if (redirect != null)
296{
297  response.sendRedirect(redirect);
298}
299else if (message == null)
300{
301  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&wait=0");
302}
303else
304{
305  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&message="+HTML.urlEncode(message));
306}
307%>
308
Note: See TracBrowser for help on using the repository browser.