1 package org
.argeo
.slc
.core
.execution
;
3 import java
.util
.ArrayList
;
4 import java
.util
.HashMap
;
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
;
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
>();
32 private StructureRegistry
<TreeSPath
> registry
= new TreeSRegistry();
34 public DefaultExecutionFlow() {
37 public DefaultExecutionFlow(ExecutionSpec executionSpec
) {
38 this.executionSpec
= executionSpec
;
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
;
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");
53 this.parameters
.putAll(parameters
);
55 //check that all the required parameters are defined
56 MapBindingResult errors
= new MapBindingResult(parameters
, "execution#"
58 for (String key
: executionSpec
.getAttributes().keySet()) {
59 ExecutionSpecAttribute attr
= executionSpec
.getAttributes()
62 if (attr
.getIsParameter() && !isSetAsParameter(key
)) {
63 errors
.rejectValue(key
, "Parameter not set");
67 if (attr
.getIsFrozen() && !isSetAsParameter(key
)) {
68 errors
.rejectValue(key
, "Frozen but not set as parameter");
72 if (attr
.getIsHidden() && !isSetAsParameter(key
)) {
73 errors
.rejectValue(key
, "Hidden but not set as parameter");
78 if (errors
.hasErrors())
79 throw new SlcException("Could not prepare execution flow: "
85 public void execute() {
86 for (Executable executable
: executables
) {
91 public void afterPropertiesSet() throws Exception
{
93 for (Executable executable
: executables
) {
94 if (executable
instanceof StructureAware
) {
95 ((StructureAware
<TreeSPath
>) executable
).notifyCurrentPath(
96 registry
, new TreeSPath(path
));
102 public void setBeanName(String name
) {
106 public void setExecutables(List
<Executable
> executables
) {
107 this.executables
= executables
;
110 public void setParameters(Map
<String
, Object
> attributes
) {
111 this.parameters
= attributes
;
114 public String
getName() {
118 public ExecutionSpec
getExecutionSpec() {
119 return executionSpec
;
122 public Object
getParameter(String name
) {
123 if (parameters
.containsKey(name
)) {
124 return parameters
.get(name
);
126 if (executionSpec
.getAttributes().containsKey(name
)) {
127 ExecutionSpecAttribute esa
= executionSpec
.getAttributes().get(
129 if (esa
.getValue() != null)
130 return esa
.getValue();
132 throw new SlcException("Key " + name
133 + " is not defined in the specifications of "
137 throw new SlcException("Key " + name
+ " is not set as parameter in "
141 public Boolean
isSetAsParameter(String key
) {
142 return parameters
.containsKey(key
)
143 || (executionSpec
.getAttributes().containsKey(key
) && executionSpec
144 .getAttributes().get(key
).getValue() != null);
147 public String
toString() {
148 return new StringBuffer("Flow ").append(name
).toString();
151 public boolean equals(Object obj
) {
152 return ((ExecutionFlow
) obj
).getName().equals(name
);
155 public String
getPath() {
159 public void setPath(String path
) {
163 public void setRegistry(StructureRegistry
<TreeSPath
> registry
) {
164 this.registry
= registry
;
167 public void notifyCurrentPath(StructureRegistry
<TreeSPath
> registry
,
169 if (this.path
== null) {
170 this.path
= path
.toString();