]> git.argeo.org Git - gpl/argeo-slc.git/blob - execution/SimpleExecutionFlow.java
Prepare next development cycle
[gpl/argeo-slc.git] / execution / SimpleExecutionFlow.java
1 package org.argeo.slc.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.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> parameters = new HashMap<String, Object>();
22 private List<Executable> executables = new ArrayList<Executable>();
23
24 public SimpleExecutionFlow() {
25
26 }
27
28 public SimpleExecutionFlow(Map<String, Object> parameters) {
29 this.parameters.putAll(parameters);
30 }
31
32 public void execute() {
33 for (Executable executable : executables) {
34 executable.execute();
35 }
36 }
37
38 public void afterPropertiesSet() throws Exception {
39 // Validate execution specs
40 if (executionSpec == null)
41 return;
42
43 MapBindingResult errors = new MapBindingResult(parameters, "execution#"
44 + getName());
45 for (String key : executionSpec.getAttributes().keySet()) {
46 ExecutionSpecAttribute executionSpecAttr = executionSpec
47 .getAttributes().get(key);
48 if (!parameters.containsKey(key)) {
49 Object defaultValue = executionSpecAttr.getValue();
50 if (defaultValue == null)
51 errors.rejectValue(key, "Not set and no default value");
52 else
53 parameters.put(key, defaultValue);
54 } else {// contains key
55 Object obj = parameters.get(key);
56 if (executionSpecAttr instanceof RefSpecAttribute) {
57 RefSpecAttribute rsa = (RefSpecAttribute) executionSpecAttr;
58 // TODO: make sure this will not cause pb with OSGi
59 Class targetClass = rsa.getTargetClass();
60 if (!targetClass.isAssignableFrom(obj.getClass())) {
61 errors.reject(key
62 + " not compatible with target class "
63 + targetClass);
64 }
65 }
66 }
67 }
68
69 if (errors.hasErrors())
70 throw new SlcException("Could not prepare execution flow: "
71 + errors.toString());
72 }
73
74 public void setBeanName(String name) {
75 this.name = name;
76 }
77
78 public void setExecutables(List<Executable> executables) {
79 this.executables = executables;
80 }
81
82 public void setExecutionSpec(ExecutionSpec executionSpec) {
83 this.executionSpec = executionSpec;
84 }
85
86 public void setParameters(Map<String, Object> attributes) {
87 this.parameters = attributes;
88 }
89
90 public String getName() {
91 return name;
92 }
93
94 public ExecutionSpec getExecutionSpec() {
95 return executionSpec;
96 }
97
98 public Object getParameter(String name) {
99 if (parameters.containsKey(name)) {
100 return parameters.get(name);
101 } else {
102 if (executionSpec.getAttributes().containsKey(name)) {
103 ExecutionSpecAttribute esa = executionSpec.getAttributes().get(
104 name);
105 if (esa.getValue() != null)
106 return esa.getValue();
107 } else {
108 throw new SlcException("Key " + name
109 + " is not defined in the specifications of "
110 + toString());
111 }
112 }
113 throw new SlcException("Key " + name + " is not set as parameter in "
114 + toString());
115 }
116
117 public String toString() {
118 return new StringBuffer("Flow ").append(name).toString();
119 }
120
121 public boolean equals(Object obj) {
122 return ((ExecutionFlow) obj).getName().equals(name);
123 }
124 }