2 * Copyright (C) 2007-2012 Mathieu Baudier
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.slc
.jcr
.dao
;
18 import java
.util
.ArrayList
;
19 import java
.util
.HashMap
;
20 import java
.util
.List
;
22 import java
.util
.SortedSet
;
23 import java
.util
.TreeSet
;
25 import javax
.jcr
.Node
;
26 import javax
.jcr
.NodeIterator
;
27 import javax
.jcr
.RepositoryException
;
28 import javax
.jcr
.query
.Query
;
30 import org
.apache
.commons
.logging
.Log
;
31 import org
.apache
.commons
.logging
.LogFactory
;
32 import org
.argeo
.jcr
.JcrUtils
;
33 import org
.argeo
.slc
.SlcException
;
34 import org
.argeo
.slc
.core
.test
.tree
.ResultAttributes
;
35 import org
.argeo
.slc
.core
.test
.tree
.TreeTestResult
;
36 import org
.argeo
.slc
.core
.test
.tree
.TreeTestResultCollection
;
37 import org
.argeo
.slc
.dao
.test
.tree
.TreeTestResultCollectionDao
;
39 /** JCR implementation of collections DAO. */
40 public class TreeTestResultCollectionDaoJcr
extends AbstractSlcJcrDao
implements
41 TreeTestResultCollectionDao
{
43 // FIXME : we handle testResultCollection by adding a property called
44 // "TestResultCollectionId "
45 final private String ttrColProp
= "collectionId";
47 private final static Log log
= LogFactory
48 .getLog(TreeTestResultCollectionDaoJcr
.class);
50 public void create(TreeTestResultCollection ttrCollection
) {
53 String colId
= ttrCollection
.getId();
54 for (TreeTestResult ttr
: ttrCollection
.getResults()) {
55 curNode
= nodeMapper
.save(getSession(), basePath(ttr
), ttr
);
56 curNode
.setProperty(ttrColProp
, colId
);
59 } catch (RepositoryException e
) {
60 throw new SlcException("Cannot create TreeTestResultCollection "
65 public TreeTestResultCollection
getTestResultCollection(String id
) {
66 TreeTestResultCollection res
= new TreeTestResultCollection();
68 NodeIterator ni
= resultNodesInCollection(id
);
69 while (ni
.hasNext()) {
71 (TreeTestResult
) nodeMapper
.load(ni
.nextNode()));
78 * FIXME : validate what this method must really do ? what happen if one of
79 * the TreeTestResult of the collection is not found in the jcr repository?
80 * Now we create ttr that are not found and update existing ones.
81 * FurtherMore if a TreeTestResult is persisted as member the collection but
82 * is not in the object passed, it is removed.
84 public void update(TreeTestResultCollection ttrCollection
) {
87 String colId
= ttrCollection
.getId();
88 // We add or update existing ones
89 for (TreeTestResult ttr
: ttrCollection
.getResults()) {
90 String queryString
= "//testresult[@uuid='" + ttr
.getUuid()
92 Node curNode
= singleNode(queryString
, Query
.XPATH
);
93 if (curNode
== null) {
94 curNode
= nodeMapper
.save(getSession(), basePath(ttr
), ttr
);
95 log
.debug("New Node added");
97 nodeMapper
.update(curNode
, ttr
);
98 log
.debug("Node updated");
101 .debug("-----------------------------------------------------------------");
102 curNode
.setProperty(ttrColProp
, colId
);
103 JcrUtils
.debug(curNode
.getSession().getRootNode());
105 // We remove those who are not part of the collection anymore
106 String queryString
= "//*[@" + ttrColProp
+ "='" + colId
+ "']";
107 Query query
= createQuery(queryString
, Query
.XPATH
);
108 log
.debug("Query :" + queryString
);
109 NodeIterator ni
= query
.execute().getNodes();
111 while (ni
.hasNext()) {
112 log
.debug("Node " + (++i
));
113 Node curNode
= ni
.nextNode();
114 String uuid
= curNode
.getProperty("uuid").getString();
115 boolean isPartOfTheSet
= false;
116 for (TreeTestResult ttr
: ttrCollection
.getResults()) {
117 if (uuid
.equals(ttr
.getUuid())) {
118 isPartOfTheSet
= true;
119 log
.debug("Node " + i
+ " found");
123 if (!isPartOfTheSet
) {
124 log
.debug("Node " + i
+ " not found. trying to remove");
125 curNode
.getProperty(ttrColProp
).remove();
129 } catch (RepositoryException e
) {
130 throw new SlcException("Cannot update TreeTestResultCollection "
135 public void delete(TreeTestResultCollection ttrCollection
) {
137 // FIXME: should not delete sub nodes
139 String colId
= ttrCollection
.getId();
140 NodeIterator ni
= resultNodesInCollection(colId
);
141 while (ni
.hasNext()) {
142 curNode
= ni
.nextNode();
146 } catch (Exception e
) {
147 throw new SlcException("Cannot delete TreeTestResultCollection "
152 public SortedSet
<TreeTestResultCollection
> listCollections() {
154 Map
<String
, TreeTestResultCollection
> lst
= new HashMap
<String
, TreeTestResultCollection
>();
155 NodeIterator nodeIterator
= query("//testresult");
156 while (nodeIterator
.hasNext()) {
157 Node node
= nodeIterator
.nextNode();
158 String colId
= property(node
, ttrColProp
);
160 if (!lst
.containsKey(colId
))
161 lst
.put(colId
, new TreeTestResultCollection(colId
));
162 TreeTestResultCollection ttrc
= lst
.get(colId
);
163 ttrc
.getResults().add((TreeTestResult
) nodeMapper
.load(node
));
166 return new TreeSet
<TreeTestResultCollection
>(lst
.values());
169 public void addResultToCollection(final TreeTestResultCollection ttrc
,
170 final String resultUuid
) {
174 String colId
= ttrc
.getId();
175 queryString
= "//testresult[@uuid='" + resultUuid
+ "']";
176 curNode
= singleNode(queryString
, Query
.XPATH
);
177 if (curNode
== null) {
178 throw new SlcException("Cannot find test result #" + resultUuid
);
180 curNode
.setProperty(ttrColProp
, colId
);
182 } catch (Exception e
) {
183 throw new SlcException("Cannot add TreeTestResult of Id "
184 + resultUuid
+ " to collection " + ttrc
, e
);
189 public void removeResultFromCollection(final TreeTestResultCollection ttrc
,
190 final String resultUuid
) {
194 queryString
= "//testresult[@uuid='" + resultUuid
+ "' and "
195 + ttrColProp
+ "='" + ttrc
.getId() + "']";
196 Query query
= createQuery(queryString
, Query
.XPATH
);
197 curNode
= JcrUtils
.querySingleNode(query
);
198 if (curNode
== null) {
199 throw new SlcException("Cannot find test result #" + resultUuid
);
201 curNode
.getProperty(ttrColProp
).remove();
204 } catch (RepositoryException e
) {
205 throw new SlcException("Cannot remove TreeTestResult of Id "
206 + resultUuid
+ " from collection " + ttrc
, e
);
210 // FIXME specify and implement this method
211 public List
<ResultAttributes
> listResultAttributes(String collectionId
) {
213 List
<ResultAttributes
> list
= new ArrayList
<ResultAttributes
>();
214 if (collectionId
== null) {
215 List
<TreeTestResult
> results
= asTreeTestResultList(resultNodes(
218 for (TreeTestResult ttr
: results
) {
219 list
.add(new ResultAttributes(ttr
));
222 NodeIterator nodeIterator
= resultNodesInCollection(collectionId
);
223 while (nodeIterator
.hasNext()) {
224 list
.add(new ResultAttributes((TreeTestResult
) nodeMapper
225 .load(nodeIterator
.nextNode())));
232 public List
<TreeTestResult
> listResults(String collectionId
,
233 Map
<String
, String
> attributes
) {
234 List
<TreeTestResult
> list
;
236 if (collectionId
== null) {
237 if (attributes
== null || attributes
.size() == 0)
238 list
= asTreeTestResultList(resultNodes(null, null));
239 else if (attributes
.size() == 1) {
240 Map
.Entry
<String
, String
> entry
= attributes
.entrySet()
242 list
= asTreeTestResultList(resultNodes(entry
.getKey(), entry
245 throw new SlcException(
246 "Multiple attributes filter are currently not supported.");
249 if (attributes
== null || attributes
.size() == 0)
250 list
= asTreeTestResultList(resultNodesInCollection(collectionId
));
251 else if (attributes
.size() == 1) {
252 Map
.Entry
<String
, String
> entry
= attributes
.entrySet()
254 list
= asTreeTestResultList(resultNodesInCollection(
255 collectionId
, entry
.getKey(), entry
.getValue()));
257 throw new SlcException(
258 "Multiple attributes filter are currently not supported.");
266 protected NodeIterator
resultNodesInCollection(String collectionId
,
267 String attributeKey
, String attributeValue
) {
268 String queryString
= "//testresult[@" + ttrColProp
+ "='"
269 + collectionId
+ "' and @" + attributeKey
+ "='"
270 + attributeValue
+ "']";
271 return query(queryString
);
274 protected NodeIterator
resultNodesInCollection(String collectionId
) {
275 String queryString
= "//testresult[@" + ttrColProp
+ "='"
276 + collectionId
+ "']";
277 return query(queryString
);
280 protected NodeIterator
resultNodes(String attributeKey
,
281 String attributeValue
) {
283 if (attributeKey
!= null)
284 queryString
= "//testresult[@" + attributeKey
+ "='"
285 + attributeValue
+ "']";
287 queryString
= "//testresult";
288 return query(queryString
);
291 protected List
<TreeTestResult
> asTreeTestResultList(
292 NodeIterator nodeIterator
) {
293 List
<TreeTestResult
> lst
= new ArrayList
<TreeTestResult
>();
294 while (nodeIterator
.hasNext()) {
295 lst
.add((TreeTestResult
) nodeMapper
.load(nodeIterator
.nextNode()));
300 private NodeIterator
query(String query
) {
302 if (log
.isDebugEnabled())
303 log
.debug("Retrieve nodes from query: " + query
);
304 Query q
= createQuery(query
, Query
.XPATH
);
305 return q
.execute().getNodes();
306 } catch (RepositoryException e
) {
307 throw new SlcException("Cannot load nodes from query: " + query
);
311 private String
property(Node node
, String key
) {
313 return node
.getProperty(key
).getString();
314 } catch (RepositoryException e
) {
315 log
.warn("Cannot retrieve property " + key
+ " of node " + node
, e
);
320 private Node
singleNode(String query
, String queryType
) {
321 Query q
= createQuery(query
, queryType
);
322 return JcrUtils
.querySingleNode(q
);