]> git.argeo.org Git - gpl/argeo-slc.git/blob - eclipse/plugins/org.argeo.slc.client.ui/src/org/argeo/slc/client/ui/views/ProcessBuilderView.java
c7f103505f067c3a862b0ca36c5c51aba14b47e3
[gpl/argeo-slc.git] / eclipse / plugins / org.argeo.slc.client.ui / src / 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.execution.ExecutionFlowDescriptor;
16 import org.argeo.slc.process.RealizedFlow;
17 import org.argeo.slc.process.SlcExecution;
18 import org.argeo.slc.runtime.SlcAgent;
19 import org.eclipse.core.commands.Command;
20 import org.eclipse.core.commands.IParameter;
21 import org.eclipse.core.commands.Parameterization;
22 import org.eclipse.core.commands.ParameterizedCommand;
23 import org.eclipse.jface.viewers.ISelectionChangedListener;
24 import org.eclipse.jface.viewers.IStructuredContentProvider;
25 import org.eclipse.jface.viewers.IStructuredSelection;
26 import org.eclipse.jface.viewers.ITableLabelProvider;
27 import org.eclipse.jface.viewers.LabelProvider;
28 import org.eclipse.jface.viewers.SelectionChangedEvent;
29 import org.eclipse.jface.viewers.TableViewer;
30 import org.eclipse.jface.viewers.Viewer;
31 import org.eclipse.jface.viewers.ViewerDropAdapter;
32 import org.eclipse.swt.SWT;
33 import org.eclipse.swt.dnd.DND;
34 import org.eclipse.swt.dnd.TextTransfer;
35 import org.eclipse.swt.dnd.Transfer;
36 import org.eclipse.swt.dnd.TransferData;
37 import org.eclipse.swt.graphics.Image;
38 import org.eclipse.swt.layout.GridData;
39 import org.eclipse.swt.widgets.Composite;
40 import org.eclipse.swt.widgets.Table;
41 import org.eclipse.swt.widgets.TableColumn;
42 import org.eclipse.ui.IWorkbench;
43 import org.eclipse.ui.IWorkbenchWindow;
44 import org.eclipse.ui.commands.ICommandService;
45 import org.eclipse.ui.handlers.IHandlerService;
46 import org.eclipse.ui.part.ViewPart;
47
48 /**
49 * Display a list of processes that are to be launched as batch. For the moment
50 * being, only one agent by batch is enabled. The batch is contructed by
51 * dropping process from the ExecutionModuleView. Wrong type of data dropped in
52 * this view might raise errors.
53 *
54 * @author bsinou
55 *
56 */
57 public class ProcessBuilderView extends ViewPart {
58 public static final String ID = "org.argeo.slc.client.ui.processBuilderView";
59
60 // private final static Log log =
61 // LogFactory.getLog(ProcessBuilderView.class);
62
63 private TableViewer viewer;
64 private List<RealizedFlow> realizedFlows = new ArrayList<RealizedFlow>();
65 private String currentAgentUuid = null;
66
67 // IoC
68 private OxmInterface oxmBean;
69 private ProcessController processController;
70
71 public void createPartControl(Composite parent) {
72 Table table = createTable(parent);
73 viewer = new TableViewer(table);
74 viewer.setLabelProvider(new ViewLabelProvider());
75 viewer.setContentProvider(new ViewContentProvider());
76 viewer.addSelectionChangedListener(new SelectionChangedListener());
77
78 int operations = DND.DROP_COPY | DND.DROP_MOVE;
79 Transfer[] tt = new Transfer[] { TextTransfer.getInstance() };
80 viewer.addDropSupport(operations, tt, new ViewDropListener(viewer));
81
82 viewer.setInput(getViewSite());
83 }
84
85 protected Table createTable(Composite parent) {
86 int style = SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL
87 | SWT.FULL_SELECTION | SWT.HIDE_SELECTION;
88
89 Table table = new Table(parent, style);
90
91 GridData gridData = new GridData(GridData.FILL_BOTH);
92 gridData.grabExcessVerticalSpace = true;
93 gridData.grabExcessHorizontalSpace = true;
94 gridData.horizontalSpan = 3;
95 table.setLayoutData(gridData);
96
97 table.setLinesVisible(true);
98 table.setHeaderVisible(true);
99
100 TableColumn column = new TableColumn(table, SWT.LEFT, 0);
101 column.setText("Module");
102 column.setWidth(200);
103
104 column = new TableColumn(table, SWT.LEFT, 1);
105 column.setText("Flow");
106 column.setWidth(200);
107
108 return table;
109 }
110
111 protected void execute() {
112 // TODO: use agent proxy to retrieve it
113 SlcAgent agent = null;
114 SlcExecution slcExecution = new SlcExecution();
115 slcExecution.setUuid(UUID.randomUUID().toString());
116 slcExecution.setRealizedFlows(realizedFlows);
117 processController.execute(agent, slcExecution);
118 }
119
120 public void setFocus() {
121 viewer.getControl().setFocus();
122 }
123
124 // update one of the parameter of a given RealizedFlow
125 public void updateParameter(int realizedFlowIndex, String paramName,
126 Object value) {
127 RealizedFlow curRealizedFlow = realizedFlows.get(realizedFlowIndex);
128 curRealizedFlow.getFlowDescriptor().getValues().put(paramName, value);
129 }
130
131 // Specific Providers for the current view.
132 protected class ViewContentProvider implements IStructuredContentProvider {
133 public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
134 }
135
136 public void dispose() {
137 }
138
139 public Object[] getElements(Object obj) {
140 return realizedFlows.toArray();
141 }
142 }
143
144 protected class ViewLabelProvider extends LabelProvider implements
145 ITableLabelProvider {
146 public String getColumnText(Object obj, int index) {
147 RealizedFlow rf = (RealizedFlow) obj;
148 switch (index) {
149 case 0:
150 return rf.getModuleName();
151 case 1:
152 return rf.getFlowDescriptor().getName();
153 }
154 return getText(obj);
155 }
156
157 public Image getColumnImage(Object obj, int index) {
158 return null;
159 }
160
161 }
162
163 // Handle Events
164 class SelectionChangedListener implements ISelectionChangedListener {
165 public void selectionChanged(SelectionChangedEvent evt) {
166
167 IStructuredSelection curSelection = (IStructuredSelection) evt
168 .getSelection();
169 Object obj = curSelection.getFirstElement();
170
171 if (obj instanceof RealizedFlow) {
172 RealizedFlow rf = (RealizedFlow) obj;
173
174 IWorkbench iw = ClientUiPlugin.getDefault().getWorkbench();
175 IHandlerService handlerService = (IHandlerService) iw
176 .getService(IHandlerService.class);
177
178 // TODO :
179 // WARNING :
180 // when marshalling an ExecutionFlowDescriptor, the Execution
181 // Spec is set correctly,
182 // but
183 // when marshalling directly a realized flow, paramters are
184 // stored under ExecutionFlowDescriptor.values
185 String result = oxmBean.marshal(rf);
186
187 // Passing parameters to the command
188 try {
189 // get the command from plugin.xml
190 IWorkbenchWindow window = iw.getActiveWorkbenchWindow();
191 ICommandService cmdService = (ICommandService) window
192 .getService(ICommandService.class);
193 Command cmd = cmdService
194 .getCommand("org.argeo.slc.client.ui.editRealizedFlowDetails");
195
196 ArrayList<Parameterization> parameters = new ArrayList<Parameterization>();
197
198 IParameter iparam;
199 Parameterization params;
200
201 // The current index to be able to records changes on
202 // parameters
203 iparam = cmd
204 .getParameter("org.argeo.slc.client.commands.realizedFlowIndex");
205 params = new Parameterization(iparam, (new Integer(
206 realizedFlows.indexOf(rf))).toString());
207
208 parameters.add(params);
209
210 // The current Realized flow marshalled as XML
211 // See warning above
212 iparam = cmd
213 .getParameter("org.argeo.slc.client.commands.realizedFlowAsXml");
214 params = new Parameterization(iparam, result);
215 parameters.add(params);
216
217 // build the parameterized command
218 ParameterizedCommand pc = new ParameterizedCommand(cmd,
219 parameters.toArray(new Parameterization[parameters
220 .size()]));
221
222 // execute the command
223 handlerService = (IHandlerService) window
224 .getService(IHandlerService.class);
225 handlerService.executeCommand(pc, null);
226
227 } catch (Exception e) {
228 e.printStackTrace();
229 throw new SlcException("Problem while rendering result. "
230 + e.getMessage());
231 }
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 else if (!currentAgentUuid.equals(agentId)) {
263 // TODO: as for now, we can only construct batch on a single
264 // Agent,
265 // 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 // return false;
270 }
271
272 RealizedFlow rf = realizedFlowFromProperties(props);
273 realizedFlows.add(rf);
274
275 getViewer().refresh();
276 return true;
277 }
278
279 private RealizedFlow realizedFlowFromProperties(Properties props) {
280 RealizedFlow rf = new RealizedFlow();
281 rf.setModuleName(props.getProperty("moduleName"));
282 rf.setModuleVersion(props.getProperty("moduleVersion"));
283 String fdXml = props.getProperty("FlowDescriptorAsXml");
284 if (fdXml != null) {
285 Object o = oxmBean.unmarshal(fdXml);
286 if (o instanceof ExecutionFlowDescriptor) {
287 rf.setFlowDescriptor((ExecutionFlowDescriptor) o);
288 System.out.println("instance of EFD !!!"
289 + rf.getFlowDescriptor().toString());
290 System.out.println(rf.getFlowDescriptor()
291 .getExecutionSpec());
292 return rf;
293 }
294 }
295 // Else
296 System.out
297 .println("***** WARNING : we should not be here; corresponding flow name"
298 + props.getProperty("flowName"));
299 ExecutionFlowDescriptor efd = new ExecutionFlowDescriptor();
300 efd.setName(props.getProperty("flowName"));
301 rf.setFlowDescriptor(efd);
302 return rf;
303 }
304
305 @Override
306 public boolean validateDrop(Object target, int operation,
307 TransferData transferType) {
308 return true;
309 }
310 }
311
312 // IoC
313 public void setOxmBean(OxmInterface oxmBean) {
314 this.oxmBean = oxmBean;
315 }
316
317 public void setProcessController(ProcessController processController) {
318 this.processController = processController;
319 }
320
321 }