]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/SimpleExecutionFlow.java
Data model for batch entry, spec editor basis.
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / core / execution / SimpleExecutionFlow.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.execution.ExecutionFlow;
12 import org.argeo.slc.execution.ExecutionSpec;
13 import org.argeo.slc.execution.ExecutionSpecAttribute;
14 import org.argeo.slc.process.Executable;
15 import org.argeo.slc.test.ExecutableTestRun;
16 import org.springframework.beans.factory.BeanNameAware;
17 import org.springframework.beans.factory.InitializingBean;
18 import org.springframework.validation.MapBindingResult;
19
20 public class SimpleExecutionFlow implements ExecutionFlow, InitializingBean,
21 BeanNameAware {
22 private ExecutionSpec executionSpec = new SimpleExecutionSpec();
23 private String name = null;
24 private Map<String, Object> parameters = new HashMap<String, Object>();
25 private List<Executable> executables = new ArrayList<Executable>();
26
27 public SimpleExecutionFlow() {
28
29 }
30
31 public SimpleExecutionFlow(Map<String, Object> parameters) {
32 this.parameters.putAll(parameters);
33 }
34
35 public void execute() {
36 for (Executable executable : executables) {
37 executable.execute();
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(parameters, "execution#"
47 + getName());
48 for (String key : executionSpec.getAttributes().keySet()) {
49 ExecutionSpecAttribute executionSpecAttr = executionSpec
50 .getAttributes().get(key);
51 if (!parameters.containsKey(key)) {
52 Object defaultValue = executionSpecAttr.getValue();
53 if (defaultValue == null)
54 errors.rejectValue(key, "Not set and no default value");
55 else
56 parameters.put(key, defaultValue);
57 } else {// contains key
58 Object obj = parameters.get(key);
59 if (executionSpecAttr instanceof RefSpecAttribute) {
60 RefSpecAttribute rsa = (RefSpecAttribute) executionSpecAttr;
61 // TODO: make sure this will not cause pb with OSGi
62 Class targetClass = rsa.getTargetClass();
63 if (!targetClass.isAssignableFrom(obj.getClass())) {
64 errors.reject(key
65 + " not compatible with target class "
66 + targetClass);
67 }
68 }
69 }
70 }
71
72 if (errors.hasErrors())
73 throw new SlcException("Could not prepare execution flow: "
74 + errors.toString());
75 }
76
77 public void setBeanName(String name) {
78 this.name = name;
79 }
80
81 public void setExecutables(List<Executable> executables) {
82 this.executables = executables;
83 }
84
85 public void setExecutionSpec(ExecutionSpec executionSpec) {
86 this.executionSpec = executionSpec;
87 }
88
89 public void setParameters(Map<String, Object> attributes) {
90 this.parameters = attributes;
91 }
92
93 public String getName() {
94 return name;
95 }
96
97 public ExecutionSpec getExecutionSpec() {
98 return executionSpec;
99 }
100
101 public Object getParameter(String name) {
102 if (parameters.containsKey(name)) {
103 return parameters.get(name);
104 } else {
105 if (executionSpec.getAttributes().containsKey(name)) {
106 ExecutionSpecAttribute esa = executionSpec.getAttributes().get(
107 name);
108 if (esa.getValue() != null)
109 return esa.getValue();
110 } else {
111 throw new SlcException("Key " + name
112 + " is not defined in the specifications of "
113 + toString());
114 }
115 }
116 throw new SlcException("Key " + name + " is not set as parameter in "
117 + toString());
118 }
119
120 public String toString() {
121 return new StringBuffer("Flow ").append(name).toString();
122 }
123
124 public boolean equals(Object obj) {
125 return ((ExecutionFlow) obj).getName().equals(name);
126 }
127 }