]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc/src/main/java/org/argeo/slc/core/test/tree/TreeTestResult.java
Improve reporting
[gpl/argeo-slc.git] / org.argeo.slc / 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.StructurePath;
15 import org.argeo.slc.core.structure.StructureRegistry;
16 import org.argeo.slc.core.structure.tree.TreeSPath;
17 import org.argeo.slc.core.test.NumericTRId;
18 import org.argeo.slc.core.test.TestResult;
19 import org.argeo.slc.core.test.TestResultId;
20 import org.argeo.slc.core.test.TestResultListener;
21 import org.argeo.slc.core.test.TestResultPart;
22
23 /**
24 * Complex implementation of a test result compatible with a tree based
25 * structure.
26 */
27 public class TreeTestResult implements TestResult, StructureAware {
28 private Log log = LogFactory.getLog(TreeTestResult.class);
29 /** For ORM */
30 private Long tid;
31
32 private NumericTRId testResultId;
33 private List<TestResultListener> listeners = new Vector<TestResultListener>();
34
35 private TreeSPath currentPath;
36
37 private Date closeDate;
38
39 private boolean isClosed = false;
40
41 private SortedMap<TreeSPath, PartSubList> resultParts = new TreeMap<TreeSPath, PartSubList>();
42
43 private StructureRegistry registry;
44
45 public TestResultId getTestResultId() {
46 return testResultId;
47 }
48
49 /**
50 * Use of a <code>NumericTRId</code> is required by Hibernate. <b>It may
51 * change in the future.</b>
52 */
53 public NumericTRId getNumericResultId() {
54 return testResultId;
55 }
56
57 /** Sets the test result id as a numeric test result id.*/
58 public void setNumericResultId(NumericTRId testResultId) {
59 this.testResultId = testResultId;
60 }
61
62 /** Sets the list of listeners.*/
63 public void setListeners(List<TestResultListener> listeners) {
64 this.listeners = listeners;
65 }
66
67 public void addResultPart(TestResultPart part) {
68 if (currentPath == null) {
69 throw new SlcException("No current path set.");
70 }
71 PartSubList subList = resultParts.get(currentPath);
72 if (subList == null) {
73 subList = new PartSubList();
74 resultParts.put(currentPath, subList);
75 }
76 subList.getParts().add(part);
77
78 // notify listeners
79 synchronized (listeners) {
80 for (TestResultListener listener : listeners) {
81 listener.resultPartAdded(this, part);
82 }
83 }
84 }
85
86 public void notifyCurrentPath(StructureRegistry registry, StructurePath path) {
87 currentPath = (TreeSPath) path;
88 this.registry = registry;
89 }
90
91 /** Gets the current path.*/
92 public TreeSPath getCurrentPath() {
93 return currentPath;
94 }
95
96 /** Gets all the results structured as a map of <code>PartSubList<code>s. */
97 public SortedMap<TreeSPath, PartSubList> getResultParts() {
98 return resultParts;
99 }
100
101 /** Used by ORM systems. */
102 void setResultParts(SortedMap<TreeSPath, PartSubList> resultParts) {
103 this.resultParts = resultParts;
104 }
105
106 public void close() {
107 if (isClosed) {
108 throw new SlcException("Test Result #" + getTestResultId()
109 + " alredy closed.");
110 }
111 closeDate = new Date();
112
113 synchronized (listeners) {
114 for (TestResultListener listener : listeners) {
115 listener.close(this);
116 }
117 listeners.clear();
118 }
119 isClosed = true;
120
121 log.info("Test Result #" + getTestResultId() + " closed.");
122 }
123
124 Long getTid() {
125 return tid;
126 }
127
128 void setTid(Long tid) {
129 this.tid = tid;
130 }
131
132 /** Gets the related registry (can be null).*/
133 public StructureRegistry getRegistry() {
134 return registry;
135 }
136
137 /** Sets the related registry.*/
138 public void setRegistry(StructureRegistry registry) {
139 this.registry = registry;
140 }
141
142 public Date getCloseDate() {
143 return closeDate;
144 }
145
146 /** Sets the close date (for ORM)*/
147 public void setCloseDate(Date closeDate) {
148 this.closeDate = closeDate;
149 }
150
151 }