source: branches/3.3-stable/www/views/jobs/index.jsp @ 6460

Last change on this file since 6460 was 6447, checked in by Nicklas Nordborg, 8 years ago

Fixes #1802: Jobs should be shareable

  • Property svn:eol-style set to native
  • Property svn:keywords set to Date Id
File size: 12.5 KB
Line 
1<%-- $Id: index.jsp 6447 2014-04-15 06:50:29Z nicklas $
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.Job"
32  import="net.sf.basedb.core.ItemQuery"
33  import="net.sf.basedb.core.ItemResultIterator"
34  import="net.sf.basedb.core.Permission"
35  import="net.sf.basedb.core.ItemContext"
36  import="net.sf.basedb.core.MultiPermissions"
37  import="net.sf.basedb.core.OwnedItem"
38  import="net.sf.basedb.core.PermissionDeniedException"
39  import="net.sf.basedb.core.ItemAlreadyExistsException"
40  import="net.sf.basedb.core.signal.SignalTransporter"
41  import="net.sf.basedb.core.signal.Signal"
42  import="net.sf.basedb.util.RemovableUtil"
43  import="net.sf.basedb.util.ShareableUtil"
44  import="net.sf.basedb.util.OwnableUtil"
45  import="net.sf.basedb.clients.web.Base"
46  import="net.sf.basedb.clients.web.WebException"
47  import="net.sf.basedb.util.Values"
48  import="net.sf.basedb.clients.web.util.HTML"
49  import="net.sf.basedb.core.plugin.GuiContext"
50  import="net.sf.basedb.util.extensions.ExtensionsInvoker"
51  import="net.sf.basedb.clients.web.extensions.ExtensionsControl"
52  import="net.sf.basedb.clients.web.extensions.JspContext"
53  import="net.sf.basedb.clients.web.extensions.list.ListColumnExportRenderer"
54  import="net.sf.basedb.clients.web.extensions.list.ListColumnUtil"
55  import="java.util.Enumeration"
56  import="java.util.Set"
57  import="java.util.HashSet"
58  import="java.util.List"
59  import="java.util.ArrayList"
60  import="java.util.Collections"
61%>
62<%@ taglib prefix="base" uri="/WEB-INF/base.tld" %>
63<%!
64  private static final ItemContext defaultContext = Base.createDefaultContext("scheduled", "name,pluginType,scheduled,percentComplete,status,statusMessage,description");
65  {
66    defaultContext.setSortDirection(ItemContext.SortDirection.DESC);
67  }
68  private static final Item itemType = Item.JOB;
69%>
70<%
71final SessionControl sc = Base.getExistingSessionControl(pageContext, true);
72final String ID = sc.getId();
73final String cmd = request.getParameter("cmd");
74final String root = request.getContextPath()+"/";
75final String mode = request.getParameter("mode");
76final String callback = request.getParameter("callback");
77final String itemId = request.getParameter("item_id");
78final String listPage = "list_jobs.jsp?ID="+ID
79  +(mode == null ? "" : "&mode="+mode)
80  +(callback == null ? "" : "&callback="+callback)
81  +(itemId == null ? "" : "&item_id="+itemId);
82final String viewPage = "view_job.jsp?ID="+ID;
83//final String editPage = "edit_job.jsp?ID="+ID;
84
85String forward = null;
86String redirect = null;
87String message = null;
88DbControl dc = null;
89
90try
91{
92  if (cmd == null || "List".equals(cmd))
93  {
94    // Display the list page without updatinging the current context
95    Base.getAndSetCurrentContext(sc, itemType, null, defaultContext, true);
96    redirect = listPage;
97  }
98  else if ("UpdateContext".equals(cmd))
99  {
100    // Display the list page after updating the current context from the request parameters
101    Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
102    redirect = listPage;
103  }
104  else if ("LoadContext".equals(cmd))
105  {
106    // Display the list page after loading a saved context
107    int contextId = Values.getInt(request.getParameter("context"));
108    Base.loadContext(sc, contextId, defaultContext);
109    redirect = listPage;
110  }
111
112  else if ("ViewItem".equals(cmd))
113  {
114    // Display the view page for a single item (opens in a popup)
115    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
116    forward = viewPage;
117  }
118  /*
119  else if ("EditItem".equals(cmd))
120  {
121    // Display the edit page for a single item (should be opened in a popup)
122    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
123    redirect = editPage;
124  }
125  */
126  else if ("DeleteItem".equals(cmd))
127  {
128    // Delete a single item and then return to the view page
129    dc = sc.newDbControl();
130    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
131    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), true);
132    dc.commit();
133    redirect = viewPage;
134  }
135  else if ("DeleteItems".equals(cmd))
136  {
137    // Delete all selected items on the list page
138    dc = sc.newDbControl();
139    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
140    int numTotal = cc.getSelected().size();
141    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), true);
142    dc.commit();
143    if (numTotal != numRemoved)
144    {
145      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be deleted, because you have no DELETE permission";
146    }
147    redirect = listPage;
148  }
149  else if ("RestoreItem".equals(cmd))
150  {
151    // Restore a single item and then return to the view page
152    dc = sc.newDbControl();
153    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
154    RemovableUtil.setRemoved(dc, itemType, Collections.singleton(cc.getId()), false);
155    dc.commit();
156    redirect = viewPage;
157  }
158  else if ("RestoreItems".equals(cmd))
159  {
160    // Restore all selected items on the list page
161    dc = sc.newDbControl();
162    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
163    int numTotal = cc.getSelected().size();
164    int numRemoved = RemovableUtil.setRemoved(dc, itemType, cc.getSelected(), false);
165    dc.commit();
166    if (numTotal != numRemoved)
167    {
168      message = (numRemoved == 0 ? "No" : "Only "+numRemoved+" of "+numTotal) + " items could be restored, because you have no WRITE permission";
169    }
170    redirect = listPage;
171  }
172  else if ("ShareItem".equals(cmd))
173  {
174    // Display a popup window for sharing a single item
175    dc = sc.newDbControl();
176    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
177    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, Collections.singleton(cc.getId()));
178    dc.close();
179    cc.setObject("MultiPermissions", permissions);
180    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
181  }
182  else if ("ShareItems".equals(cmd))
183  {
184    // Display a popup window for sharing all selected items on the list page
185    dc = sc.newDbControl();
186    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
187    MultiPermissions permissions = ShareableUtil.getMultiPermissions(dc, itemType, cc.getSelected());
188    dc.close();
189    cc.setObject("MultiPermissions", permissions);
190    redirect = "../../common/share/share.jsp?ID="+ID+"&item_type="+itemType.name();
191  }
192  else if ("SetOwnerOfItem".equals(cmd))
193  {
194    // Change owner of items selected on a list page
195    dc = sc.newDbControl();
196    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
197    OwnedItem item = (OwnedItem)itemType.getById(dc, cc.getId());
198    cc.setObject("OwnedItems", Collections.singleton(item));
199    redirect = "../../common/ownership/ownership.jsp?ID="+ID+"&item_type="+itemType.name();
200  }
201  else if ("SetOwnerOfItems".equals(cmd))
202  {
203    // Change owner of items selected on a list page
204    dc = sc.newDbControl();
205    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
206    Set<OwnedItem> items = new HashSet<OwnedItem>();
207    for (Integer id : cc.getSelected())
208    {
209      if (id != null) items.add((OwnedItem)itemType.getById(dc, id));
210    }
211    dc.close();
212    cc.setObject("OwnedItems", items);
213    redirect = "../../common/ownership/ownership.jsp?ID="+ID+"&item_type="+itemType.name();
214  }
215  else if ("ExportItems".equals(cmd))
216  {
217    // Run an export plugin in a list context
218    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
219    final ItemQuery<Job> query = Job.getQuery();
220    dc = sc.newDbControl();
221    cc.configureQuery(dc, query, true);
222    cc.setQuery(query);
223    JspContext jspContext = ExtensionsControl.createContext(dc, pageContext, GuiContext.list(itemType), null);
224    ExtensionsInvoker listInvoker = ListColumnUtil.useExtensions(jspContext);
225    listInvoker.render(new ListColumnExportRenderer(cc));
226    dc.close();
227    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Export+jobs";
228  }
229  else if ("ExportItem".equals(cmd))
230  {
231    // Run an export plugin in single-item context
232    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
233    redirect = "../../common/export/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Export+job";
234  }
235  else if ("ImportItems".equals(cmd))
236  {
237    // Run an import plugin in a list context
238    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
239    final ItemQuery<Job> query = Job.getQuery();
240    dc = sc.newDbControl();
241    cc.configureQuery(dc, query, true);
242    dc.close();
243    cc.setQuery(query);
244    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&title=Import+jobs";
245  }
246  else if ("ImportItem".equals(cmd))
247  {
248    // Run an import plugin in single-item context
249    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
250    redirect = "../../common/import/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&title=Import+job";
251  }
252  else if ("RunListPlugin".equals(cmd))
253  {
254    // Run another plugin in a list context
255    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
256    final ItemQuery<Job> query = Job.getQuery();
257    dc = sc.newDbControl();
258    cc.configureQuery(dc, query, true);
259    dc.close();
260    cc.setQuery(query);
261    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=LIST&main_type=OTHER&title=Run+plugin";
262  }
263  else if ("RunPlugin".equals(cmd))
264  {
265    // Run another plugin in single-item context
266    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
267    redirect = "../../common/plugin/index.jsp?ID="+ID+"&cmd=SelectPlugin&item_type="+itemType.name()+"&context_type=ITEM&main_type=OTHER&title=Run+plugin";
268  }
269  else if ("RestartJob".equals(cmd))
270  {
271    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
272    boolean useLatestConfiguration = Values.getBoolean(request.getParameter("useLatestConfiguration"), false);
273    boolean clearDryRun = Values.getBoolean(request.getParameter("clearDryRun"), false);
274    dc = sc.newDbControl();
275    Job job = Job.getById(dc, cc.getId());
276    job.retry(useLatestConfiguration, clearDryRun);
277    dc.commit();
278    redirect = viewPage;
279  }
280  else if ("AbortJob".equals(cmd))
281  {
282    ItemContext cc = Base.getAndSetCurrentContext(sc, itemType, pageContext, defaultContext);
283    dc = sc.newDbControl();
284    Job job = Job.getById(dc, cc.getId());
285    SignalTransporter signalTransporter = job.getSignalTransporter();
286    if (signalTransporter == null)
287    {
288      if (job.getStatus() == Job.Status.WAITING)
289      {
290        job.doneError("Aborted by user");
291      }
292    }
293    else
294    {
295      signalTransporter.send(Signal.ABORT);
296    }
297    dc.commit();
298    // Wait some time to give the plug-in a chance to response
299    // Otherwise the user has to wait until next refresh to see the status
300    Thread.sleep(500);
301    redirect = viewPage;
302  }
303  else
304  {
305    throw new WebException("popup", "Invalid command", "The command {1} is not recognised as a valid command.", cmd);
306  }
307}
308finally
309{
310  if (dc != null) dc.close();
311}
312
313if (forward != null)
314{
315  sc.setSessionSetting("alert-message", message);
316  pageContext.forward(forward);
317}
318else if (redirect != null)
319{
320  sc.setSessionSetting("alert-message", message);
321  response.sendRedirect(redirect);
322}
323else if (message == null)
324{
325  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&wait=0");
326}
327else
328{
329  response.sendRedirect(root + "common/close_popup.jsp?refresh_opener=1&message="+HTML.urlEncode(message));
330}
331%>
332
Note: See TracBrowser for help on using the repository browser.