2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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.
17 package org
.argeo
.slc
.jcr
.dao
;
19 import java
.util
.ArrayList
;
20 import java
.util
.HashMap
;
21 import java
.util
.List
;
23 import java
.util
.SortedSet
;
24 import java
.util
.TreeSet
;
26 import javax
.jcr
.Node
;
27 import javax
.jcr
.NodeIterator
;
28 import javax
.jcr
.RepositoryException
;
29 import javax
.jcr
.query
.Query
;
31 import org
.apache
.commons
.logging
.Log
;
32 import org
.apache
.commons
.logging
.LogFactory
;
33 import org
.argeo
.jcr
.JcrUtils
;
34 import org
.argeo
.slc
.SlcException
;
35 import org
.argeo
.slc
.core
.test
.tree
.ResultAttributes
;
36 import org
.argeo
.slc
.core
.test
.tree
.TreeTestResult
;
37 import org
.argeo
.slc
.core
.test
.tree
.TreeTestResultCollection
;
38 import org
.argeo
.slc
.dao
.test
.tree
.TreeTestResultCollectionDao
;
40 /** JCR implementation of collections DAO. */
41 public class TreeTestResultCollectionDaoJcr
extends AbstractSlcJcrDao
implements
42 TreeTestResultCollectionDao
{
44 // FIXME : we handle testResultCollection by adding a property called
45 // "TestResultCollectionId "
46 final private String ttrColProp
= "collectionId";
48 private final static Log log
= LogFactory
49 .getLog(TreeTestResultCollectionDaoJcr
.class);
51 public void create(TreeTestResultCollection ttrCollection
) {
54 String colId
= ttrCollection
.getId();
55 for (TreeTestResult ttr
: ttrCollection
.getResults()) {
56 curNode
= nodeMapper
.save(getSession(), basePath(ttr
), ttr
);
57 curNode
.setProperty(ttrColProp
, colId
);
60 } catch (RepositoryException e
) {
61 throw new SlcException("Cannot create TreeTestResultCollection "
66 public TreeTestResultCollection
getTestResultCollection(String id
) {
67 TreeTestResultCollection res
= new TreeTestResultCollection();
69 NodeIterator ni
= resultNodesInCollection(id
);
70 while (ni
.hasNext()) {
72 (TreeTestResult
) nodeMapper
.load(ni
.nextNode()));
79 * FIXME : validate what this method must really do ? what happen if one of
80 * the TreeTestResult of the collection is not found in the jcr repository?
81 * Now we create ttr that are not found and update existing ones.
82 * FurtherMore if a TreeTestResult is persisted as member the collection but
83 * is not in the object passed, it is removed.
85 public void update(TreeTestResultCollection ttrCollection
) {
88 String colId
= ttrCollection
.getId();
89 // We add or update existing ones
90 for (TreeTestResult ttr
: ttrCollection
.getResults()) {
91 String queryString
= "//testresult[@uuid='" + ttr
.getUuid()
93 Node curNode
= singleNode(queryString
, Query
.XPATH
);
94 if (curNode
== null) {
95 curNode
= nodeMapper
.save(getSession(), basePath(ttr
), ttr
);
96 log
.debug("New Node added");
98 nodeMapper
.update(curNode
, ttr
);
99 log
.debug("Node updated");
102 .debug("-----------------------------------------------------------------");
103 curNode
.setProperty(ttrColProp
, colId
);
104 JcrUtils
.debug(curNode
.getSession().getRootNode());
106 // We remove those who are not part of the collection anymore
107 String queryString
= "//*[@" + ttrColProp
+ "='" + colId
+ "']";
108 Query query
= createQuery(queryString
, Query
.XPATH
);
109 log
.debug("Query :" + queryString
);
110 NodeIterator ni
= query
.execute().getNodes();
112 while (ni
.hasNext()) {
113 log
.debug("Node " + (++i
));
114 Node curNode
= ni
.nextNode();
115 String uuid
= curNode
.getProperty("uuid").getString();
116 boolean isPartOfTheSet
= false;
117 for (TreeTestResult ttr
: ttrCollection
.getResults()) {
118 if (uuid
.equals(ttr
.getUuid())) {
119 isPartOfTheSet
= true;
120 log
.debug("Node " + i
+ " found");
124 if (!isPartOfTheSet
) {
125 log
.debug("Node " + i
+ " not found. trying to remove");
126 curNode
.getProperty(ttrColProp
).remove();
130 } catch (RepositoryException e
) {
131 throw new SlcException("Cannot update TreeTestResultCollection "
136 public void delete(TreeTestResultCollection ttrCollection
) {
138 // FIXME: should not delete sub nodes
140 String colId
= ttrCollection
.getId();
141 NodeIterator ni
= resultNodesInCollection(colId
);
142 while (ni
.hasNext()) {
143 curNode
= ni
.nextNode();
147 } catch (Exception e
) {
148 throw new SlcException("Cannot delete TreeTestResultCollection "
153 public SortedSet
<TreeTestResultCollection
> listCollections() {
155 Map
<String
, TreeTestResultCollection
> lst
= new HashMap
<String
, TreeTestResultCollection
>();
156 NodeIterator nodeIterator
= query("//testresult");
157 while (nodeIterator
.hasNext()) {
158 Node node
= nodeIterator
.nextNode();
159 String colId
= property(node
, ttrColProp
);
161 if (!lst
.containsKey(colId
))
162 lst
.put(colId
, new TreeTestResultCollection(colId
));
163 TreeTestResultCollection ttrc
= lst
.get(colId
);
164 ttrc
.getResults().add((TreeTestResult
) nodeMapper
.load(node
));
167 return new TreeSet
<TreeTestResultCollection
>(lst
.values());
170 public void addResultToCollection(final TreeTestResultCollection ttrc
,
171 final String resultUuid
) {
175 String colId
= ttrc
.getId();
176 queryString
= "//testresult[@uuid='" + resultUuid
+ "']";
177 curNode
= singleNode(queryString
, Query
.XPATH
);
178 if (curNode
== null) {
179 throw new SlcException("Cannot find test result #" + resultUuid
);
181 curNode
.setProperty(ttrColProp
, colId
);
183 } catch (Exception e
) {
184 throw new SlcException("Cannot add TreeTestResult of Id "
185 + resultUuid
+ " to collection " + ttrc
, e
);
190 public void removeResultFromCollection(final TreeTestResultCollection ttrc
,
191 final String resultUuid
) {
195 queryString
= "//testresult[@uuid='" + resultUuid
+ "' and "
196 + ttrColProp
+ "='" + ttrc
.getId() + "']";
197 Query query
= createQuery(queryString
, Query
.XPATH
);
198 curNode
= JcrUtils
.querySingleNode(query
);
199 if (curNode
== null) {
200 throw new SlcException("Cannot find test result #" + resultUuid
);
202 curNode
.getProperty(ttrColProp
).remove();
205 } catch (RepositoryException e
) {
206 throw new SlcException("Cannot remove TreeTestResult of Id "
207 + resultUuid
+ " from collection " + ttrc
, e
);
211 // FIXME specify and implement this method
212 public List
<ResultAttributes
> listResultAttributes(String collectionId
) {
214 List
<ResultAttributes
> list
= new ArrayList
<ResultAttributes
>();
215 if (collectionId
== null) {
216 List
<TreeTestResult
> results
= asTreeTestResultList(resultNodes(
219 for (TreeTestResult ttr
: results
) {
220 list
.add(new ResultAttributes(ttr
));
223 NodeIterator nodeIterator
= resultNodesInCollection(collectionId
);
224 while (nodeIterator
.hasNext()) {
225 list
.add(new ResultAttributes((TreeTestResult
) nodeMapper
226 .load(nodeIterator
.nextNode())));
233 public List
<TreeTestResult
> listResults(String collectionId
,
234 Map
<String
, String
> attributes
) {
235 List
<TreeTestResult
> list
;
237 if (collectionId
== null) {
238 if (attributes
== null || attributes
.size() == 0)
239 list
= asTreeTestResultList(resultNodes(null, null));
240 else if (attributes
.size() == 1) {
241 Map
.Entry
<String
, String
> entry
= attributes
.entrySet()
243 list
= asTreeTestResultList(resultNodes(entry
.getKey(), entry
246 throw new SlcException(
247 "Multiple attributes filter are currently not supported.");
250 if (attributes
== null || attributes
.size() == 0)
251 list
= asTreeTestResultList(resultNodesInCollection(collectionId
));
252 else if (attributes
.size() == 1) {
253 Map
.Entry
<String
, String
> entry
= attributes
.entrySet()
255 list
= asTreeTestResultList(resultNodesInCollection(
256 collectionId
, entry
.getKey(), entry
.getValue()));
258 throw new SlcException(
259 "Multiple attributes filter are currently not supported.");
267 protected NodeIterator
resultNodesInCollection(String collectionId
,
268 String attributeKey
, String attributeValue
) {
269 String queryString
= "//testresult[@" + ttrColProp
+ "='"
270 + collectionId
+ "' and @" + attributeKey
+ "='"
271 + attributeValue
+ "']";
272 return query(queryString
);
275 protected NodeIterator
resultNodesInCollection(String collectionId
) {
276 String queryString
= "//testresult[@" + ttrColProp
+ "='"
277 + collectionId
+ "']";
278 return query(queryString
);
281 protected NodeIterator
resultNodes(String attributeKey
,
282 String attributeValue
) {
284 if (attributeKey
!= null)
285 queryString
= "//testresult[@" + attributeKey
+ "='"
286 + attributeValue
+ "']";
288 queryString
= "//testresult";
289 return query(queryString
);
292 protected List
<TreeTestResult
> asTreeTestResultList(
293 NodeIterator nodeIterator
) {
294 List
<TreeTestResult
> lst
= new ArrayList
<TreeTestResult
>();
295 while (nodeIterator
.hasNext()) {
296 lst
.add((TreeTestResult
) nodeMapper
.load(nodeIterator
.nextNode()));
301 private NodeIterator
query(String query
) {
303 if (log
.isDebugEnabled())
304 log
.debug("Retrieve nodes from query: " + query
);
305 Query q
= createQuery(query
, Query
.XPATH
);
306 return q
.execute().getNodes();
307 } catch (RepositoryException e
) {
308 throw new SlcException("Cannot load nodes from query: " + query
);
312 private String
property(Node node
, String key
) {
314 return node
.getProperty(key
).getString();
315 } catch (RepositoryException e
) {
316 log
.warn("Cannot retrieve property " + key
+ " of node " + node
, e
);
321 private Node
singleNode(String query
, String queryType
) {
322 Query q
= createQuery(query
, queryType
);
323 return JcrUtils
.querySingleNode(q
);