• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package autotest.afe;
2 
3 import autotest.common.JsonRpcCallback;
4 import autotest.common.JsonRpcProxy;
5 import autotest.common.table.Filter;
6 import autotest.common.table.SelectionManager;
7 import autotest.common.table.TableDecorator;
8 import autotest.common.table.DynamicTable.DynamicTableListener;
9 import autotest.common.ui.ContextMenu;
10 import autotest.common.ui.NotifyManager;
11 import autotest.common.ui.TabView;
12 import autotest.common.ui.TableActionsPanel.TableActionsListener;
13 
14 import com.google.gwt.event.dom.client.ClickEvent;
15 import com.google.gwt.event.dom.client.ClickHandler;
16 import com.google.gwt.i18n.client.DateTimeFormat;
17 import com.google.gwt.json.client.JSONArray;
18 import com.google.gwt.json.client.JSONNumber;
19 import com.google.gwt.json.client.JSONObject;
20 import com.google.gwt.json.client.JSONString;
21 import com.google.gwt.json.client.JSONValue;
22 import com.google.gwt.user.client.Command;
23 import com.google.gwt.user.client.ui.Button;
24 import com.google.gwt.user.client.ui.FlexTable;
25 import com.google.gwt.user.client.ui.HasAlignment;
26 import com.google.gwt.user.client.ui.HorizontalPanel;
27 import com.google.gwt.user.client.ui.Label;
28 import com.google.gwt.user.client.ui.Panel;
29 import com.google.gwt.user.client.ui.TextBox;
30 import com.google.gwt.user.client.ui.VerticalPanel;
31 import com.google.gwt.user.client.ui.Widget;
32 
33 import java.util.Set;
34 
35 public class RecurringView extends TabView implements TableActionsListener {
36     private static final int RECURRINGRUN_PER_PAGE = 30;
37     private static final int DEFAULT_LOOP_DELAY = 3600;
38     private static final int DEFAULT_LOOP_COUNT = 1;
39 
40     private RecurringSelectListener selectListener;
41     private RecurringTable recurringTable;
42     private TableDecorator tableDecorator;
43     private Filter ownerFilter;
44     private SelectionManager selectionManager;
45     private VerticalPanel createRecurringPanel;
46     private Label jobIdLbl = new Label("");
47     private TextBox startDate = new TextBox();
48     private TextBox loopDelay = new TextBox();
49     private TextBox loopCount = new TextBox();
50     private FlexTable createRecurTable;
51 
52     private JsonRpcProxy rpcProxy = JsonRpcProxy.getProxy();
53 
54     interface RecurringSelectListener {
onRecurringSelected(int schedId)55         public void onRecurringSelected(int schedId);
56     }
57 
58     @Override
getElementId()59     public String getElementId() {
60         return "recurring_list";
61     }
62 
63     @Override
refresh()64     public void refresh() {
65         super.refresh();
66         recurringTable.refresh();
67     }
68 
RecurringView(RecurringSelectListener listener)69     public RecurringView(RecurringSelectListener listener) {
70         selectListener = listener;
71     }
72 
73     @Override
initialize()74     public void initialize() {
75         recurringTable = new RecurringTable();
76 
77         recurringTable.setRowsPerPage(RECURRINGRUN_PER_PAGE);
78         recurringTable.setClickable(true);
79         recurringTable.addListener(new DynamicTableListener() {
80             public void onRowClicked(int rowIndex, JSONObject row, boolean isRightClick) {
81                 JSONObject job = row.get("job").isObject();
82                 int jobId = (int) job.get("id").isNumber().doubleValue();
83                 selectListener.onRecurringSelected(jobId);
84             }
85 
86             public void onTableRefreshed() {}
87         });
88 
89         tableDecorator = new TableDecorator(recurringTable);
90         tableDecorator.addPaginators();
91         selectionManager = tableDecorator.addSelectionManager(false);
92         recurringTable.setWidgetFactory(selectionManager);
93         tableDecorator.addTableActionsPanel(this, true);
94         addWidget(tableDecorator, "recurring_table");
95 
96 
97         ownerFilter = new JobOwnerFilter("owner__login");
98         recurringTable.addFilter(ownerFilter);
99         Panel ownerFilterPanel = new HorizontalPanel();
100         ownerFilterPanel.add(new Label("View recurring jobs for user:"));
101         ownerFilterPanel.add(ownerFilter.getWidget());
102         addWidget(ownerFilterPanel, "recurring_user_list");
103 
104         initRecurringPanel();
105 
106         addWidget(createRecurringPanel, "recurring_create_panel");
107     }
108 
getActionMenu()109     public ContextMenu getActionMenu() {
110         ContextMenu menu = new ContextMenu();
111         menu.addItem("Remove recurring runs", new Command() {
112             public void execute() {
113                 removeSelectedRecurring();
114             }
115         });
116         if (selectionManager.isEmpty())
117             menu.setEnabled(false);
118         return menu;
119     }
120 
initRecurringPanel()121     private void initRecurringPanel() {
122         createRecurTable = new FlexTable();
123 
124         Label createLbl = new Label("Creating recurring job");
125         Button createBtn = new Button("Create recurring job");
126         Button resetBtn = new Button("Reset");
127         Button cancelBtn = new Button("Cancel");
128 
129         createRecurringPanel = new VerticalPanel();
130         createRecurringPanel.setVisible(false);
131 
132         createLbl.setStyleName("title");
133         createLbl.setHorizontalAlignment(HasAlignment.ALIGN_CENTER);
134 
135         setCreateTableRow(0, "Template Job Id:", jobIdLbl);
136         setCreateTableRow(1, "Start time (on server):", startDate);
137         setCreateTableRow(2, "Loop delay (in sec.):", loopDelay);
138         setCreateTableRow(3, "Loop count:", loopCount);
139 
140         createRecurTable.setWidget(4, 0, createBtn);
141         createRecurTable.setWidget(4, 1, resetBtn);
142         createRecurTable.setWidget(4, 2, cancelBtn);
143 
144         createRecurringPanel.add(createLbl);
145         createRecurringPanel.add(createRecurTable);
146 
147         resetBtn.addClickHandler(new ClickHandler() {
148             public void onClick(ClickEvent event) {
149                 resetCreate();
150             }
151         });
152 
153         createBtn.addClickHandler(new ClickHandler() {
154             public void onClick(ClickEvent event) {
155                 submitRecurringJob();
156             }
157         });
158 
159         cancelBtn.addClickHandler(new ClickHandler() {
160             public void onClick(ClickEvent event) {
161                 createRecurringPanel.setVisible(false);
162             }
163         });
164 
165     }
166 
setCreateTableRow(int row, String name, Widget control)167     private void setCreateTableRow(int row, String name, Widget control) {
168         createRecurTable.setText(row, 0, name);
169         createRecurTable.setWidget(row, 1, control);
170         createRecurTable.getFlexCellFormatter().setStyleName(row, 0, "field-name");
171     }
172 
createRecurringJob(int jobId)173     public void createRecurringJob(int jobId) {
174         createRecurringPanel.setVisible(true);
175         jobIdLbl.setText(Integer.toString(jobId));
176         resetCreate();
177     }
178 
submitRecurringJob()179     private void submitRecurringJob() {
180         final int delayValue, countValue;
181         try {
182             delayValue = AfeUtils.parsePositiveIntegerInput(loopDelay.getText(),
183                                                             "loop delay");
184             countValue = AfeUtils.parsePositiveIntegerInput(loopCount.getText(),
185                                                             "loop count");
186            checkDate();
187         } catch (IllegalArgumentException exc) {
188             return;
189         }
190 
191         JSONObject args = new JSONObject();
192         args.put("job_id", new JSONNumber(Integer.parseInt(jobIdLbl.getText())));
193         args.put("start_date", new JSONString(startDate.getText()));
194         args.put("loop_period", new JSONNumber(delayValue));
195         args.put("loop_count", new JSONNumber(countValue));
196 
197         rpcProxy.rpcCall("create_recurring_run", args, new JsonRpcCallback() {
198             @Override
199             public void onSuccess(JSONValue result) {
200                 int id = (int) result.isNumber().doubleValue();
201                 createRecurringPanel.setVisible(false);
202                 NotifyManager.getInstance().showMessage("Recurring run " +
203                                                         Integer.toString(id) +
204                                                         " created");
205                 refresh();
206             }
207         });
208     }
209 
resetCreate()210     private void resetCreate() {
211         getServerTime();
212         loopDelay.setText(Integer.toString(DEFAULT_LOOP_DELAY));
213         loopCount.setText(Integer.toString(DEFAULT_LOOP_COUNT));
214     }
215 
getServerTime()216     private void getServerTime() {
217         rpcProxy.rpcCall("get_server_time", null, new JsonRpcCallback() {
218             @Override
219             public void onSuccess(JSONValue result) {
220                 String sTime = result.isString().stringValue();
221                 startDate.setText(sTime);
222             }
223         });
224     }
225 
checkDate()226     private void checkDate() {
227         try {
228             DateTimeFormat fmt = DateTimeFormat.getFormat("yyyy-MM-dd HH:mm");
229             fmt.parse(startDate.getText());
230         }
231         catch (IllegalArgumentException exc) {
232             String error = "Please enter a correct date/time " +
233                            "format: yyyy-MM-dd HH:mm";
234             NotifyManager.getInstance().showError(error);
235             throw new IllegalArgumentException();
236         }
237     }
238 
removeSelectedRecurring()239     private void removeSelectedRecurring() {
240         Set<JSONObject> selectedSet = selectionManager.getSelectedObjects();
241         if (selectedSet.isEmpty()) {
242             NotifyManager.getInstance().showError("No recurring run selected");
243             return;
244         }
245 
246         JSONArray ids = new JSONArray();
247         for(JSONObject jsonObj : selectedSet) {
248             ids.set(ids.size(), jsonObj.get("id"));
249         }
250 
251         JSONObject params = new JSONObject();
252         params.put("id__in", ids);
253         callRemove(params);
254     }
255 
callRemove(JSONObject params)256     private void callRemove(JSONObject params) {
257         JsonRpcProxy rpcProxy = JsonRpcProxy.getProxy();
258         rpcProxy.rpcCall("delete_recurring_runs", params,
259                          new JsonRpcCallback() {
260             @Override
261             public void onSuccess(JSONValue result) {
262                     NotifyManager.getInstance().showMessage("Recurring runs " +
263                                                             "removed");
264                     refresh();
265             }
266         });
267     }
268 }
269