]> 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
fd05c6425dc66bc3cd25aed7dc933f69438cecc7
[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 import java.util.UUID;
8
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;
23
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>();
30
31 private String path;
32 private StructureRegistry<TreeSPath> registry = new TreeSRegistry();
33
34 public DefaultExecutionFlow() {
35 }
36
37 public DefaultExecutionFlow(ExecutionSpec executionSpec) {
38 this.executionSpec = executionSpec;
39 }
40
41 public DefaultExecutionFlow(ExecutionSpec executionSpec, Map<String, Object> parameters) {
42 this.parameters.putAll(parameters);
43 this.executionSpec = executionSpec;
44 }
45
46 public void execute() {
47 for (Executable executable : executables) {
48 executable.execute();
49 }
50 }
51
52 public void afterPropertiesSet() throws Exception {
53 // Validate execution specs
54 if (executionSpec == null)
55 return;
56
57 MapBindingResult errors = new MapBindingResult(parameters, "execution#"
58 + getName());
59 for (String key : executionSpec.getAttributes().keySet()) {
60 ExecutionSpecAttribute attr = executionSpec.getAttributes()
61 .get(key);
62
63 if (attr.getIsParameter() && !isSetAsParameter(key)) {
64 errors.rejectValue(key, "Parameter not set");
65 break;
66 }
67
68 if (attr.getIsFrozen() && !isSetAsParameter(key)) {
69 errors.rejectValue(key, "Frozen but not set as parameter");
70 break;
71 }
72
73 if (attr.getIsHidden() && !isSetAsParameter(key)) {
74 errors.rejectValue(key, "Hidden but not set as parameter");
75 break;
76 }
77
78 /*
79 * if (!parameters.containsKey(key)) { Object defaultValue =
80 * attr.getValue(); if (defaultValue == null)
81 * errors.rejectValue(key, "Not set and no default value"); else
82 * parameters.put(key, defaultValue); } else {// contains key Object
83 * obj = parameters.get(key); if (attr instanceof RefSpecAttribute)
84 * { RefSpecAttribute rsa = (RefSpecAttribute) attr; // TODO: make
85 * sure this will not cause pb with OSGi Class targetClass =
86 * rsa.getTargetClass(); if
87 * (!targetClass.isAssignableFrom(obj.getClass())) {
88 * errors.reject(key + " not compatible with target class " +
89 * targetClass); } } }
90 */
91 }
92
93 if (errors.hasErrors())
94 throw new SlcException("Could not prepare execution flow: "
95 + errors.toString());
96
97 // if (path == null) {
98 // path = "/" + executionSpec.getName() + "/" + name;
99 // }
100
101 if (path != null) {
102 for (Executable executable : executables) {
103 if (executable instanceof StructureAware) {
104 ((StructureAware<TreeSPath>) executable).notifyCurrentPath(
105 registry, new TreeSPath(path));
106 }
107 }
108 }
109 }
110
111 public void setBeanName(String name) {
112 this.name = name;
113 }
114
115 public void setExecutables(List<Executable> executables) {
116 this.executables = executables;
117 }
118
119 // public void setExecutionSpec(ExecutionSpec executionSpec) {
120 // this.executionSpec = executionSpec;
121 // }
122
123 public void setParameters(Map<String, Object> attributes) {
124 this.parameters = attributes;
125 }
126
127 public String getName() {
128 return name;
129 }
130
131 public ExecutionSpec getExecutionSpec() {
132 return executionSpec;
133 }
134
135 public Object getParameter(String name) {
136 if (parameters.containsKey(name)) {
137 return parameters.get(name);
138 } else {
139 if (executionSpec.getAttributes().containsKey(name)) {
140 ExecutionSpecAttribute esa = executionSpec.getAttributes().get(
141 name);
142 if (esa.getValue() != null)
143 return esa.getValue();
144 } else {
145 throw new SlcException("Key " + name
146 + " is not defined in the specifications of "
147 + toString());
148 }
149 }
150 throw new SlcException("Key " + name + " is not set as parameter in "
151 + toString());
152 }
153
154 public Boolean isSetAsParameter(String key) {
155 return parameters.containsKey(key)
156 || (executionSpec.getAttributes().containsKey(key) && executionSpec
157 .getAttributes().get(key).getValue() != null);
158 }
159
160 public String toString() {
161 return new StringBuffer("Flow ").append(name).toString();
162 }
163
164 public boolean equals(Object obj) {
165 return ((ExecutionFlow) obj).getName().equals(name);
166 }
167
168 public String getPath() {
169 return path;
170 }
171
172 public void setPath(String path) {
173 this.path = path;
174 }
175
176 public void setRegistry(StructureRegistry<TreeSPath> registry) {
177 this.registry = registry;
178 }
179
180 public void notifyCurrentPath(StructureRegistry<TreeSPath> registry,
181 TreeSPath path) {
182 if (this.path == null) {
183 this.path = path.toString();
184 }
185 }
186
187 }