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