]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/core/execution/DefaultExecutionFlow.java
Improve execution specs
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.simple / src / main / java / org / argeo / slc / core / execution / DefaultExecutionFlow.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 DefaultExecutionFlow implements ExecutionFlow, InitializingBean,
21 BeanNameAware {
22 private ExecutionSpec executionSpec = new DefaultExecutionSpec();
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 DefaultExecutionFlow() {
28
29 }
30
31 public DefaultExecutionFlow(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 attr = executionSpec.getAttributes()
50 .get(key);
51
52 if (attr.getIsParameter() && !isSetAsParameter(key)) {
53 errors.rejectValue(key, "Parameter not set");
54 break;
55 }
56
57 if (attr.getIsFrozen() && !isSetAsParameter(key)) {
58 errors.rejectValue(key, "Frozen but not set as parameter");
59 break;
60 }
61
62 if (attr.getIsHidden() && !isSetAsParameter(key)) {
63 errors.rejectValue(key, "Hidden but not set as parameter");
64 break;
65 }
66
67 /*
68 * if (!parameters.containsKey(key)) { Object defaultValue =
69 * attr.getValue(); if (defaultValue == null)
70 * errors.rejectValue(key, "Not set and no default value"); else
71 * parameters.put(key, defaultValue); } else {// contains key Object
72 * obj = parameters.get(key); if (attr instanceof RefSpecAttribute)
73 * { RefSpecAttribute rsa = (RefSpecAttribute) attr; // TODO: make
74 * sure this will not cause pb with OSGi Class targetClass =
75 * rsa.getTargetClass(); if
76 * (!targetClass.isAssignableFrom(obj.getClass())) {
77 * errors.reject(key + " not compatible with target class " +
78 * targetClass); } } }
79 */
80 }
81
82 if (errors.hasErrors())
83 throw new SlcException("Could not prepare execution flow: "
84 + errors.toString());
85 }
86
87 public void setBeanName(String name) {
88 this.name = name;
89 }
90
91 public void setExecutables(List<Executable> executables) {
92 this.executables = executables;
93 }
94
95 public void setExecutionSpec(ExecutionSpec executionSpec) {
96 this.executionSpec = executionSpec;
97 }
98
99 public void setParameters(Map<String, Object> attributes) {
100 this.parameters = attributes;
101 }
102
103 public String getName() {
104 return name;
105 }
106
107 public ExecutionSpec getExecutionSpec() {
108 return executionSpec;
109 }
110
111 public Object getParameter(String name) {
112 if (parameters.containsKey(name)) {
113 return parameters.get(name);
114 } else {
115 if (executionSpec.getAttributes().containsKey(name)) {
116 ExecutionSpecAttribute esa = executionSpec.getAttributes().get(
117 name);
118 if (esa.getValue() != null)
119 return esa.getValue();
120 } else {
121 throw new SlcException("Key " + name
122 + " is not defined in the specifications of "
123 + toString());
124 }
125 }
126 throw new SlcException("Key " + name + " is not set as parameter in "
127 + toString());
128 }
129
130 public Boolean isSetAsParameter(String key) {
131 return parameters.containsKey(key)
132 || (executionSpec.getAttributes().containsKey(key) && executionSpec
133 .getAttributes().get(key).getValue() != null);
134 }
135
136 public String toString() {
137 return new StringBuffer("Flow ").append(name).toString();
138 }
139
140 public boolean equals(Object obj) {
141 return ((ExecutionFlow) obj).getName().equals(name);
142 }
143 }