+/*\r
+ * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>\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
+\r
package org.argeo.slc.jcr.dao;\r
\r
import java.util.ArrayList;\r
\r
import javax.jcr.Node;\r
import javax.jcr.NodeIterator;\r
+import javax.jcr.RepositoryException;\r
import javax.jcr.query.Query;\r
\r
import org.apache.commons.logging.Log;\r
\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
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
public TreeTestResultCollection getTestResultCollection(String id) {\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 = resultNodesInCollection(id);\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
}\r
\r
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
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
}\r
}\r
getSession().save();\r
- } catch (Exception e) {\r
+ } catch (RepositoryException e) {\r
throw new SlcException("Cannot update TreeTestResultCollection "\r
+ ttrCollection, e);\r
}\r
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
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
}\r
\r
return list;\r
- // throw new UnsupportedOperationException();\r
- // List<ResultAttributes> list;\r
- // if (collectionId == null)\r
- // list = getHibernateTemplate().find(\r
- // "select new org.argeo.slc.core.test.tree.ResultAttributes(ttr)"\r
- // + " from TreeTestResult ttr");\r
- // else\r
- // 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=?",\r
- // collectionId);\r
- //\r
- // return list;\r
}\r
\r
public List<TreeTestResult> listResults(String collectionId,\r
String attributeValue) {\r
String queryString;\r
if (attributeKey != null)\r
- queryString = "//testResult[@" + attributeKey + "='"\r
+ queryString = "//testresult[@" + attributeKey + "='"\r
+ attributeValue + "']";\r
else\r
- queryString = "//testResult";\r
+ queryString = "//testresult";\r
return query(queryString);\r
}\r
\r
try {\r
if (log.isDebugEnabled())\r
log.debug("Retrieve nodes from query: " + query);\r
- Query q = queryManager.createQuery(query, Query.XPATH);\r
+ Query q = createQuery(query, Query.XPATH);\r
return q.execute().getNodes();\r
- } catch (Exception e) {\r
+ } catch (RepositoryException e) {\r
throw new SlcException("Cannot load nodes from query: " + query);\r
}\r
}\r
private String property(Node node, String key) {\r
try {\r
return node.getProperty(key).getString();\r
- } catch (Exception e) {\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