1 package org
.argeo
.slc
.client
.ui
.editors
;
3 import java
.util
.HashMap
;
8 import javax
.jcr
.NodeIterator
;
9 import javax
.jcr
.Property
;
10 import javax
.jcr
.Repository
;
11 import javax
.jcr
.RepositoryException
;
12 import javax
.jcr
.Session
;
14 import org
.argeo
.api
.cms
.CmsConstants
;
15 import org
.argeo
.eclipse
.ui
.dialogs
.ErrorFeedback
;
16 import org
.argeo
.jcr
.JcrUtils
;
17 import org
.argeo
.slc
.SlcException
;
18 import org
.argeo
.slc
.SlcNames
;
19 import org
.argeo
.slc
.SlcTypes
;
20 import org
.argeo
.slc
.client
.ui
.ClientUiPlugin
;
21 import org
.argeo
.slc
.client
.ui
.controllers
.ProcessController
;
22 import org
.argeo
.slc
.execution
.ExecutionModulesManager
;
23 import org
.argeo
.slc
.execution
.ExecutionProcess
;
24 import org
.argeo
.slc
.jcr
.SlcJcrUtils
;
25 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
26 import org
.eclipse
.rap
.rwt
.service
.ServerPushSession
;
27 import org
.eclipse
.ui
.IEditorInput
;
28 import org
.eclipse
.ui
.IEditorSite
;
29 import org
.eclipse
.ui
.IWorkbenchPage
;
30 import org
.eclipse
.ui
.PartInitException
;
31 import org
.eclipse
.ui
.PlatformUI
;
32 import org
.eclipse
.ui
.forms
.editor
.FormEditor
;
34 /** Editor for an execution process. */
35 public class ProcessEditor
extends FormEditor
implements SlcTypes
, SlcNames
{
36 private static final long serialVersionUID
= 509589737739132467L;
38 public final static String ID
= ClientUiPlugin
.ID
+ ".processEditor";
40 private Repository repository
;
41 private Session homeSession
;
42 private Session agentSession
;
43 private Node processNode
;
44 private ProcessController processController
;
45 private ServerPushSession pushSession
;
47 private ProcessBuilderPage builderPage
;
49 private ExecutionModulesManager modulesManager
;
52 public void init(IEditorSite site
, IEditorInput input
) throws PartInitException
{
53 super.init(site
, input
);
54 pushSession
= new ServerPushSession();
57 homeSession
= repository
.login(CmsConstants
.HOME_WORKSPACE
);
58 agentSession
= repository
.login();
59 } catch (RepositoryException e1
) {
60 throw new SlcException("Cannot log in to repository");
63 ProcessEditorInput pei
= (ProcessEditorInput
) input
;
64 String processPath
= pei
.getProcessPath();
66 if (processPath
!= null) {
67 if (!homeSession
.itemExists(processPath
))
68 throw new SlcException("Process " + processPath
+ " does not exist");
69 processNode
= homeSession
.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
) throws RepositoryException
{
81 String uuid
= UUID
.randomUUID().toString();
82 String processPath
= SlcJcrUtils
.createExecutionProcessPath(homeSession
, uuid
);
83 Node processNode
= JcrUtils
.mkdirs(homeSession
, processPath
, SLC_PROCESS
);
84 processNode
.setProperty(SLC_UUID
, uuid
);
85 processNode
.setProperty(SLC_STATUS
, ExecutionProcess
.NEW
);
86 Node processFlow
= processNode
.addNode(SLC_FLOW
);
87 processFlow
.addMixin(SLC_REALIZED_FLOW
);
92 public boolean isDirty() {
93 if (getProcessStatus().equals(ExecutionProcess
.NEW
))
95 return super.isDirty();
98 protected String
getProcessStatus() {
100 return processNode
.getProperty(SLC_STATUS
).getString();
101 } catch (RepositoryException e
) {
102 throw new SlcException("Cannot retrieve status for " + processNode
, e
);
107 public void dispose() {
108 JcrUtils
.logoutQuietly(homeSession
);
109 JcrUtils
.logoutQuietly(agentSession
);
110 if (pushSession
!= null)
115 /** Actually runs the process. */
117 // the modifications have to be saved before execution
119 processNode
.setProperty(SLC_STATUS
, ExecutionProcess
.SCHEDULED
);
120 } catch (RepositoryException e
) {
121 throw new SlcException("Cannot update status of " + processNode
, e
);
128 // make sure modules are started for all nodes
129 for (NodeIterator nit
= processNode
.getNode(SLC_FLOW
).getNodes(); nit
.hasNext();) {
130 Node flowNode
= nit
.nextNode();
132 String flowDefPath
= flowNode
.getNode(SLC_ADDRESS
).getProperty(Property
.JCR_PATH
).getString();
133 Node executionModuleNode
= agentSession
.getNode(SlcJcrUtils
.modulePath(flowDefPath
));
134 if (!executionModuleNode
.getProperty(SLC_STARTED
).getBoolean())
135 ClientUiPlugin
.startStopExecutionModule(modulesManager
, executionModuleNode
);
136 } catch (Exception e
) {
137 ErrorFeedback
.show("Cannot start execution module related to " + flowNode
, e
);
142 ExecutionProcess process
= processController
.process(processNode
);
143 Map
<String
, String
> properties
= new HashMap
<String
, String
>();
144 properties
.put(ExecutionModulesManager
.SLC_PROCESS_ID
, process
.getUuid());
145 // modulesManager.registerProcessNotifier(this, properties);
146 } catch (Exception e
) {
147 ErrorFeedback
.show("Execution of " + processNode
+ " failed", e
);
152 processController
.kill(processNode
);
155 /** Opens a new editor with a copy of this process */
158 Node duplicatedNode
= duplicateProcess();
159 IWorkbenchPage activePage
= PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getActivePage();
160 activePage
.openEditor(new ProcessEditorInput(duplicatedNode
.getPath()), ProcessEditor
.ID
);
162 } catch (Exception e1
) {
163 throw new SlcException("Cannot relaunch " + processNode
, e1
);
167 /** Duplicates the process */
168 protected Node
duplicateProcess() {
170 Session session
= processNode
.getSession();
171 String uuid
= UUID
.randomUUID().toString();
172 String destPath
= SlcJcrUtils
.createExecutionProcessPath(session
, uuid
);
173 Node newNode
= JcrUtils
.mkdirs(session
, destPath
, SlcTypes
.SLC_PROCESS
);
175 Node rootRealizedFlowNode
= newNode
.addNode(SLC_FLOW
);
177 JcrUtils
.copy(processNode
.getNode(SLC_FLOW
), rootRealizedFlowNode
);
179 newNode
.setProperty(SLC_UUID
, uuid
);
180 newNode
.setProperty(SLC_STATUS
, ExecutionProcess
.INITIALIZED
);
182 // reset realized flow status
183 // we just manage one level for the time being
184 NodeIterator nit
= rootRealizedFlowNode
.getNodes(SLC_FLOW
);
185 while (nit
.hasNext()) {
186 nit
.nextNode().setProperty(SLC_STATUS
, ExecutionProcess
.INITIALIZED
);
191 } catch (RepositoryException e
) {
192 throw new SlcException("Cannot duplicate process", e
);
197 protected void addPages() {
199 builderPage
= new ProcessBuilderPage(this, processNode
);
200 addPage(builderPage
);
201 firePropertyChange(PROP_DIRTY
);
202 } catch (PartInitException e
) {
203 throw new SlcException("Cannot add pages", e
);
209 public void doSave(IProgressMonitor monitor
) {
211 String status
= processNode
.getProperty(SLC_STATUS
).getString();
212 if (status
.equals(ExecutionProcess
.NEW
))
213 processNode
.setProperty(SLC_STATUS
, ExecutionProcess
.INITIALIZED
);
215 builderPage
.commit(true);
216 editorDirtyStateChanged();
217 } catch (RepositoryException e
) {
218 throw new SlcException("Cannot save " + processNode
, e
);
220 // JcrUtils.discardQuietly(session);
224 public void setEditorTitle(String title
) {
229 public void doSaveAs() {
233 public boolean isSaveAsAllowed() {
237 public void setRepository(Repository repository
) {
238 this.repository
= repository
;
241 public void setProcessController(ProcessController processController
) {
242 this.processController
= processController
;
245 public void setModulesManager(ExecutionModulesManager modulesManager
) {
246 this.modulesManager
= modulesManager
;