1 package org
.argeo
.slc
.core
.execution
;
3 import java
.util
.ArrayList
;
4 import java
.util
.HashMap
;
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
;
20 public class DefaultExecutionFlow
implements ExecutionFlow
, InitializingBean
,
21 BeanNameAware
, StructureAware
<TreeSPath
> {
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
>();
29 private StructureRegistry
<TreeSPath
> registry
= new TreeSRegistry();
31 public DefaultExecutionFlow() {
32 this.executionSpec
= new DefaultExecutionSpec();
35 public DefaultExecutionFlow(ExecutionSpec executionSpec
) {
36 this.executionSpec
= executionSpec
;
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()
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");
54 this.parameters
.putAll(parameters
);
56 // check that all the required parameters are defined
57 MapBindingResult errors
= new MapBindingResult(parameters
, "execution#"
59 for (String key
: executionSpec
.getAttributes().keySet()) {
60 ExecutionSpecAttribute attr
= executionSpec
.getAttributes()
63 if (attr
.getIsParameter() && !isSetAsParameter(key
)) {
64 errors
.rejectValue(key
, "Parameter not set");
68 if (attr
.getIsFrozen() && !isSetAsParameter(key
)) {
69 errors
.rejectValue(key
, "Frozen but not set as parameter");
73 if (attr
.getIsHidden() && !isSetAsParameter(key
)) {
74 errors
.rejectValue(key
, "Hidden but not set as parameter");
79 if (errors
.hasErrors())
80 throw new SlcException("Could not prepare execution flow: "
86 for (Runnable executable
: executables
) {
91 @SuppressWarnings(value
= { "unchecked" })
92 public void afterPropertiesSet() throws Exception
{
94 for (Runnable executable
: executables
) {
95 if (executable
instanceof StructureAware
) {
96 ((StructureAware
<TreeSPath
>) executable
).notifyCurrentPath(
97 registry
, new TreeSPath(path
));
103 public void setBeanName(String name
) {
107 public void setExecutables(List
<Runnable
> executables
) {
108 this.executables
= executables
;
111 public void setParameters(Map
<String
, Object
> attributes
) {
112 this.parameters
= attributes
;
115 public String
getName() {
119 public ExecutionSpec
getExecutionSpec() {
120 return executionSpec
;
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
);
129 throw new SlcException("Key " + parameterName
130 + " is not defined in the specifications of " + toString());
133 if (parameters
.containsKey(parameterName
)) {
134 Object paramValue
= parameters
.get(parameterName
);
137 if (specAttr
.getValue() != null) {
138 return specAttr
.getValue();
141 throw new SlcException("Key " + parameterName
142 + " is not set as parameter in " + toString());
145 public Boolean
isSetAsParameter(String key
) {
146 return parameters
.containsKey(key
)
147 || (executionSpec
.getAttributes().containsKey(key
) && executionSpec
148 .getAttributes().get(key
).getValue() != null);
151 public String
toString() {
152 return new StringBuffer("Flow ").append(name
).toString();
155 public boolean equals(Object obj
) {
156 return ((ExecutionFlow
) obj
).getName().equals(name
);
159 public String
getPath() {
163 public void setPath(String path
) {
167 public void setRegistry(StructureRegistry
<TreeSPath
> registry
) {
168 this.registry
= registry
;
171 public void notifyCurrentPath(StructureRegistry
<TreeSPath
> registry
,
173 if (this.path
== null) {
174 this.path
= path
.toString();