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

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

Fixes #1875: Abort multiple jobs

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