2 * Copyright (C) 2007-2012 Argeo GmbH
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org
.argeo
.slc
.client
.ui
.editors
;
18 import java
.util
.HashMap
;
20 import java
.util
.UUID
;
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
;
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
;
46 /** Editor for an execution process. */
47 public class ProcessEditor
extends FormEditor
implements SlcTypes
, SlcNames
{
48 public final static String ID
= ClientUiPlugin
.ID
+ ".processEditor";
50 private Session session
;
51 private Node processNode
;
52 private ProcessController processController
;
54 private ProcessBuilderPage builderPage
;
56 private ExecutionModulesManager modulesManager
;
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();
65 if (processPath
!= null) {
66 if (!session
.itemExists(processPath
))
67 throw new SlcException("Process " + processPath
69 processNode
= session
.getNode(processPath
);
71 processNode
= newProcessNode(pei
);
73 setPartName(processNode
.getName());
74 } catch (RepositoryException e
) {
75 throw new SlcException("Cannot initialize editor for " + pei
, e
);
80 protected Node
newProcessNode(ProcessEditorInput pei
)
81 throws RepositoryException
{
82 String uuid
= UUID
.randomUUID().toString();
83 String processPath
= SlcJcrUtils
.createExecutionProcessPath(session
,
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
);
94 public boolean isDirty() {
95 if (getProcessStatus().equals(ExecutionProcess
.NEW
))
97 return super.isDirty();
100 protected String
getProcessStatus() {
102 return processNode
.getProperty(SLC_STATUS
).getString();
103 } catch (RepositoryException e
) {
104 throw new SlcException("Cannot retrieve status for " + processNode
,
110 public void dispose() {
111 JcrUtils
.logoutQuietly(session
);
114 /** Actually runs the process. */
116 // the modifications have to be saved before execution
118 processNode
.setProperty(SLC_STATUS
, ExecutionProcess
.SCHEDULED
);
119 } catch (RepositoryException e
) {
120 throw new SlcException("Cannot update status of " + processNode
, e
);
127 // make sure modules are started for all nodes
128 for (NodeIterator nit
= processNode
.getNode(SLC_FLOW
).getNodes(); nit
130 Node flowNode
= nit
.nextNode();
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
)
138 ClientUiPlugin
.startStopExecutionModule(modulesManager
,
139 executionModuleNode
);
140 } catch (Exception e
) {
142 "Cannot start execution module related to "
148 ExecutionProcess process
= processController
.process(processNode
);
149 Map
<String
, String
> properties
= new HashMap
<String
, String
>();
150 properties
.put(ExecutionModulesManager
.SLC_PROCESS_ID
,
152 // modulesManager.registerProcessNotifier(this, properties);
153 } catch (Exception e
) {
154 ErrorFeedback
.show("Execution of " + processNode
+ " failed", e
);
159 processController
.kill(processNode
);
162 /** Opens a new editor with a copy of this process */
165 Node duplicatedNode
= duplicateProcess();
166 IWorkbenchPage activePage
= PlatformUI
.getWorkbench()
167 .getActiveWorkbenchWindow().getActivePage();
168 activePage
.openEditor(
169 new ProcessEditorInput(duplicatedNode
.getPath()),
172 } catch (Exception e1
) {
173 throw new SlcException("Cannot relaunch " + processNode
, e1
);
177 /** Duplicates the process */
178 protected Node
duplicateProcess() {
180 Session session
= processNode
.getSession();
181 String uuid
= UUID
.randomUUID().toString();
182 String destPath
= SlcJcrUtils
.createExecutionProcessPath(session
,
184 Node newNode
= JcrUtils
.mkdirs(session
, destPath
,
185 SlcTypes
.SLC_PROCESS
);
187 Node rootRealizedFlowNode
= newNode
.addNode(SLC_FLOW
);
189 JcrUtils
.copy(processNode
.getNode(SLC_FLOW
), rootRealizedFlowNode
);
191 newNode
.setProperty(SLC_UUID
, uuid
);
192 newNode
.setProperty(SLC_STATUS
, ExecutionProcess
.INITIALIZED
);
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
);
204 } catch (RepositoryException e
) {
205 throw new SlcException("Cannot duplicate process", e
);
210 protected void addPages() {
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
);
222 public void doSave(IProgressMonitor monitor
) {
224 String status
= processNode
.getProperty(SLC_STATUS
).getString();
225 if (status
.equals(ExecutionProcess
.NEW
))
226 processNode
.setProperty(SLC_STATUS
,
227 ExecutionProcess
.INITIALIZED
);
229 builderPage
.commit(true);
230 editorDirtyStateChanged();
231 } catch (RepositoryException e
) {
232 throw new SlcException("Cannot save " + processNode
, e
);
234 JcrUtils
.discardQuietly(session
);
239 public void doSaveAs() {
243 public boolean isSaveAsAllowed() {
247 // public void updateStatus(ExecutionProcess process, String oldStatus,
248 // String newStatus) {
251 // public void addSteps(ExecutionProcess process, List<ExecutionStep> steps)
255 /** Expects one session per editor. */
256 public void setSession(Session session
) {
257 this.session
= session
;
260 public void setProcessController(ProcessController processController
) {
261 this.processController
= processController
;
264 public void setModulesManager(ExecutionModulesManager modulesManager
) {
265 this.modulesManager
= modulesManager
;