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