1 package org
.argeo
.slc
.core
.test
.tree
;
3 import org
.apache
.commons
.logging
.Log
;
4 import org
.apache
.commons
.logging
.LogFactory
;
6 import org
.argeo
.slc
.core
.structure
.SimpleSElement
;
7 import org
.argeo
.slc
.core
.structure
.StructureRegistry
;
8 import org
.argeo
.slc
.core
.structure
.tree
.TreeSPath
;
9 import org
.argeo
.slc
.core
.structure
.tree
.TreeSRegistry
;
10 import org
.argeo
.slc
.dao
.structure
.tree
.TreeSPathDao
;
11 import org
.argeo
.slc
.dao
.structure
.tree
.TreeSRegistryDao
;
12 import org
.argeo
.slc
.dao
.test
.TestResultDao
;
15 * Listener persisting tree-based results.
19 public class TreeTestResultPersister
extends AsynchronousTreeTestResultListener
{
20 private static Log log
= LogFactory
.getLog(TreeTestResultPersister
.class);
22 private TestResultDao testResultDao
;
23 private TreeSPathDao treeSPathDao
;
24 private TreeSRegistryDao treeSRegistryDao
;
27 protected void resultPartAdded(PartStruct partStruct
) {
29 TreeTestResult persistedResult
= (TreeTestResult
) testResultDao
30 .getTestResult(partStruct
.resultId
);
32 TreeSPath path
= treeSPathDao
.getOrCreate(partStruct
.path
);
34 StructureRegistry localRegistry
= partStruct
.result
.getRegistry();
35 TreeSRegistry registry
= getOrCreateTreeSRegistry(path
);
36 syncPath(registry
, localRegistry
, path
);
38 if (persistedResult
== null) {
39 persistedResult
= new TreeTestResult();
40 persistedResult
.setNumericResultId(partStruct
.resultId
);
41 PartSubList subList
= new PartSubList();
42 subList
.getParts().add(partStruct
.part
);
43 persistedResult
.getResultParts().put(path
, subList
);
45 testResultDao
.create(persistedResult
);
47 PartSubList subList
= persistedResult
.getResultParts()
49 if (subList
== null) {
50 subList
= new PartSubList();
51 persistedResult
.getResultParts().put(path
, subList
);
53 persistedResult
.getResultParts().get(path
).getParts().add(
56 if (log
.isTraceEnabled()) {
57 log
.trace("ResultId:" + persistedResult
.getTestResultId());
58 log
.trace("ResultParts size:"
59 + persistedResult
.getResultParts().size());
60 log
.trace("Sublist size:" + subList
.getParts().size());
61 log
.trace("Part: " + partStruct
.part
);
63 testResultDao
.update(persistedResult
);
65 } catch (Exception e
) {
66 log
.error("Could not persist part for result #"
67 + partStruct
.resultId
, e
);
72 protected void postClose(TreeTestResult testResult
) {
73 TreeTestResult persistedResult
= (TreeTestResult
) testResultDao
74 .getTestResult(testResult
.getTestResultId());
76 if (persistedResult
!= null) {
77 persistedResult
.setCloseDate(testResult
.getCloseDate());
78 testResultDao
.update(persistedResult
);
80 if (log
.isDebugEnabled())
81 log
.debug("Closed result persister for result "
82 + testResult
.getNumericResultId());
85 private TreeSRegistry
getOrCreateTreeSRegistry(TreeSPath path
) {
86 TreeSRegistry registry
= treeSRegistryDao
.getTreeSRegistry(path
);
87 if (registry
== null) {
88 registry
= new TreeSRegistry();
89 TreeSPath root
= treeSPathDao
.getOrCreate(path
.getRoot());
90 registry
.setRoot(root
);
91 treeSRegistryDao
.create(registry
);
92 return treeSRegistryDao
.getTreeSRegistry(path
);
98 /** Sets the DAO to use in order to persist the results. */
99 public void setTestResultDao(TestResultDao testResultDao
) {
100 this.testResultDao
= testResultDao
;
103 /** Sets the tree structure path DAO. */
104 public void setTreeSPathDao(TreeSPathDao treeSPathDao
) {
105 this.treeSPathDao
= treeSPathDao
;
108 /** Sets the tree structure registry DAO. */
109 public void setTreeSRegistryDao(TreeSRegistryDao treeSRegistryDao
) {
110 this.treeSRegistryDao
= treeSRegistryDao
;
113 private void syncPath(TreeSRegistry registry
,
114 StructureRegistry localRegistry
, TreeSPath path
) {
115 if (registry
.getElement(path
) == null) {
116 if (localRegistry
!= null) {
117 registry
.register(path
, localRegistry
.getElement(path
));
119 registry
.register(path
, new SimpleSElement(path
.getName()));
121 treeSRegistryDao
.update(registry
);
124 if (path
.getParent() != null) {
125 TreeSPath parent
= treeSPathDao
.getOrCreate(path
.getParent());
126 syncPath(registry
, localRegistry
, parent
);