]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/dao/TreeTestResultCollectionDaoJcr.java
Introduce module meta data
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.jcr / src / main / java / org / argeo / slc / jcr / dao / TreeTestResultCollectionDaoJcr.java
1 /*
2 * Copyright (C) 2007-2012 Mathieu Baudier
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16 package org.argeo.slc.jcr.dao;
17
18 import java.util.ArrayList;
19 import java.util.HashMap;
20 import java.util.List;
21 import java.util.Map;
22 import java.util.SortedSet;
23 import java.util.TreeSet;
24
25 import javax.jcr.Node;
26 import javax.jcr.NodeIterator;
27 import javax.jcr.RepositoryException;
28 import javax.jcr.query.Query;
29
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;
38
39 /** JCR implementation of collections DAO. */
40 public class TreeTestResultCollectionDaoJcr extends AbstractSlcJcrDao implements
41 TreeTestResultCollectionDao {
42
43 // FIXME : we handle testResultCollection by adding a property called
44 // "TestResultCollectionId "
45 final private String ttrColProp = "collectionId";
46
47 private final static Log log = LogFactory
48 .getLog(TreeTestResultCollectionDaoJcr.class);
49
50 public void create(TreeTestResultCollection ttrCollection) {
51 try {
52 Node curNode;
53 String colId = ttrCollection.getId();
54 for (TreeTestResult ttr : ttrCollection.getResults()) {
55 curNode = nodeMapper.save(getSession(), basePath(ttr), ttr);
56 curNode.setProperty(ttrColProp, colId);
57 }
58 getSession().save();
59 } catch (RepositoryException e) {
60 throw new SlcException("Cannot create TreeTestResultCollection "
61 + ttrCollection, e);
62 }
63 }
64
65 public TreeTestResultCollection getTestResultCollection(String id) {
66 TreeTestResultCollection res = new TreeTestResultCollection();
67 res.setId(id);
68 NodeIterator ni = resultNodesInCollection(id);
69 while (ni.hasNext()) {
70 res.getResults().add(
71 (TreeTestResult) nodeMapper.load(ni.nextNode()));
72 }
73 return res;
74 }
75
76 /**
77 *
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.
83 */
84 public void update(TreeTestResultCollection ttrCollection) {
85 try {
86 log.debug("Update ");
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()
91 + "']";
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");
96 } else {
97 nodeMapper.update(curNode, ttr);
98 log.debug("Node updated");
99 }
100 log
101 .debug("-----------------------------------------------------------------");
102 curNode.setProperty(ttrColProp, colId);
103 JcrUtils.debug(curNode.getSession().getRootNode());
104 }
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();
110 int i = 0;
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");
120 break;
121 }
122 }
123 if (!isPartOfTheSet) {
124 log.debug("Node " + i + " not found. trying to remove");
125 curNode.getProperty(ttrColProp).remove();
126 }
127 }
128 getSession().save();
129 } catch (RepositoryException e) {
130 throw new SlcException("Cannot update TreeTestResultCollection "
131 + ttrCollection, e);
132 }
133 }
134
135 public void delete(TreeTestResultCollection ttrCollection) {
136 try {
137 // FIXME: should not delete sub nodes
138 Node curNode;
139 String colId = ttrCollection.getId();
140 NodeIterator ni = resultNodesInCollection(colId);
141 while (ni.hasNext()) {
142 curNode = ni.nextNode();
143 curNode.remove();
144 }
145 getSession().save();
146 } catch (Exception e) {
147 throw new SlcException("Cannot delete TreeTestResultCollection "
148 + ttrCollection, e);
149 }
150 }
151
152 public SortedSet<TreeTestResultCollection> listCollections() {
153 // FIXME: optimize
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);
159 if (colId != null) {
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));
164 }
165 }
166 return new TreeSet<TreeTestResultCollection>(lst.values());
167 }
168
169 public void addResultToCollection(final TreeTestResultCollection ttrc,
170 final String resultUuid) {
171 try {
172 String queryString;
173 Node curNode;
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);
179 } else
180 curNode.setProperty(ttrColProp, colId);
181 getSession().save();
182 } catch (Exception e) {
183 throw new SlcException("Cannot add TreeTestResult of Id "
184 + resultUuid + " to collection " + ttrc, e);
185 }
186
187 }
188
189 public void removeResultFromCollection(final TreeTestResultCollection ttrc,
190 final String resultUuid) {
191 try {
192 String queryString;
193 Node curNode;
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);
200 } else {
201 curNode.getProperty(ttrColProp).remove();
202 }
203 getSession().save();
204 } catch (RepositoryException e) {
205 throw new SlcException("Cannot remove TreeTestResult of Id "
206 + resultUuid + " from collection " + ttrc, e);
207 }
208 }
209
210 // FIXME specify and implement this method
211 public List<ResultAttributes> listResultAttributes(String collectionId) {
212 // FIXME: optimize
213 List<ResultAttributes> list = new ArrayList<ResultAttributes>();
214 if (collectionId == null) {
215 List<TreeTestResult> results = asTreeTestResultList(resultNodes(
216 null, null));
217
218 for (TreeTestResult ttr : results) {
219 list.add(new ResultAttributes(ttr));
220 }
221 } else {
222 NodeIterator nodeIterator = resultNodesInCollection(collectionId);
223 while (nodeIterator.hasNext()) {
224 list.add(new ResultAttributes((TreeTestResult) nodeMapper
225 .load(nodeIterator.nextNode())));
226 }
227 }
228
229 return list;
230 }
231
232 public List<TreeTestResult> listResults(String collectionId,
233 Map<String, String> attributes) {
234 List<TreeTestResult> list;
235
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()
241 .iterator().next();
242 list = asTreeTestResultList(resultNodes(entry.getKey(), entry
243 .getValue()));
244 } else {
245 throw new SlcException(
246 "Multiple attributes filter are currently not supported.");
247 }
248 } else {
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()
253 .iterator().next();
254 list = asTreeTestResultList(resultNodesInCollection(
255 collectionId, entry.getKey(), entry.getValue()));
256 } else {
257 throw new SlcException(
258 "Multiple attributes filter are currently not supported.");
259 }
260 }
261 return list;
262 }
263
264 // UTILITIES
265
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);
272 }
273
274 protected NodeIterator resultNodesInCollection(String collectionId) {
275 String queryString = "//testresult[@" + ttrColProp + "='"
276 + collectionId + "']";
277 return query(queryString);
278 }
279
280 protected NodeIterator resultNodes(String attributeKey,
281 String attributeValue) {
282 String queryString;
283 if (attributeKey != null)
284 queryString = "//testresult[@" + attributeKey + "='"
285 + attributeValue + "']";
286 else
287 queryString = "//testresult";
288 return query(queryString);
289 }
290
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()));
296 }
297 return lst;
298 }
299
300 private NodeIterator query(String query) {
301 try {
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);
308 }
309 }
310
311 private String property(Node node, String key) {
312 try {
313 return node.getProperty(key).getString();
314 } catch (RepositoryException e) {
315 log.warn("Cannot retrieve property " + key + " of node " + node, e);
316 return null;
317 }
318 }
319
320 private Node singleNode(String query, String queryType) {
321 Query q = createQuery(query, queryType);
322 return JcrUtils.querySingleNode(q);
323 }
324
325 }