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
.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
;
47 /** Editor for an execution process. */
48 public class ProcessEditor
extends FormEditor
implements SlcTypes
, SlcNames
{
49 private static final long serialVersionUID
= 509589737739132467L;
51 public final static String ID
= ClientUiPlugin
.ID
+ ".processEditor";
53 private Repository repository
;
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
);
67 session
= repository
.login();
68 } catch (RepositoryException e1
) {
69 throw new SlcException("Cannot log in to repository");
72 ProcessEditorInput pei
= (ProcessEditorInput
) input
;
73 String processPath
= pei
.getProcessPath();
75 if (processPath
!= null) {
76 if (!session
.itemExists(processPath
))
77 throw new SlcException("Process " + processPath
79 processNode
= session
.getNode(processPath
);
81 processNode
= newProcessNode(pei
);
83 setPartName(processNode
.getName());
84 } catch (RepositoryException e
) {
85 throw new SlcException("Cannot initialize editor for " + pei
, e
);
90 protected Node
newProcessNode(ProcessEditorInput pei
)
91 throws RepositoryException
{
92 String uuid
= UUID
.randomUUID().toString();
93 String processPath
= SlcJcrUtils
.createExecutionProcessPath(session
,
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
);
104 public boolean isDirty() {
105 if (getProcessStatus().equals(ExecutionProcess
.NEW
))
107 return super.isDirty();
110 protected String
getProcessStatus() {
112 return processNode
.getProperty(SLC_STATUS
).getString();
113 } catch (RepositoryException e
) {
114 throw new SlcException("Cannot retrieve status for " + processNode
,
120 public void dispose() {
121 JcrUtils
.logoutQuietly(session
);
125 /** Actually runs the process. */
127 // the modifications have to be saved before execution
129 processNode
.setProperty(SLC_STATUS
, ExecutionProcess
.SCHEDULED
);
130 } catch (RepositoryException e
) {
131 throw new SlcException("Cannot update status of " + processNode
, e
);
138 // make sure modules are started for all nodes
139 for (NodeIterator nit
= processNode
.getNode(SLC_FLOW
).getNodes(); nit
141 Node flowNode
= nit
.nextNode();
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
)
149 ClientUiPlugin
.startStopExecutionModule(modulesManager
,
150 executionModuleNode
);
151 } catch (Exception e
) {
153 "Cannot start execution module related to "
159 ExecutionProcess process
= processController
.process(processNode
);
160 Map
<String
, String
> properties
= new HashMap
<String
, String
>();
161 properties
.put(ExecutionModulesManager
.SLC_PROCESS_ID
,
163 // modulesManager.registerProcessNotifier(this, properties);
164 } catch (Exception e
) {
165 ErrorFeedback
.show("Execution of " + processNode
+ " failed", e
);
170 processController
.kill(processNode
);
173 /** Opens a new editor with a copy of this process */
176 Node duplicatedNode
= duplicateProcess();
177 IWorkbenchPage activePage
= PlatformUI
.getWorkbench()
178 .getActiveWorkbenchWindow().getActivePage();
179 activePage
.openEditor(
180 new ProcessEditorInput(duplicatedNode
.getPath()),
183 } catch (Exception e1
) {
184 throw new SlcException("Cannot relaunch " + processNode
, e1
);
188 /** Duplicates the process */
189 protected Node
duplicateProcess() {
191 Session session
= processNode
.getSession();
192 String uuid
= UUID
.randomUUID().toString();
193 String destPath
= SlcJcrUtils
.createExecutionProcessPath(session
,
195 Node newNode
= JcrUtils
.mkdirs(session
, destPath
,
196 SlcTypes
.SLC_PROCESS
);
198 Node rootRealizedFlowNode
= newNode
.addNode(SLC_FLOW
);
200 JcrUtils
.copy(processNode
.getNode(SLC_FLOW
), rootRealizedFlowNode
);
202 newNode
.setProperty(SLC_UUID
, uuid
);
203 newNode
.setProperty(SLC_STATUS
, ExecutionProcess
.INITIALIZED
);
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
);
215 } catch (RepositoryException e
) {
216 throw new SlcException("Cannot duplicate process", e
);
221 protected void addPages() {
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
);
233 public void doSave(IProgressMonitor monitor
) {
235 String status
= processNode
.getProperty(SLC_STATUS
).getString();
236 if (status
.equals(ExecutionProcess
.NEW
))
237 processNode
.setProperty(SLC_STATUS
,
238 ExecutionProcess
.INITIALIZED
);
240 builderPage
.commit(true);
241 editorDirtyStateChanged();
242 } catch (RepositoryException e
) {
243 throw new SlcException("Cannot save " + processNode
, e
);
245 // JcrUtils.discardQuietly(session);
249 public void setEditorTitle(String title
) {
254 public void doSaveAs() {
258 public boolean isSaveAsAllowed() {
262 public void setRepository(Repository repository
) {
263 this.repository
= repository
;
266 public void setProcessController(ProcessController processController
) {
267 this.processController
= processController
;
270 public void setModulesManager(ExecutionModulesManager modulesManager
) {
271 this.modulesManager
= modulesManager
;