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