]> git.argeo.org Git - gpl/argeo-slc.git/blob - legacy/org.argeo.slc.client.ui/src/org/argeo/slc/client/ui/editors/ProcessEditor.java
Clarify overall project structure.
[gpl/argeo-slc.git] / legacy / org.argeo.slc.client.ui / src / org / argeo / slc / client / ui / editors / ProcessEditor.java
1 package org.argeo.slc.client.ui.editors;
2
3 import java.util.HashMap;
4 import java.util.Map;
5 import java.util.UUID;
6
7 import javax.jcr.Node;
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;
13
14 import org.argeo.api.NodeConstants;
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;
33
34 /** Editor for an execution process. */
35 public class ProcessEditor extends FormEditor implements SlcTypes, SlcNames {
36 private static final long serialVersionUID = 509589737739132467L;
37
38 public final static String ID = ClientUiPlugin.ID + ".processEditor";
39
40 private Repository repository;
41 private Session homeSession;
42 private Session agentSession;
43 private Node processNode;
44 private ProcessController processController;
45 private ServerPushSession pushSession;
46
47 private ProcessBuilderPage builderPage;
48
49 private ExecutionModulesManager modulesManager;
50
51 @Override
52 public void init(IEditorSite site, IEditorInput input) throws PartInitException {
53 super.init(site, input);
54 pushSession = new ServerPushSession();
55 pushSession.start();
56 try {
57 homeSession = repository.login(NodeConstants.HOME_WORKSPACE);
58 agentSession = repository.login();
59 } catch (RepositoryException e1) {
60 throw new SlcException("Cannot log in to repository");
61 }
62
63 ProcessEditorInput pei = (ProcessEditorInput) input;
64 String processPath = pei.getProcessPath();
65 try {
66 if (processPath != null) {
67 if (!homeSession.itemExists(processPath))
68 throw new SlcException("Process " + processPath + " does not exist");
69 processNode = homeSession.getNode(processPath);
70 } else {// new
71 processNode = newProcessNode(pei);
72 }
73 setPartName(processNode.getName());
74 } catch (RepositoryException e) {
75 throw new SlcException("Cannot initialize editor for " + pei, e);
76 }
77
78 }
79
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);
88 return processNode;
89 }
90
91 @Override
92 public boolean isDirty() {
93 if (getProcessStatus().equals(ExecutionProcess.NEW))
94 return true;
95 return super.isDirty();
96 }
97
98 protected String getProcessStatus() {
99 try {
100 return processNode.getProperty(SLC_STATUS).getString();
101 } catch (RepositoryException e) {
102 throw new SlcException("Cannot retrieve status for " + processNode, e);
103 }
104 }
105
106 @Override
107 public void dispose() {
108 JcrUtils.logoutQuietly(homeSession);
109 JcrUtils.logoutQuietly(agentSession);
110 if (pushSession != null)
111 pushSession.stop();
112 super.dispose();
113 }
114
115 /** Actually runs the process. */
116 void process() {
117 // the modifications have to be saved before execution
118 try {
119 processNode.setProperty(SLC_STATUS, ExecutionProcess.SCHEDULED);
120 } catch (RepositoryException e) {
121 throw new SlcException("Cannot update status of " + processNode, e);
122 }
123
124 // save
125 doSave(null);
126
127 try {
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();
131 try {
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);
138 }
139 }
140
141 // Actually process
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);
148 }
149 }
150
151 void kill() {
152 processController.kill(processNode);
153 }
154
155 /** Opens a new editor with a copy of this process */
156 void relaunch() {
157 try {
158 Node duplicatedNode = duplicateProcess();
159 IWorkbenchPage activePage = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
160 activePage.openEditor(new ProcessEditorInput(duplicatedNode.getPath()), ProcessEditor.ID);
161 close(false);
162 } catch (Exception e1) {
163 throw new SlcException("Cannot relaunch " + processNode, e1);
164 }
165 }
166
167 /** Duplicates the process */
168 protected Node duplicateProcess() {
169 try {
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);
174
175 Node rootRealizedFlowNode = newNode.addNode(SLC_FLOW);
176 // copy node
177 JcrUtils.copy(processNode.getNode(SLC_FLOW), rootRealizedFlowNode);
178
179 newNode.setProperty(SLC_UUID, uuid);
180 newNode.setProperty(SLC_STATUS, ExecutionProcess.INITIALIZED);
181
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);
187 }
188
189 session.save();
190 return newNode;
191 } catch (RepositoryException e) {
192 throw new SlcException("Cannot duplicate process", e);
193 }
194 }
195
196 @Override
197 protected void addPages() {
198 try {
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);
204 }
205
206 }
207
208 @Override
209 public void doSave(IProgressMonitor monitor) {
210 try {
211 String status = processNode.getProperty(SLC_STATUS).getString();
212 if (status.equals(ExecutionProcess.NEW))
213 processNode.setProperty(SLC_STATUS, ExecutionProcess.INITIALIZED);
214 homeSession.save();
215 builderPage.commit(true);
216 editorDirtyStateChanged();
217 } catch (RepositoryException e) {
218 throw new SlcException("Cannot save " + processNode, e);
219 // } finally {
220 // JcrUtils.discardQuietly(session);
221 }
222 }
223
224 public void setEditorTitle(String title) {
225 setPartName(title);
226 }
227
228 @Override
229 public void doSaveAs() {
230 }
231
232 @Override
233 public boolean isSaveAsAllowed() {
234 return false;
235 }
236
237 public void setRepository(Repository repository) {
238 this.repository = repository;
239 }
240
241 public void setProcessController(ProcessController processController) {
242 this.processController = processController;
243 }
244
245 public void setModulesManager(ExecutionModulesManager modulesManager) {
246 this.modulesManager = modulesManager;
247 }
248 }