From: Mathieu Baudier Date: Sun, 18 Apr 2010 15:28:17 +0000 (+0000) Subject: Take over JCR implementation X-Git-Tag: argeo-slc-2.1.7~1362 X-Git-Url: http://git.argeo.org/?a=commitdiff_plain;h=b554c0910f6ddce935ceea5f99d399fe23af6f58;p=gpl%2Fargeo-slc.git Take over JCR implementation git-svn-id: https://svn.argeo.org/slc/trunk@3475 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc --- diff --git a/runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/dao/AbstractSlcJcrDao.java b/runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/dao/AbstractSlcJcrDao.java index 328ec10d5..2bf9cc64c 100644 --- a/runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/dao/AbstractSlcJcrDao.java +++ b/runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/dao/AbstractSlcJcrDao.java @@ -4,24 +4,41 @@ import java.util.Calendar; import java.util.Date; import java.util.GregorianCalendar; +import javax.jcr.RepositoryException; import javax.jcr.Session; +import javax.jcr.Workspace; +import javax.jcr.query.QueryManager; import org.argeo.jcr.JcrUtils; +import org.argeo.jcr.NodeMapper; import org.argeo.jcr.NodeMapperProvider; +import org.argeo.slc.SlcException; import org.argeo.slc.runtime.SlcAgentDescriptor; import org.argeo.slc.test.TestResult; - +import org.argeo.slc.test.TestRunDescriptor; public abstract class AbstractSlcJcrDao { private Session session; - //We inject the nodeMapperProvider that define a default node mapper as an + protected Workspace workspace; + protected QueryManager queryManager; + protected NodeMapper nodeMapper; + + // We inject the nodeMapperProvider that define a default node mapper as an // entry point of the NodeMapper private NodeMapperProvider nodeMapperProvider; - - + public void init() { + try { + workspace = getSession().getWorkspace(); + queryManager = workspace.getQueryManager(); + nodeMapper = getNodeMapperProvider().findNodeMapper(null); + } catch (RepositoryException e) { + throw new SlcException("Cannot initialize DAO", e); + } + } + public void setSession(Session session) { this.session = session; } @@ -29,12 +46,12 @@ public abstract class AbstractSlcJcrDao { protected Session getSession() { return session; } - + // IoC public void setNodeMapperProvider(NodeMapperProvider nodeMapperProvider) { this.nodeMapperProvider = nodeMapperProvider; } - + // TODO : define a strategy to define basePathes protected String basePath(TestResult testResult) { Calendar cal = new GregorianCalendar(); @@ -42,7 +59,7 @@ public abstract class AbstractSlcJcrDao { // cal.setTime(slcExecution.getStartDate()); return "/slc/testresult/" + JcrUtils.dateAsPath(cal) + "testresult"; } - + protected String basePath(SlcAgentDescriptor slcAgentDescriptor) { Calendar cal = new GregorianCalendar(); cal.setTime(new Date()); @@ -51,9 +68,16 @@ public abstract class AbstractSlcJcrDao { + JcrUtils.hostAsPath(slcAgentDescriptor.getHost() + "/agent"); } - protected NodeMapperProvider getNodeMapperProvider(){ + protected String basePath(TestRunDescriptor testRunDescriptor) { + Calendar cal = new GregorianCalendar(); + cal.setTime(new Date()); + // cal.setTime(slcExecution.getStartDate()); + return "/slc/testruns/" + testRunDescriptor.getTestRunUuid() + + "/testrun"; + } + + protected NodeMapperProvider getNodeMapperProvider() { return this.nodeMapperProvider; } - } diff --git a/runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/dao/SlcAgentDescriptorDaoJcr.java b/runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/dao/SlcAgentDescriptorDaoJcr.java index 91a14c084..14c838aa7 100644 --- a/runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/dao/SlcAgentDescriptorDaoJcr.java +++ b/runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/dao/SlcAgentDescriptorDaoJcr.java @@ -1,22 +1,15 @@ package org.argeo.slc.jcr.dao; import java.util.ArrayList; -import java.util.Calendar; -import java.util.Date; -import java.util.GregorianCalendar; import java.util.List; import javax.jcr.Node; import javax.jcr.NodeIterator; -import javax.jcr.RepositoryException; -import javax.jcr.Workspace; import javax.jcr.query.Query; -import javax.jcr.query.QueryManager; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.argeo.jcr.JcrUtils; -import org.argeo.jcr.NodeMapper; import org.argeo.slc.SlcException; import org.argeo.slc.dao.runtime.SlcAgentDescriptorDao; import org.argeo.slc.runtime.SlcAgentDescriptor; @@ -26,23 +19,7 @@ public class SlcAgentDescriptorDaoJcr extends AbstractSlcJcrDao implements private final static Log log = LogFactory .getLog(SlcAgentDescriptorDaoJcr.class); - private Workspace workspace; - private QueryManager queryManager; - private NodeMapper nodeMapper; - - public void init() { - try { - workspace = getSession().getWorkspace(); - queryManager = workspace.getQueryManager(); - nodeMapper = getNodeMapperProvider().findNodeMapper(null); - } catch (RepositoryException e) { - throw new SlcException("Cannot initialize DAO", e); - } - } - public void create(SlcAgentDescriptor slcAgentDescriptor) { - if (log.isDebugEnabled()) - log.debug("in SlcAgentDescriptorDaoJcr.create"); try { nodeMapper.save(getSession(), basePath(slcAgentDescriptor), slcAgentDescriptor); @@ -67,14 +44,24 @@ public class SlcAgentDescriptorDaoJcr extends AbstractSlcJcrDao implements } public void delete(SlcAgentDescriptor slcAgentDescriptor) { - if (log.isDebugEnabled()) - log.debug("slcAgentDescriptorDaoJcr.delete(slcAgentDescriptor)"); + try { + String queryString = "//agent[@uuid='" + + slcAgentDescriptor.getUuid() + "']"; + Query query = queryManager.createQuery(queryString, Query.XPATH); + Node node = JcrUtils.querySingleNode(query); + if (node != null) { + node.remove(); + getSession().save(); + } else + log.warn("No node found for agent descriptor: " + + slcAgentDescriptor); + } catch (Exception e) { + throw new SlcException("Cannot delete " + slcAgentDescriptor, e); + } } public void delete(String agentId) { - if (log.isDebugEnabled()) - log.debug("slcAgentDescriptorDaoJcr.delete(agentID)"); try { // TODO: optimize query String queryString = "//agent[@uuid='" + agentId + "']"; @@ -89,9 +76,6 @@ public class SlcAgentDescriptorDaoJcr extends AbstractSlcJcrDao implements } public List listSlcAgentDescriptors() { - if (log.isDebugEnabled()) - log.debug("slcAgentDescriptorDaoJcr.delete(agentID)"); - try { String queryString = "//agent"; Query query = queryManager.createQuery(queryString, Query.XPATH); @@ -101,7 +85,7 @@ public class SlcAgentDescriptorDaoJcr extends AbstractSlcJcrDao implements NodeIterator ni = query.execute().getNodes(); while (ni.hasNext()) { Node curNode = (Node) ni.next(); - JcrUtils.debug(curNode); + // JcrUtils.debug(curNode); listSad.add((SlcAgentDescriptor) nodeMapper.load(curNode)); } diff --git a/runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/dao/SlcExecutionDaoJcr.java b/runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/dao/SlcExecutionDaoJcr.java index d2626f69a..c64139eae 100644 --- a/runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/dao/SlcExecutionDaoJcr.java +++ b/runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/dao/SlcExecutionDaoJcr.java @@ -8,40 +8,19 @@ import java.util.List; import javax.jcr.Node; import javax.jcr.NodeIterator; -import javax.jcr.RepositoryException; -import javax.jcr.Workspace; import javax.jcr.query.Query; -import javax.jcr.query.QueryManager; import javax.jcr.query.QueryResult; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import org.argeo.jcr.JcrUtils; -import org.argeo.jcr.NodeMapper; -import org.argeo.jcr.NodeMapperProvider; import org.argeo.slc.SlcException; import org.argeo.slc.dao.process.SlcExecutionDao; import org.argeo.slc.process.SlcExecution; import org.argeo.slc.process.SlcExecutionStep; -public class SlcExecutionDaoJcr extends AbstractSlcJcrDao implements SlcExecutionDao { - private final static Log log = LogFactory.getLog(SlcExecutionDaoJcr.class); - private Workspace workspace; - private QueryManager queryManager; - - private NodeMapper nodeMapper; - private NodeMapperProvider nodeMapperProvider; - - public void init() { - try { - workspace = getSession().getWorkspace(); - queryManager = workspace.getQueryManager(); - nodeMapper = nodeMapperProvider.findNodeMapper(null); - - } catch (RepositoryException e) { - throw new SlcException("Cannot initialize DAO", e); - } - } +public class SlcExecutionDaoJcr extends AbstractSlcJcrDao implements + SlcExecutionDao { + // private final static Log log = + // LogFactory.getLog(SlcExecutionDaoJcr.class); public void addSteps(String slcExecutionId, List additionalSteps) { @@ -53,15 +32,12 @@ public class SlcExecutionDaoJcr extends AbstractSlcJcrDao implements SlcExecutio } public void create(SlcExecution slcExecution) { - if (log.isDebugEnabled()) - log.debug("create"); - try { - nodeMapper.save(getSession(), basePath(slcExecution), - slcExecution); + nodeMapper.save(getSession(), basePath(slcExecution), slcExecution); getSession().save(); } catch (Exception e) { - throw new SlcException("Cannot create slcExecution" + slcExecution, e); + throw new SlcException("Cannot create slcExecution" + slcExecution, + e); } } @@ -70,14 +46,10 @@ public class SlcExecutionDaoJcr extends AbstractSlcJcrDao implements SlcExecutio cal.setTime(new Date()); // cal.setTime(slcExecution.getStartDate()); return "/slc/processes/" + JcrUtils.hostAsPath(slcExecution.getHost()) - + '/' + JcrUtils.dateAsPath(cal) + "process"; + + '/' + JcrUtils.dateAsPath(cal) + "process"; } - public SlcExecution getSlcExecution(String uuid) { - if (log.isDebugEnabled()) - log.debug("getSlcExecution"); - try { // TODO: optimize query String queryString = "//process[@uuid='" + uuid + "']"; @@ -92,9 +64,6 @@ public class SlcExecutionDaoJcr extends AbstractSlcJcrDao implements SlcExecutio } public List listSlcExecutions() { - if (log.isDebugEnabled()) - log.debug("listSlcExecutions"); - List res = new ArrayList(); // TODO: optimize query String queryString = "//process"; @@ -119,9 +88,6 @@ public class SlcExecutionDaoJcr extends AbstractSlcJcrDao implements SlcExecutio } public void update(SlcExecution slcExecution) { - if (log.isDebugEnabled()) - log.debug("update"); - // TODO: optimize query String queryString = "//process[@uuid='" + slcExecution.getUuid() + "']"; @@ -135,8 +101,4 @@ public class SlcExecutionDaoJcr extends AbstractSlcJcrDao implements SlcExecutio } } - //IoC - public void setNodeMapperProvider(NodeMapperProvider nodeMapperProvider) { - this.nodeMapperProvider = nodeMapperProvider; - } } diff --git a/runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/dao/TestRunDescriptorDaoJcr.java b/runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/dao/TestRunDescriptorDaoJcr.java new file mode 100644 index 000000000..44db33def --- /dev/null +++ b/runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/dao/TestRunDescriptorDaoJcr.java @@ -0,0 +1,38 @@ +package org.argeo.slc.jcr.dao; + +import javax.jcr.Node; +import javax.jcr.query.Query; + +import org.argeo.jcr.JcrUtils; +import org.argeo.slc.SlcException; +import org.argeo.slc.dao.test.TestRunDescriptorDao; +import org.argeo.slc.test.TestRunDescriptor; + +public class TestRunDescriptorDaoJcr extends AbstractSlcJcrDao implements + TestRunDescriptorDao { + + public TestRunDescriptor getTestRunDescriptor(String id) { + try { + // TODO: optimize query + String queryString = "//testrun[@testRunUuid='" + id + "']"; + Query query = queryManager.createQuery(queryString, Query.XPATH); + Node node = JcrUtils.querySingleNode(query); + if (node == null) + return null; + return (TestRunDescriptor) nodeMapper.load(node); + } catch (Exception e) { + throw new SlcException("Cannot load test run descriptor" + id, e); + } + } + + public void saveOrUpdate(TestRunDescriptor testRunDescriptor) { + try { + nodeMapper.save(getSession(), basePath(testRunDescriptor), + testRunDescriptor); + getSession().save(); + } catch (Exception e) { + throw new SlcException("Cannot import " + testRunDescriptor, e); + } + } + +} diff --git a/runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/dao/TreeTestResultCollectionDaoJcr.java b/runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/dao/TreeTestResultCollectionDaoJcr.java index 33e4ffbf5..6a9824dfb 100644 --- a/runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/dao/TreeTestResultCollectionDaoJcr.java +++ b/runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/dao/TreeTestResultCollectionDaoJcr.java @@ -1,20 +1,19 @@ package org.argeo.slc.jcr.dao; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.SortedSet; +import java.util.TreeSet; import javax.jcr.Node; import javax.jcr.NodeIterator; -import javax.jcr.RepositoryException; -import javax.jcr.Workspace; import javax.jcr.query.Query; -import javax.jcr.query.QueryManager; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.argeo.jcr.JcrUtils; -import org.argeo.jcr.NodeMapper; import org.argeo.slc.SlcException; import org.argeo.slc.core.test.tree.ResultAttributes; import org.argeo.slc.core.test.tree.TreeTestResult; @@ -32,20 +31,6 @@ public class TreeTestResultCollectionDaoJcr extends AbstractSlcJcrDao implements private final static Log log = LogFactory .getLog(TreeTestResultCollectionDaoJcr.class); - private Workspace workspace; - private QueryManager queryManager; - private NodeMapper nodeMapper; - - public void init() { - try { - workspace = getSession().getWorkspace(); - queryManager = workspace.getQueryManager(); - nodeMapper = getNodeMapperProvider().findNodeMapper(null); - } catch (RepositoryException e) { - throw new SlcException("Cannot initialize DAO", e); - } - } - public void create(TreeTestResultCollection ttrCollection) { try { Node curNode; @@ -62,26 +47,21 @@ public class TreeTestResultCollectionDaoJcr extends AbstractSlcJcrDao implements } public TreeTestResultCollection getTestResultCollection(String id) { - try { - TreeTestResultCollection res = new TreeTestResultCollection(); - res.setId(id); + TreeTestResultCollection res = new TreeTestResultCollection(); + res.setId(id); - String queryString = "//*[@" + ttrColProp + "='" + id + "']"; - Query query = queryManager.createQuery(queryString, Query.XPATH); - log.debug("retrieving all nodes of a col - " + queryString); - int i = 0; - NodeIterator ni = query.execute().getNodes(); - while (ni.hasNext()) { - i++; - res.getResults().add( - (TreeTestResult) nodeMapper.load(ni.nextNode())); - } - log.debug(i + " nodes found"); - return res; - } catch (RepositoryException e) { - throw new SlcException( - "Cannot get TreeTestResultCollection for id " + id, e); + // String queryString = "//*[@" + ttrColProp + "='" + id + "']"; + // Query query = queryManager.createQuery(queryString, Query.XPATH); + // log.debug("retrieving all nodes of a col - " + queryString); + int i = 0; + NodeIterator ni = resultNodesInCollection(id); + while (ni.hasNext()) { + i++; + res.getResults().add( + (TreeTestResult) nodeMapper.load(ni.nextNode())); } + // log.debug(i + " nodes found"); + return res; } /** @@ -111,7 +91,8 @@ public class TreeTestResultCollectionDaoJcr extends AbstractSlcJcrDao implements nodeMapper.update(curNode, ttr); log.debug("Node updated"); } - log.debug("-----------------------------------------------------------------"); + log + .debug("-----------------------------------------------------------------"); curNode.setProperty(ttrColProp, colId); JcrUtils.debug(curNode.getSession().getRootNode()); } @@ -147,11 +128,10 @@ public class TreeTestResultCollectionDaoJcr extends AbstractSlcJcrDao implements public void delete(TreeTestResultCollection ttrCollection) { try { + // FIXME: should not delete sub nodes Node curNode; String colId = ttrCollection.getId(); - String queryString = "//*[@" + ttrColProp + "='" + colId + "']"; - Query query = queryManager.createQuery(queryString, Query.XPATH); - NodeIterator ni = query.execute().getNodes(); + NodeIterator ni = resultNodesInCollection(colId); while (ni.hasNext()) { curNode = ni.nextNode(); curNode.remove(); @@ -163,11 +143,21 @@ public class TreeTestResultCollectionDaoJcr extends AbstractSlcJcrDao implements } } - // FIXME Implement this method public SortedSet listCollections() { - // return new TreeSet(getHibernateTemplate() - // .find("from TreeTestResultCollection")); - return null; + // FIXME: optimize + Map lst = new HashMap(); + NodeIterator nodeIterator = query("//testresult"); + while (nodeIterator.hasNext()) { + Node node = nodeIterator.nextNode(); + String colId = property(node, ttrColProp); + if (colId != null) { + if (!lst.containsKey(colId)) + lst.put(colId, new TreeTestResultCollection(colId)); + TreeTestResultCollection ttrc = lst.get(colId); + ttrc.getResults().add((TreeTestResult) nodeMapper.load(node)); + } + } + return new TreeSet(lst.values()); } public void addResultToCollection(final TreeTestResultCollection ttrc, @@ -221,51 +211,127 @@ public class TreeTestResultCollectionDaoJcr extends AbstractSlcJcrDao implements // FIXME specify and implement this method public List listResultAttributes(String collectionId) { - /** - * List list; if (collectionId == null) list = - * getHibernateTemplate().find( - * "select new org.argeo.slc.core.test.tree.ResultAttributes(ttr)" + - * " from TreeTestResult ttr"); else list = getHibernateTemplate() - * .find( - * "select new org.argeo.slc.core.test.tree.ResultAttributes(ttr) " + - * " from TreeTestResult ttr, TreeTestResultCollection ttrc " + - * " where ttr in elements(ttrc.results) and ttrc.id=?", collectionId); - * - * return list; - */ - return null; - } + // FIXME: optimize + List list = new ArrayList(); + if (collectionId == null) { + List results = asTreeTestResultList(resultNodes( + null, null)); - // FIXME specify and implement this method + for (TreeTestResult ttr : results) { + list.add(new ResultAttributes(ttr)); + } + } else { + NodeIterator nodeIterator = resultNodesInCollection(collectionId); + while (nodeIterator.hasNext()) { + list.add(new ResultAttributes((TreeTestResult) nodeMapper + .load(nodeIterator.nextNode()))); + } + } + + return list; + // throw new UnsupportedOperationException(); + // List list; + // if (collectionId == null) + // list = getHibernateTemplate().find( + // "select new org.argeo.slc.core.test.tree.ResultAttributes(ttr)" + // + " from TreeTestResult ttr"); + // else + // list = getHibernateTemplate() + // .find( + // "select new org.argeo.slc.core.test.tree.ResultAttributes(ttr) " + // + " from TreeTestResult ttr, TreeTestResultCollection ttrc " + // + " where ttr in elements(ttrc.results) and ttrc.id=?", + // collectionId); + // + // return list; + } public List listResults(String collectionId, Map attributes) { - /** - * List list; - * - * if (collectionId == null) { if (attributes == null || - * attributes.size() == 0) list = - * getHibernateTemplate().find("from TreeTestResult"); else if - * (attributes.size() == 1) { Map.Entry entry = - * attributes.entrySet() .iterator().next(); Object[] args = { - * entry.getKey(), entry.getValue() }; list = - * getHibernateTemplate().find( "select ttr from TreeTestResult ttr" + - * " where attributes[?]=?", args); } else { throw new SlcException( - * "Multiple attributes filter are currently not supported."); } } else - * { if (attributes == null || attributes.size() == 0) list = - * getHibernateTemplate() .find( "select ttr " + - * " from TreeTestResult ttr, TreeTestResultCollection ttrc " + - * " where ttr in elements(ttrc.results) and ttrc.id=?", collectionId); - * else if (attributes.size() == 1) { Map.Entry entry = - * attributes.entrySet() .iterator().next(); Object[] args = { - * collectionId, entry.getKey(), entry.getValue() }; list = - * getHibernateTemplate() .find( - * "select ttr from TreeTestResult ttr, TreeTestResultCollection ttrc " - * + " where ttr in elements(ttrc.results) and ttrc.id=?" + - * " and attributes[?]=?", args); } else { throw new SlcException( - * "Multiple attributes filter are currently not supported."); } } - * return list; - */ - return null; + List list; + + if (collectionId == null) { + if (attributes == null || attributes.size() == 0) + list = asTreeTestResultList(resultNodes(null, null)); + else if (attributes.size() == 1) { + Map.Entry entry = attributes.entrySet() + .iterator().next(); + list = asTreeTestResultList(resultNodes(entry.getKey(), entry + .getValue())); + } else { + throw new SlcException( + "Multiple attributes filter are currently not supported."); + } + } else { + if (attributes == null || attributes.size() == 0) + list = asTreeTestResultList(resultNodesInCollection(collectionId)); + else if (attributes.size() == 1) { + Map.Entry entry = attributes.entrySet() + .iterator().next(); + list = asTreeTestResultList(resultNodesInCollection( + collectionId, entry.getKey(), entry.getValue())); + } else { + throw new SlcException( + "Multiple attributes filter are currently not supported."); + } + } + return list; + } + + // UTILITIES + + protected NodeIterator resultNodesInCollection(String collectionId, + String attributeKey, String attributeValue) { + String queryString = "//testresult[@" + ttrColProp + "='" + + collectionId + "' and @" + attributeKey + "='" + + attributeValue + "']"; + return query(queryString); + } + + protected NodeIterator resultNodesInCollection(String collectionId) { + String queryString = "//testresult[@" + ttrColProp + "='" + + collectionId + "']"; + return query(queryString); + } + + protected NodeIterator resultNodes(String attributeKey, + String attributeValue) { + String queryString; + if (attributeKey != null) + queryString = "//testResult[@" + attributeKey + "='" + + attributeValue + "']"; + else + queryString = "//testResult"; + return query(queryString); + } + + protected List asTreeTestResultList( + NodeIterator nodeIterator) { + List lst = new ArrayList(); + while (nodeIterator.hasNext()) { + lst.add((TreeTestResult) nodeMapper.load(nodeIterator.nextNode())); + } + return lst; + } + + private NodeIterator query(String query) { + try { + if (log.isDebugEnabled()) + log.debug("Retrieve nodes from query: " + query); + Query q = queryManager.createQuery(query, Query.XPATH); + return q.execute().getNodes(); + } catch (Exception e) { + throw new SlcException("Cannot load nodes from query: " + query); + } } + + private String property(Node node, String key) { + try { + return node.getProperty(key).getString(); + } catch (Exception e) { + log.warn("Cannot retrieve property " + key + " of node " + node, e); + return null; + } + } + } diff --git a/runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/dao/TreeTestResultDaoJcr.java b/runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/dao/TreeTestResultDaoJcr.java index 61a5b7573..df0a8f507 100644 --- a/runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/dao/TreeTestResultDaoJcr.java +++ b/runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/dao/TreeTestResultDaoJcr.java @@ -9,9 +9,7 @@ import java.util.Map; import javax.jcr.Node; import javax.jcr.NodeIterator; -import javax.jcr.RepositoryException; import javax.jcr.Session; -import javax.jcr.Workspace; import javax.jcr.query.Query; import javax.jcr.query.QueryManager; import javax.jcr.query.QueryResult; @@ -19,7 +17,6 @@ import javax.jcr.query.QueryResult; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.argeo.jcr.JcrUtils; -import org.argeo.jcr.NodeMapper; import org.argeo.slc.SlcException; import org.argeo.slc.core.attachment.SimpleAttachment; import org.argeo.slc.core.structure.tree.TreeSPath; @@ -41,35 +38,16 @@ public class TreeTestResultDaoJcr extends AbstractSlcJcrDao implements private final static Log log = LogFactory .getLog(TreeTestResultDaoJcr.class); - private Workspace workspace; - private QueryManager queryManager; - private NodeMapper nodeMapper; - - - public TreeTestResultDaoJcr() { - } - - public void init() { - try { - workspace = getSession().getWorkspace(); - queryManager = workspace.getQueryManager(); - nodeMapper = getNodeMapperProvider().findNodeMapper(null); - } catch (RepositoryException e) { - throw new SlcException("Cannot initialize DAO", e); - } - } - - public void create(TestResult testResult) { + public synchronized void create(TestResult testResult) { try { nodeMapper.save(getSession(), basePath(testResult), testResult); getSession().save(); - //JcrUtils.debug(getSession().getRootNode()); } catch (Exception e) { throw new SlcException("Cannot create testResult " + testResult, e); } } - public void update(TestResult testResult) { + public synchronized void update(TestResult testResult) { try { nodeMapper.save(getSession(), basePath(testResult), testResult); getSession().save(); @@ -143,15 +121,20 @@ public class TreeTestResultDaoJcr extends AbstractSlcJcrDao implements } } - public void close(final String testResultId, final Date closeDate) { + public synchronized void close(final String testResultId, + final Date closeDate) { try { // TODO: optimize query - String queryString = "//testresult[@uiid='" + testResultId + "']"; + String queryString = "//testresult[@uuid='" + testResultId + "']"; Query query = queryManager.createQuery(queryString, Query.XPATH); Node resNode = JcrUtils.querySingleNode(query); Calendar cal = new GregorianCalendar(); cal.setTime(closeDate); - resNode.setProperty("closeDate", cal); + if (resNode != null) + resNode.setProperty("closeDate", cal); + else if (log.isDebugEnabled()) + log.debug("Cannot close because a node for test result # " + + testResultId + " was not found"); getSession().save(); } catch (Exception e) { throw new SlcException("Cannot close TestResult " + testResultId, e); @@ -168,13 +151,13 @@ public class TreeTestResultDaoJcr extends AbstractSlcJcrDao implements */ // TODO do we load objects, do treatment and persist them or do we work // directly in JCR - public void addResultPart(final String testResultId, final TreeSPath path, - final SimpleResultPart resultPart, + public synchronized void addResultPart(final String testResultId, + final TreeSPath path, final SimpleResultPart resultPart, final Map relatedElements) { try { // TODO: optimize query - String queryString = "//*[@uuid='" + testResultId + "']"; + String queryString = "//testresult[@uuid='" + testResultId + "']"; Query query = queryManager.createQuery(queryString, Query.XPATH); Node resNode = JcrUtils.querySingleNode(query); @@ -226,7 +209,12 @@ public class TreeTestResultDaoJcr extends AbstractSlcJcrDao implements Map tags = relatedElements.get(key) .getTags(); for (String tag : tags.keySet()) { - curNode.setProperty(tag, tags.get(tag)); + String cleanTag = JcrUtils + .removeForbiddenCharacters(tag); + if (!cleanTag.equals(tag)) + log.warn("Tag '" + tag + "' persisted as '" + + cleanTag + "'"); + curNode.setProperty(cleanTag, tags.get(tag)); } // We set the class in order to be able to retrieve @@ -241,7 +229,7 @@ public class TreeTestResultDaoJcr extends AbstractSlcJcrDao implements } } - public void addAttachment(final String testResultId, + public synchronized void addAttachment(final String testResultId, final SimpleAttachment attachment) { try { @@ -299,24 +287,22 @@ public class TreeTestResultDaoJcr extends AbstractSlcJcrDao implements } } - public void updateAttributes(final String testResultId, + public synchronized void updateAttributes(final String testResultId, final Map attributes) { try { String queryString = "//testresult[@uuid='" + testResultId + "']"; Query query = queryManager.createQuery(queryString, Query.XPATH); Node node = JcrUtils.querySingleNode(query); - for (String key: attributes.keySet()){ - node.setProperty(key, attributes.get(key)); + for (String key : attributes.keySet()) { + node.setProperty(key, attributes.get(key)); } getSession().save(); } catch (Exception e) { - throw new SlcException("Cannot update Attributes on TestResult with ID " - + testResultId , e); + throw new SlcException( + "Cannot update Attributes on TestResult with ID " + + testResultId, e); } } - - - } diff --git a/runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/dao/TreeTestResultNodeMapper.java b/runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/dao/TreeTestResultNodeMapper.java index 9cb59edb7..471de7100 100644 --- a/runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/dao/TreeTestResultNodeMapper.java +++ b/runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/dao/TreeTestResultNodeMapper.java @@ -14,7 +14,6 @@ import javax.jcr.NodeIterator; import javax.jcr.Property; import javax.jcr.PropertyIterator; import javax.jcr.RepositoryException; -import javax.jcr.Session; import javax.jcr.query.Query; import javax.jcr.query.QueryManager; @@ -95,7 +94,12 @@ public class TreeTestResultNodeMapper extends BeanNodeMapper { // We add the tags Map tags = elements.get(key).getTags(); for (String tag : tags.keySet()) { - childNode.setProperty(tag, tags.get(tag)); + // remove forbidden characters + String cleanTag = JcrUtils.removeForbiddenCharacters(tag); + if (!cleanTag.equals(tag)) + log.warn("Tag '" + tag + "' persisted as '" + cleanTag + + "'"); + childNode.setProperty(cleanTag, tags.get(tag)); } // We set the class in order to be able to retrieve @@ -135,7 +139,7 @@ public class TreeTestResultNodeMapper extends BeanNodeMapper { Node listNode; int i; for (i = 0; i < list.size(); i++) { - TestResultPart trp = list.get(i); + // TestResultPart trp = list.get(i); // FIXME : ResultParts are systematicaly added. // There no check to see if already exists. listNode = childNode.addNode("resultpart"); @@ -190,8 +194,7 @@ public class TreeTestResultNodeMapper extends BeanNodeMapper { if (log.isTraceEnabled()) log.debug("Map node " + node.getPath() + " to bean " + clssName); - - // It's a very specific implementation, + // It's a very specific implementation, // We don't need to use a bean wrapper. TreeTestResult ttr = new TreeTestResult(); @@ -210,7 +213,7 @@ public class TreeTestResultNodeMapper extends BeanNodeMapper { props: while (propIt.hasNext()) { Property prop = propIt.nextProperty(); - //TODO Define a rule to generalize it (Namespace ??) + // TODO Define a rule to generalize it (Namespace ??) // Get rid of specific case. mainly uuid if ("uuid".equals(prop.getName()) || prop.getName().equals(getClassProperty()) @@ -238,8 +241,8 @@ public class TreeTestResultNodeMapper extends BeanNodeMapper { ttr.setAttachments(attachments); } - // STRUCTURED ELEMENTS - + // STRUCTURED ELEMENTS + String basePath = node.getPath(); SortedMap resultParts = new TreeMap(); SortedMap elements = new TreeMap(); @@ -271,9 +274,9 @@ public class TreeTestResultNodeMapper extends BeanNodeMapper { PropertyIterator tagIt = curNode.getProperties(); tags: while (tagIt.hasNext()) { Property prop = tagIt.nextProperty(); - log.debug("Handling property named : " + prop.getName()); + //log.debug("Handling property named : " + prop.getName()); - //TODO Define a rule to generalize it + // TODO Define a rule to generalize it // Specific case. mainly uuid if ("uuid".equals(prop.getName()) || prop.getName().equals(getClassProperty()) @@ -293,7 +296,7 @@ public class TreeTestResultNodeMapper extends BeanNodeMapper { ttr.setElements(elements); // RESULTPARTS - + // We have to had the uuid of the current node to be sure that we are in // its sub tree queryString = "//testresult[@uuid='" + uuid + "']";