]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/tree/TreeTestResult.java
Add web service notification of results.
[gpl/argeo-slc.git] / org.argeo.slc.core / src / main / java / org / argeo / slc / core / test / tree / TreeTestResult.java
1 package org.argeo.slc.core.test.tree;
2
3 import java.util.Date;
4 import java.util.List;
5 import java.util.SortedMap;
6 import java.util.TreeMap;
7 import java.util.Vector;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11
12 import org.argeo.slc.core.SlcException;
13 import org.argeo.slc.core.structure.StructureAware;
14 import org.argeo.slc.core.structure.StructureElement;
15 import org.argeo.slc.core.structure.StructureRegistry;
16 import org.argeo.slc.core.structure.tree.TreeSPath;
17 import org.argeo.slc.core.test.TestResult;
18 import org.argeo.slc.core.test.TestResultListener;
19 import org.argeo.slc.core.test.TestResultPart;
20 import org.argeo.slc.core.test.TestRun;
21 import org.argeo.slc.core.test.TestRunAware;
22
23 /**
24 * Complex implementation of a test result compatible with a tree based
25 * structure.
26 */
27 public class TreeTestResult implements TestResult, StructureAware<TreeSPath> {
28 private Log log = LogFactory.getLog(TreeTestResult.class);
29
30 private List<TestResultListener<TreeTestResult>> listeners = new Vector<TestResultListener<TreeTestResult>>();
31
32 private TreeSPath currentPath;
33 private TestRun currentTestRun;
34
35 private Date closeDate;
36
37 private boolean isClosed = false;
38
39 private String uuid;
40
41 private SortedMap<TreeSPath, PartSubList> resultParts = new TreeMap<TreeSPath, PartSubList>();
42 private SortedMap<TreeSPath, StructureElement> elements = new TreeMap<TreeSPath, StructureElement>();
43
44 /** Sets the list of listeners. */
45 public void setListeners(List<TestResultListener<TreeTestResult>> listeners) {
46 this.listeners = listeners;
47 }
48
49 public void addResultPart(TestResultPart part) {
50 if (currentPath == null) {
51 throw new SlcException("No current path set.");
52 }
53 PartSubList subList = resultParts.get(currentPath);
54 if (subList == null) {
55 subList = new PartSubList();
56 resultParts.put(currentPath, subList);
57 }
58 if (part instanceof TestRunAware && currentTestRun != null) {
59 ((TestRunAware) part).notifyTestRun(currentTestRun);
60 }
61 subList.getParts().add(part);
62
63 // notify listeners
64 synchronized (listeners) {
65 for (TestResultListener<TreeTestResult> listener : listeners) {
66 listener.resultPartAdded(this, part);
67 }
68 }
69 }
70
71 public void notifyCurrentPath(StructureRegistry<TreeSPath> registry,
72 TreeSPath path) {
73 if (registry != null) {
74 for (TreeSPath p : path.getHierarchyAsList()) {
75 if (!elements.containsKey(p)) {
76 StructureElement elem = registry.getElement(p);
77 if (elem != null) {
78 elements.put(p, elem);
79 }
80 } else {
81 if (log.isTraceEnabled())
82 log.trace("An element is already registered for path "
83 + p + " and was not updated");
84 }
85
86 }
87 }
88
89 currentPath = (TreeSPath) path;
90 }
91
92 /** Gets the current path. */
93 public TreeSPath getCurrentPath() {
94 return currentPath;
95 }
96
97 /** Gets all the results structured as a map of <code>PartSubList<code>s. */
98 public SortedMap<TreeSPath, PartSubList> getResultParts() {
99 return resultParts;
100 }
101
102 /** Used by ORM systems. */
103 void setResultParts(SortedMap<TreeSPath, PartSubList> resultParts) {
104 this.resultParts = resultParts;
105 }
106
107 public void close() {
108 if (isClosed) {
109 throw new SlcException("Test Result #" + getUuid()
110 + " alredy closed.");
111 }
112 closeDate = new Date();
113
114 synchronized (listeners) {
115 for (TestResultListener<TreeTestResult> listener : listeners) {
116 listener.close(this);
117 }
118 listeners.clear();
119 }
120 isClosed = true;
121
122 log.info("Test Result #" + getUuid() + " closed.");
123 }
124
125 public Date getCloseDate() {
126 return closeDate;
127 }
128
129 /** Sets the close date (for ORM) */
130 public void setCloseDate(Date closeDate) {
131 this.closeDate = closeDate;
132 }
133
134 public void notifyTestRun(TestRun testRun) {
135 currentTestRun = testRun;
136 }
137
138 public SortedMap<TreeSPath, StructureElement> getElements() {
139 return elements;
140 }
141
142 public void setElements(SortedMap<TreeSPath, StructureElement> pathNames) {
143 this.elements = pathNames;
144 }
145
146 public String getUuid() {
147 return uuid;
148 }
149
150 public void setUuid(String uuid) {
151 this.uuid = uuid;
152 }
153
154 public SortedMap<TreeSPath, StructureElement> getRelatedElements(
155 TreeSPath path) {
156 SortedMap<TreeSPath, StructureElement> relatedElements = new TreeMap<TreeSPath, StructureElement>();
157 List<TreeSPath> hierarchy = path.getHierarchyAsList();
158 for (TreeSPath currPath : elements.keySet()) {
159 if (hierarchy.contains(currPath)) {
160 relatedElements.put(currPath, elements.get(currPath));
161 }
162 }
163 return relatedElements;
164 }
165
166 public TestRun getCurrentTestRun() {
167 return currentTestRun;
168 }
169
170 }