]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.core/src/main/java/org/argeo/slc/core/execution/DefaultExecutionFlow.java
Modular distributions
[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 {
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 } else if (executable instanceof DefaultExecutionFlow) {
99 // so we don't need to have DefaultExecutionFlow
100 // implementing StructureAware
101 DefaultExecutionFlow flow = (DefaultExecutionFlow) executable;
102 flow.setPath(path + '/' + flow.getName());
103 }
104 }
105 }
106 }
107
108 public void setBeanName(String name) {
109 this.name = name;
110 }
111
112 public void setExecutables(List<Runnable> executables) {
113 this.executables = executables;
114 }
115
116 public void setParameters(Map<String, Object> attributes) {
117 this.parameters = attributes;
118 }
119
120 public String getName() {
121 return name;
122 }
123
124 public ExecutionSpec getExecutionSpec() {
125 return executionSpec;
126 }
127
128 public Object getParameter(String parameterName) {
129 // Verify that there is a spec attribute
130 ExecutionSpecAttribute specAttr = null;
131 if (executionSpec.getAttributes().containsKey(parameterName)) {
132 specAttr = executionSpec.getAttributes().get(parameterName);
133 } else {
134 throw new SlcException("Key " + parameterName
135 + " is not defined in the specifications of " + toString());
136 }
137
138 if (parameters.containsKey(parameterName)) {
139 Object paramValue = parameters.get(parameterName);
140 return paramValue;
141 } else {
142 if (specAttr.getValue() != null) {
143 return specAttr.getValue();
144 }
145 }
146 throw new SlcException("Key " + parameterName
147 + " is not set as parameter in " + toString());
148 }
149
150 public Boolean isSetAsParameter(String key) {
151 return parameters.containsKey(key)
152 || (executionSpec.getAttributes().containsKey(key) && executionSpec
153 .getAttributes().get(key).getValue() != null);
154 }
155
156 public String toString() {
157 return new StringBuffer("Flow ").append(name).toString();
158 }
159
160 public boolean equals(Object obj) {
161 return ((ExecutionFlow) obj).getName().equals(name);
162 }
163
164 public String getPath() {
165 return path;
166 }
167
168 public void setPath(String path) {
169 this.path = path;
170 }
171
172 public void setRegistry(StructureRegistry<TreeSPath> registry) {
173 this.registry = registry;
174 }
175
176 }