]> git.argeo.org Git - gpl/argeo-slc.git/blob - TreeTestResultCollectionDaoJcr.java
33e4ffbf5ac8a06ddf9fbd2137eec1e259da73cd
[gpl/argeo-slc.git] / TreeTestResultCollectionDaoJcr.java
1 package org.argeo.slc.jcr.dao;
2
3 import java.util.List;
4 import java.util.Map;
5 import java.util.SortedSet;
6
7 import javax.jcr.Node;
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;
13
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;
23
24 /** JCR implementation of collections DAO. */
25 public class TreeTestResultCollectionDaoJcr extends AbstractSlcJcrDao implements
26 TreeTestResultCollectionDao {
27
28 // FIXME : we handle testResultCollection by adding a property called
29 // "TestResultCollectionId "
30 final private String ttrColProp = "TestResultCollectionId";
31
32 private final static Log log = LogFactory
33 .getLog(TreeTestResultCollectionDaoJcr.class);
34
35 private Workspace workspace;
36 private QueryManager queryManager;
37 private NodeMapper nodeMapper;
38
39 public void init() {
40 try {
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);
46 }
47 }
48
49 public void create(TreeTestResultCollection ttrCollection) {
50 try {
51 Node curNode;
52 String colId = ttrCollection.getId();
53 for (TreeTestResult ttr : ttrCollection.getResults()) {
54 curNode = nodeMapper.save(getSession(), basePath(ttr), ttr);
55 curNode.setProperty(ttrColProp, colId);
56 }
57 getSession().save();
58 } catch (Exception e) {
59 throw new SlcException("Cannot create TreeTestResultCollection "
60 + ttrCollection, e);
61 }
62 }
63
64 public TreeTestResultCollection getTestResultCollection(String id) {
65 try {
66 TreeTestResultCollection res = new TreeTestResultCollection();
67 res.setId(id);
68
69 String queryString = "//*[@" + ttrColProp + "='" + id + "']";
70 Query query = queryManager.createQuery(queryString, Query.XPATH);
71 log.debug("retrieving all nodes of a col - " + queryString);
72 int i = 0;
73 NodeIterator ni = query.execute().getNodes();
74 while (ni.hasNext()) {
75 i++;
76 res.getResults().add(
77 (TreeTestResult) nodeMapper.load(ni.nextNode()));
78 }
79 log.debug(i + " nodes found");
80 return res;
81 } catch (RepositoryException e) {
82 throw new SlcException(
83 "Cannot get TreeTestResultCollection for id " + id, e);
84 }
85 }
86
87 /**
88 *
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.
94 */
95 public void update(TreeTestResultCollection ttrCollection) {
96 try {
97 log.debug("Update ");
98 String queryString;
99 Query query;
100 Node curNode;
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");
110 } else {
111 nodeMapper.update(curNode, ttr);
112 log.debug("Node updated");
113 }
114 log.debug("-----------------------------------------------------------------");
115 curNode.setProperty(ttrColProp, colId);
116 JcrUtils.debug(curNode.getSession().getRootNode());
117 }
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();
123 int i = 0;
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");
133 break;
134 }
135 }
136 if (!isPartOfTheSet) {
137 log.debug("Node " + i + " not found. trying to remove");
138 curNode.getProperty(ttrColProp).remove();
139 }
140 }
141 getSession().save();
142 } catch (Exception e) {
143 throw new SlcException("Cannot update TreeTestResultCollection "
144 + ttrCollection, e);
145 }
146 }
147
148 public void delete(TreeTestResultCollection ttrCollection) {
149 try {
150 Node curNode;
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();
157 curNode.remove();
158 }
159 getSession().save();
160 } catch (Exception e) {
161 throw new SlcException("Cannot delete TreeTestResultCollection "
162 + ttrCollection, e);
163 }
164 }
165
166 // FIXME Implement this method
167 public SortedSet<TreeTestResultCollection> listCollections() {
168 // return new TreeSet<TreeTestResultCollection>(getHibernateTemplate()
169 // .find("from TreeTestResultCollection"));
170 return null;
171 }
172
173 public void addResultToCollection(final TreeTestResultCollection ttrc,
174 final String resultUuid) {
175 try {
176 String queryString;
177 Node curNode;
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);
185 } else
186 curNode.setProperty(ttrColProp, colId);
187 getSession().save();
188 } catch (Exception e) {
189 throw new SlcException("Cannot add TreeTestResult of Id "
190 + resultUuid + " to collection " + ttrc, e);
191 }
192
193 }
194
195 public void removeResultFromCollection(final TreeTestResultCollection ttrc,
196 final String resultUuid) {
197 try {
198 log.debug("remove result");
199 String queryString;
200 Node curNode;
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);
210 } else {
211 curNode.getProperty(ttrColProp).remove();
212 log.debug("Property removed : "
213 + curNode.getProperty(ttrColProp).getString());
214 }
215 getSession().save();
216 } catch (Exception e) {
217 throw new SlcException("Cannot remove TreeTestResult of Id "
218 + resultUuid + " from collection " + ttrc, e);
219 }
220 }
221
222 // FIXME specify and implement this method
223 public List<ResultAttributes> listResultAttributes(String collectionId) {
224 /**
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()
229 * .find(
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);
233 *
234 * return list;
235 */
236 return null;
237 }
238
239 // FIXME specify and implement this method
240
241 public List<TreeTestResult> listResults(String collectionId,
242 Map<String, String> attributes) {
243 /**
244 * List<TreeTestResult> list;
245 *
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."); } }
267 * return list;
268 */
269 return null;
270 }
271 }