2 * Copyright (C) 2007-2012 Mathieu Baudier
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
.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
.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
;
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";
53 private Session session
;
54 private Node processNode
;
55 private ProcessController processController
;
57 private ProcessBuilderPage builderPage
;
58 //private ProcessLogPage logPage;
60 private ExecutionModulesManager modulesManager
;
62 //private Boolean switchToLog = false;
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();
71 if (processPath
!= null) {
72 if (!session
.itemExists(processPath
))
73 throw new SlcException("Process " + processPath
75 processNode
= session
.getNode(processPath
);
77 processNode
= newProcessNode(pei
);
79 setPartName(processNode
.getName());
80 } catch (RepositoryException e
) {
81 throw new SlcException("Cannot initialize editor for " + pei
, e
);
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
);
99 public boolean isDirty() {
100 if (getProcessStatus().equals(ExecutionProcess
.NEW
))
102 return super.isDirty();
105 protected String
getProcessStatus() {
107 return processNode
.getProperty(SLC_STATUS
).getString();
108 } catch (RepositoryException e
) {
109 throw new SlcException("Cannot retrieve status for " + processNode
,
115 public void dispose() {
116 JcrUtils
.logoutQuietly(session
);
119 /** Actually runs the process. */
121 // the modifications have to be saved before execution
123 processNode
.setProperty(SLC_STATUS
, ExecutionProcess
.SCHEDULED
);
124 } catch (RepositoryException e
) {
125 throw new SlcException("Cannot update status of " + processNode
, e
);
131 // setActivePage(logPage.getId());
133 ExecutionProcess process
= processController
.process(processNode
);
134 Map
<String
, String
> properties
= new HashMap
<String
, String
>();
135 properties
.put(ExecutionModulesManager
.SLC_PROCESS_ID
,
137 modulesManager
.registerProcessNotifier(this, properties
);
138 } catch (Exception e
) {
139 ErrorFeedback
.show("Execution of " + processNode
+ " failed", e
);
144 processController
.kill(processNode
);
147 /** Opens a new editor with a copy of this process */
150 Node duplicatedNode
= duplicateProcess();
151 IWorkbenchPage activePage
= PlatformUI
.getWorkbench()
152 .getActiveWorkbenchWindow().getActivePage();
153 activePage
.openEditor(
154 new ProcessEditorInput(duplicatedNode
.getPath()),
157 } catch (Exception e1
) {
158 throw new SlcException("Cannot relaunch " + processNode
, e1
);
162 /** Duplicates the process */
163 protected Node
duplicateProcess() {
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
);
171 Node rootRealizedFlowNode
= newNode
.addNode(SLC_FLOW
);
173 JcrUtils
.copy(processNode
.getNode(SLC_FLOW
), rootRealizedFlowNode
);
175 newNode
.setProperty(SLC_UUID
, uuid
);
176 newNode
.setProperty(SLC_STATUS
, ExecutionProcess
.INITIALIZED
);
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
);
188 } catch (RepositoryException e
) {
189 throw new SlcException("Cannot duplicate process", e
);
194 protected void addPages() {
196 builderPage
= new ProcessBuilderPage(this, processNode
);
197 addPage(builderPage
);
198 firePropertyChange(PROP_DIRTY
);
199 // logPage = new ProcessLogPage(this, processNode);
201 } catch (PartInitException e
) {
202 throw new SlcException("Cannot add pages", e
);
208 public void doSave(IProgressMonitor monitor
) {
210 String status
= processNode
.getProperty(SLC_STATUS
).getString();
211 if (status
.equals(ExecutionProcess
.NEW
))
212 processNode
.setProperty(SLC_STATUS
,
213 ExecutionProcess
.INITIALIZED
);
215 builderPage
.commit(true);
216 editorDirtyStateChanged();
217 } catch (RepositoryException e
) {
218 throw new SlcException("Cannot save " + processNode
, e
);
220 JcrUtils
.discardQuietly(session
);
225 public void doSaveAs() {
229 public boolean isSaveAsAllowed() {
233 public void updateStatus(ExecutionProcess process
, String oldStatus
,
237 public void addSteps(ExecutionProcess process
, List
<ExecutionStep
> steps
) {
238 // logPage.addSteps(steps);
241 /** Expects one session per editor. */
242 public void setSession(Session session
) {
243 this.session
= session
;
246 public void setProcessController(ProcessController processController
) {
247 this.processController
= processController
;
250 public void setModulesManager(ExecutionModulesManager modulesManager
) {
251 this.modulesManager
= modulesManager
;