]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/DefaultExecutionFlow.java
Move to execution package
[gpl/argeo-slc.git] / runtime / org.argeo.slc.core / src / main / java / org / argeo / slc / core / execution / DefaultExecutionFlow.java
1 package org.argeo.slc.core.execution;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7
8 import org.argeo.slc.SlcException;
9 import org.argeo.slc.core.structure.tree.TreeSPath;
10 import org.argeo.slc.core.structure.tree.TreeSRegistry;
11 import org.argeo.slc.execution.ExecutionFlow;
12 import org.argeo.slc.execution.ExecutionSpec;
13 import org.argeo.slc.execution.ExecutionSpecAttribute;
14 import org.argeo.slc.structure.StructureAware;
15 import org.argeo.slc.structure.StructureRegistry;
16 import org.springframework.beans.factory.BeanNameAware;
17 import org.springframework.beans.factory.InitializingBean;
18 import org.springframework.validation.MapBindingResult;
19
20 public class DefaultExecutionFlow implements ExecutionFlow, InitializingBean,
21 BeanNameAware, StructureAware<TreeSPath> {
22
23 private final ExecutionSpec executionSpec;
24 private String name = null;
25 private Map<String, Object> parameters = new HashMap<String, Object>();
26 private List<Runnable> executables = new ArrayList<Runnable>();
27
28 private String path;
29 private StructureRegistry<TreeSPath> registry = new TreeSRegistry();
30
31 public DefaultExecutionFlow() {
32 this.executionSpec = new DefaultExecutionSpec();
33 }
34
35 public DefaultExecutionFlow(ExecutionSpec executionSpec) {
36 this.executionSpec = executionSpec;
37 }
38
39 public DefaultExecutionFlow(ExecutionSpec executionSpec,
40 Map<String, Object> parameters) {
41 // be sure to have an execution spec
42 this.executionSpec = (executionSpec == null) ? new DefaultExecutionSpec()
43 : executionSpec;
44
45 // only parameters contained in the executionSpec can be set
46 for (String parameter : parameters.keySet()) {
47 if (!executionSpec.getAttributes().containsKey(parameter)) {
48 throw new SlcException("Parameter " + parameter
49 + " is not defined in the ExecutionSpec");
50 }
51 }
52
53 // set the parameters
54 this.parameters.putAll(parameters);
55
56 // check that all the required parameters are defined
57 MapBindingResult errors = new MapBindingResult(parameters, "execution#"
58 + getName());
59 for (String key : executionSpec.getAttributes().keySet()) {
60 ExecutionSpecAttribute attr = executionSpec.getAttributes()
61 .get(key);
62
63 if (attr.getIsParameter() && !isSetAsParameter(key)) {
64 errors.rejectValue(key, "Parameter not set");
65 break;
66 }
67
68 if (attr.getIsFrozen() && !isSetAsParameter(key)) {
69 errors.rejectValue(key, "Frozen but not set as parameter");
70 break;
71 }
72
73 if (attr.getIsHidden() && !isSetAsParameter(key)) {
74 errors.rejectValue(key, "Hidden but not set as parameter");
75 break;
76 }
77 }
78
79 if (errors.hasErrors())
80 throw new SlcException("Could not prepare execution flow: "
81 + errors.toString());
82
83 }
84
85 public void run() {
86 for (Runnable executable : executables) {
87 executable.run();
88 }
89 }
90
91 @SuppressWarnings(value = { "unchecked" })
92 public void afterPropertiesSet() throws Exception {
93 if (path != null) {
94 for (Runnable executable : executables) {
95 if (executable instanceof StructureAware) {
96 ((StructureAware<TreeSPath>) executable).notifyCurrentPath(
97 registry, new TreeSPath(path));
98 }
99 }
100 }
101 }
102
103 public void setBeanName(String name) {
104 this.name = name;
105 }
106
107 public void setExecutables(List<Runnable> executables) {
108 this.executables = executables;
109 }
110
111 public void setParameters(Map<String, Object> attributes) {
112 this.parameters = attributes;
113 }
114
115 public String getName() {
116 return name;
117 }
118
119 public ExecutionSpec getExecutionSpec() {
120 return executionSpec;
121 }
122
123 public Object getParameter(String parameterName) {
124 // Verify that there is a spec attribute
125 ExecutionSpecAttribute specAttr = null;
126 if (executionSpec.getAttributes().containsKey(parameterName)) {
127 specAttr = executionSpec.getAttributes().get(parameterName);
128 } else {
129 throw new SlcException("Key " + parameterName
130 + " is not defined in the specifications of " + toString());
131 }
132
133 if (parameters.containsKey(parameterName)) {
134 Object paramValue = parameters.get(parameterName);
135 return paramValue;
136 } else {
137 if (specAttr.getValue() != null) {
138 return specAttr.getValue();
139 }
140 }
141 throw new SlcException("Key " + parameterName
142 + " is not set as parameter in " + toString());
143 }
144
145 public Boolean isSetAsParameter(String key) {
146 return parameters.containsKey(key)
147 || (executionSpec.getAttributes().containsKey(key) && executionSpec
148 .getAttributes().get(key).getValue() != null);
149 }
150
151 public String toString() {
152 return new StringBuffer("Flow ").append(name).toString();
153 }
154
155 public boolean equals(Object obj) {
156 return ((ExecutionFlow) obj).getName().equals(name);
157 }
158
159 public String getPath() {
160 return path;
161 }
162
163 public void setPath(String path) {
164 this.path = path;
165 }
166
167 public void setRegistry(StructureRegistry<TreeSPath> registry) {
168 this.registry = registry;
169 }
170
171 public void notifyCurrentPath(StructureRegistry<TreeSPath> registry,
172 TreeSPath path) {
173 if (this.path == null) {
174 this.path = path.toString();
175 }
176 }
177
178 }