]> git.argeo.org Git - gpl/argeo-slc.git/blob - eclipse/plugins/org.argeo.slc.client.ui/src/main/java/org/argeo/slc/client/ui/views/ProcessBuilderView.java
Improve process management
[gpl/argeo-slc.git] / eclipse / plugins / org.argeo.slc.client.ui / src / main / java / org / argeo / slc / client / ui / views / ProcessBuilderView.java
1 package org.argeo.slc.client.ui.views;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.IOException;
5 import java.util.ArrayList;
6 import java.util.List;
7 import java.util.Properties;
8 import java.util.UUID;
9
10 import org.apache.commons.io.IOUtils;
11 import org.argeo.slc.SlcException;
12 import org.argeo.slc.client.oxm.OxmInterface;
13 import org.argeo.slc.client.ui.ClientUiPlugin;
14 import org.argeo.slc.client.ui.controllers.ProcessController;
15 import org.argeo.slc.process.RealizedFlow;
16 import org.argeo.slc.process.SlcExecution;
17 import org.argeo.slc.runtime.SlcAgent;
18 import org.eclipse.jface.viewers.ISelectionChangedListener;
19 import org.eclipse.jface.viewers.IStructuredContentProvider;
20 import org.eclipse.jface.viewers.IStructuredSelection;
21 import org.eclipse.jface.viewers.ITableLabelProvider;
22 import org.eclipse.jface.viewers.LabelProvider;
23 import org.eclipse.jface.viewers.SelectionChangedEvent;
24 import org.eclipse.jface.viewers.TableViewer;
25 import org.eclipse.jface.viewers.Viewer;
26 import org.eclipse.jface.viewers.ViewerDropAdapter;
27 import org.eclipse.swt.SWT;
28 import org.eclipse.swt.dnd.DND;
29 import org.eclipse.swt.dnd.TextTransfer;
30 import org.eclipse.swt.dnd.Transfer;
31 import org.eclipse.swt.dnd.TransferData;
32 import org.eclipse.swt.graphics.Image;
33 import org.eclipse.swt.widgets.Composite;
34 import org.eclipse.swt.widgets.Table;
35 import org.eclipse.swt.widgets.TableColumn;
36 import org.eclipse.ui.PartInitException;
37 import org.eclipse.ui.part.ViewPart;
38
39 /**
40 * Display a list of processes that are to be launched as batch. For the moment
41 * being, only one agent by batch is enabled. The batch is constructed by
42 * dropping process from the ExecutionModuleView. Wrong type of data dropped in
43 * this view raises an error.
44 *
45 * @author bsinou
46 *
47 */
48 public class ProcessBuilderView extends ViewPart {
49 // private final static Log log =
50 // LogFactory.getLog(ProcessBuilderView.class);
51
52 public static final String ID = "org.argeo.slc.client.ui.processBuilderView";
53
54 private TableViewer viewer;
55 private List<RealizedFlow> realizedFlows = new ArrayList<RealizedFlow>();
56 private String currentAgentUuid = null;
57 private String host = null;
58
59 // TODO find a better way to get index of the current selected row
60 // used in removeSelected
61 private int curSelectedRow = -1;
62
63 // IoC
64 private OxmInterface oxmBean;
65 private ProcessController processController;
66 private List<SlcAgent> slcAgents;
67
68 public void createPartControl(Composite parent) {
69 Table table = createTable(parent);
70 viewer = new TableViewer(table);
71 viewer.setLabelProvider(new ViewLabelProvider());
72 viewer.setContentProvider(new ViewContentProvider());
73 viewer.addSelectionChangedListener(new SelectionChangedListener());
74
75 int operations = DND.DROP_COPY | DND.DROP_MOVE;
76 Transfer[] tt = new Transfer[] { TextTransfer.getInstance() };
77 viewer.addDropSupport(operations, tt, new ViewDropListener(viewer));
78
79 viewer.setInput(getViewSite());
80 }
81
82 protected Table createTable(Composite parent) {
83 int style = SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL
84 | SWT.FULL_SELECTION;
85
86 Table table = new Table(parent, style);
87
88 table.setLinesVisible(true);
89 table.setHeaderVisible(true);
90
91 TableColumn column = new TableColumn(table, SWT.LEFT, 0);
92 column.setText("Module");
93 column.setWidth(200);
94
95 column = new TableColumn(table, SWT.LEFT, 1);
96 column.setText("Flow");
97 column.setWidth(200);
98
99 return table;
100 }
101
102 protected void execute() {
103 // TODO: use agent proxy to retrieve it
104 SlcAgent agent = null;
105 SlcExecution slcExecution = new SlcExecution();
106 slcExecution.setUuid(UUID.randomUUID().toString());
107 slcExecution.setRealizedFlows(realizedFlows);
108 processController.execute(agent, slcExecution);
109 }
110
111 public void setFocus() {
112 viewer.getControl().setFocus();
113 }
114
115 // update one of the parameter of a given RealizedFlow
116 public void updateParameter(int realizedFlowIndex, String paramName,
117 Object value) {
118 RealizedFlow curRealizedFlow = realizedFlows.get(realizedFlowIndex);
119 curRealizedFlow.getFlowDescriptor().getValues().put(paramName, value);
120 }
121
122 // clear the realizedFlow<List>
123 public void clearBatch() {
124 // we clear the list
125 realizedFlows = new ArrayList<RealizedFlow>();
126 curSelectedRow = -1;
127 refreshParameterview();
128 viewer.refresh();
129 }
130
131 // Remove the selected process from the batch
132 public void removeSelected() {
133 if (curSelectedRow == -1)
134 return;
135 else
136 realizedFlows.remove(curSelectedRow);
137 curSelectedRow = -1;
138 refreshParameterview();
139 viewer.refresh();
140 }
141
142 // calling this method with index =-1 will cause the reset of the view.
143 private void refreshParameterview() {
144 // We choose to directly access the view rather than going through
145 // commands.
146 ProcessParametersView ppView;
147 try {
148 ppView = (ProcessParametersView) ClientUiPlugin.getDefault()
149 .getWorkbench().getActiveWorkbenchWindow().getActivePage()
150 .showView(ProcessParametersView.ID);
151
152 if (curSelectedRow == -1)
153 ppView.setRealizedFlow(-1, null);
154 else
155 ppView.setRealizedFlow(curSelectedRow,
156 realizedFlows.get(curSelectedRow));
157 } catch (PartInitException e) {
158 throw new SlcException(
159 "Cannot Retrieve ProcessParameterView to edit parameters of selected process",
160 e);
161 }
162 }
163
164 // Launches the execution of listed realized flow with specified parameters.
165 public void launchBatch() {
166 SlcExecution slcExecution = new SlcExecution();
167 slcExecution.setUuid(UUID.randomUUID().toString());
168
169 slcExecution.setRealizedFlows(realizedFlows);
170 slcExecution.setHost(host);
171
172 // TODO : insure that the concept has been well understood & the
173 // specification respected
174 SlcAgent curAgent;
175 for (int i = 0; i < slcAgents.size(); i++) {
176 if (currentAgentUuid == null)
177 throw new SlcException(
178 "Cannot launch a batch if no agent is specified");
179 if (currentAgentUuid.equals(slcAgents.get(i).getAgentUuid())) {
180 curAgent = slcAgents.get(i);
181 processController.execute(curAgent, slcExecution);
182 break;
183 }
184 }
185 }
186
187 // Specific Providers for the current view.
188 protected class ViewContentProvider implements IStructuredContentProvider {
189 public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
190 }
191
192 public void dispose() {
193 }
194
195 public Object[] getElements(Object obj) {
196 return realizedFlows.toArray();
197 }
198 }
199
200 protected class ViewLabelProvider extends LabelProvider implements
201 ITableLabelProvider {
202 public String getColumnText(Object obj, int index) {
203 RealizedFlow rf = (RealizedFlow) obj;
204 switch (index) {
205 case 0:
206 return rf.getModuleName();
207 case 1:
208 return rf.getFlowDescriptor().getName();
209 }
210 return getText(obj);
211 }
212
213 public Image getColumnImage(Object obj, int index) {
214 return null;
215 }
216
217 }
218
219 // Parameter view is updated each time a new line is selected
220 class SelectionChangedListener implements ISelectionChangedListener {
221 public void selectionChanged(SelectionChangedEvent evt) {
222
223 IStructuredSelection curSelection = (IStructuredSelection) evt
224 .getSelection();
225 Object obj = curSelection.getFirstElement();
226
227 if (obj instanceof RealizedFlow) {
228 RealizedFlow rf = (RealizedFlow) obj;
229 curSelectedRow = realizedFlows.indexOf(rf);
230 refreshParameterview();
231 setFocus();
232 }
233 }
234 }
235
236 // Implementation of the Drop Listener
237 protected class ViewDropListener extends ViewerDropAdapter {
238
239 public ViewDropListener(Viewer viewer) {
240 super(viewer);
241 }
242
243 @Override
244 public boolean performDrop(Object data) {
245
246 Properties props = new Properties();
247
248 // TODO : Handle wrong type of dropped data
249 ByteArrayInputStream in = new ByteArrayInputStream(data.toString()
250 .getBytes());
251 try {
252 props.load(in);
253 } catch (IOException e) {
254 throw new SlcException("Cannot create read flow node", e);
255 } finally {
256 IOUtils.closeQuietly(in);
257 }
258
259 String agentId = props.getProperty("agentId");
260 if (currentAgentUuid == null) {
261 currentAgentUuid = agentId;
262 host = props.getProperty("host");
263 } else if (!currentAgentUuid.equals(agentId)) {
264 // TODO: as for now, we can only construct batch on a single
265 // Agent, must be upgraded to enable batch on various agent.
266 throw new SlcException(
267 "Cannot create batch on two (or more) distinct agents",
268 null);
269 }
270
271 String fdXml = props.getProperty("RealizedFlowAsXml");
272 if (fdXml == null)
273 return false;
274 RealizedFlow rf = (RealizedFlow) oxmBean.unmarshal(fdXml);
275 realizedFlows.add(rf);
276 curSelectedRow = realizedFlows.indexOf(rf);
277 refreshParameterview();
278 getViewer().refresh();
279 return true;
280 }
281
282 @Override
283 public boolean validateDrop(Object target, int operation,
284 TransferData transferType) {
285 return true;
286 }
287 }
288
289 // IoC
290 public void setSlcAgents(List<SlcAgent> slcAgents) {
291 this.slcAgents = slcAgents;
292 }
293
294 public void setOxmBean(OxmInterface oxmBean) {
295 this.oxmBean = oxmBean;
296 }
297
298 public void setProcessController(ProcessController processController) {
299 this.processController = processController;
300 }
301
302 }