X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;ds=sidebyside;f=runtime%2Forg.argeo.slc.support.jcr%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fjcr%2Fdao%2FTreeTestResultCollectionDaoJcr.java;h=0004bad9344d2c6d8164b2b82ddf2ecad5cdc526;hb=5fe677bf811739592b1089471181e39f9cbe4f3f;hp=33e4ffbf5ac8a06ddf9fbd2137eec1e259da73cd;hpb=dfe0c5ca8925056d9483a180d72ab7607e28b03a;p=gpl%2Fargeo-slc.git 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..0004bad93 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,35 @@ +/* + * Copyright (C) 2007-2012 Mathieu Baudier + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ 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; @@ -27,25 +42,11 @@ public class TreeTestResultCollectionDaoJcr extends AbstractSlcJcrDao implements // FIXME : we handle testResultCollection by adding a property called // "TestResultCollectionId " - final private String ttrColProp = "TestResultCollectionId"; + final private String ttrColProp = "collectionId"; 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; @@ -55,33 +56,21 @@ public class TreeTestResultCollectionDaoJcr extends AbstractSlcJcrDao implements curNode.setProperty(ttrColProp, colId); } getSession().save(); - } catch (Exception e) { + } catch (RepositoryException e) { throw new SlcException("Cannot create TreeTestResultCollection " + ttrCollection, e); } } public TreeTestResultCollection getTestResultCollection(String id) { - try { - 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); + TreeTestResultCollection res = new TreeTestResultCollection(); + res.setId(id); + NodeIterator ni = resultNodesInCollection(id); + while (ni.hasNext()) { + res.getResults().add( + (TreeTestResult) nodeMapper.load(ni.nextNode())); } + return res; } /** @@ -95,15 +84,12 @@ public class TreeTestResultCollectionDaoJcr extends AbstractSlcJcrDao implements public void update(TreeTestResultCollection ttrCollection) { try { log.debug("Update "); - String queryString; - Query query; - Node curNode; String colId = ttrCollection.getId(); // We add or update existing ones for (TreeTestResult ttr : ttrCollection.getResults()) { - queryString = "//*[@uuid='" + ttr.getUuid() + "']"; - query = queryManager.createQuery(queryString, Query.XPATH); - curNode = JcrUtils.querySingleNode(query); + String queryString = "//testresult[@uuid='" + ttr.getUuid() + + "']"; + Node curNode = singleNode(queryString, Query.XPATH); if (curNode == null) { curNode = nodeMapper.save(getSession(), basePath(ttr), ttr); log.debug("New Node added"); @@ -111,19 +97,20 @@ 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()); } // We remove those who are not part of the collection anymore - queryString = "//*[@" + ttrColProp + "='" + colId + "']"; - query = queryManager.createQuery(queryString, Query.XPATH); + String queryString = "//*[@" + ttrColProp + "='" + colId + "']"; + Query query = createQuery(queryString, Query.XPATH); log.debug("Query :" + queryString); NodeIterator ni = query.execute().getNodes(); int i = 0; while (ni.hasNext()) { log.debug("Node " + (++i)); - curNode = ni.nextNode(); + Node curNode = ni.nextNode(); String uuid = curNode.getProperty("uuid").getString(); boolean isPartOfTheSet = false; for (TreeTestResult ttr : ttrCollection.getResults()) { @@ -139,7 +126,7 @@ public class TreeTestResultCollectionDaoJcr extends AbstractSlcJcrDao implements } } getSession().save(); - } catch (Exception e) { + } catch (RepositoryException e) { throw new SlcException("Cannot update TreeTestResultCollection " + ttrCollection, e); } @@ -147,11 +134,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 +149,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, @@ -176,12 +172,10 @@ public class TreeTestResultCollectionDaoJcr extends AbstractSlcJcrDao implements String queryString; Node curNode; String colId = ttrc.getId(); - queryString = "//*[@uuid='" + resultUuid + "']"; - Query query = queryManager.createQuery(queryString, Query.XPATH); - curNode = JcrUtils.querySingleNode(query); + queryString = "//testresult[@uuid='" + resultUuid + "']"; + curNode = singleNode(queryString, Query.XPATH); if (curNode == null) { - throw new SlcException("Cannot add TreeTestResult of Id " - + resultUuid + " to collection " + colId); + throw new SlcException("Cannot find test result #" + resultUuid); } else curNode.setProperty(ttrColProp, colId); getSession().save(); @@ -195,25 +189,19 @@ public class TreeTestResultCollectionDaoJcr extends AbstractSlcJcrDao implements public void removeResultFromCollection(final TreeTestResultCollection ttrc, final String resultUuid) { try { - log.debug("remove result"); String queryString; Node curNode; - String colId = ttrc.getId(); - queryString = "//*[@uuid='" + resultUuid + "']"; - Query query = queryManager.createQuery(queryString, Query.XPATH); + queryString = "//testresult[@uuid='" + resultUuid + "' and " + + ttrColProp + "='" + ttrc.getId() + "']"; + Query query = createQuery(queryString, Query.XPATH); curNode = JcrUtils.querySingleNode(query); - log.debug("Query : " + queryString + " - Node retrieved " - + curNode.getPath()); if (curNode == null) { - throw new SlcException("Cannot remove TreeTestResult of Id " - + resultUuid + " from collection " + colId); + throw new SlcException("Cannot find test result #" + resultUuid); } else { curNode.getProperty(ttrColProp).remove(); - log.debug("Property removed : " - + curNode.getProperty(ttrColProp).getString()); } getSession().save(); - } catch (Exception e) { + } catch (RepositoryException e) { throw new SlcException("Cannot remove TreeTestResult of Id " + resultUuid + " from collection " + ttrc, e); } @@ -221,51 +209,117 @@ 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; + } 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 = createQuery(query, Query.XPATH); + return q.execute().getNodes(); + } catch (RepositoryException e) { + throw new SlcException("Cannot load nodes from query: " + query); + } + } + + private String property(Node node, String key) { + try { + return node.getProperty(key).getString(); + } catch (RepositoryException e) { + log.warn("Cannot retrieve property " + key + " of node " + node, e); + return null; + } + } + + private Node singleNode(String query, String queryType) { + Query q = createQuery(query, queryType); + return JcrUtils.querySingleNode(q); + } + }