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