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
.Repository
;
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
.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
;
47 /** Editor for an execution process. */
48 public class ProcessEditor
extends FormEditor
implements SlcTypes
, SlcNames
{
49 public final static String ID
= ClientUiPlugin
.ID
+ ".processEditor";
51 private Session session
;
52 private Node processNode
;
53 private ProcessController processController
;
55 private ProcessBuilderPage builderPage
;
57 private ExecutionModulesManager modulesManager
;
60 public void init(IEditorSite site
, IEditorInput input
)
61 throws PartInitException
{
62 super.init(site
, input
);
63 ProcessEditorInput pei
= (ProcessEditorInput
) input
;
64 String processPath
= pei
.getProcessPath();
66 if (processPath
!= null) {
67 if (!session
.itemExists(processPath
))
68 throw new SlcException("Process " + processPath
70 processNode
= session
.getNode(processPath
);
72 processNode
= newProcessNode(pei
);
74 setPartName(processNode
.getName());
75 } catch (RepositoryException e
) {
76 throw new SlcException("Cannot initialize editor for " + pei
, e
);
81 protected Node
newProcessNode(ProcessEditorInput pei
)
82 throws RepositoryException
{
83 String uuid
= UUID
.randomUUID().toString();
84 String processPath
= SlcJcrUtils
.createExecutionProcessPath(session
,
86 Node processNode
= JcrUtils
.mkdirs(session
, processPath
, SLC_PROCESS
);
87 processNode
.setProperty(SLC_UUID
, uuid
);
88 processNode
.setProperty(SLC_STATUS
, ExecutionProcess
.NEW
);
89 Node processFlow
= processNode
.addNode(SLC_FLOW
);
90 processFlow
.addMixin(SLC_REALIZED_FLOW
);
95 public boolean isDirty() {
96 if (getProcessStatus().equals(ExecutionProcess
.NEW
))
98 return super.isDirty();
101 protected String
getProcessStatus() {
103 return processNode
.getProperty(SLC_STATUS
).getString();
104 } catch (RepositoryException e
) {
105 throw new SlcException("Cannot retrieve status for " + processNode
,
111 public void dispose() {
112 JcrUtils
.logoutQuietly(session
);
116 /** Actually runs the process. */
118 // the modifications have to be saved before execution
120 processNode
.setProperty(SLC_STATUS
, ExecutionProcess
.SCHEDULED
);
121 } catch (RepositoryException e
) {
122 throw new SlcException("Cannot update status of " + processNode
, e
);
129 // make sure modules are started for all nodes
130 for (NodeIterator nit
= processNode
.getNode(SLC_FLOW
).getNodes(); nit
132 Node flowNode
= nit
.nextNode();
134 String flowDefPath
= flowNode
.getNode(SLC_ADDRESS
)
135 .getProperty(Property
.JCR_PATH
).getString();
136 Node executionModuleNode
= flowNode
.getSession().getNode(
137 SlcJcrUtils
.modulePath(flowDefPath
));
138 if (!executionModuleNode
.getProperty(SLC_STARTED
)
140 ClientUiPlugin
.startStopExecutionModule(modulesManager
,
141 executionModuleNode
);
142 } catch (Exception e
) {
144 "Cannot start execution module related to "
150 ExecutionProcess process
= processController
.process(processNode
);
151 Map
<String
, String
> properties
= new HashMap
<String
, String
>();
152 properties
.put(ExecutionModulesManager
.SLC_PROCESS_ID
,
154 // modulesManager.registerProcessNotifier(this, properties);
155 } catch (Exception e
) {
156 ErrorFeedback
.show("Execution of " + processNode
+ " failed", e
);
161 processController
.kill(processNode
);
164 /** Opens a new editor with a copy of this process */
167 Node duplicatedNode
= duplicateProcess();
168 IWorkbenchPage activePage
= PlatformUI
.getWorkbench()
169 .getActiveWorkbenchWindow().getActivePage();
170 activePage
.openEditor(
171 new ProcessEditorInput(duplicatedNode
.getPath()),
174 } catch (Exception e1
) {
175 throw new SlcException("Cannot relaunch " + processNode
, e1
);
179 /** Duplicates the process */
180 protected Node
duplicateProcess() {
182 Session session
= processNode
.getSession();
183 String uuid
= UUID
.randomUUID().toString();
184 String destPath
= SlcJcrUtils
.createExecutionProcessPath(session
,
186 Node newNode
= JcrUtils
.mkdirs(session
, destPath
,
187 SlcTypes
.SLC_PROCESS
);
189 Node rootRealizedFlowNode
= newNode
.addNode(SLC_FLOW
);
191 JcrUtils
.copy(processNode
.getNode(SLC_FLOW
), rootRealizedFlowNode
);
193 newNode
.setProperty(SLC_UUID
, uuid
);
194 newNode
.setProperty(SLC_STATUS
, ExecutionProcess
.INITIALIZED
);
196 // reset realized flow status
197 // we just manage one level for the time being
198 NodeIterator nit
= rootRealizedFlowNode
.getNodes(SLC_FLOW
);
199 while (nit
.hasNext()) {
200 nit
.nextNode().setProperty(SLC_STATUS
,
201 ExecutionProcess
.INITIALIZED
);
206 } catch (RepositoryException e
) {
207 throw new SlcException("Cannot duplicate process", e
);
212 protected void addPages() {
214 builderPage
= new ProcessBuilderPage(this, processNode
);
215 addPage(builderPage
);
216 firePropertyChange(PROP_DIRTY
);
217 } catch (PartInitException e
) {
218 throw new SlcException("Cannot add pages", e
);
224 public void doSave(IProgressMonitor monitor
) {
226 String status
= processNode
.getProperty(SLC_STATUS
).getString();
227 if (status
.equals(ExecutionProcess
.NEW
))
228 processNode
.setProperty(SLC_STATUS
,
229 ExecutionProcess
.INITIALIZED
);
231 builderPage
.commit(true);
232 editorDirtyStateChanged();
233 } catch (RepositoryException e
) {
234 throw new SlcException("Cannot save " + processNode
, e
);
236 // JcrUtils.discardQuietly(session);
240 public void setEditorTitle(String title
) {
245 public void doSaveAs() {
249 public boolean isSaveAsAllowed() {
253 // public void updateStatus(ExecutionProcess process, String oldStatus,
254 // String newStatus) {
257 // public void addSteps(ExecutionProcess process, List<ExecutionStep> steps)
261 /** Expects one session per editor. */
263 public void setSession(Session session
) {
264 this.session
= session
;
267 public void setRepository(Repository repository
) {
269 session
= repository
.login();
270 } catch (RepositoryException re
) {
271 throw new SlcException("Unable to log in Repository " + repository
,
276 public void setProcessController(ProcessController processController
) {
277 this.processController
= processController
;
280 public void setModulesManager(ExecutionModulesManager modulesManager
) {
281 this.modulesManager
= modulesManager
;