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