source: trunk/www/biomaterials/tags/index.jsp @ 6335

Last change on this file since 6335 was 6335, checked in by olle, 9 years ago

Refs #1773. Support for annotatable Tag items added.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Id
File size: 13.3 KB
Line 
1<%-- $Id: index.jsp 6335 2013-10-21 13:46:22Z olle $
2  ------------------------------------------------------------------
3  Copyright (C) 2005 Nicklas Nordborg
4  Copyright (C) 2006 Jari Häkkinen, Nicklas Nordborg, Martin Svensson
5
6  This file is part of BASE - BioArray Software Environment.
7  Available at http://base.thep.lu.se/
8
9  BASE is free software; you can redistribute it and/or
10  modify it under the terms of the GNU General Public License
11  as published by the Free Software Foundation; either version 3
12  of the License, or (at your option) any later version.
13
14  BASE is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  GNU General Public License for more details.
18
19  You should have received a copy of the GNU General Public License
20  along with BASE. If not, see <http://www.gnu.org/licenses/>.
21  ------------------------------------------------------------------
22
23  @author Nicklas
24  @version 2.0
25--%>
26<%@ page pageEncoding="UTF-8" session="false"
27  import="net.sf.basedb.core.SessionControl"
28  import="net.sf.basedb.core.DbControl"
29  import="net.sf.basedb.core.Item"
30  import="net.sf.basedb.core.Include"
31  import="net.sf.basedb.core.Tag"
32  import="net.sf.basedb.core.ItemSubtype"
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.OwnedItem"
39  import="net.sf.basedb.core.PermissionDeniedException"
40  import="net.sf.basedb.core.ItemAlreadyExistsException"
41  import="net.sf.basedb.util.RemovableUtil"
42  import="net.sf.basedb.util.ShareableUtil"
43  import="net.sf.basedb.util.OwnableUtil"
44  import="net.sf.basedb.clients.web.Base"
45  import="net.sf.basedb.clients.web.WebException"
46  import="net.sf.basedb.util.Values"
47  import="net.sf.basedb.clients.web.util.HTML"
48  import="net.sf.basedb.core.plugin.GuiContext"
49  import="net.sf.basedb.util.extensions.ExtensionsInvoker"
50  import="net.sf.basedb.clients.web.extensions.ExtensionsControl"
51  import="net.sf.basedb.clients.web.extensions.JspContext"
52  import="net.sf.basedb.clients.web.extensions.edit.EditUtil"
53  import="net.sf.basedb.clients.web.extensions.edit.OnSaveRenderer"
54  import="net.sf.basedb.clients.web.extensions.list.ListColumnExportRenderer"
55  import="net.sf.basedb.clients.web.extensions.list.ListColumnUtil"
56  import="java.util.Enumeration"
57  import="java.util.Set"
58  import="java.util.HashSet"
59  import="java.util.List"
60  import="java.util.ArrayList"
61  import="java.util.Collections"
62%>
63<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
64<%!
65  private static final ItemContext defaultContext = Base.createDefaultContext("name", "name,itemSubtype,description");
66  private static final Item itemType = Item.TAG;
67%>
68<%
69final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
70final String ID = sc.getId();
71final String cmd = request.getParameter("cmd");
72final String root = request.getContextPath()+"/";
73final String mode = request.getParameter("mode");
74final String callback = request.getParameter("callback");
75final String itemId = request.getParameter("item_id");
76final String listPage = "list_tags.jsp?ID="+ID
77  +(mode == null ? "" : "&mode="+mode)
78  +(callback == null ? "" : "&callback="+callback)
79  +(itemId == null ? "" : "&item_id="+itemId);
80final String viewPage = "view_tag.jsp?ID="+ID;
81final String editPage = "edit_tag.jsp?ID="+ID;
82
83String forward = null;
84String redirect = null;
85String message = null;
86DbControl dc = null;
87
88try
89{
90  if (cmd == null || "List".equals(cmd))
91  {
92    // Display the list page without updatinging the current context
93    Base.getAndSetCurrentContext(sc, itemType, null, defaultContext, true);
94    redirect = listPage;
95  }
96  else if ("UpdateContext".equals(cmd))
97  {
98    // Display the list page after updating the current context from the request parameters
99    Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
100    redirect = listPage;
101  }
102  else if ("LoadContext".equals(cmd))
103  {
104    // Display the list page after loading a saved context
105    int contextId = Values.getInt(request.getParameter("context"));
106    Base.loadContext(sc, contextId, defaultContext);
107    redirect = listPage;
108  }
109
110  else if ("ViewItem".equals(cmd))
111  {
112    // Display the view page for a single item
113    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
114    forward = viewPage;
115  }
116  else if ("EditItem".equals(cmd))
117  {
118    // Display the edit page for a single item (should be opened in a popup)
119    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
120    redirect = editPage;
121  }
122  else if ("NewItem".equals(cmd))
123  {
124    // Display the edit page for a new item (should be opened in a popup)
125    if (!sc.hasPermission(Permission.CREATE, itemType))
126    {
127      throw new PermissionDeniedException(Permission.CREATE, itemType.toString());
128    }
129    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
130    cc.setId(0);
131    redirect = editPage;
132  }
133  else if ("UpdateItem".equals(cmd))
134  {
135    // Update the properties on an item (will close the popup)
136    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, null, defaultContext);
137    final int maxRecent = Base.getMaxRecent(sc);
138    dc = sc.newDbControl();
139    Tag tag = (Tag)cc.getObject("item");
140    if (tag == null)
141    {
142      tag = Tag.getNew(dc);
143      message = "Tag created";
144      dc.saveItem(tag);
145    }
146    else
147    {
148      dc.reattachItem(tag, false);
149      message = "Tag updated";
150    }
151    JspContext jspContext = ExtensionsControl.createContext(dc, pageContext, GuiContext.item(itemType), tag);
152    ExtensionsInvoker invoker = EditUtil.useOnSaveExtensions(jspContext);
153    try
154    {
155      tag.setName(Values.getStringOrNull(request.getParameter("name")));
156      tag.setDescription(Values.getStringOrNull(request.getParameter("description")));
157      int tagTypeId = Values.getInt(request.getParameter("tagtype_id"), -1);
158      if (tagTypeId >= 0) // < 0 = denied or unchanged
159      {
160        ItemSubtype subtype = tagTypeId == 0 ? null : ItemSubtype.getById(dc, tagTypeId);
161        tag.setItemSubtype(subtype);
162        if (subtype != null) cc.setRecent(subtype, maxRecent);
163      }
164
165      Base.updateAnnotations(dc, tag, tag, request);
166      // OnSave extensions
167      invoker.render(OnSaveRenderer.ON_SAVE);
168      dc.commit();
169      invoker.render(OnSaveRenderer.ON_COMMIT);
170    }
171    catch (Exception ex)
172    {
173      invoker.render(OnSaveRenderer.onRollback(ex));
174      throw ex;
175    }
176    finally
177    {
178      cc.removeObject("item");
179    }
180  }
181  else if ("DeleteItem".equals(cmd))
182  {
183    // Delete a single item and then return to the view page
184    dc = sc.newDbControl();
185    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
186    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), true);
187    dc.commit();
188    redirect = viewPage;
189  }
190  else if ("DeleteItems".equals(cmd))
191  {
192    // Delete all selected items on the list page
193    dc = sc.newDbControl();
194    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
195    int numTotal = cc.getSelected().size();
196    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), true);
197    dc.commit();
198    if (numTotal != numRemoved)
199    {
200      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be deleted, because you have no DELETE permission";
201    }
202    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
203  }
204  else if ("RestoreItem".equals(cmd))
205  {
206    // Restore a single item and then return to the view page
207    dc = sc.newDbControl();
208    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
209    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), false);
210    dc.commit();
211    redirect = viewPage;
212  }
213  else if ("RestoreItems".equals(cmd))
214  {
215    // Restore all selected items on the list page
216    dc = sc.newDbControl();
217    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
218    int numTotal = cc.getSelected().size();
219    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), false);
220    dc.commit();
221    if (numTotal != numRemoved)
222    {
223      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be restored, because you have no WRITE permission";
224    }
225    redirect = listPage+(message != null ? "&popmessage="+HTML.urlEncode(message) : "");
226  }
227  else if ("ShareItem".equals(cmd))
228  {
229    // Display a popup window for sharing a single item
230    dc = sc.newDbControl();
231    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
232    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, Collections.singleton(cc.getId()));
233    dc.close();
234    cc.setObject("MultiPermissions", permissions);
235    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
236  }
237  else if ("ShareItems".equals(cmd))
238  {
239    // Display a popup window for sharing all selected items on the list page
240    dc = sc.newDbControl();
241    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
242    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, cc.getSelected());
243    dc.close();
244    cc.setObject("MultiPermissions", permissions);
245    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
246  }
247  else if ("SetOwnerOfItem".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    OwnedItem item = (OwnedItem)itemType.getById(dc, cc.getId());
253    cc.setObject("OwnedItems", Collections.singleton(item));
254    redirect = "../../common/ownership/ownership.jsp?ID="+ID+"&item_type="+itemType.name();
255  }
256  else if ("SetOwnerOfItems".equals(cmd))
257  {
258    // Change owner of items selected on a list page
259    dc = sc.newDbControl();
260    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
261    Set<OwnedItem> items = new HashSet<OwnedItem>();
262    for (Integer id : cc.getSelected())
263    {
264      if (id != null) items.add((OwnedItem)itemType.getById(dc, id));
265    }
266    dc.close();
267    cc.setObject("OwnedItems", items);
268    redirect = "../../common/ownership/ownership.jsp?ID="+ID+"&item_type="+itemType.name();
269  }
270  else if ("ExportItems".equals(cmd))
271  {
272    // Run an export plugin in a list context
273    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
274    final ItemQuery<Tag> query = Tag.getQuery();
275    dc = sc.newDbControl();
276    cc.configureQuery(dc, query, true);
277    cc.setQuery(query);
278    JspContext jspContext = ExtensionsControl.createContext(dc, pageContext, GuiContext.list(itemType), null);
279    ExtensionsInvoker listInvoker = ListColumnUtil.useExtensions(jspContext);
280    listInvoker.render(new ListColumnExportRenderer(cc));
281    dc.close();
282    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Export+tags";
283  }
284  else if ("ExportItem".equals(cmd))
285  {
286    // Run an export plugin in single-item context
287    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
288    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Export+tag";
289  }
290  else if ("ImportItems".equals(cmd))
291  {
292    // Run an import plugin in a list context
293    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
294    final ItemQuery<Tag> query = Tag.getQuery();
295    dc = sc.newDbControl();
296    cc.configureQuery(dc, query, true);
297    dc.close();
298    cc.setQuery(query);
299    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Import+tags";
300  }
301  else if ("ImportItem".equals(cmd))
302  {
303    // Run an import plugin in single-item context
304    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
305    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Import+tag";
306  }
307  else if ("RunListPlugin".equals(cmd))
308  {
309    // Run another plugin in a list context
310    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
311    final ItemQuery<Tag> query = Tag.getQuery();
312    dc = sc.newDbControl();
313    cc.configureQuery(dc, query, true);
314    dc.close();
315    cc.setQuery(query);
316    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&main_type=OTHER&title=Run+plugin";
317  }
318  else if ("RunPlugin".equals(cmd))
319  {
320    // Run another plugin in single-item context
321    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
322    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&main_type=OTHER&title=Run+plugin";
323  }
324  else
325  {
326    throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd);
327  }
328}
329finally
330{
331  if (dc != null) dc.close();
332}
333
334if (forward != null)
335{
336  sc.setSessionSetting("alert-message", message);
337  pageContext.forward(forward);
338}
339else if (redirect != null)
340{
341  sc.setSessionSetting("alert-message", message);
342  response.sendRedirect(redirect);
343}
344else if (message == null)
345{
346  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&wait=0");
347}
348else
349{
350  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&message="+HTML.urlEncode(message));
351}
352%>
353
Note: See TracBrowser for help on using the repository browser.