]> 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
8c06dc579f62db2d01f4c1f70e335cc64ee7e3a6
[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.core.structure.tree.TreeSPath;
12 import org.argeo.slc.core.structure.tree.TreeSRegistry;
13 import org.argeo.slc.execution.Executable;
14 import org.argeo.slc.execution.ExecutionFlow;
15 import org.argeo.slc.execution.ExecutionSpec;
16 import org.argeo.slc.execution.ExecutionSpecAttribute;
17 import org.argeo.slc.structure.StructureAware;
18 import org.argeo.slc.structure.StructureRegistry;
19 import org.argeo.slc.test.ExecutableTestRun;
20 import org.springframework.beans.factory.BeanNameAware;
21 import org.springframework.beans.factory.InitializingBean;
22 import org.springframework.validation.MapBindingResult;
23
24 public class DefaultExecutionFlow implements ExecutionFlow, InitializingBean,
25 BeanNameAware {
26 private ExecutionSpec executionSpec = new DefaultExecutionSpec();
27 private String name = null;
28 private Map<String, Object> parameters = new HashMap<String, Object>();
29 private List<Executable> executables = new ArrayList<Executable>();
30
31 private String path;
32 private StructureRegistry<TreeSPath> registry = new TreeSRegistry();
33
34 public DefaultExecutionFlow() {
35
36 }
37
38 public DefaultExecutionFlow(Map<String, Object> parameters) {
39 this.parameters.putAll(parameters);
40 }
41
42 public void execute() {
43 for (Executable executable : executables) {
44 executable.execute();
45 }
46 }
47
48 public void afterPropertiesSet() throws Exception {
49 // Validate execution specs
50 if (executionSpec == null)
51 return;
52
53 MapBindingResult errors = new MapBindingResult(parameters, "execution#"
54 + getName());
55 for (String key : executionSpec.getAttributes().keySet()) {
56 ExecutionSpecAttribute attr = executionSpec.getAttributes()
57 .get(key);
58
59 if (attr.getIsParameter() && !isSetAsParameter(key)) {
60 errors.rejectValue(key, "Parameter not set");
61 break;
62 }
63
64 if (attr.getIsFrozen() && !isSetAsParameter(key)) {
65 errors.rejectValue(key, "Frozen but not set as parameter");
66 break;
67 }
68
69 if (attr.getIsHidden() && !isSetAsParameter(key)) {
70 errors.rejectValue(key, "Hidden but not set as parameter");
71 break;
72 }
73
74 /*
75 * if (!parameters.containsKey(key)) { Object defaultValue =
76 * attr.getValue(); if (defaultValue == null)
77 * errors.rejectValue(key, "Not set and no default value"); else
78 * parameters.put(key, defaultValue); } else {// contains key Object
79 * obj = parameters.get(key); if (attr instanceof RefSpecAttribute)
80 * { RefSpecAttribute rsa = (RefSpecAttribute) attr; // TODO: make
81 * sure this will not cause pb with OSGi Class targetClass =
82 * rsa.getTargetClass(); if
83 * (!targetClass.isAssignableFrom(obj.getClass())) {
84 * errors.reject(key + " not compatible with target class " +
85 * targetClass); } } }
86 */
87 }
88
89 if (errors.hasErrors())
90 throw new SlcException("Could not prepare execution flow: "
91 + errors.toString());
92
93 // if (path == null) {
94 // path = "/" + executionSpec.getName() + "/" + name;
95 // }
96
97 if (path != null) {
98 for (Executable executable : executables) {
99 if (executable instanceof StructureAware) {
100 ((StructureAware<TreeSPath>) executable).notifyCurrentPath(
101 registry, new TreeSPath(path));
102 }
103 }
104 }
105 }
106
107 public void setBeanName(String name) {
108 this.name = name;
109 }
110
111 public void setExecutables(List<Executable> executables) {
112 this.executables = executables;
113 }
114
115 public void setExecutionSpec(ExecutionSpec executionSpec) {
116 this.executionSpec = executionSpec;
117 }
118
119 public void setParameters(Map<String, Object> attributes) {
120 this.parameters = attributes;
121 }
122
123 public String getName() {
124 return name;
125 }
126
127 public ExecutionSpec getExecutionSpec() {
128 return executionSpec;
129 }
130
131 public Object getParameter(String name) {
132 if (parameters.containsKey(name)) {
133 return parameters.get(name);
134 } else {
135 if (executionSpec.getAttributes().containsKey(name)) {
136 ExecutionSpecAttribute esa = executionSpec.getAttributes().get(
137 name);
138 if (esa.getValue() != null)
139 return esa.getValue();
140 } else {
141 throw new SlcException("Key " + name
142 + " is not defined in the specifications of "
143 + toString());
144 }
145 }
146 throw new SlcException("Key " + name + " is not set as parameter in "
147 + toString());
148 }
149
150 public Boolean isSetAsParameter(String key) {
151 return parameters.containsKey(key)
152 || (executionSpec.getAttributes().containsKey(key) && executionSpec
153 .getAttributes().get(key).getValue() != null);
154 }
155
156 public String toString() {
157 return new StringBuffer("Flow ").append(name).toString();
158 }
159
160 public boolean equals(Object obj) {
161 return ((ExecutionFlow) obj).getName().equals(name);
162 }
163
164 public String getPath() {
165 return path;
166 }
167
168 }