]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/dao/TreeTestResultCollectionDaoJcr.java
Adapt to changes in Argeo Commons
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.jcr / src / main / java / org / argeo / slc / jcr / dao / TreeTestResultCollectionDaoJcr.java
index 33e4ffbf5ac8a06ddf9fbd2137eec1e259da73cd..0004bad9344d2c6d8164b2b82ddf2ecad5cdc526 100644 (file)
@@ -1,20 +1,35 @@
+/*\r
+ * Copyright (C) 2007-2012 Mathieu Baudier\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ *         http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ */\r
 package org.argeo.slc.jcr.dao;\r
 \r
+import java.util.ArrayList;\r
+import java.util.HashMap;\r
 import java.util.List;\r
 import java.util.Map;\r
 import java.util.SortedSet;\r
+import java.util.TreeSet;\r
 \r
 import javax.jcr.Node;\r
 import javax.jcr.NodeIterator;\r
 import javax.jcr.RepositoryException;\r
-import javax.jcr.Workspace;\r
 import javax.jcr.query.Query;\r
-import javax.jcr.query.QueryManager;\r
 \r
 import org.apache.commons.logging.Log;\r
 import org.apache.commons.logging.LogFactory;\r
 import org.argeo.jcr.JcrUtils;\r
-import org.argeo.jcr.NodeMapper;\r
 import org.argeo.slc.SlcException;\r
 import org.argeo.slc.core.test.tree.ResultAttributes;\r
 import org.argeo.slc.core.test.tree.TreeTestResult;\r
@@ -27,25 +42,11 @@ public class TreeTestResultCollectionDaoJcr extends AbstractSlcJcrDao implements
 \r
        // FIXME : we handle testResultCollection by adding a property called\r
        // "TestResultCollectionId "\r
-       final private String ttrColProp = "TestResultCollectionId";\r
+       final private String ttrColProp = "collectionId";\r
 \r
        private final static Log log = LogFactory\r
                        .getLog(TreeTestResultCollectionDaoJcr.class);\r
 \r
-       private Workspace workspace;\r
-       private QueryManager queryManager;\r
-       private NodeMapper nodeMapper;\r
-\r
-       public void init() {\r
-               try {\r
-                       workspace = getSession().getWorkspace();\r
-                       queryManager = workspace.getQueryManager();\r
-                       nodeMapper = getNodeMapperProvider().findNodeMapper(null);\r
-               } catch (RepositoryException e) {\r
-                       throw new SlcException("Cannot initialize DAO", e);\r
-               }\r
-       }\r
-\r
        public void create(TreeTestResultCollection ttrCollection) {\r
                try {\r
                        Node curNode;\r
@@ -55,33 +56,21 @@ public class TreeTestResultCollectionDaoJcr extends AbstractSlcJcrDao implements
                                curNode.setProperty(ttrColProp, colId);\r
                        }\r
                        getSession().save();\r
-               } catch (Exception e) {\r
+               } catch (RepositoryException e) {\r
                        throw new SlcException("Cannot create TreeTestResultCollection "\r
                                        + ttrCollection, e);\r
                }\r
        }\r
 \r
        public TreeTestResultCollection getTestResultCollection(String id) {\r
-               try {\r
-                       TreeTestResultCollection res = new TreeTestResultCollection();\r
-                       res.setId(id);\r
-\r
-                       String queryString = "//*[@" + ttrColProp + "='" + id + "']";\r
-                       Query query = queryManager.createQuery(queryString, Query.XPATH);\r
-                       log.debug("retrieving all nodes of a col - " + queryString);\r
-                       int i = 0;\r
-                       NodeIterator ni = query.execute().getNodes();\r
-                       while (ni.hasNext()) {\r
-                               i++;\r
-                               res.getResults().add(\r
-                                               (TreeTestResult) nodeMapper.load(ni.nextNode()));\r
-                       }\r
-                       log.debug(i + " nodes found");\r
-                       return res;\r
-               } catch (RepositoryException e) {\r
-                       throw new SlcException(\r
-                                       "Cannot get TreeTestResultCollection for id " + id, e);\r
+               TreeTestResultCollection res = new TreeTestResultCollection();\r
+               res.setId(id);\r
+               NodeIterator ni = resultNodesInCollection(id);\r
+               while (ni.hasNext()) {\r
+                       res.getResults().add(\r
+                                       (TreeTestResult) nodeMapper.load(ni.nextNode()));\r
                }\r
+               return res;\r
        }\r
 \r
        /**\r
@@ -95,15 +84,12 @@ public class TreeTestResultCollectionDaoJcr extends AbstractSlcJcrDao implements
        public void update(TreeTestResultCollection ttrCollection) {\r
                try {\r
                        log.debug("Update ");\r
-                       String queryString;\r
-                       Query query;\r
-                       Node curNode;\r
                        String colId = ttrCollection.getId();\r
                        // We add or update existing ones\r
                        for (TreeTestResult ttr : ttrCollection.getResults()) {\r
-                               queryString = "//*[@uuid='" + ttr.getUuid() + "']";\r
-                               query = queryManager.createQuery(queryString, Query.XPATH);\r
-                               curNode = JcrUtils.querySingleNode(query);\r
+                               String queryString = "//testresult[@uuid='" + ttr.getUuid()\r
+                                               + "']";\r
+                               Node curNode = singleNode(queryString, Query.XPATH);\r
                                if (curNode == null) {\r
                                        curNode = nodeMapper.save(getSession(), basePath(ttr), ttr);\r
                                        log.debug("New Node added");\r
@@ -111,19 +97,20 @@ public class TreeTestResultCollectionDaoJcr extends AbstractSlcJcrDao implements
                                        nodeMapper.update(curNode, ttr);\r
                                        log.debug("Node updated");\r
                                }\r
-                               log.debug("-----------------------------------------------------------------");\r
+                               log\r
+                                               .debug("-----------------------------------------------------------------");\r
                                curNode.setProperty(ttrColProp, colId);\r
                                JcrUtils.debug(curNode.getSession().getRootNode());\r
                        }\r
                        // We remove those who are not part of the collection anymore\r
-                       queryString = "//*[@" + ttrColProp + "='" + colId + "']";\r
-                       query = queryManager.createQuery(queryString, Query.XPATH);\r
+                       String queryString = "//*[@" + ttrColProp + "='" + colId + "']";\r
+                       Query query = createQuery(queryString, Query.XPATH);\r
                        log.debug("Query :" + queryString);\r
                        NodeIterator ni = query.execute().getNodes();\r
                        int i = 0;\r
                        while (ni.hasNext()) {\r
                                log.debug("Node " + (++i));\r
-                               curNode = ni.nextNode();\r
+                               Node curNode = ni.nextNode();\r
                                String uuid = curNode.getProperty("uuid").getString();\r
                                boolean isPartOfTheSet = false;\r
                                for (TreeTestResult ttr : ttrCollection.getResults()) {\r
@@ -139,7 +126,7 @@ public class TreeTestResultCollectionDaoJcr extends AbstractSlcJcrDao implements
                                }\r
                        }\r
                        getSession().save();\r
-               } catch (Exception e) {\r
+               } catch (RepositoryException e) {\r
                        throw new SlcException("Cannot update TreeTestResultCollection "\r
                                        + ttrCollection, e);\r
                }\r
@@ -147,11 +134,10 @@ public class TreeTestResultCollectionDaoJcr extends AbstractSlcJcrDao implements
 \r
        public void delete(TreeTestResultCollection ttrCollection) {\r
                try {\r
+                       // FIXME: should not delete sub nodes\r
                        Node curNode;\r
                        String colId = ttrCollection.getId();\r
-                       String queryString = "//*[@" + ttrColProp + "='" + colId + "']";\r
-                       Query query = queryManager.createQuery(queryString, Query.XPATH);\r
-                       NodeIterator ni = query.execute().getNodes();\r
+                       NodeIterator ni = resultNodesInCollection(colId);\r
                        while (ni.hasNext()) {\r
                                curNode = ni.nextNode();\r
                                curNode.remove();\r
@@ -163,11 +149,21 @@ public class TreeTestResultCollectionDaoJcr extends AbstractSlcJcrDao implements
                }\r
        }\r
 \r
-       // FIXME Implement this method\r
        public SortedSet<TreeTestResultCollection> listCollections() {\r
-               // return new TreeSet<TreeTestResultCollection>(getHibernateTemplate()\r
-               // .find("from TreeTestResultCollection"));\r
-               return null;\r
+               // FIXME: optimize\r
+               Map<String, TreeTestResultCollection> lst = new HashMap<String, TreeTestResultCollection>();\r
+               NodeIterator nodeIterator = query("//testresult");\r
+               while (nodeIterator.hasNext()) {\r
+                       Node node = nodeIterator.nextNode();\r
+                       String colId = property(node, ttrColProp);\r
+                       if (colId != null) {\r
+                               if (!lst.containsKey(colId))\r
+                                       lst.put(colId, new TreeTestResultCollection(colId));\r
+                               TreeTestResultCollection ttrc = lst.get(colId);\r
+                               ttrc.getResults().add((TreeTestResult) nodeMapper.load(node));\r
+                       }\r
+               }\r
+               return new TreeSet<TreeTestResultCollection>(lst.values());\r
        }\r
 \r
        public void addResultToCollection(final TreeTestResultCollection ttrc,\r
@@ -176,12 +172,10 @@ public class TreeTestResultCollectionDaoJcr extends AbstractSlcJcrDao implements
                        String queryString;\r
                        Node curNode;\r
                        String colId = ttrc.getId();\r
-                       queryString = "//*[@uuid='" + resultUuid + "']";\r
-                       Query query = queryManager.createQuery(queryString, Query.XPATH);\r
-                       curNode = JcrUtils.querySingleNode(query);\r
+                       queryString = "//testresult[@uuid='" + resultUuid + "']";\r
+                       curNode = singleNode(queryString, Query.XPATH);\r
                        if (curNode == null) {\r
-                               throw new SlcException("Cannot add TreeTestResult of Id "\r
-                                               + resultUuid + " to collection " + colId);\r
+                               throw new SlcException("Cannot find test result #" + resultUuid);\r
                        } else\r
                                curNode.setProperty(ttrColProp, colId);\r
                        getSession().save();\r
@@ -195,25 +189,19 @@ public class TreeTestResultCollectionDaoJcr extends AbstractSlcJcrDao implements
        public void removeResultFromCollection(final TreeTestResultCollection ttrc,\r
                        final String resultUuid) {\r
                try {\r
-                       log.debug("remove result");\r
                        String queryString;\r
                        Node curNode;\r
-                       String colId = ttrc.getId();\r
-                       queryString = "//*[@uuid='" + resultUuid + "']";\r
-                       Query query = queryManager.createQuery(queryString, Query.XPATH);\r
+                       queryString = "//testresult[@uuid='" + resultUuid + "' and "\r
+                                       + ttrColProp + "='" + ttrc.getId() + "']";\r
+                       Query query = createQuery(queryString, Query.XPATH);\r
                        curNode = JcrUtils.querySingleNode(query);\r
-                       log.debug("Query : " + queryString + " - Node retrieved "\r
-                                       + curNode.getPath());\r
                        if (curNode == null) {\r
-                               throw new SlcException("Cannot remove TreeTestResult of Id "\r
-                                               + resultUuid + " from collection " + colId);\r
+                               throw new SlcException("Cannot find test result #" + resultUuid);\r
                        } else {\r
                                curNode.getProperty(ttrColProp).remove();\r
-                               log.debug("Property removed : "\r
-                                               + curNode.getProperty(ttrColProp).getString());\r
                        }\r
                        getSession().save();\r
-               } catch (Exception e) {\r
+               } catch (RepositoryException e) {\r
                        throw new SlcException("Cannot remove TreeTestResult of Id "\r
                                        + resultUuid + " from collection " + ttrc, e);\r
                }\r
@@ -221,51 +209,117 @@ public class TreeTestResultCollectionDaoJcr extends AbstractSlcJcrDao implements
 \r
        // FIXME specify and implement this method\r
        public List<ResultAttributes> listResultAttributes(String collectionId) {\r
-               /**\r
-                * List<ResultAttributes> list; if (collectionId == null) list =\r
-                * getHibernateTemplate().find(\r
-                * "select new org.argeo.slc.core.test.tree.ResultAttributes(ttr)" +\r
-                * " from TreeTestResult ttr"); else list = getHibernateTemplate()\r
-                * .find(\r
-                * "select new org.argeo.slc.core.test.tree.ResultAttributes(ttr) " +\r
-                * " from TreeTestResult ttr, TreeTestResultCollection ttrc " +\r
-                * " where ttr in elements(ttrc.results) and ttrc.id=?", collectionId);\r
-                * \r
-                * return list;\r
-                */\r
-               return null;\r
-       }\r
+               // FIXME: optimize\r
+               List<ResultAttributes> list = new ArrayList<ResultAttributes>();\r
+               if (collectionId == null) {\r
+                       List<TreeTestResult> results = asTreeTestResultList(resultNodes(\r
+                                       null, null));\r
 \r
-       // FIXME specify and implement this method\r
+                       for (TreeTestResult ttr : results) {\r
+                               list.add(new ResultAttributes(ttr));\r
+                       }\r
+               } else {\r
+                       NodeIterator nodeIterator = resultNodesInCollection(collectionId);\r
+                       while (nodeIterator.hasNext()) {\r
+                               list.add(new ResultAttributes((TreeTestResult) nodeMapper\r
+                                               .load(nodeIterator.nextNode())));\r
+                       }\r
+               }\r
+\r
+               return list;\r
+       }\r
 \r
        public List<TreeTestResult> listResults(String collectionId,\r
                        Map<String, String> attributes) {\r
-               /**\r
-                * List<TreeTestResult> list;\r
-                * \r
-                * if (collectionId == null) { if (attributes == null ||\r
-                * attributes.size() == 0) list =\r
-                * getHibernateTemplate().find("from TreeTestResult"); else if\r
-                * (attributes.size() == 1) { Map.Entry<String, String> entry =\r
-                * attributes.entrySet() .iterator().next(); Object[] args = {\r
-                * entry.getKey(), entry.getValue() }; list =\r
-                * getHibernateTemplate().find( "select ttr from TreeTestResult ttr" +\r
-                * " where attributes[?]=?", args); } else { throw new SlcException(\r
-                * "Multiple attributes filter are currently not supported."); } } else\r
-                * { if (attributes == null || attributes.size() == 0) list =\r
-                * getHibernateTemplate() .find( "select ttr " +\r
-                * " from TreeTestResult ttr, TreeTestResultCollection ttrc " +\r
-                * " where ttr in elements(ttrc.results) and ttrc.id=?", collectionId);\r
-                * else if (attributes.size() == 1) { Map.Entry<String, String> entry =\r
-                * attributes.entrySet() .iterator().next(); Object[] args = {\r
-                * collectionId, entry.getKey(), entry.getValue() }; list =\r
-                * getHibernateTemplate() .find(\r
-                * "select ttr from TreeTestResult ttr, TreeTestResultCollection ttrc "\r
-                * + " where ttr in elements(ttrc.results) and ttrc.id=?" +\r
-                * " and attributes[?]=?", args); } else { throw new SlcException(\r
-                * "Multiple attributes filter are currently not supported."); } }\r
-                * return list;\r
-                */\r
-               return null;\r
+               List<TreeTestResult> list;\r
+\r
+               if (collectionId == null) {\r
+                       if (attributes == null || attributes.size() == 0)\r
+                               list = asTreeTestResultList(resultNodes(null, null));\r
+                       else if (attributes.size() == 1) {\r
+                               Map.Entry<String, String> entry = attributes.entrySet()\r
+                                               .iterator().next();\r
+                               list = asTreeTestResultList(resultNodes(entry.getKey(), entry\r
+                                               .getValue()));\r
+                       } else {\r
+                               throw new SlcException(\r
+                                               "Multiple attributes filter are currently not supported.");\r
+                       }\r
+               } else {\r
+                       if (attributes == null || attributes.size() == 0)\r
+                               list = asTreeTestResultList(resultNodesInCollection(collectionId));\r
+                       else if (attributes.size() == 1) {\r
+                               Map.Entry<String, String> entry = attributes.entrySet()\r
+                                               .iterator().next();\r
+                               list = asTreeTestResultList(resultNodesInCollection(\r
+                                               collectionId, entry.getKey(), entry.getValue()));\r
+                       } else {\r
+                               throw new SlcException(\r
+                                               "Multiple attributes filter are currently not supported.");\r
+                       }\r
+               }\r
+               return list;\r
+       }\r
+\r
+       // UTILITIES\r
+\r
+       protected NodeIterator resultNodesInCollection(String collectionId,\r
+                       String attributeKey, String attributeValue) {\r
+               String queryString = "//testresult[@" + ttrColProp + "='"\r
+                               + collectionId + "' and @" + attributeKey + "='"\r
+                               + attributeValue + "']";\r
+               return query(queryString);\r
+       }\r
+\r
+       protected NodeIterator resultNodesInCollection(String collectionId) {\r
+               String queryString = "//testresult[@" + ttrColProp + "='"\r
+                               + collectionId + "']";\r
+               return query(queryString);\r
+       }\r
+\r
+       protected NodeIterator resultNodes(String attributeKey,\r
+                       String attributeValue) {\r
+               String queryString;\r
+               if (attributeKey != null)\r
+                       queryString = "//testresult[@" + attributeKey + "='"\r
+                                       + attributeValue + "']";\r
+               else\r
+                       queryString = "//testresult";\r
+               return query(queryString);\r
        }\r
+\r
+       protected List<TreeTestResult> asTreeTestResultList(\r
+                       NodeIterator nodeIterator) {\r
+               List<TreeTestResult> lst = new ArrayList<TreeTestResult>();\r
+               while (nodeIterator.hasNext()) {\r
+                       lst.add((TreeTestResult) nodeMapper.load(nodeIterator.nextNode()));\r
+               }\r
+               return lst;\r
+       }\r
+\r
+       private NodeIterator query(String query) {\r
+               try {\r
+                       if (log.isDebugEnabled())\r
+                               log.debug("Retrieve nodes from query: " + query);\r
+                       Query q = createQuery(query, Query.XPATH);\r
+                       return q.execute().getNodes();\r
+               } catch (RepositoryException e) {\r
+                       throw new SlcException("Cannot load nodes from query: " + query);\r
+               }\r
+       }\r
+\r
+       private String property(Node node, String key) {\r
+               try {\r
+                       return node.getProperty(key).getString();\r
+               } catch (RepositoryException e) {\r
+                       log.warn("Cannot retrieve property " + key + " of node " + node, e);\r
+                       return null;\r
+               }\r
+       }\r
+\r
+       private Node singleNode(String query, String queryType) {\r
+                       Query q = createQuery(query, queryType);\r
+                       return JcrUtils.querySingleNode(q);\r
+       }\r
+\r
 }\r