]> 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
Add Spring Security JSP tags as a dependency
[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.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.argeo.slc.SlcException;
14 import org.argeo.slc.client.oxm.OxmInterface;
15 import org.argeo.slc.client.ui.ClientUiPlugin;
16 import org.argeo.slc.client.ui.controllers.ProcessController;
17 import org.argeo.slc.process.RealizedFlow;
18 import org.argeo.slc.process.SlcExecution;
19 import org.argeo.slc.runtime.SlcAgent;
20 import org.eclipse.core.commands.Command;
21 import org.eclipse.core.commands.IParameter;
22 import org.eclipse.core.commands.Parameterization;
23 import org.eclipse.core.commands.ParameterizedCommand;
24 import org.eclipse.jface.viewers.ISelectionChangedListener;
25 import org.eclipse.jface.viewers.IStructuredContentProvider;
26 import org.eclipse.jface.viewers.IStructuredSelection;
27 import org.eclipse.jface.viewers.ITableLabelProvider;
28 import org.eclipse.jface.viewers.LabelProvider;
29 import org.eclipse.jface.viewers.SelectionChangedEvent;
30 import org.eclipse.jface.viewers.TableViewer;
31 import org.eclipse.jface.viewers.Viewer;
32 import org.eclipse.jface.viewers.ViewerDropAdapter;
33 import org.eclipse.swt.SWT;
34 import org.eclipse.swt.dnd.DND;
35 import org.eclipse.swt.dnd.TextTransfer;
36 import org.eclipse.swt.dnd.Transfer;
37 import org.eclipse.swt.dnd.TransferData;
38 import org.eclipse.swt.graphics.Image;
39 import org.eclipse.swt.layout.GridData;
40 import org.eclipse.swt.widgets.Composite;
41 import org.eclipse.swt.widgets.Table;
42 import org.eclipse.swt.widgets.TableColumn;
43 import org.eclipse.ui.IWorkbench;
44 import org.eclipse.ui.IWorkbenchWindow;
45 import org.eclipse.ui.commands.ICommandService;
46 import org.eclipse.ui.handlers.IHandlerService;
47 import org.eclipse.ui.part.ViewPart;
48
49 /**
50 * Display a list of processes that are to be launched as batch. For the moment
51 * being, only one agent by batch is enabled. The batch is contructed by
52 * dropping process from the ExecutionModuleView. Wrong type of data dropped in
53 * this view might raise errors.
54 *
55 * @author bsinou
56 *
57 */
58 public class ProcessBuilderView extends ViewPart {
59 private final static Log log = LogFactory.getLog(ProcessBuilderView.class);
60
61 public static final String ID = "org.argeo.slc.client.ui.processBuilderView";
62 private static final String EDIT_CMD = "org.argeo.slc.client.ui.editRealizedFlowDetails";
63 private static final String FLOWASXML_PARAM = "org.argeo.slc.client.commands.realizedFlowAsXml";
64 private static final String INDEX_PARAM = "org.argeo.slc.client.commands.realizedFlowIndex";
65
66 // private final static Log log =
67 // LogFactory.getLog(ProcessBuilderView.class);
68
69 private TableViewer viewer;
70 private List<RealizedFlow> realizedFlows = new ArrayList<RealizedFlow>();
71 private String currentAgentUuid = null;
72 private String host = null;
73
74 // TODO find a better way to get index of the current selected row
75 // used in removeSelected
76 private int curSelectedRow = -1;
77
78 // IoC
79 private OxmInterface oxmBean;
80 private ProcessController processController;
81 private List<SlcAgent> slcAgents;
82
83 public void createPartControl(Composite parent) {
84 Table table = createTable(parent);
85 viewer = new TableViewer(table);
86 viewer.setLabelProvider(new ViewLabelProvider());
87 viewer.setContentProvider(new ViewContentProvider());
88 viewer.addSelectionChangedListener(new SelectionChangedListener());
89
90 int operations = DND.DROP_COPY | DND.DROP_MOVE;
91 Transfer[] tt = new Transfer[] { TextTransfer.getInstance() };
92 viewer.addDropSupport(operations, tt, new ViewDropListener(viewer));
93
94 viewer.setInput(getViewSite());
95 }
96
97 protected Table createTable(Composite parent) {
98 int style = SWT.SINGLE | SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL
99 | SWT.FULL_SELECTION | SWT.HIDE_SELECTION;
100
101 Table table = new Table(parent, style);
102
103 GridData gridData = new GridData(GridData.FILL_BOTH);
104 gridData.grabExcessVerticalSpace = true;
105 gridData.grabExcessHorizontalSpace = true;
106 gridData.horizontalSpan = 3;
107 table.setLayoutData(gridData);
108
109 table.setLinesVisible(true);
110 table.setHeaderVisible(true);
111
112 TableColumn column = new TableColumn(table, SWT.LEFT, 0);
113 column.setText("Module");
114 column.setWidth(200);
115
116 column = new TableColumn(table, SWT.LEFT, 1);
117 column.setText("Flow");
118 column.setWidth(200);
119
120 return table;
121 }
122
123 protected void execute() {
124 // TODO: use agent proxy to retrieve it
125 SlcAgent agent = null;
126 SlcExecution slcExecution = new SlcExecution();
127 slcExecution.setUuid(UUID.randomUUID().toString());
128 slcExecution.setRealizedFlows(realizedFlows);
129 processController.execute(agent, slcExecution);
130 }
131
132 public void setFocus() {
133 viewer.getControl().setFocus();
134 }
135
136 // update one of the parameter of a given RealizedFlow
137 public void updateParameter(int realizedFlowIndex, String paramName,
138 Object value) {
139 RealizedFlow curRealizedFlow = realizedFlows.get(realizedFlowIndex);
140 curRealizedFlow.getFlowDescriptor().getValues().put(paramName, value);
141 }
142
143 // clear the realizedFlow<List>
144 public void clearBatch() {
145 // we clear the list
146 realizedFlows = new ArrayList<RealizedFlow>();
147 curSelectedRow = -1;
148 refreshParameterview(null);
149 viewer.refresh();
150 }
151
152 // Remove the selected process from the batch
153 public void removeSelected() {
154 if (curSelectedRow == -1)
155 return;
156 else
157 realizedFlows.remove(curSelectedRow);
158 curSelectedRow = -1;
159 refreshParameterview(null);
160 viewer.refresh();
161 }
162
163 // calling this method with index =-1 will cause the reset of the view.
164 private void refreshParameterview(RealizedFlow rf) {
165 IWorkbench iw = ClientUiPlugin.getDefault().getWorkbench();
166 IHandlerService handlerService = (IHandlerService) iw
167 .getService(IHandlerService.class);
168 try {
169 // get the command from plugin.xml
170 IWorkbenchWindow window = iw.getActiveWorkbenchWindow();
171 ICommandService cmdService = (ICommandService) window
172 .getService(ICommandService.class);
173 Command cmd = cmdService.getCommand(EDIT_CMD);
174
175 ArrayList<Parameterization> parameters = new ArrayList<Parameterization>();
176
177 IParameter iparam;
178 Parameterization params;
179
180 // The current index to be able to records changes on
181 // parameters
182 iparam = cmd.getParameter(INDEX_PARAM);
183 params = new Parameterization(iparam,
184 (new Integer(curSelectedRow)).toString());
185 parameters.add(params);
186
187 if (curSelectedRow != -1) {
188 // The current Realized flow marshalled as XML
189 String result = oxmBean.marshal(rf);
190 iparam = cmd.getParameter(FLOWASXML_PARAM);
191 params = new Parameterization(iparam, result);
192 parameters.add(params);
193 }
194 // build the parameterized command
195 ParameterizedCommand pc = new ParameterizedCommand(cmd,
196 parameters.toArray(new Parameterization[parameters.size()]));
197
198 // execute the command
199 handlerService = (IHandlerService) window
200 .getService(IHandlerService.class);
201 handlerService.executeCommand(pc, null);
202
203 } catch (Exception e) {
204 e.printStackTrace();
205 throw new SlcException("Problem while rendering result. "
206 + e.getMessage());
207 }
208
209 }
210
211 // Return the list of the processes to execute.
212 public void launchBatch() {
213 SlcExecution slcExecution = new SlcExecution();
214 slcExecution.setUuid(UUID.randomUUID().toString());
215
216 slcExecution.setRealizedFlows(realizedFlows);
217 slcExecution.setHost(host);
218
219 // TODO : insure that the concept has been well understood & the
220 // specification respected
221 SlcAgent curAgent;
222 for (int i = 0; i < slcAgents.size(); i++) {
223 if (currentAgentUuid == null)
224 throw new SlcException(
225 "Cannot launch a batch if no agent is specified");
226 if (currentAgentUuid.equals(slcAgents.get(i).getAgentUuid())) {
227 curAgent = slcAgents.get(i);
228 processController.execute(curAgent, slcExecution);
229 break;
230 }
231 }
232 }
233
234 // Specific Providers for the current view.
235 protected class ViewContentProvider implements IStructuredContentProvider {
236 public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
237 }
238
239 public void dispose() {
240 }
241
242 public Object[] getElements(Object obj) {
243 return realizedFlows.toArray();
244 }
245 }
246
247 protected class ViewLabelProvider extends LabelProvider implements
248 ITableLabelProvider {
249 public String getColumnText(Object obj, int index) {
250 RealizedFlow rf = (RealizedFlow) obj;
251 switch (index) {
252 case 0:
253 return rf.getModuleName();
254 case 1:
255 return rf.getFlowDescriptor().getName();
256 }
257 return getText(obj);
258 }
259
260 public Image getColumnImage(Object obj, int index) {
261 return null;
262 }
263
264 }
265
266 // Handle Events
267 class SelectionChangedListener implements ISelectionChangedListener {
268 public void selectionChanged(SelectionChangedEvent evt) {
269
270 IStructuredSelection curSelection = (IStructuredSelection) evt
271 .getSelection();
272 Object obj = curSelection.getFirstElement();
273
274 if (obj instanceof RealizedFlow) {
275 RealizedFlow rf = (RealizedFlow) obj;
276 curSelectedRow = realizedFlows.indexOf(rf);
277 refreshParameterview(rf);
278 }
279 }
280 }
281
282 // Implementation of the Drop Listener
283 protected class ViewDropListener extends ViewerDropAdapter {
284
285 public ViewDropListener(Viewer viewer) {
286 super(viewer);
287 }
288
289 @Override
290 public boolean performDrop(Object data) {
291
292 Properties props = new Properties();
293
294 // TODO : Handle wrong type of dropped data
295 ByteArrayInputStream in = new ByteArrayInputStream(data.toString()
296 .getBytes());
297 try {
298 props.load(in);
299 } catch (IOException e) {
300 throw new SlcException("Cannot create read flow node", e);
301 } finally {
302 IOUtils.closeQuietly(in);
303 }
304
305 String agentId = props.getProperty("agentId");
306 if (currentAgentUuid == null) {
307 currentAgentUuid = agentId;
308 host = props.getProperty("host");
309 } else if (!currentAgentUuid.equals(agentId)) {
310 // TODO: as for now, we can only construct batch on a single
311 // Agent, must be upgraded to enable batch on various agent.
312 throw new SlcException(
313 "Cannot create batch on two (or more) distinct agents",
314 null);
315 // return false;
316 }
317
318 String fdXml = props.getProperty("RealizedFlowAsXml");
319 if (fdXml == null)
320 return false;
321 RealizedFlow rf = (RealizedFlow) oxmBean.unmarshal(fdXml);
322 realizedFlows.add(rf);
323
324 getViewer().refresh();
325 return true;
326 }
327
328 @Override
329 public boolean validateDrop(Object target, int operation,
330 TransferData transferType) {
331 return true;
332 }
333 }
334
335 // IoC
336 public void setSlcAgents(List<SlcAgent> slcAgents) {
337 this.slcAgents = slcAgents;
338 }
339
340 public void setOxmBean(OxmInterface oxmBean) {
341 this.oxmBean = oxmBean;
342 }
343
344 public void setProcessController(ProcessController processController) {
345 this.processController = processController;
346 }
347
348 }