]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc/src/main/java/org/argeo/slc/core/test/tree/TreeTestResultPersister.java
Improve reporting
[gpl/argeo-slc.git] / org.argeo.slc / src / main / java / org / argeo / slc / core / test / tree / TreeTestResultPersister.java
1 package org.argeo.slc.core.test.tree;
2
3 import org.apache.commons.logging.Log;
4 import org.apache.commons.logging.LogFactory;
5
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;
13
14 /**
15 * Listener persisting tree-based results.
16 *
17 * @see TreeTestResult
18 */
19 public class TreeTestResultPersister extends AsynchronousTreeTestResultListener {
20 private static Log log = LogFactory.getLog(TreeTestResultPersister.class);
21
22 private TestResultDao testResultDao;
23 private TreeSPathDao treeSPathDao;
24 private TreeSRegistryDao treeSRegistryDao;
25
26 @Override
27 protected void resultPartAdded(PartStruct partStruct) {
28 try {
29 TreeTestResult persistedResult = (TreeTestResult) testResultDao
30 .getTestResult(partStruct.resultId);
31
32 TreeSPath path = treeSPathDao.getOrCreate(partStruct.path);
33
34 StructureRegistry localRegistry = partStruct.result.getRegistry();
35 TreeSRegistry registry = getOrCreateTreeSRegistry(path);
36 syncPath(registry, localRegistry, path);
37 treeSRegistryDao.update(registry);
38
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);
45
46 testResultDao.create(persistedResult);
47 } else {
48 PartSubList subList = persistedResult.getResultParts()
49 .get(path);
50 if (subList == null) {
51 subList = new PartSubList();
52 persistedResult.getResultParts().put(path, subList);
53 }
54 persistedResult.getResultParts().get(path).getParts().add(
55 partStruct.part);
56
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);
63 }
64 testResultDao.update(persistedResult);
65 }
66 } catch (Exception e) {
67 log.error("Could not persist part for result #"
68 + partStruct.resultId, e);
69 }
70 }
71
72 @Override
73 protected void postClose(TreeTestResult testResult) {
74 TreeTestResult persistedResult = (TreeTestResult) testResultDao
75 .getTestResult(testResult.getTestResultId());
76
77 if (persistedResult != null) {
78 persistedResult.setCloseDate(testResult.getCloseDate());
79 testResultDao.update(persistedResult);
80 }
81 if (log.isDebugEnabled())
82 log.debug("Closed result persister for result "
83 + testResult.getNumericResultId());
84 }
85
86 private TreeSRegistry getOrCreateTreeSRegistry(TreeSPath path) {
87 TreeSRegistry registry = treeSRegistryDao.getTreeSRegistry(path);
88 if (registry == null) {
89 registry = new TreeSRegistry();
90 TreeSPath root = treeSPathDao.getOrCreate(path.getRoot());
91 registry.setRoot(root);
92 treeSRegistryDao.create(registry);
93 return treeSRegistryDao.getTreeSRegistry(path);
94 } else {
95 return registry;
96 }
97 }
98
99 /** Sets the DAO to use in order to persist the results. */
100 public void setTestResultDao(TestResultDao testResultDao) {
101 this.testResultDao = testResultDao;
102 }
103
104 /** Sets the tree structure path DAO. */
105 public void setTreeSPathDao(TreeSPathDao treeSPathDao) {
106 this.treeSPathDao = treeSPathDao;
107 }
108
109 /** Sets the tree structure registry DAO. */
110 public void setTreeSRegistryDao(TreeSRegistryDao treeSRegistryDao) {
111 this.treeSRegistryDao = treeSRegistryDao;
112 }
113
114 private void syncPath(TreeSRegistry registry,
115 StructureRegistry localRegistry, TreeSPath path) {
116 if (registry.getElement(path) == null) {
117 if (localRegistry != null) {
118 registry.register(path, localRegistry.getElement(path));
119 } else {
120 registry.register(path, new SimpleSElement(path.getName()));
121 }
122 }
123
124 if (path.getParent() != null) {
125 TreeSPath parent = treeSPathDao.getOrCreate(path.getParent());
126 syncPath(registry, localRegistry, parent);
127 }
128 }
129 }