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