]> git.argeo.org Git - gpl/argeo-slc.git/blob - sandbox/argeo.slc.executionflow/src/main/java/org/argeo/slc/executionflow/SimpleExecutionFlow.java
cb27e68401327d34e1342a0cf3408b9d9f9a4d53
[gpl/argeo-slc.git] / sandbox / argeo.slc.executionflow / src / main / java / org / argeo / slc / executionflow / SimpleExecutionFlow.java
1 package org.argeo.slc.executionflow;
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.process.Executable;
12 import org.argeo.slc.test.ExecutableTestRun;
13 import org.springframework.beans.factory.BeanNameAware;
14 import org.springframework.beans.factory.InitializingBean;
15 import org.springframework.validation.MapBindingResult;
16
17 public class SimpleExecutionFlow implements ExecutionFlow, InitializingBean,
18 BeanNameAware {
19 private ExecutionSpec executionSpec = new SimpleExecutionSpec();
20 private String name = null;
21 private Map<String, Object> attributes = new HashMap<String, Object>();
22 private List<Executable> executables = new ArrayList<Executable>();
23
24 private final String uuid = UUID.randomUUID().toString();
25
26 public void execute() {
27 for (Executable executable : executables) {
28 executable.execute();
29 }
30 }
31
32 public void afterPropertiesSet() throws Exception {
33 // Validate execution specs
34 if (executionSpec == null)
35 return;
36
37 MapBindingResult errors = new MapBindingResult(attributes, "execution#"
38 + getUuid());
39 for (String key : executionSpec.getAttributes().keySet()) {
40 ExecutionSpecAttribute executionSpecAttr = executionSpec
41 .getAttributes().get(key);
42 if (!attributes.containsKey(key)) {
43 Object defaultValue = executionSpecAttr.getValue();
44 if (defaultValue == null)
45 errors.rejectValue(key, "Not set and no default value");
46 else
47 attributes.put(key, defaultValue);
48 } else {// contains key
49 Object obj = attributes.get(key);
50 if (executionSpecAttr instanceof RefSpecAttribute) {
51 RefSpecAttribute rsa = (RefSpecAttribute) executionSpecAttr;
52 Class targetClass = rsa.getTargetClass();
53 if (!targetClass.isAssignableFrom(obj.getClass())) {
54 errors.reject(key
55 + " not compatible with target class "
56 + targetClass);
57 }
58 }
59 }
60 }
61
62 if (errors.hasErrors())
63 throw new SlcException("Could not prepare execution flow: "
64 + errors.toString());
65 }
66
67 public void setBeanName(String name) {
68 this.name = name;
69 }
70
71 public void setExecutables(List<Executable> executables) {
72 this.executables = executables;
73 }
74
75 public void setExecutionSpec(ExecutionSpec executionSpec) {
76 this.executionSpec = executionSpec;
77 }
78
79 public void setAttributes(Map<String, Object> attributes) {
80 this.attributes = attributes;
81 }
82
83 public Map<String, Object> getAttributes() {
84 return attributes;
85 }
86
87 public String getUuid() {
88 return uuid;
89 }
90
91 public ExecutionSpec getExecutionSpec() {
92 return executionSpec;
93 }
94
95 public String toString() {
96 return new StringBuffer("Flow ").append(name).toString();// .append(" [#")
97 // .append(uuid).append(']').toString();
98 }
99 }