1 package org
.argeo
.slc
.jcr
.dao
;
5 import java
.util
.SortedSet
;
8 import javax
.jcr
.NodeIterator
;
9 import javax
.jcr
.RepositoryException
;
10 import javax
.jcr
.Workspace
;
11 import javax
.jcr
.query
.Query
;
12 import javax
.jcr
.query
.QueryManager
;
14 import org
.apache
.commons
.logging
.Log
;
15 import org
.apache
.commons
.logging
.LogFactory
;
16 import org
.argeo
.jcr
.JcrUtils
;
17 import org
.argeo
.jcr
.NodeMapper
;
18 import org
.argeo
.slc
.SlcException
;
19 import org
.argeo
.slc
.core
.test
.tree
.ResultAttributes
;
20 import org
.argeo
.slc
.core
.test
.tree
.TreeTestResult
;
21 import org
.argeo
.slc
.core
.test
.tree
.TreeTestResultCollection
;
22 import org
.argeo
.slc
.dao
.test
.tree
.TreeTestResultCollectionDao
;
24 /** JCR implementation of collections DAO. */
25 public class TreeTestResultCollectionDaoJcr
extends AbstractSlcJcrDao
implements
26 TreeTestResultCollectionDao
{
28 // FIXME : we handle testResultCollection by adding a property called
29 // "TestResultCollectionId "
30 final private String ttrColProp
= "TestResultCollectionId";
32 private final static Log log
= LogFactory
33 .getLog(TreeTestResultCollectionDaoJcr
.class);
35 private Workspace workspace
;
36 private QueryManager queryManager
;
37 private NodeMapper nodeMapper
;
41 workspace
= getSession().getWorkspace();
42 queryManager
= workspace
.getQueryManager();
43 nodeMapper
= getNodeMapperProvider().findNodeMapper(null);
44 } catch (RepositoryException e
) {
45 throw new SlcException("Cannot initialize DAO", e
);
49 public void create(TreeTestResultCollection ttrCollection
) {
52 String colId
= ttrCollection
.getId();
53 for (TreeTestResult ttr
: ttrCollection
.getResults()) {
54 curNode
= nodeMapper
.save(getSession(), basePath(ttr
), ttr
);
55 curNode
.setProperty(ttrColProp
, colId
);
58 } catch (Exception e
) {
59 throw new SlcException("Cannot create TreeTestResultCollection "
64 public TreeTestResultCollection
getTestResultCollection(String id
) {
66 TreeTestResultCollection res
= new TreeTestResultCollection();
69 String queryString
= "//*[@" + ttrColProp
+ "='" + id
+ "']";
70 Query query
= queryManager
.createQuery(queryString
, Query
.XPATH
);
71 log
.debug("retrieving all nodes of a col - " + queryString
);
73 NodeIterator ni
= query
.execute().getNodes();
74 while (ni
.hasNext()) {
77 (TreeTestResult
) nodeMapper
.load(ni
.nextNode()));
79 log
.debug(i
+ " nodes found");
81 } catch (RepositoryException e
) {
82 throw new SlcException(
83 "Cannot get TreeTestResultCollection for id " + id
, e
);
89 * FIXME : validate what this method must really do ? what happen if one of
90 * the TreeTestResult of the collection is not found in the jcr repository?
91 * Now we create ttr that are not found and update existing ones.
92 * FurtherMore if a TreeTestResult is persisted as member the collection but
93 * is not in the object passed, it is removed.
95 public void update(TreeTestResultCollection ttrCollection
) {
101 String colId
= ttrCollection
.getId();
102 // We add or update existing ones
103 for (TreeTestResult ttr
: ttrCollection
.getResults()) {
104 queryString
= "//*[@uuid='" + ttr
.getUuid() + "']";
105 query
= queryManager
.createQuery(queryString
, Query
.XPATH
);
106 curNode
= JcrUtils
.querySingleNode(query
);
107 if (curNode
== null) {
108 curNode
= nodeMapper
.save(getSession(), basePath(ttr
), ttr
);
109 log
.debug("New Node added");
111 nodeMapper
.update(curNode
, ttr
);
112 log
.debug("Node updated");
114 log
.debug("-----------------------------------------------------------------");
115 curNode
.setProperty(ttrColProp
, colId
);
116 JcrUtils
.debug(curNode
.getSession().getRootNode());
118 // We remove those who are not part of the collection anymore
119 queryString
= "//*[@" + ttrColProp
+ "='" + colId
+ "']";
120 query
= queryManager
.createQuery(queryString
, Query
.XPATH
);
121 log
.debug("Query :" + queryString
);
122 NodeIterator ni
= query
.execute().getNodes();
124 while (ni
.hasNext()) {
125 log
.debug("Node " + (++i
));
126 curNode
= ni
.nextNode();
127 String uuid
= curNode
.getProperty("uuid").getString();
128 boolean isPartOfTheSet
= false;
129 for (TreeTestResult ttr
: ttrCollection
.getResults()) {
130 if (uuid
.equals(ttr
.getUuid())) {
131 isPartOfTheSet
= true;
132 log
.debug("Node " + i
+ " found");
136 if (!isPartOfTheSet
) {
137 log
.debug("Node " + i
+ " not found. trying to remove");
138 curNode
.getProperty(ttrColProp
).remove();
142 } catch (Exception e
) {
143 throw new SlcException("Cannot update TreeTestResultCollection "
148 public void delete(TreeTestResultCollection ttrCollection
) {
151 String colId
= ttrCollection
.getId();
152 String queryString
= "//*[@" + ttrColProp
+ "='" + colId
+ "']";
153 Query query
= queryManager
.createQuery(queryString
, Query
.XPATH
);
154 NodeIterator ni
= query
.execute().getNodes();
155 while (ni
.hasNext()) {
156 curNode
= ni
.nextNode();
160 } catch (Exception e
) {
161 throw new SlcException("Cannot delete TreeTestResultCollection "
166 // FIXME Implement this method
167 public SortedSet
<TreeTestResultCollection
> listCollections() {
168 // return new TreeSet<TreeTestResultCollection>(getHibernateTemplate()
169 // .find("from TreeTestResultCollection"));
173 public void addResultToCollection(final TreeTestResultCollection ttrc
,
174 final String resultUuid
) {
178 String colId
= ttrc
.getId();
179 queryString
= "//*[@uuid='" + resultUuid
+ "']";
180 Query query
= queryManager
.createQuery(queryString
, Query
.XPATH
);
181 curNode
= JcrUtils
.querySingleNode(query
);
182 if (curNode
== null) {
183 throw new SlcException("Cannot add TreeTestResult of Id "
184 + resultUuid
+ " to collection " + colId
);
186 curNode
.setProperty(ttrColProp
, colId
);
188 } catch (Exception e
) {
189 throw new SlcException("Cannot add TreeTestResult of Id "
190 + resultUuid
+ " to collection " + ttrc
, e
);
195 public void removeResultFromCollection(final TreeTestResultCollection ttrc
,
196 final String resultUuid
) {
198 log
.debug("remove result");
201 String colId
= ttrc
.getId();
202 queryString
= "//*[@uuid='" + resultUuid
+ "']";
203 Query query
= queryManager
.createQuery(queryString
, Query
.XPATH
);
204 curNode
= JcrUtils
.querySingleNode(query
);
205 log
.debug("Query : " + queryString
+ " - Node retrieved "
206 + curNode
.getPath());
207 if (curNode
== null) {
208 throw new SlcException("Cannot remove TreeTestResult of Id "
209 + resultUuid
+ " from collection " + colId
);
211 curNode
.getProperty(ttrColProp
).remove();
212 log
.debug("Property removed : "
213 + curNode
.getProperty(ttrColProp
).getString());
216 } catch (Exception e
) {
217 throw new SlcException("Cannot remove TreeTestResult of Id "
218 + resultUuid
+ " from collection " + ttrc
, e
);
222 // FIXME specify and implement this method
223 public List
<ResultAttributes
> listResultAttributes(String collectionId
) {
225 * List<ResultAttributes> list; if (collectionId == null) list =
226 * getHibernateTemplate().find(
227 * "select new org.argeo.slc.core.test.tree.ResultAttributes(ttr)" +
228 * " from TreeTestResult ttr"); else list = getHibernateTemplate()
230 * "select new org.argeo.slc.core.test.tree.ResultAttributes(ttr) " +
231 * " from TreeTestResult ttr, TreeTestResultCollection ttrc " +
232 * " where ttr in elements(ttrc.results) and ttrc.id=?", collectionId);
239 // FIXME specify and implement this method
241 public List
<TreeTestResult
> listResults(String collectionId
,
242 Map
<String
, String
> attributes
) {
244 * List<TreeTestResult> list;
246 * if (collectionId == null) { if (attributes == null ||
247 * attributes.size() == 0) list =
248 * getHibernateTemplate().find("from TreeTestResult"); else if
249 * (attributes.size() == 1) { Map.Entry<String, String> entry =
250 * attributes.entrySet() .iterator().next(); Object[] args = {
251 * entry.getKey(), entry.getValue() }; list =
252 * getHibernateTemplate().find( "select ttr from TreeTestResult ttr" +
253 * " where attributes[?]=?", args); } else { throw new SlcException(
254 * "Multiple attributes filter are currently not supported."); } } else
255 * { if (attributes == null || attributes.size() == 0) list =
256 * getHibernateTemplate() .find( "select ttr " +
257 * " from TreeTestResult ttr, TreeTestResultCollection ttrc " +
258 * " where ttr in elements(ttrc.results) and ttrc.id=?", collectionId);
259 * else if (attributes.size() == 1) { Map.Entry<String, String> entry =
260 * attributes.entrySet() .iterator().next(); Object[] args = {
261 * collectionId, entry.getKey(), entry.getValue() }; list =
262 * getHibernateTemplate() .find(
263 * "select ttr from TreeTestResult ttr, TreeTestResultCollection ttrc "
264 * + " where ttr in elements(ttrc.results) and ttrc.id=?" +
265 * " and attributes[?]=?", args); } else { throw new SlcException(
266 * "Multiple attributes filter are currently not supported."); } }