]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.client.ui/src/org/argeo/slc/client/ui/editors/ProcessEditor.java
First working version of SLC Runtime v2
[gpl/argeo-slc.git] / org.argeo.slc.client.ui / src / org / argeo / slc / client / ui / editors / ProcessEditor.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 package org.argeo.slc.client.ui.editors;
17
18 import java.util.HashMap;
19 import java.util.Map;
20 import java.util.UUID;
21
22 import javax.jcr.Node;
23 import javax.jcr.NodeIterator;
24 import javax.jcr.Property;
25 import javax.jcr.Repository;
26 import javax.jcr.RepositoryException;
27 import javax.jcr.Session;
28
29 import org.argeo.eclipse.ui.dialogs.ErrorFeedback;
30 import org.argeo.jcr.JcrUtils;
31 import org.argeo.slc.SlcException;
32 import org.argeo.slc.client.ui.ClientUiPlugin;
33 import org.argeo.slc.client.ui.controllers.ProcessController;
34 import org.argeo.slc.execution.ExecutionModulesManager;
35 import org.argeo.slc.execution.ExecutionProcess;
36 import org.argeo.slc.jcr.SlcJcrUtils;
37 import org.argeo.slc.jcr.SlcNames;
38 import org.argeo.slc.jcr.SlcTypes;
39 import org.eclipse.core.runtime.IProgressMonitor;
40 import org.eclipse.ui.IEditorInput;
41 import org.eclipse.ui.IEditorSite;
42 import org.eclipse.ui.IWorkbenchPage;
43 import org.eclipse.ui.PartInitException;
44 import org.eclipse.ui.PlatformUI;
45 import org.eclipse.ui.forms.editor.FormEditor;
46
47 /** Editor for an execution process. */
48 public class ProcessEditor extends FormEditor implements SlcTypes, SlcNames {
49 public final static String ID = ClientUiPlugin.ID + ".processEditor";
50
51 private Session session;
52 private Node processNode;
53 private ProcessController processController;
54
55 private ProcessBuilderPage builderPage;
56
57 private ExecutionModulesManager modulesManager;
58
59 @Override
60 public void init(IEditorSite site, IEditorInput input)
61 throws PartInitException {
62 super.init(site, input);
63 ProcessEditorInput pei = (ProcessEditorInput) input;
64 String processPath = pei.getProcessPath();
65 try {
66 if (processPath != null) {
67 if (!session.itemExists(processPath))
68 throw new SlcException("Process " + processPath
69 + " does not exist");
70 processNode = session.getNode(processPath);
71 } else {// new
72 processNode = newProcessNode(pei);
73 }
74 setPartName(processNode.getName());
75 } catch (RepositoryException e) {
76 throw new SlcException("Cannot initialize editor for " + pei, e);
77 }
78
79 }
80
81 protected Node newProcessNode(ProcessEditorInput pei)
82 throws RepositoryException {
83 String uuid = UUID.randomUUID().toString();
84 String processPath = SlcJcrUtils.createExecutionProcessPath(session,
85 uuid);
86 Node processNode = JcrUtils.mkdirs(session, processPath, SLC_PROCESS);
87 processNode.setProperty(SLC_UUID, uuid);
88 processNode.setProperty(SLC_STATUS, ExecutionProcess.NEW);
89 Node processFlow = processNode.addNode(SLC_FLOW);
90 processFlow.addMixin(SLC_REALIZED_FLOW);
91 return processNode;
92 }
93
94 @Override
95 public boolean isDirty() {
96 if (getProcessStatus().equals(ExecutionProcess.NEW))
97 return true;
98 return super.isDirty();
99 }
100
101 protected String getProcessStatus() {
102 try {
103 return processNode.getProperty(SLC_STATUS).getString();
104 } catch (RepositoryException e) {
105 throw new SlcException("Cannot retrieve status for " + processNode,
106 e);
107 }
108 }
109
110 @Override
111 public void dispose() {
112 JcrUtils.logoutQuietly(session);
113 super.dispose();
114 }
115
116 /** Actually runs the process. */
117 void process() {
118 // the modifications have to be saved before execution
119 try {
120 processNode.setProperty(SLC_STATUS, ExecutionProcess.SCHEDULED);
121 } catch (RepositoryException e) {
122 throw new SlcException("Cannot update status of " + processNode, e);
123 }
124
125 // save
126 doSave(null);
127
128 try {
129 // make sure modules are started for all nodes
130 for (NodeIterator nit = processNode.getNode(SLC_FLOW).getNodes(); nit
131 .hasNext();) {
132 Node flowNode = nit.nextNode();
133 try {
134 String flowDefPath = flowNode.getNode(SLC_ADDRESS)
135 .getProperty(Property.JCR_PATH).getString();
136 Node executionModuleNode = flowNode.getSession().getNode(
137 SlcJcrUtils.modulePath(flowDefPath));
138 if (!executionModuleNode.getProperty(SLC_STARTED)
139 .getBoolean())
140 ClientUiPlugin.startStopExecutionModule(modulesManager,
141 executionModuleNode);
142 } catch (Exception e) {
143 ErrorFeedback.show(
144 "Cannot start execution module related to "
145 + flowNode, e);
146 }
147 }
148
149 // Actually process
150 ExecutionProcess process = processController.process(processNode);
151 Map<String, String> properties = new HashMap<String, String>();
152 properties.put(ExecutionModulesManager.SLC_PROCESS_ID,
153 process.getUuid());
154 // modulesManager.registerProcessNotifier(this, properties);
155 } catch (Exception e) {
156 ErrorFeedback.show("Execution of " + processNode + " failed", e);
157 }
158 }
159
160 void kill() {
161 processController.kill(processNode);
162 }
163
164 /** Opens a new editor with a copy of this process */
165 void relaunch() {
166 try {
167 Node duplicatedNode = duplicateProcess();
168 IWorkbenchPage activePage = PlatformUI.getWorkbench()
169 .getActiveWorkbenchWindow().getActivePage();
170 activePage.openEditor(
171 new ProcessEditorInput(duplicatedNode.getPath()),
172 ProcessEditor.ID);
173 close(false);
174 } catch (Exception e1) {
175 throw new SlcException("Cannot relaunch " + processNode, e1);
176 }
177 }
178
179 /** Duplicates the process */
180 protected Node duplicateProcess() {
181 try {
182 Session session = processNode.getSession();
183 String uuid = UUID.randomUUID().toString();
184 String destPath = SlcJcrUtils.createExecutionProcessPath(session,
185 uuid);
186 Node newNode = JcrUtils.mkdirs(session, destPath,
187 SlcTypes.SLC_PROCESS);
188
189 Node rootRealizedFlowNode = newNode.addNode(SLC_FLOW);
190 // copy node
191 JcrUtils.copy(processNode.getNode(SLC_FLOW), rootRealizedFlowNode);
192
193 newNode.setProperty(SLC_UUID, uuid);
194 newNode.setProperty(SLC_STATUS, ExecutionProcess.INITIALIZED);
195
196 // reset realized flow status
197 // we just manage one level for the time being
198 NodeIterator nit = rootRealizedFlowNode.getNodes(SLC_FLOW);
199 while (nit.hasNext()) {
200 nit.nextNode().setProperty(SLC_STATUS,
201 ExecutionProcess.INITIALIZED);
202 }
203
204 session.save();
205 return newNode;
206 } catch (RepositoryException e) {
207 throw new SlcException("Cannot duplicate process", e);
208 }
209 }
210
211 @Override
212 protected void addPages() {
213 try {
214 builderPage = new ProcessBuilderPage(this, processNode);
215 addPage(builderPage);
216 firePropertyChange(PROP_DIRTY);
217 } catch (PartInitException e) {
218 throw new SlcException("Cannot add pages", e);
219 }
220
221 }
222
223 @Override
224 public void doSave(IProgressMonitor monitor) {
225 try {
226 String status = processNode.getProperty(SLC_STATUS).getString();
227 if (status.equals(ExecutionProcess.NEW))
228 processNode.setProperty(SLC_STATUS,
229 ExecutionProcess.INITIALIZED);
230 session.save();
231 builderPage.commit(true);
232 editorDirtyStateChanged();
233 } catch (RepositoryException e) {
234 throw new SlcException("Cannot save " + processNode, e);
235 // } finally {
236 // JcrUtils.discardQuietly(session);
237 }
238 }
239
240 public void setEditorTitle(String title) {
241 setPartName(title);
242 }
243
244 @Override
245 public void doSaveAs() {
246 }
247
248 @Override
249 public boolean isSaveAsAllowed() {
250 return false;
251 }
252
253 // public void updateStatus(ExecutionProcess process, String oldStatus,
254 // String newStatus) {
255 // }
256 //
257 // public void addSteps(ExecutionProcess process, List<ExecutionStep> steps)
258 // {
259 // }
260
261 /** Expects one session per editor. */
262 @Deprecated
263 public void setSession(Session session) {
264 this.session = session;
265 }
266
267 public void setRepository(Repository repository) {
268 try {
269 session = repository.login();
270 } catch (RepositoryException re) {
271 throw new SlcException("Unable to log in Repository " + repository,
272 re);
273 }
274 }
275
276 public void setProcessController(ProcessController processController) {
277 this.processController = processController;
278 }
279
280 public void setModulesManager(ExecutionModulesManager modulesManager) {
281 this.modulesManager = modulesManager;
282 }
283 }