]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc/src/main/java/org/argeo/slc/core/test/tree/TreeTestResultPersister.java
Manage with Maven
[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
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);
44
45 testResultDao.create(persistedResult);
46 } else {
47 PartSubList subList = persistedResult.getResultParts()
48 .get(path);
49 if (subList == null) {
50 subList = new PartSubList();
51 persistedResult.getResultParts().put(path, subList);
52 }
53 persistedResult.getResultParts().get(path).getParts().add(
54 partStruct.part);
55
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);
62 }
63 testResultDao.update(persistedResult);
64 }
65 } catch (Exception e) {
66 log.error("Could not persist part for result #"
67 + partStruct.resultId, e);
68 }
69 }
70
71 @Override
72 protected void postClose(TreeTestResult testResult) {
73 TreeTestResult persistedResult = (TreeTestResult) testResultDao
74 .getTestResult(testResult.getTestResultId());
75
76 if (persistedResult != null) {
77 persistedResult.setCloseDate(testResult.getCloseDate());
78 testResultDao.update(persistedResult);
79 }
80 if (log.isDebugEnabled())
81 log.debug("Closed result persister for result "
82 + testResult.getNumericResultId());
83 }
84
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);
93 } else {
94 return registry;
95 }
96 }
97
98 /** Sets the DAO to use in order to persist the results. */
99 public void setTestResultDao(TestResultDao testResultDao) {
100 this.testResultDao = testResultDao;
101 }
102
103 /** Sets the tree structure path DAO. */
104 public void setTreeSPathDao(TreeSPathDao treeSPathDao) {
105 this.treeSPathDao = treeSPathDao;
106 }
107
108 /** Sets the tree structure registry DAO. */
109 public void setTreeSRegistryDao(TreeSRegistryDao treeSRegistryDao) {
110 this.treeSRegistryDao = treeSRegistryDao;
111 }
112
113 private void syncPath(TreeSRegistry registry,
114 StructureRegistry localRegistry, TreeSPath path) {
115 if (path.getParent() != null) {
116 TreeSPath parent = treeSPathDao.getOrCreate(path.getParent());
117 syncPath(registry, localRegistry, parent);
118 }
119
120 if (registry.getElement(path) == null) {
121 if (localRegistry != null) {
122 registry.register(path, localRegistry.getElement(path));
123 } else {
124 registry.register(path, new SimpleSElement(path.getName()));
125 }
126 treeSRegistryDao.update(registry);
127 }
128
129 }
130 }