]> 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
Add path in execution flow descriptor
[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 for (Executable executable : executables) {
98 if (executable instanceof StructureAware) {
99 ((StructureAware<TreeSPath>) executable).notifyCurrentPath(
100 registry, new TreeSPath(path));
101 }
102 }
103 }
104
105 public void setBeanName(String name) {
106 this.name = name;
107 }
108
109 public void setExecutables(List<Executable> executables) {
110 this.executables = executables;
111 }
112
113 public void setExecutionSpec(ExecutionSpec executionSpec) {
114 this.executionSpec = executionSpec;
115 }
116
117 public void setParameters(Map<String, Object> attributes) {
118 this.parameters = attributes;
119 }
120
121 public String getName() {
122 return name;
123 }
124
125 public ExecutionSpec getExecutionSpec() {
126 return executionSpec;
127 }
128
129 public Object getParameter(String name) {
130 if (parameters.containsKey(name)) {
131 return parameters.get(name);
132 } else {
133 if (executionSpec.getAttributes().containsKey(name)) {
134 ExecutionSpecAttribute esa = executionSpec.getAttributes().get(
135 name);
136 if (esa.getValue() != null)
137 return esa.getValue();
138 } else {
139 throw new SlcException("Key " + name
140 + " is not defined in the specifications of "
141 + toString());
142 }
143 }
144 throw new SlcException("Key " + name + " is not set as parameter in "
145 + toString());
146 }
147
148 public Boolean isSetAsParameter(String key) {
149 return parameters.containsKey(key)
150 || (executionSpec.getAttributes().containsKey(key) && executionSpec
151 .getAttributes().get(key).getValue() != null);
152 }
153
154 public String toString() {
155 return new StringBuffer("Flow ").append(name).toString();
156 }
157
158 public boolean equals(Object obj) {
159 return ((ExecutionFlow) obj).getName().equals(name);
160 }
161
162 public String getPath() {
163 return path;
164 }
165
166
167 }