]> 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
Introduce system calls
[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
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;
22
23 public class DefaultExecutionFlow implements ExecutionFlow, InitializingBean,
24 BeanNameAware, StructureAware<TreeSPath> {
25
26 private final static Log log = LogFactory
27 .getLog(DefaultExecutionFlow.class);
28
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>();
33
34 private String path;
35 private StructureRegistry<TreeSPath> registry = new TreeSRegistry();
36
37 public DefaultExecutionFlow() {
38 }
39
40 public DefaultExecutionFlow(ExecutionSpec executionSpec) {
41 this.executionSpec = executionSpec;
42 }
43
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;
47
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");
52 }
53 }
54
55 // set the parameters
56 this.parameters.putAll(parameters);
57
58 //check that all the required parameters are defined
59 MapBindingResult errors = new MapBindingResult(parameters, "execution#"
60 + getName());
61 for (String key : executionSpec.getAttributes().keySet()) {
62 ExecutionSpecAttribute attr = executionSpec.getAttributes()
63 .get(key);
64
65 if (attr.getIsParameter() && !isSetAsParameter(key)) {
66 errors.rejectValue(key, "Parameter not set");
67 break;
68 }
69
70 if (attr.getIsFrozen() && !isSetAsParameter(key)) {
71 errors.rejectValue(key, "Frozen but not set as parameter");
72 break;
73 }
74
75 if (attr.getIsHidden() && !isSetAsParameter(key)) {
76 errors.rejectValue(key, "Hidden but not set as parameter");
77 break;
78 }
79 }
80
81 if (errors.hasErrors())
82 throw new SlcException("Could not prepare execution flow: "
83 + errors.toString());
84
85
86 }
87
88 public void execute() {
89 for (Executable executable : executables) {
90 executable.execute();
91 }
92 }
93
94 public void afterPropertiesSet() throws Exception {
95 if (path != null) {
96 for (Executable executable : executables) {
97 if (executable instanceof StructureAware) {
98 ((StructureAware<TreeSPath>) executable).notifyCurrentPath(
99 registry, new TreeSPath(path));
100 }
101 }
102 }
103 }
104
105 public void setBeanName(String name) {
106 this.name = name;
107 }
108
109 public void setExecutables(List<Executable> executables) {
110 this.executables = executables;
111 }
112
113 public void setParameters(Map<String, Object> attributes) {
114 this.parameters = attributes;
115 }
116
117 public String getName() {
118 return name;
119 }
120
121 public ExecutionSpec getExecutionSpec() {
122 return executionSpec;
123 }
124
125 public Object getParameter(String parameterName) {
126 if (parameters.containsKey(parameterName)) {
127 return parameters.get(parameterName);
128 } else {
129 if (executionSpec.getAttributes().containsKey(parameterName)) {
130 ExecutionSpecAttribute esa = executionSpec.getAttributes().get(
131 parameterName);
132 if (esa.getValue() != null) {
133 return esa.getValue();
134 }
135 } else {
136 throw new SlcException("Key " + parameterName
137 + " is not defined in the specifications of "
138 + toString());
139 }
140 }
141 throw new SlcException("Key " + parameterName + " is not set as parameter in "
142 + toString());
143 }
144
145 public Boolean isSetAsParameter(String key) {
146 return parameters.containsKey(key)
147 || (executionSpec.getAttributes().containsKey(key) && executionSpec
148 .getAttributes().get(key).getValue() != null);
149 }
150
151 public String toString() {
152 return new StringBuffer("Flow ").append(name).toString();
153 }
154
155 public boolean equals(Object obj) {
156 return ((ExecutionFlow) obj).getName().equals(name);
157 }
158
159 public String getPath() {
160 return path;
161 }
162
163 public void setPath(String path) {
164 this.path = path;
165 }
166
167 public void setRegistry(StructureRegistry<TreeSPath> registry) {
168 this.registry = registry;
169 }
170
171 public void notifyCurrentPath(StructureRegistry<TreeSPath> registry,
172 TreeSPath path) {
173 if (this.path == null) {
174 this.path = path.toString();
175 }
176 }
177
178 }