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
.StructureElement
;
8 import org
.argeo
.slc
.core
.structure
.StructureRegistry
;
9 import org
.argeo
.slc
.core
.structure
.tree
.TreeSPath
;
10 import org
.argeo
.slc
.core
.structure
.tree
.TreeSRegistry
;
11 import org
.argeo
.slc
.dao
.structure
.tree
.TreeSPathDao
;
12 import org
.argeo
.slc
.dao
.structure
.tree
.TreeSRegistryDao
;
13 import org
.argeo
.slc
.dao
.test
.TestResultDao
;
16 * Listener persisting tree-based results.
20 public class TreeTestResultPersister
extends AsynchronousTreeTestResultListener
{
21 private static Log log
= LogFactory
.getLog(TreeTestResultPersister
.class);
23 private TestResultDao testResultDao
;
24 private TreeSPathDao treeSPathDao
;
25 private TreeSRegistryDao treeSRegistryDao
;
28 protected void resultPartAdded(PartStruct partStruct
) {
30 TreeTestResult persistedResult
= (TreeTestResult
) testResultDao
31 .getTestResult(partStruct
.resultId
);
33 TreeSPath path
= treeSPathDao
.getOrCreate(partStruct
.path
);
35 StructureRegistry localRegistry
= partStruct
.result
.getRegistry();
36 TreeSRegistry registry
= getOrCreateTreeSRegistry(path
);
37 syncPath(registry
, localRegistry
, path
);
39 if (persistedResult
== null) {
40 persistedResult
= new TreeTestResult();
41 persistedResult
.setNumericResultId(partStruct
.resultId
);
42 PartSubList subList
= new PartSubList();
43 subList
.getParts().add(partStruct
.part
);
44 persistedResult
.getResultParts().put(path
, subList
);
46 testResultDao
.create(persistedResult
);
48 PartSubList subList
= persistedResult
.getResultParts()
50 if (subList
== null) {
51 subList
= new PartSubList();
52 persistedResult
.getResultParts().put(path
, subList
);
54 persistedResult
.getResultParts().get(path
).getParts().add(
57 if (log
.isTraceEnabled()) {
58 log
.trace("ResultId:" + persistedResult
.getTestResultId());
59 log
.trace("ResultParts size:"
60 + persistedResult
.getResultParts().size());
61 log
.trace("Sublist size:" + subList
.getParts().size());
62 log
.trace("Part: " + partStruct
.part
);
64 testResultDao
.update(persistedResult
);
66 } catch (Exception e
) {
67 log
.error("Could not persist part for result #"
68 + partStruct
.resultId
, e
);
73 protected void postClose(TreeTestResult testResult
) {
74 TreeTestResult persistedResult
= (TreeTestResult
) testResultDao
75 .getTestResult(testResult
.getTestResultId());
77 if (persistedResult
!= null) {
78 persistedResult
.setCloseDate(testResult
.getCloseDate());
79 testResultDao
.update(persistedResult
);
81 if (log
.isDebugEnabled())
82 log
.debug("Closed result persister for result "
83 + testResult
.getNumericResultId());
86 private TreeSRegistry
getOrCreateTreeSRegistry(TreeSPath path
) {
87 TreeSRegistry registry
= treeSRegistryDao
.getActiveTreeSRegistry();
88 if (registry
== null) {
89 registry
= new TreeSRegistry();
90 TreeSPath root
= treeSPathDao
.getOrCreate(path
.getRoot());
91 registry
.setStatus(TreeSRegistry
.STATUS_ACTIVE
);
92 treeSRegistryDao
.create(registry
);
93 return treeSRegistryDao
.getActiveTreeSRegistry();
99 /** Sets the DAO to use in order to persist the results. */
100 public void setTestResultDao(TestResultDao testResultDao
) {
101 this.testResultDao
= testResultDao
;
104 /** Sets the tree structure path DAO. */
105 public void setTreeSPathDao(TreeSPathDao treeSPathDao
) {
106 this.treeSPathDao
= treeSPathDao
;
109 /** Sets the tree structure registry DAO. */
110 public void setTreeSRegistryDao(TreeSRegistryDao treeSRegistryDao
) {
111 this.treeSRegistryDao
= treeSRegistryDao
;
114 private void syncPath(TreeSRegistry registry
,
115 StructureRegistry localRegistry
, TreeSPath path
) {
116 if (path
.getParent() != null) {
117 TreeSPath parent
= treeSPathDao
.getOrCreate(path
.getParent());
118 syncPath(registry
, localRegistry
, parent
);
121 if (registry
.getElement(path
) == null) {
122 if (localRegistry
!= null) {
123 registry
.register(path
, localRegistry
.getElement(path
));
125 registry
.register(path
, new SimpleSElement(path
.getName()));
127 treeSRegistryDao
.update(registry
);
129 if (localRegistry
!= null) {
130 StructureElement sElement
= localRegistry
.getElement(path
);
131 if (sElement
!= null) {
132 registry
.register(path
, sElement
);
133 treeSRegistryDao
.update(registry
);