]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/tree/TreeTestResultPersister.java
Structure element update
[gpl/argeo-slc.git] / org.argeo.slc.core / 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.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;
14
15 /**
16 * Listener persisting tree-based results.
17 *
18 * @see TreeTestResult
19 */
20 public class TreeTestResultPersister extends AsynchronousTreeTestResultListener {
21 private static Log log = LogFactory.getLog(TreeTestResultPersister.class);
22
23 private TestResultDao testResultDao;
24 private TreeSPathDao treeSPathDao;
25 private TreeSRegistryDao treeSRegistryDao;
26
27 @Override
28 protected void resultPartAdded(PartStruct partStruct) {
29 try {
30 TreeTestResult persistedResult = (TreeTestResult) testResultDao
31 .getTestResult(partStruct.resultId);
32
33 TreeSPath path = treeSPathDao.getOrCreate(partStruct.path);
34
35 StructureRegistry localRegistry = partStruct.result.getRegistry();
36 TreeSRegistry registry = getOrCreateTreeSRegistry(path);
37 syncPath(registry, localRegistry, path);
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.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();
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 (path.getParent() != null) {
117 TreeSPath parent = treeSPathDao.getOrCreate(path.getParent());
118 syncPath(registry, localRegistry, parent);
119 }
120
121 if (registry.getElement(path) == null) {
122 if (localRegistry != null) {
123 registry.register(path, localRegistry.getElement(path));
124 } else {
125 registry.register(path, new SimpleSElement(path.getName()));
126 }
127 treeSRegistryDao.update(registry);
128 } else {
129 if (localRegistry != null) {
130 StructureElement sElement = localRegistry.getElement(path);
131 if (sElement != null) {
132 registry.register(path, sElement);
133 treeSRegistryDao.update(registry);
134 }
135 }
136 }
137
138 }
139 }