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