1 package org
.argeo
.slc
.core
.execution
;
3 import java
.util
.ArrayList
;
4 import java
.util
.HashMap
;
8 import org
.apache
.commons
.logging
.Log
;
9 import org
.apache
.commons
.logging
.LogFactory
;
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
.springframework
.beans
.factory
.BeanNameAware
;
20 import org
.springframework
.beans
.factory
.InitializingBean
;
21 import org
.springframework
.validation
.MapBindingResult
;
23 public class DefaultExecutionFlow
implements ExecutionFlow
, InitializingBean
,
24 BeanNameAware
, StructureAware
<TreeSPath
> {
26 private final static Log log
= LogFactory
27 .getLog(DefaultExecutionFlow
.class);
29 private ExecutionSpec executionSpec
= new DefaultExecutionSpec();
30 private String name
= null;
31 private Map
<String
, Object
> parameters
= new HashMap
<String
, Object
>();
32 private List
<Executable
> executables
= new ArrayList
<Executable
>();
35 private StructureRegistry
<TreeSPath
> registry
= new TreeSRegistry();
37 public DefaultExecutionFlow() {
40 public DefaultExecutionFlow(ExecutionSpec executionSpec
) {
41 this.executionSpec
= executionSpec
;
44 public DefaultExecutionFlow(ExecutionSpec executionSpec
, Map
<String
, Object
> parameters
) {
45 // be sure to have an execution spec
46 this.executionSpec
= (executionSpec
== null) ?
new DefaultExecutionSpec() : executionSpec
;
48 //only parameters contained in the executionSpec can be set
49 for(String parameter
: parameters
.keySet()) {
50 if(!executionSpec
.getAttributes().containsKey(parameter
)) {
51 throw new SlcException("Parameter " + parameter
+ " is not defined in the ExecutionSpec");
56 this.parameters
.putAll(parameters
);
58 //check that all the required parameters are defined
59 MapBindingResult errors
= new MapBindingResult(parameters
, "execution#"
61 for (String key
: executionSpec
.getAttributes().keySet()) {
62 ExecutionSpecAttribute attr
= executionSpec
.getAttributes()
65 if (attr
.getIsParameter() && !isSetAsParameter(key
)) {
66 errors
.rejectValue(key
, "Parameter not set");
70 if (attr
.getIsFrozen() && !isSetAsParameter(key
)) {
71 errors
.rejectValue(key
, "Frozen but not set as parameter");
75 if (attr
.getIsHidden() && !isSetAsParameter(key
)) {
76 errors
.rejectValue(key
, "Hidden but not set as parameter");
81 if (errors
.hasErrors())
82 throw new SlcException("Could not prepare execution flow: "
88 public void execute() {
89 for (Executable executable
: executables
) {
94 public void afterPropertiesSet() throws Exception
{
96 for (Executable executable
: executables
) {
97 if (executable
instanceof StructureAware
) {
98 ((StructureAware
<TreeSPath
>) executable
).notifyCurrentPath(
99 registry
, new TreeSPath(path
));
105 public void setBeanName(String name
) {
109 public void setExecutables(List
<Executable
> executables
) {
110 this.executables
= executables
;
113 public void setParameters(Map
<String
, Object
> attributes
) {
114 this.parameters
= attributes
;
117 public String
getName() {
121 public ExecutionSpec
getExecutionSpec() {
122 return executionSpec
;
125 public Object
getParameter(String parameterName
) {
126 if (parameters
.containsKey(parameterName
)) {
127 return parameters
.get(parameterName
);
129 if (executionSpec
.getAttributes().containsKey(parameterName
)) {
130 ExecutionSpecAttribute esa
= executionSpec
.getAttributes().get(
132 if (esa
.getValue() != null) {
133 return esa
.getValue();
136 throw new SlcException("Key " + parameterName
137 + " is not defined in the specifications of "
141 throw new SlcException("Key " + parameterName
+ " is not set as parameter in "
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();