1 package autotest.afe; 2 3 import autotest.common.SimpleCallback; 4 import autotest.common.CustomHistory.HistoryToken; 5 import autotest.common.table.DatetimeSegmentFilter; 6 import autotest.common.table.Filter; 7 import autotest.common.table.RadioButtonSetFilter; 8 import autotest.common.table.SearchFilter; 9 import autotest.common.table.SelectionManager; 10 import autotest.common.table.TableDecorator; 11 import autotest.common.table.DynamicTable.DynamicTableListener; 12 import autotest.common.ui.ContextMenu; 13 import autotest.common.ui.NotifyManager; 14 import autotest.common.ui.TabView; 15 import autotest.common.ui.TableActionsPanel.TableActionsListener; 16 import autotest.common.ui.ToolTip; 17 18 import com.google.gwt.json.client.JSONArray; 19 import com.google.gwt.json.client.JSONBoolean; 20 import com.google.gwt.json.client.JSONObject; 21 import com.google.gwt.json.client.JSONString; 22 import com.google.gwt.user.client.Command; 23 import com.google.gwt.user.client.ui.HorizontalPanel; 24 import com.google.gwt.user.client.ui.HTMLPanel; 25 26 import java.util.Map; 27 import java.util.Set; 28 29 30 public class JobListView extends TabView implements TableActionsListener { 31 protected static final String SELECTED_LINK_STYLE = "selected-link"; 32 protected static final int JOBS_PER_PAGE = 30; 33 protected static final int STATUS_QUEUED = 0, STATUS_RUNNING = 1, 34 STATUS_FINISHED = 2, STATUS_ALL = 3, 35 STATUS_RADIO_BUTTON_COUNT = 4; 36 protected static final int STATUS_DEFAULT_BUTTON = STATUS_ALL; 37 protected static final int TYPE_SUITE = 0, TYPE_SUB = 1, TYPE_ALL = 2, 38 TYPE_RADIO_BUTTON_COUNT = 3; 39 protected static final int TYPE_DEFAULT_BUTTON = TYPE_ALL; 40 private static final String[] statusHistoryTokens = {"queued", "running", 41 "finished", "all"}; 42 private static final String[] statusRadioButtonLabels = {"Queued Jobs", "Running Jobs", 43 "Finished Jobs", "All Jobs"}; 44 private static final String[] statusFilterStrings = {"not_yet_run", "running", 45 "finished"}; 46 private static final String[] typeHistoryTokens = {"suite", "sub", "all"}; 47 private static final String[] typeRadioButtonLabels = {"Parent Jobs", "Child Jobs", 48 "All Jobs"}; 49 private static final String[] typeFilterStrings = {"suite", "sub"}; 50 51 private JobSelectListener selectListener; 52 53 private JobTable jobTable; 54 private TableDecorator tableDecorator; 55 private JobStateFilter jobStateFilter; 56 private JobTypeFilter jobTypeFilter; 57 private Filter ownerFilter; 58 private SearchFilter nameFilter; 59 private JobStartingDatetimeFilter startingDatetimeFilter; 60 private SelectionManager selectionManager; 61 62 interface JobSelectListener { onJobSelected(int jobId)63 public void onJobSelected(int jobId); 64 } 65 66 static class JobStateFilter extends RadioButtonSetFilter { JobStateFilter()67 public JobStateFilter() { 68 super("job-status"); 69 } 70 71 @Override addParams(JSONObject params)72 public void addParams(JSONObject params) { 73 params.put(statusFilterStrings[getSelectedButtonIndex()], 74 JSONBoolean.getInstance(true)); 75 } 76 77 @Override isActive()78 public boolean isActive() { 79 return getSelectedButtonIndex() < STATUS_ALL; 80 } 81 } 82 83 static class JobTypeFilter extends RadioButtonSetFilter { JobTypeFilter()84 public JobTypeFilter() { 85 super("job-type"); 86 } 87 88 @Override addParams(JSONObject params)89 public void addParams(JSONObject params) { 90 params.put(typeFilterStrings[getSelectedButtonIndex()], 91 JSONBoolean.getInstance(true)); 92 } 93 94 @Override isActive()95 public boolean isActive() { 96 return getSelectedButtonIndex() < TYPE_ALL; 97 } 98 } 99 100 static class JobStartingDatetimeFilter extends DatetimeSegmentFilter { 101 @Override addParams(JSONObject params)102 public void addParams(JSONObject params) { 103 String start_time = startDatetimeBox.getText(); 104 if (start_time == "") { 105 setStartTimeToPlaceHolderValue(); 106 start_time = startDatetimeBox.getText(); 107 } 108 params.put("created_on__gt", new JSONString(start_time)); 109 110 String end_time = endDatetimeBox.getText(); 111 if (start_time == "") { 112 setEndTimeToPlaceHolderValue(); 113 end_time = endDatetimeBox.getText(); 114 } 115 params.put("created_on__lt", new JSONString(end_time)); 116 } 117 118 } 119 abortSelectedJobs()120 public void abortSelectedJobs() { 121 Set<JSONObject> selectedSet = selectionManager.getSelectedObjects(); 122 if (selectedSet.isEmpty()) { 123 NotifyManager.getInstance().showError("No jobs selected"); 124 return; 125 } 126 127 JSONArray ids = new JSONArray(); 128 for(JSONObject jsonObj : selectedSet) { 129 ids.set(ids.size(), jsonObj.get("id")); 130 } 131 132 JSONObject params = new JSONObject(); 133 params.put("job__id__in", ids); 134 AfeUtils.callAbort(params, new SimpleCallback() { 135 public void doCallback(Object source) { 136 refresh(); 137 } 138 }); 139 } 140 141 @Override getElementId()142 public String getElementId() { 143 return "job_list"; 144 } 145 146 @Override refresh()147 public void refresh() { 148 super.refresh(); 149 jobTable.refresh(); 150 } 151 JobListView(JobSelectListener listener)152 public JobListView(JobSelectListener listener) { 153 selectListener = listener; 154 } 155 156 @Override initialize()157 public void initialize() { 158 super.initialize(); 159 jobTable = new JobTable(); 160 jobTable.setRowsPerPage(JOBS_PER_PAGE); 161 jobTable.setClickable(true); 162 jobTable.addListener(new DynamicTableListener() { 163 public void onRowClicked(int rowIndex, JSONObject row, boolean isRightClick) { 164 int jobId = (int) row.get("id").isNumber().doubleValue(); 165 selectListener.onJobSelected(jobId); 166 } 167 168 public void onTableRefreshed() {} 169 }); 170 171 tableDecorator = new TableDecorator(jobTable); 172 tableDecorator.addPaginators(); 173 selectionManager = tableDecorator.addSelectionManager(false); 174 jobTable.setWidgetFactory(selectionManager); 175 tableDecorator.addTableActionsPanel(this, true); 176 addWidget(tableDecorator, "job_table"); 177 178 ownerFilter = new JobOwnerFilter("owner"); 179 jobTable.addFilter(ownerFilter); 180 addWidget(ownerFilter.getWidget(), "user_list"); 181 182 nameFilter = new SearchFilter("name", "Filter", false); 183 jobTable.addFilter(nameFilter); 184 addWidget(nameFilter.getWidget(), "jl_name_search"); 185 186 jobStateFilter = new JobStateFilter(); 187 for (int i = 0; i < STATUS_RADIO_BUTTON_COUNT; i++) 188 jobStateFilter.addRadioButon(statusRadioButtonLabels[i]); 189 // All Jobs is selected by default 190 jobStateFilter.setSelectedButton(STATUS_DEFAULT_BUTTON); 191 jobStateFilter.addCallback(new SimpleCallback() { 192 public void doCallback(Object source) { 193 updateHistory(); 194 } 195 }); 196 jobTable.addFilter(jobStateFilter); 197 addWidget(jobStateFilter.getWidget(), "job_status_controls"); 198 199 jobTypeFilter = new JobTypeFilter(); 200 for (int i = 0; i < TYPE_RADIO_BUTTON_COUNT; i++) 201 jobTypeFilter.addRadioButon(typeRadioButtonLabels[i]); 202 // All Jobs is selected by default 203 jobTypeFilter.setSelectedButton(TYPE_DEFAULT_BUTTON); 204 jobTypeFilter.addCallback(new SimpleCallback() { 205 public void doCallback(Object source) { 206 updateHistory(); 207 } 208 }); 209 jobTable.addFilter(jobTypeFilter); 210 addWidget(new ToolTip("?", "Suite jobs: jobs with child jobs. " + 211 "Sub jobs: jobs with a parent jobs. "), 212 "job_type_tooltip"); 213 addWidget(jobTypeFilter.getWidget(), "job_type_controls"); 214 215 startingDatetimeFilter = new JobStartingDatetimeFilter(); 216 jobTable.addFilter(startingDatetimeFilter); 217 addWidget(startingDatetimeFilter.getWidget(), "job_starting_datetime_controls"); 218 } 219 220 @Override getHistoryArguments()221 public HistoryToken getHistoryArguments() { 222 HistoryToken arguments = super.getHistoryArguments(); 223 arguments.put("state_filter", 224 statusHistoryTokens[jobStateFilter.getSelectedButtonIndex()]); 225 arguments.put("type_filter", 226 typeHistoryTokens[jobTypeFilter.getSelectedButtonIndex()]); 227 return arguments; 228 } 229 230 @Override handleHistoryArguments(Map<String, String> arguments)231 public void handleHistoryArguments(Map<String, String> arguments) { 232 String stateFilter = arguments.get("state_filter"); 233 if (stateFilter == null) { 234 jobStateFilter.setSelectedButton(STATUS_DEFAULT_BUTTON); 235 } else { 236 for (int i = 0; i < STATUS_RADIO_BUTTON_COUNT; i++) { 237 if (stateFilter.equals(statusHistoryTokens[i])) { 238 jobStateFilter.setSelectedButton(i); 239 break; 240 } 241 } 242 } 243 244 String typeFilter = arguments.get("type_filter"); 245 if (typeFilter == null) { 246 jobTypeFilter.setSelectedButton(TYPE_DEFAULT_BUTTON); 247 } else { 248 for (int i = 0; i < TYPE_RADIO_BUTTON_COUNT; i++) { 249 if (typeFilter.equals(typeHistoryTokens[i])) { 250 jobTypeFilter.setSelectedButton(i); 251 break; 252 } 253 } 254 } 255 } 256 getActionMenu()257 public ContextMenu getActionMenu() { 258 ContextMenu menu = new ContextMenu(); 259 menu.addItem("Abort jobs", new Command() { 260 public void execute() { 261 abortSelectedJobs(); 262 } 263 }); 264 if (selectionManager.isEmpty()) 265 menu.setEnabled(false); 266 return menu; 267 } 268 } 269