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