]> git.argeo.org Git - gpl/argeo-slc.git/blob - eclipse/plugins/org.argeo.slc.client.ui/src/main/java/org/argeo/slc/client/ui/editors/ProcessEditor.java
Improve imports
[gpl/argeo-slc.git] / eclipse / plugins / org.argeo.slc.client.ui / src / main / java / org / argeo / slc / client / ui / editors / ProcessEditor.java
1 package org.argeo.slc.client.ui.editors;
2
3 import java.util.UUID;
4
5 import javax.jcr.Node;
6 import javax.jcr.RepositoryException;
7 import javax.jcr.Session;
8
9 import org.argeo.eclipse.ui.Error;
10 import org.argeo.jcr.JcrUtils;
11 import org.argeo.slc.SlcException;
12 import org.argeo.slc.client.ui.ClientUiPlugin;
13 import org.argeo.slc.client.ui.controllers.ProcessController;
14 import org.argeo.slc.execution.ExecutionProcess;
15 import org.argeo.slc.jcr.SlcJcrUtils;
16 import org.argeo.slc.jcr.SlcNames;
17 import org.argeo.slc.jcr.SlcTypes;
18 import org.eclipse.core.runtime.IProgressMonitor;
19 import org.eclipse.ui.IEditorInput;
20 import org.eclipse.ui.IEditorSite;
21 import org.eclipse.ui.PartInitException;
22 import org.eclipse.ui.forms.editor.FormEditor;
23
24 public class ProcessEditor extends FormEditor implements SlcTypes, SlcNames {
25 public final static String ID = ClientUiPlugin.ID + ".processEditor";
26
27 private Session session;
28 private Node processNode;
29 private ProcessController processController;
30
31 private ProcessBuilderPage builderPage;
32 private ProcessLogPage logPage;
33
34 @Override
35 public void init(IEditorSite site, IEditorInput input)
36 throws PartInitException {
37 super.init(site, input);
38 ProcessEditorInput pei = (ProcessEditorInput) input;
39 String processPath = pei.getProcessPath();
40 try {
41 if (processPath != null) {
42 if (!session.itemExists(processPath))
43 throw new SlcException("Process " + processPath
44 + " does not exist");
45 processNode = session.getNode(processPath);
46 } else {// new
47 processNode = newProcessNode(pei);
48 }
49 setPartName(processNode.getName());
50 } catch (RepositoryException e) {
51 throw new SlcException("Cannot initialize editor for " + pei, e);
52 }
53
54 }
55
56 protected Node newProcessNode(ProcessEditorInput pei)
57 throws RepositoryException {
58 String uuid = UUID.randomUUID().toString();
59 String processPath = SlcJcrUtils.createExecutionProcessPath(uuid);
60 Node processNode = JcrUtils.mkdirs(session, processPath, SLC_PROCESS);
61 processNode.setProperty(SLC_UUID, uuid);
62 processNode.setProperty(SLC_STATUS, ExecutionProcess.NEW);
63 Node processFlow = processNode.addNode(SLC_FLOW);
64 processFlow.addMixin(SLC_REALIZED_FLOW);
65 return processNode;
66 }
67
68 @Override
69 public boolean isDirty() {
70 if (getProcessStatus().equals(ExecutionProcess.NEW))
71 return true;
72 return super.isDirty();
73 }
74
75 protected String getProcessStatus() {
76 try {
77 return processNode.getProperty(SLC_STATUS).getString();
78 } catch (RepositoryException e) {
79 throw new SlcException("Cannot retrieve status for " + processNode,
80 e);
81 }
82 }
83
84 @Override
85 public void dispose() {
86 JcrUtils.logoutQuietly(session);
87 }
88
89 /** Actually runs the process. */
90 public void process() {
91 // the modifications have to be saved before execution
92 try {
93 processNode.setProperty(SLC_STATUS, ExecutionProcess.SCHEDULED);
94 } catch (RepositoryException e) {
95 throw new SlcException("Cannot update status of " + processNode, e);
96 }
97 doSave(null);
98 try {
99 processController.process(processNode);
100 } catch (Exception e) {
101 Error.show("Execution of " + processNode + " failed", e);
102 }
103 }
104
105 @Override
106 protected void addPages() {
107 try {
108 builderPage = new ProcessBuilderPage(this, processNode);
109 addPage(builderPage);
110 firePropertyChange(PROP_DIRTY);
111 logPage = new ProcessLogPage(this);
112 addPage(logPage);
113 } catch (PartInitException e) {
114 throw new SlcException("Cannot add pages", e);
115 }
116
117 }
118
119 @Override
120 public void doSave(IProgressMonitor monitor) {
121 try {
122 String status = processNode.getProperty(SLC_STATUS).getString();
123 if (status.equals(ExecutionProcess.NEW))
124 processNode.setProperty(SLC_STATUS,
125 ExecutionProcess.INITIALIZED);
126 session.save();
127 builderPage.commit(true);
128 editorDirtyStateChanged();
129 } catch (RepositoryException e) {
130 throw new SlcException("Cannot save " + processNode, e);
131 } finally {
132 JcrUtils.discardQuietly(session);
133 }
134 }
135
136 @Override
137 public void doSaveAs() {
138 }
139
140 @Override
141 public boolean isSaveAsAllowed() {
142 return false;
143 }
144
145 /** Expects one session per editor. */
146 public void setSession(Session session) {
147 this.session = session;
148 }
149
150 public void setProcessController(ProcessController processController) {
151 this.processController = processController;
152 }
153
154 }