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
,
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
));
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());
108 public void setBeanName(String name
) {
112 public void setExecutables(List
<Runnable
> executables
) {
113 this.executables
= executables
;
116 public void setParameters(Map
<String
, Object
> attributes
) {
117 this.parameters
= attributes
;
120 public String
getName() {
124 public ExecutionSpec
getExecutionSpec() {
125 return executionSpec
;
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
);
134 throw new SlcException("Key " + parameterName
135 + " is not defined in the specifications of " + toString());
138 if (parameters
.containsKey(parameterName
)) {
139 Object paramValue
= parameters
.get(parameterName
);
142 if (specAttr
.getValue() != null) {
143 return specAttr
.getValue();
146 throw new SlcException("Key " + parameterName
147 + " is not set as parameter in " + toString());
150 public Boolean
isSetAsParameter(String key
) {
151 return parameters
.containsKey(key
)
152 || (executionSpec
.getAttributes().containsKey(key
) && executionSpec
153 .getAttributes().get(key
).getValue() != null);
156 public String
toString() {
157 return new StringBuffer("Flow ").append(name
).toString();
160 public boolean equals(Object obj
) {
161 return ((ExecutionFlow
) obj
).getName().equals(name
);
164 public String
getPath() {
168 public void setPath(String path
) {
172 public void setRegistry(StructureRegistry
<TreeSPath
> registry
) {
173 this.registry
= registry
;