• Home
  • History
  • Annotate
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package autotest.afe;
2 
3 import autotest.common.JSONArrayList;
4 import autotest.common.SimpleCallback;
5 import autotest.common.Utils;
6 
7 import com.google.gwt.event.dom.client.ClickEvent;
8 import com.google.gwt.event.dom.client.ClickHandler;
9 import com.google.gwt.json.client.JSONArray;
10 import com.google.gwt.json.client.JSONObject;
11 import com.google.gwt.json.client.JSONValue;
12 import com.google.gwt.user.client.ui.Button;
13 import com.google.gwt.user.client.ui.DialogBox;
14 import com.google.gwt.user.client.ui.HorizontalPanel;
15 import com.google.gwt.user.client.ui.Label;
16 import com.google.gwt.user.client.ui.Panel;
17 import com.google.gwt.user.client.ui.VerticalPanel;
18 
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Set;
25 
26 class AbortSynchronousDialog extends DialogBox implements ClickHandler {
27     private SimpleCallback abortAsynchronousEntries;
28     private JSONArray synchronousJobArgs;
29     private Button abortAll, abortAsynchronous, cancel;
30 
AbortSynchronousDialog(SimpleCallback abortAsynchronousEntries, Collection<JSONObject> synchronousEntries, boolean showAbortAsynchronous)31     public AbortSynchronousDialog(SimpleCallback abortAsynchronousEntries,
32                                   Collection<JSONObject> synchronousEntries,
33                                   boolean showAbortAsynchronous) {
34         super(false, true);
35         setText("Aborting synchronous jobs");
36         this.abortAsynchronousEntries = abortAsynchronousEntries;
37         String jobList = processJobs(synchronousEntries);
38 
39         String message = "The following jobs are synchronous. To abort part of the job, you must " +
40                          "abort the entire job.";
41 
42         Panel contents = new VerticalPanel();
43         contents.add(new Label(message));
44         Label jobListLabel = new Label(jobList);
45         jobListLabel.getElement().getStyle().setProperty("padding", "1em");
46         contents.add(jobListLabel);
47 
48         abortAll = new Button("Abort full jobs");
49         abortAll.addClickHandler(this);
50         abortAsynchronous = new Button("Abort asynchronous only");
51         abortAsynchronous.addClickHandler(this);
52         cancel = new Button("Cancel");
53         cancel.addClickHandler(this);
54 
55         Panel buttons = new HorizontalPanel();
56         buttons.add(abortAll);
57         if (showAbortAsynchronous) {
58             buttons.add(abortAsynchronous);
59         }
60         buttons.add(cancel);
61         contents.add(buttons);
62 
63         add(contents);
64     }
65 
getGroupTag(JSONObject queueEntry)66     private static String getGroupTag(JSONObject queueEntry) {
67         JSONObject job = queueEntry.get("job").isObject();
68         return AfeUtils.getJobTag(job) + "/" + Utils.jsonToString(queueEntry.get("execution_subdir"));
69     }
70 
71     /**
72      * Compute a list of job IDs and a comma-separated list of job tags, returning the latter.
73      */
processJobs(Collection<JSONObject> synchronousEntries)74     private String processJobs(Collection<JSONObject> synchronousEntries) {
75         Set<String> groupTags = new HashSet<String>();
76         synchronousJobArgs = new JSONArray();
77         for (JSONObject entry : synchronousEntries) {
78             String groupTag = getGroupTag(entry);
79             if (groupTags.contains(groupTag)) {
80                 continue;
81             }
82             groupTags.add(groupTag);
83             JSONValue jobId = entry.get("job").isObject().get("id");
84             JSONObject groupArgs = new JSONObject();
85             groupArgs.put("job__id", jobId);
86             groupArgs.put("execution_subdir", entry.get("execution_subdir"));
87             synchronousJobArgs.set(synchronousJobArgs.size(), groupArgs);
88         }
89         List<String> groupTagList = new ArrayList<String>(groupTags);
90         Collections.sort(groupTagList);
91         return Utils.joinStrings(", ", groupTagList);
92     }
93 
onClick(ClickEvent event)94     public void onClick(ClickEvent event) {
95         if (event.getSource() == abortAll) {
96             for (JSONObject groupParams : new JSONArrayList<JSONObject>(synchronousJobArgs)) {
97                 AfeUtils.callAbort(groupParams, null, false);
98             }
99             abortAsynchronousEntries.doCallback(this);
100         } else if (event.getSource() == abortAsynchronous) {
101             abortAsynchronousEntries.doCallback(this);
102         }
103 
104         hide();
105     }
106 }
107