]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/dao/TreeTestResultDaoJcr.java
JCR UI can run processes
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.jcr / src / main / java / org / argeo / slc / jcr / dao / TreeTestResultDaoJcr.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.Calendar;
21 import java.util.Date;
22 import java.util.GregorianCalendar;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.SortedMap;
26
27 import javax.jcr.Node;
28 import javax.jcr.NodeIterator;
29 import javax.jcr.RepositoryException;
30 import javax.jcr.Session;
31 import javax.jcr.query.Query;
32 import javax.jcr.query.QueryManager;
33 import javax.jcr.query.QueryResult;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37 import org.argeo.jcr.JcrUtils;
38 import org.argeo.slc.SlcException;
39 import org.argeo.slc.core.attachment.SimpleAttachment;
40 import org.argeo.slc.core.structure.tree.TreeSPath;
41 import org.argeo.slc.core.test.SimpleResultPart;
42 import org.argeo.slc.core.test.tree.TreeTestResult;
43 import org.argeo.slc.dao.test.tree.TreeTestResultDao;
44 import org.argeo.slc.structure.StructureElement;
45 import org.argeo.slc.test.TestResult;
46
47 /**
48 * The JCR implementation for tree-based result of the test result dao.
49 *
50 * @see TreeTestResult
51 */
52
53 public class TreeTestResultDaoJcr extends AbstractSlcJcrDao implements
54 TreeTestResultDao {
55
56 private final static Log log = LogFactory
57 .getLog(TreeTestResultDaoJcr.class);
58
59 public synchronized void create(TestResult testResult) {
60 try {
61 nodeMapper.save(getSession(), basePath(testResult), testResult);
62 getSession().save();
63 } catch (Exception e) {
64 throw new SlcException("Cannot create testResult " + testResult, e);
65 }
66 }
67
68 public synchronized void update(TestResult testResult) {
69 try {
70 nodeMapper.save(getSession(), basePath(testResult), testResult);
71 getSession().save();
72 } catch (Exception e) {
73 throw new SlcException("Cannot update testResult" + testResult, e);
74 }
75 }
76
77 public TreeTestResult getTestResult(String uuid) {
78 String queryString = "//testresult[@uuid='" + uuid + "']";
79 Query query = createQuery(queryString, Query.XPATH);
80 Node node = JcrUtils.querySingleNode(query);
81 if (node == null)
82 return null;
83 return (TreeTestResult) nodeMapper.load(node);
84
85 }
86
87 public List<TreeTestResult> listTestResults() {
88 try {
89 // TODO: optimize query
90 String queryString = "//testresult";
91 Query query = createQuery(queryString, Query.XPATH);
92 QueryResult queryResult = query.execute();
93 NodeIterator nodeIterator = queryResult.getNodes();
94 if (nodeIterator.hasNext()) {
95 List<TreeTestResult> list = new ArrayList<TreeTestResult>();
96 nodes: while (nodeIterator.hasNext()) {
97 Node curNode = (Node) nodeIterator.next();
98
99 // TODO improve architecture and get rid of this hack
100 if ("slc".equals(curNode.getParent().getName()))
101 continue nodes;
102
103 list.add((TreeTestResult) nodeMapper.load(curNode));
104 }
105 return list;
106 } else
107 return null;
108
109 } catch (RepositoryException e) {
110 throw new SlcException("Cannot load list of TestResult ", e);
111 }
112 }
113
114 public List<TreeTestResult> listResults(TreeSPath path) {
115 try {
116 // TODO: optimize query
117 String queryString = "//testresult" + path.getAsUniqueString();
118 Query query = createQuery(queryString, Query.XPATH);
119 QueryResult queryResult = query.execute();
120 NodeIterator nodeIterator = queryResult.getNodes();
121 if (nodeIterator.hasNext()) {
122 List<TreeTestResult> list = new ArrayList<TreeTestResult>();
123 while (nodeIterator.hasNext()) {
124 list.add((TreeTestResult) nodeMapper
125 .load((Node) nodeIterator.next()));
126 }
127 return list;
128 } else
129 return null;
130
131 } catch (RepositoryException e) {
132 throw new SlcException("Cannot load list of TestResult ", e);
133 }
134 }
135
136 public synchronized void close(final String testResultId,
137 final Date closeDate) {
138 try {
139 // TODO: optimize query
140 String queryString = "//testresult[@uuid='" + testResultId + "']";
141 Query query = createQuery(queryString, Query.XPATH);
142 Node resNode = JcrUtils.querySingleNode(query);
143 Calendar cal = new GregorianCalendar();
144 cal.setTime(closeDate);
145 if (resNode != null)
146 resNode.setProperty("closeDate", cal);
147 else if (log.isDebugEnabled())
148 log.debug("Cannot close because a node for test result # "
149 + testResultId + " was not found");
150 getSession().save();
151 } catch (RepositoryException e) {
152 throw new SlcException("Cannot close TestResult " + testResultId, e);
153 }
154
155 }
156
157 /**
158 * Add a SimpleResultPart to the TreeTestResult of ID testResultId at
159 * treeSPath path
160 *
161 * May also add some relatedElements
162 *
163 */
164 // TODO do we load objects, do treatment and persist them or do we work
165 // directly in JCR
166 public synchronized void addResultPart(final String testResultId,
167 final TreeSPath path, final SimpleResultPart resultPart,
168 final Map<TreeSPath, StructureElement> relatedElements) {
169
170 try {
171 // TODO: optimize query
172 String queryString = "//testresult[@uuid='" + testResultId + "']";
173 Query query = createQuery(queryString, Query.XPATH);
174 Node resNode = JcrUtils.querySingleNode(query);
175
176 Node curNode;
177 String usedPath = path.getAsUniqueString().substring(1)
178 + "/partsublist";
179
180 if (resNode.hasNode(usedPath))
181 curNode = resNode.getNode(usedPath);
182 else {
183
184 // TODO Factorize that
185 Node tmpNode = resNode;
186 String[] pathes = usedPath.split("/");
187 for (int i = 0; i < pathes.length; i++) {
188 if (tmpNode.hasNode(pathes[i]))
189 tmpNode = tmpNode.getNode(pathes[i]);
190 else
191 tmpNode = tmpNode.addNode(pathes[i]);
192 }
193 curNode = tmpNode;
194 }
195
196 nodeMapper.update(curNode.addNode("resultPart"), resultPart);
197
198 if (relatedElements != null) {
199 for (TreeSPath key : relatedElements.keySet()) {
200 String relPath = key.getAsUniqueString().substring(1);
201
202 // check if already exists.
203 if (!resNode.hasNode(relPath)) {
204
205 // TODO Factorize that
206 Node tmpNode = resNode;
207 String[] pathes = usedPath.split("/");
208 for (int i = 0; i < pathes.length; i++) {
209 if (tmpNode.hasNode(pathes[i]))
210 tmpNode = tmpNode.getNode(pathes[i]);
211 else
212 tmpNode = tmpNode.addNode(pathes[i]);
213 }
214 curNode = tmpNode;
215 } else
216 curNode = resNode.getNode(relPath);
217
218 curNode.setProperty("label", relatedElements.get(key)
219 .getLabel());
220 // We add the tags
221 Map<String, String> tags = relatedElements.get(key)
222 .getTags();
223
224 for (String tag : tags.keySet()) {
225 NodeIterator tagIt = curNode.getNodes("tag");
226 Node tagNode = null;
227 while (tagIt.hasNext()) {
228 Node n = tagIt.nextNode();
229 if (n.getProperty("name").getString().equals(tag)) {
230 tagNode = n;
231 }
232 }
233
234 if (tagNode == null) {
235 tagNode = curNode.addNode("tag");
236 tagNode.setProperty("name", tag);
237 }
238
239 tagNode.setProperty("value", tags.get(tag));
240
241 // remove forbidden characters
242 // String cleanTag =
243 // JcrUtils.removeForbiddenCharacters(tag);
244 // if (!cleanTag.equals(tag))
245 // log.warn("Tag '" + tag + "' persisted as '" +
246 // cleanTag
247 // + "'");
248 // childNode.setProperty(cleanTag, tags.get(tag));
249 }
250 // for (String tag : tags.keySet()) {
251 // String cleanTag = JcrUtils
252 // .removeForbiddenCharacters(tag);
253 // if (!cleanTag.equals(tag))
254 // log.warn("Tag '" + tag + "' persisted as '"
255 // + cleanTag + "'");
256 // curNode.setProperty(cleanTag, tags.get(tag));
257 // }
258
259 // We set the class in order to be able to retrieve
260 curNode.setProperty("class", StructureElement.class
261 .getName());
262 }
263 }
264 getSession().save();
265
266 } catch (RepositoryException e) {
267 throw new SlcException("Cannot add resultPart", e);
268 }
269 }
270
271 public synchronized void addAttachment(final String testResultId,
272 final SimpleAttachment attachment) {
273
274 try {
275 // TODO: optimize query
276 // Might not be OK.
277 // Do we have a notion of "currentNode" when we call JCRUtils one
278 // more time.
279
280 // Check if attachment already exists
281 String queryString = "//testresult[@uuid='" + testResultId + "']";
282 Query query = createQuery(queryString, Query.XPATH);
283 Node resNode = JcrUtils.querySingleNode(query);
284
285 queryString = ".//*[@uuid='" + attachment.getUuid() + "']";
286 query = createQuery(queryString, Query.XPATH);
287 Node atNode = JcrUtils.querySingleNode(query);
288
289 if (atNode != null) {
290 if (log.isDebugEnabled())
291 log.debug("Attachement already There ");
292 } else {
293 if (resNode.hasNode("attachments"))
294 atNode = resNode.getNode("attachments");
295 else {
296 atNode = resNode.addNode("attachments");
297 }
298 Node attachNode;
299 attachNode = atNode.addNode(attachment.getName());
300 attachNode.setProperty("uuid", attachment.getUuid());
301 attachNode.setProperty("contentType", attachment
302 .getContentType());
303 getSession().save();
304 }
305
306 } catch (RepositoryException e) {
307 throw new SlcException("Cannot Add Attachment to " + testResultId,
308 e);
309 }
310 }
311
312 protected TreeTestResult getTreeTestResult(Session session,
313 String testResultId) {
314 try {
315 String queryString = "//testresult[@uuid='" + testResultId + "']";
316 QueryManager qm = session.getWorkspace().getQueryManager();
317 Query query = qm.createQuery(queryString, Query.XPATH);
318 Node node = JcrUtils.querySingleNode(query);
319 if (node == null)
320 return null;
321 return (TreeTestResult) nodeMapper.load(node);
322
323 } catch (RepositoryException e) {
324 throw new SlcException("Cannot load TestResult with ID "
325 + testResultId + " For Session " + session, e);
326 }
327 }
328
329 public synchronized void updateAttributes(final String testResultId,
330 final SortedMap<String, String> attributes) {
331 try {
332 String queryString = "//testresult[@uuid='" + testResultId + "']";
333 Query query = createQuery(queryString, Query.XPATH);
334 Node node = JcrUtils.querySingleNode(query);
335
336 for (String key : attributes.keySet()) {
337 node.setProperty(key, attributes.get(key));
338 }
339 getSession().save();
340 } catch (RepositoryException e) {
341 throw new SlcException(
342 "Cannot update Attributes on TestResult with ID "
343 + testResultId, e);
344 }
345 }
346
347 }