X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=org.argeo.slc%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fcore%2Ftest%2Ftree%2FTreeTestResultPersister.java;h=ca92b49084e9f045fcc5ed824e2d1b0987a2b7d5;hb=b5c4e0c9c2fcf788a56d6ce72989fe15182e057d;hp=17cf66499c463af4174eeb03ea748fec88f66b07;hpb=9eb5d8493deed66f11248550fe5ec2202151846f;p=gpl%2Fargeo-slc.git diff --git a/org.argeo.slc/src/main/java/org/argeo/slc/core/test/tree/TreeTestResultPersister.java b/org.argeo.slc/src/main/java/org/argeo/slc/core/test/tree/TreeTestResultPersister.java index 17cf66499..ca92b4908 100644 --- a/org.argeo.slc/src/main/java/org/argeo/slc/core/test/tree/TreeTestResultPersister.java +++ b/org.argeo.slc/src/main/java/org/argeo/slc/core/test/tree/TreeTestResultPersister.java @@ -3,36 +3,55 @@ package org.argeo.slc.core.test.tree; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.argeo.slc.core.structure.SimpleSElement; +import org.argeo.slc.core.structure.StructureRegistry; +import org.argeo.slc.core.structure.tree.TreeSPath; +import org.argeo.slc.core.structure.tree.TreeSRegistry; +import org.argeo.slc.dao.structure.tree.TreeSPathDao; +import org.argeo.slc.dao.structure.tree.TreeSRegistryDao; import org.argeo.slc.dao.test.TestResultDao; +/** + * Listener persisting tree-based results. + * + * @see TreeTestResult + */ public class TreeTestResultPersister extends AsynchronousTreeTestResultListener { private static Log log = LogFactory.getLog(TreeTestResultPersister.class); private TestResultDao testResultDao; + private TreeSPathDao treeSPathDao; + private TreeSRegistryDao treeSRegistryDao; @Override protected void resultPartAdded(PartStruct partStruct) { try { TreeTestResult persistedResult = (TreeTestResult) testResultDao .getTestResult(partStruct.resultId); + + TreeSPath path = treeSPathDao.getOrCreate(partStruct.path); + + StructureRegistry localRegistry = partStruct.result.getRegistry(); + TreeSRegistry registry = getOrCreateTreeSRegistry(path); + syncPath(registry, localRegistry, path); + if (persistedResult == null) { persistedResult = new TreeTestResult(); persistedResult.setNumericResultId(partStruct.resultId); PartSubList subList = new PartSubList(); subList.getParts().add(partStruct.part); - persistedResult.getResultParts().put(partStruct.path, subList); + persistedResult.getResultParts().put(path, subList); testResultDao.create(persistedResult); } else { - PartSubList subList = persistedResult.getResultParts().get( - partStruct.path); + PartSubList subList = persistedResult.getResultParts() + .get(path); if (subList == null) { subList = new PartSubList(); - persistedResult.getResultParts().put(partStruct.path, - subList); + persistedResult.getResultParts().put(path, subList); } - persistedResult.getResultParts().get(partStruct.path) - .getParts().add(partStruct.part); + persistedResult.getResultParts().get(path).getParts().add( + partStruct.part); if (log.isTraceEnabled()) { log.trace("ResultId:" + persistedResult.getTestResultId()); @@ -49,8 +68,63 @@ public class TreeTestResultPersister extends AsynchronousTreeTestResultListener } } + @Override + protected void postClose(TreeTestResult testResult) { + TreeTestResult persistedResult = (TreeTestResult) testResultDao + .getTestResult(testResult.getTestResultId()); + + if (persistedResult != null) { + persistedResult.setCloseDate(testResult.getCloseDate()); + testResultDao.update(persistedResult); + } + if (log.isDebugEnabled()) + log.debug("Closed result persister for result " + + testResult.getNumericResultId()); + } + + private TreeSRegistry getOrCreateTreeSRegistry(TreeSPath path) { + TreeSRegistry registry = treeSRegistryDao.getTreeSRegistry(path); + if (registry == null) { + registry = new TreeSRegistry(); + TreeSPath root = treeSPathDao.getOrCreate(path.getRoot()); + registry.setRoot(root); + treeSRegistryDao.create(registry); + return treeSRegistryDao.getTreeSRegistry(path); + } else { + return registry; + } + } + + /** Sets the DAO to use in order to persist the results. */ public void setTestResultDao(TestResultDao testResultDao) { this.testResultDao = testResultDao; } + /** Sets the tree structure path DAO. */ + public void setTreeSPathDao(TreeSPathDao treeSPathDao) { + this.treeSPathDao = treeSPathDao; + } + + /** Sets the tree structure registry DAO. */ + public void setTreeSRegistryDao(TreeSRegistryDao treeSRegistryDao) { + this.treeSRegistryDao = treeSRegistryDao; + } + + private void syncPath(TreeSRegistry registry, + StructureRegistry localRegistry, TreeSPath path) { + if (path.getParent() != null) { + TreeSPath parent = treeSPathDao.getOrCreate(path.getParent()); + syncPath(registry, localRegistry, parent); + } + + if (registry.getElement(path) == null) { + if (localRegistry != null) { + registry.register(path, localRegistry.getElement(path)); + } else { + registry.register(path, new SimpleSElement(path.getName())); + } + treeSRegistryDao.update(registry); + } + + } }