]> git.argeo.org Git - gpl/argeo-slc.git/blob - server/org.argeo.slc.ria/src/argeo-ria-lib/slc/class/org/argeo/slc/ria/BatchView.js
6a7f50b8e2efc7f24aee4fa1591d859863e23475
[gpl/argeo-slc.git] / server / org.argeo.slc.ria / src / argeo-ria-lib / slc / class / org / argeo / slc / ria / BatchView.js
1 /**
2 * Applet for the batch manager
3 */
4 qx.Class.define("org.argeo.slc.ria.BatchView",
5 {
6 extend : qx.ui.container.Composite,
7 implement : [org.argeo.ria.components.IView],
8
9 properties :
10 {
11 /**
12 * The commands definition Map that will be automatically added and wired to the menubar and toolbar.
13 * See {@link org.argeo.ria.event.CommandsManager#definitions} for the keys to use for defining commands.
14 */
15 commands : {
16 init : {
17 "submitform" : {
18 label : "Execute Batch",
19 icon : "resource/slc/media-playback-start.png",
20 shortcut : null,
21 enabled : false,
22 menu : "Launcher",
23 toolbar : "batch",
24 callback : function(e) {
25 if (this.getBatchAgentId()) {
26 this.executeBatchOnAgent(this.getBatchAgentId());
27 }
28 },
29 command : null
30 },
31 /*
32 "toggleopenonadd" : {
33 label : "Auto edit on Add",
34 icon : "resource/slc/document-open.png",
35 shortcut : null,
36 enabled : true,
37 toggle : true,
38 toggleInitialState : true,
39 menu : "Launcher",
40 toolbar : "launcher",
41 callback : function(event) {
42 var state = event.getTarget().getUserData("slc.command.toggleState");
43 this.setAutoOpen(state);
44 },
45 command : null
46 },
47 "editexecutionspecs" : {
48 label : "Edit Execution Specs",
49 icon : "resource/slc/document-open.png",
50 shortcut : null,
51 enabled : false,
52 menu : "Launcher",
53 toolbar : "batch",
54 callback : function(e) {
55 var sel = this.list.getSortedSelection();
56 var spec = sel[0].getUserData("batchEntrySpec");
57 if (spec.hasEditableValues()) {
58 var specEditor = new org.argeo.slc.ria.execution.SpecEditor(spec);
59 specEditor.attachAndShow();
60 }
61 },
62 selectionChange : function(viewId, selection) {
63 if (viewId != "batch:list")
64 return;
65 this.setEnabled(false);
66 if ((selection && selection.length == 1)) {
67 var selectedItemSpec = selection[0].getUserData("batchEntrySpec");
68 if (selectedItemSpec.hasEditableValues()) {
69 this.setEnabled(true);
70 }
71 }
72 },
73 command : null
74 },
75 */
76 "removefrombatch" : {
77 label : "Remove from batch",
78 icon : "resource/slc/edit-delete.png",
79 shortcut : null,
80 enabled : false,
81 menu : "Launcher",
82 toolbar : "batch",
83 callback : function(e) {
84 var sel = this.list.getSortedSelection();
85 var modal = new org.argeo.ria.components.Modal("Confirm", null);
86 modal.addConfirm("Are you sure you want to remove<br> the selected test"
87 + (sel.length > 1 ? "s" : "")
88 + " from the Batch?");
89 modal.addListener("ok", function() {
90 for (var i = 0; i < sel.length; i++) {
91 this.list.remove(sel[i]);
92 }
93 if (!this.list.hasChildren()) {
94 this.setBatchAgentId(null);
95 }
96 }, this);
97 modal.attachAndShow();
98 },
99 selectionChange : function(viewId, selection) {
100 if (viewId != "batch:list")
101 return;
102 this.setEnabled(false);
103 if ((selection && selection.length > 0))
104 this.setEnabled(true);
105 },
106 command : null
107 },
108 "clearbatch" : {
109 label : "Clear batch",
110 icon : "resource/slc/user-trash-full.png",
111 shortcut : null,
112 enabled : true,
113 menu : "Launcher",
114 toolbar : "batch",
115 callback : function(e) {
116 if(!this.list.hasChildren()) return;
117 this.list.selectAll();
118 this.getCommands()["removefrombatch"].command.execute();
119 },
120 selectionChange : function(viewId, selection) {
121 },
122 command : null
123 }
124 }
125 },
126 view : {
127 init : null
128 },
129 viewSelection : {
130 nullable:false,
131 check:"org.argeo.ria.components.ViewSelection"
132 },
133 instanceId : {init:""},
134 instanceLabel : {init:""},
135 /**
136 * A boolean registering whether the SpecEditor must autoOpen or not
137 * when a spec is added to the Batch.
138 */
139 autoOpen : {
140 init : true,
141 check : "Boolean"
142 },
143 batchAgentId : {
144 init : null,
145 nullable : true,
146 check : "String",
147 event : "changeBatchAgentId"
148 }
149 },
150
151 construct : function(){
152 this.base(arguments);
153 this.setLayout(new qx.ui.layout.Dock());
154 },
155
156 members : {
157 /**
158 * The implementation should contain the GUI initialisation.
159 * This is the role of the manager to actually add the graphical component to the pane,
160 * so it's not necessary to do it here.
161 * @param viewPane {org.argeo.ria.components.ViewPane} The pane manager
162 * @param data {Mixed} Any object or data passed by the initiator of the view
163 * @return {Boolean}
164 */
165 init : function(viewPane, data){
166 this.setView(viewPane);
167 this.setViewSelection(new org.argeo.ria.components.ViewSelection(viewPane.getViewId()));
168 this._emptyAgentString = "Empty Batch (Drop scripts here)";
169 this._crtAgentString = "Batch Execution on Agent ";
170
171 },
172 /**
173 * The implementation should contain the real data loading (i.o. query...)
174 * @return {Boolean}
175 */
176 load : function(){
177 this._createLayout();
178 this.getView().setViewTitle(this._emptyAgentString);
179 //this.getView().setViewTitle("");
180 },
181
182 /**
183 * Creates the main applet layout.
184 */
185 _createLayout : function() {
186 this.listPane = new qx.ui.container.Composite(new qx.ui.layout.Dock());
187 this.addListener("changeBatchAgentId", function(event) {
188 var value = event.getData();
189 if (value == null) {
190 this.getView().setViewTitle(this._emptyAgentString);
191 } else {
192 var selectorView = org.argeo.ria.components.ViewsManager.getInstance().getViewPaneById("selector").getContent();
193 if(selectorView){
194 var agentsMap = selectorView.getAgentsMap();
195 this.getView().setViewTitle(this._crtAgentString + "'" + agentsMap[value] + "'");
196 }
197 }
198 }, this);
199
200 var indicator = new qx.ui.core.Widget();
201 indicator.setDecorator(new qx.ui.decoration.Single().set({
202 top : [1, "solid", "#33508D"]
203 }));
204 indicator.setHeight(0);
205 indicator.setOpacity(0.5);
206 indicator.setZIndex(100);
207 indicator.setLayoutProperties({
208 left : -1000,
209 top : -1000
210 });
211 org.argeo.ria.Application.INSTANCE.getRoot().add(indicator);
212
213 this.list = new qx.ui.form.List();
214 this.list.setDecorator(null);
215 this.list.setSelectionMode("multi");
216 this.list.setDroppable(true);
217 this.list.setDraggable(true);
218 this.list.setContextMenu(org.argeo.ria.event.CommandsManager
219 .getInstance().createMenuFromIds(["removefrombatch"]));
220
221 this.list.addListener("dragstart", function(e) {
222 e.addType(["items"]);
223 e.addAction(["move"]);
224 }, this);
225 this.list.addListener("dragend", function(e) {
226 indicator.setDomPosition(-1000, -1000);
227 });
228 this.list.addListener("dragover", function(e) {
229 var orig = e.getOriginalTarget();
230 var origCoords = orig.getContainerLocation();
231 indicator.setWidth(orig.getBounds().width);
232 indicator.setDomPosition(origCoords.left,
233 origCoords.bottom);
234 });
235 this.list.addListener("drag", function(e) {
236 var orig = e.getOriginalTarget();
237 var origCoords = orig.getContainerLocation();
238 indicator.setWidth(orig.getBounds().width);
239 indicator.setDomPosition(origCoords.left,
240 origCoords.bottom);
241 });
242
243 this.list.addListener("drop", function(e) {
244 var target = e.getRelatedTarget();
245 var afterItem = e.getOriginalTarget();
246 indicator.setDomPosition(-1000, -1000);
247 if (afterItem.classname != "qx.ui.form.ListItem")
248 afterItem = null;
249 if (!target) {
250 target = this.list.getSortedSelection()[0];
251 this.addFlowToBatch(target, afterItem);
252 } else {
253 org.argeo.ria.event.CommandsManager.getInstance().executeCommand("addtobatch");
254 }
255 }, this);
256 this.listPane.add(this.list, {
257 edge : "center"
258 });
259
260 this.list.addListener("changeSelection", function(e) {
261 var viewSelection = this.getViewSelection();
262 viewSelection.setViewId("batch:list");
263 viewSelection.clear();
264 var listSel = this.list.getSortedSelection();
265 for (var i = 0; i < listSel.length; i++) {
266 viewSelection.addNode(listSel[i]);
267 }
268 }, this);
269
270 this.dropDecorator = new qx.ui.decoration.Background();
271 this.dropDecorator.set({
272 backgroundImage : "resource/slc/drophere.gif",
273 backgroundRepeat : "no-repeat"
274 });
275
276
277 listChangeListener = function() {
278 var command = org.argeo.ria.event.CommandsManager.getInstance()
279 .getCommandById("submitform");
280 command.setEnabled(this.list.hasChildren());
281 var command2 = org.argeo.ria.event.CommandsManager.getInstance()
282 .getCommandById("clearbatch");
283 command2.setEnabled(this.list.hasChildren());
284 this.list.setDecorator((this.list.hasChildren()?null:this.dropDecorator));
285 };
286 this.list.addListener("addItem", listChangeListener, this);
287 this.list.addListener("removeItem", listChangeListener, this);
288
289 this.list.setDecorator(this.dropDecorator);
290
291 this.add(this.listPane);
292 },
293
294
295 /**
296 * Adds a given ExecutionFlow to the batch
297 *
298 * @param target
299 * {mixed} The dropped target, can be a TreeFile (add) or a
300 * ListItem (reorder).
301 * @param after
302 * {qx.ui.form.ListItem} Optional list item : if set, the
303 * flow will be added as a new list item positionned after
304 * this one.
305 * @param skipAutoOpen
306 * {boolean} Whether the formular should open or not.
307 */
308 addFlowToBatch : function(target, after, skipAutoOpen) {
309 if (target && target.classname == "qx.ui.form.ListItem") {
310 if (!after)
311 return;
312 if (after == "first")
313 this.list.addAt(target, 0);
314 else
315 this.list.addAfter(target, after);
316 return;
317 }
318
319 // Folder case
320 if (qx.Class.isSubClassOf(qx.Class.getByName(target.classname),
321 qx.ui.tree.TreeFolder)) {
322 var allChildren = target.getItems(true);
323 for (var i = 0; i < allChildren.length; i++) {
324 if (allChildren[i].getUserData("executionFlow")) {
325 try{
326 this.addFlowToBatch(allChildren[i], null, true);
327 }catch(e){
328 return;
329 }
330 }
331 }
332 return;
333 }
334
335 // Check agent Uuid against current batch agent Id.
336 var agentUuid = target.getUserData("agentUuid");
337 if (!this.getBatchAgentId()) {
338 this.setBatchAgentId(agentUuid);
339 } else if (this.getBatchAgentId() != agentUuid) {
340 this.error("Batch can contain tests only of the same agent!");
341 throw new Error("Batch can contain tests only of the same agent!");
342 }
343
344 var executionModule = target.getUserData("executionModule");
345 var executionFlow = target.getUserData("executionFlow");
346 var batchEntry = new org.argeo.slc.ria.execution.BatchEntrySpec(
347 executionModule, executionFlow);
348 var label = batchEntry.getLabel();
349 var icon = target.getIcon() || "resource/slc/office-document.png";
350 var item = new qx.ui.form.ListItem(label, icon);
351 /*
352 item.addListener("dblclick", function(e) {
353 this.getCommands()["editexecutionspecs"].command
354 .execute();
355 }, this);
356 */
357 item.setUserData("batchEntrySpec", batchEntry);
358 item.setPaddingTop(1);
359 item.setPaddingBottom(2);
360 if (after) {
361 if (after == "first")
362 this.list.addAt(item, 0);
363 else
364 this.list.addAfter(item, after);
365 } else {
366 this.list.add(item);
367 }
368 this.list.select(item);
369 /*
370 if (this.getAutoOpen() && !skipAutoOpen) {
371 this.getCommands()["editexecutionspecs"].command.execute();
372 }
373 */
374 },
375
376 /**
377 * Called at execution
378 *
379 * @param agentUuid
380 * {String} The id of the target agent
381 */
382 executeBatchOnAgent : function(agentUuid) {
383 var selection = this.list.getChildren();
384 if (!selection.length)
385 return;
386 var slcExecMessage = new org.argeo.slc.ria.execution.Message();
387 for (var i = 0; i < selection.length; i++) {
388 var batchEntrySpec = selection[i].getUserData("batchEntrySpec");
389 slcExecMessage.addBatchEntrySpec(batchEntrySpec);
390 }
391 var req = org.argeo.slc.ria.SlcApi.getNewSlcExecutionService(
392 agentUuid, slcExecMessage.toXml());
393 req.send();
394 // Force logs refresh right now!
395 qx.event.Timer.once(function() {
396 var command = org.argeo.ria.event.CommandsManager
397 .getInstance().getCommandById("reloadlogs");
398 if (command) {
399 command.execute();
400 }
401 }, this, 2000);
402 },
403
404 clearBatchForAgentId : function(agentId){
405 if(this.getBatchAgentId() == agentId){
406 this.list.removeAll();
407 this.setBatchAgentId(null);
408 }
409 },
410
411 /**
412 * Whether this component is already contained in a scroller (return false) or not (return true).
413 * @return {Boolean}
414 */
415 addScroll : function(){return false;},
416 /**
417 * Called at destruction time
418 * Perform all the clean operations (stopping polling queries, etc.)
419 */
420 close : function(){return true;}
421 }
422
423
424 });