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