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