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
;
19 import java
.util
.List
;
21 import java
.util
.UUID
;
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
;
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
;
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";
54 private Session session
;
55 private Node processNode
;
56 private ProcessController processController
;
58 private ProcessBuilderPage builderPage
;
60 private ExecutionModulesManager modulesManager
;
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();
69 if (processPath
!= null) {
70 if (!session
.itemExists(processPath
))
71 throw new SlcException("Process " + processPath
73 processNode
= session
.getNode(processPath
);
75 processNode
= newProcessNode(pei
);
77 setPartName(processNode
.getName());
78 } catch (RepositoryException e
) {
79 throw new SlcException("Cannot initialize editor for " + pei
, e
);
84 protected Node
newProcessNode(ProcessEditorInput pei
)
85 throws RepositoryException
{
86 String uuid
= UUID
.randomUUID().toString();
87 String processPath
= SlcJcrUtils
.createExecutionProcessPath(session
,
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
);
98 public boolean isDirty() {
99 if (getProcessStatus().equals(ExecutionProcess
.NEW
))
101 return super.isDirty();
104 protected String
getProcessStatus() {
106 return processNode
.getProperty(SLC_STATUS
).getString();
107 } catch (RepositoryException e
) {
108 throw new SlcException("Cannot retrieve status for " + processNode
,
114 public void dispose() {
115 JcrUtils
.logoutQuietly(session
);
118 /** Actually runs the process. */
120 // the modifications have to be saved before execution
122 processNode
.setProperty(SLC_STATUS
, ExecutionProcess
.SCHEDULED
);
123 } catch (RepositoryException e
) {
124 throw new SlcException("Cannot update status of " + processNode
, e
);
131 // make sure modules are started for all nodes
132 for (NodeIterator nit
= processNode
.getNode(SLC_FLOW
).getNodes(); nit
134 Node flowNode
= nit
.nextNode();
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
)
142 ClientUiPlugin
.startStopExecutionModule(modulesManager
,
143 executionModuleNode
);
144 } catch (Exception e
) {
146 "Cannot start execution module related to "
152 ExecutionProcess process
= processController
.process(processNode
);
153 Map
<String
, String
> properties
= new HashMap
<String
, String
>();
154 properties
.put(ExecutionModulesManager
.SLC_PROCESS_ID
,
156 modulesManager
.registerProcessNotifier(this, properties
);
157 } catch (Exception e
) {
158 ErrorFeedback
.show("Execution of " + processNode
+ " failed", e
);
163 processController
.kill(processNode
);
166 /** Opens a new editor with a copy of this process */
169 Node duplicatedNode
= duplicateProcess();
170 IWorkbenchPage activePage
= PlatformUI
.getWorkbench()
171 .getActiveWorkbenchWindow().getActivePage();
172 activePage
.openEditor(
173 new ProcessEditorInput(duplicatedNode
.getPath()),
176 } catch (Exception e1
) {
177 throw new SlcException("Cannot relaunch " + processNode
, e1
);
181 /** Duplicates the process */
182 protected Node
duplicateProcess() {
184 Session session
= processNode
.getSession();
185 String uuid
= UUID
.randomUUID().toString();
186 String destPath
= SlcJcrUtils
.createExecutionProcessPath(session
,
188 Node newNode
= JcrUtils
.mkdirs(session
, destPath
,
189 SlcTypes
.SLC_PROCESS
);
191 Node rootRealizedFlowNode
= newNode
.addNode(SLC_FLOW
);
193 JcrUtils
.copy(processNode
.getNode(SLC_FLOW
), rootRealizedFlowNode
);
195 newNode
.setProperty(SLC_UUID
, uuid
);
196 newNode
.setProperty(SLC_STATUS
, ExecutionProcess
.INITIALIZED
);
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
);
208 } catch (RepositoryException e
) {
209 throw new SlcException("Cannot duplicate process", e
);
214 protected void addPages() {
216 builderPage
= new ProcessBuilderPage(this, processNode
,
218 addPage(builderPage
);
219 firePropertyChange(PROP_DIRTY
);
220 } catch (PartInitException e
) {
221 throw new SlcException("Cannot add pages", e
);
227 public void doSave(IProgressMonitor monitor
) {
229 String status
= processNode
.getProperty(SLC_STATUS
).getString();
230 if (status
.equals(ExecutionProcess
.NEW
))
231 processNode
.setProperty(SLC_STATUS
,
232 ExecutionProcess
.INITIALIZED
);
234 builderPage
.commit(true);
235 editorDirtyStateChanged();
236 } catch (RepositoryException e
) {
237 throw new SlcException("Cannot save " + processNode
, e
);
239 JcrUtils
.discardQuietly(session
);
244 public void doSaveAs() {
248 public boolean isSaveAsAllowed() {
252 public void updateStatus(ExecutionProcess process
, String oldStatus
,
256 public void addSteps(ExecutionProcess process
, List
<ExecutionStep
> steps
) {
259 /** Expects one session per editor. */
260 public void setSession(Session session
) {
261 this.session
= session
;
264 public void setProcessController(ProcessController processController
) {
265 this.processController
= processController
;
268 public void setModulesManager(ExecutionModulesManager modulesManager
) {
269 this.modulesManager
= modulesManager
;