]> git.argeo.org Git - gpl/argeo-slc.git/blob - runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/JcrTestResult.java
introduce a new view to display JcrResults has a tree.
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.jcr / src / main / java / org / argeo / slc / jcr / JcrTestResult.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;
17
18 import java.util.Date;
19 import java.util.GregorianCalendar;
20 import java.util.HashMap;
21 import java.util.Map;
22 import java.util.UUID;
23
24 import javax.jcr.Node;
25 import javax.jcr.Property;
26 import javax.jcr.PropertyIterator;
27 import javax.jcr.Repository;
28 import javax.jcr.Session;
29 import javax.jcr.query.Query;
30 import javax.jcr.query.QueryManager;
31
32 import org.argeo.jcr.JcrUtils;
33 import org.argeo.slc.SlcException;
34 import org.argeo.slc.core.attachment.Attachment;
35 import org.argeo.slc.core.attachment.AttachmentsEnabled;
36 import org.argeo.slc.test.TestResult;
37 import org.argeo.slc.test.TestResultPart;
38 import org.argeo.slc.test.TestRun;
39 import org.argeo.slc.test.TestStatus;
40
41 /**
42 * {@link TestResult} wrapping a JCR node of type
43 * {@link SlcTypes#SLC_TEST_RESULT}.
44 */
45 public class JcrTestResult implements TestResult, SlcNames, AttachmentsEnabled {
46 /** Should only be set for an already existing result. */
47 private String uuid;
48 private Repository repository;
49 private Session session;
50 private String resultType = SlcTypes.SLC_TEST_RESULT;
51
52 /** cached for performance purposes */
53 private String nodeIdentifier = null;
54
55 private Boolean logoutWhenDestroyed = true;
56
57 private Map<String, String> attributes = new HashMap<String, String>();
58
59 public void init() {
60 try {
61 session = repository.login();
62 if (uuid == null) {
63 // create new result
64 uuid = UUID.randomUUID().toString();
65 String path = SlcJcrUtils.createResultPath(session, uuid);
66 Node resultNode = JcrUtils.mkdirs(session, path, resultType);
67 resultNode.setProperty(SLC_UUID, uuid);
68 for (String attr : attributes.keySet()) {
69 String property = attr;
70 // compatibility with legacy applications
71 if ("testCase".equals(attr))
72 property = SLC_TEST_CASE;
73 else if ("testCaseType".equals(attr))
74 property = SLC_TEST_CASE_TYPE;
75 resultNode.setProperty(property, attributes.get(attr));
76 }
77 session.save();
78 }
79 } catch (Exception e) {
80 JcrUtils.discardQuietly(session);
81 throw new SlcException("Cannot initialize JCR result", e);
82 }
83 }
84
85 public void destroy() {
86 if (logoutWhenDestroyed)
87 JcrUtils.logoutQuietly(session);
88 }
89
90 public Node getNode() {
91 try {
92 Node resultNode;
93 if (nodeIdentifier != null) {
94 return session.getNodeByIdentifier(nodeIdentifier);
95 } else {
96 QueryManager qm = session.getWorkspace().getQueryManager();
97 Query q = qm.createQuery("select * from ["
98 + SlcTypes.SLC_TEST_RESULT + "] where [slc:uuid]='"
99 + uuid + "'", Query.JCR_SQL2);
100 resultNode = JcrUtils.querySingleNode(q);
101 if (resultNode != null)
102 nodeIdentifier = resultNode.getIdentifier();
103 }
104 return resultNode;
105 } catch (Exception e) {
106 throw new SlcException("Cannot get result node", e);
107 }
108 }
109
110 public void notifyTestRun(TestRun testRun) {
111 }
112
113 public void addResultPart(TestResultPart testResultPart) {
114 Node node = getNode();
115 try {
116 Node resultPartNode = node.addNode(SlcNames.SLC_STATUS,
117 SlcTypes.SLC_CHECK);
118 resultPartNode.setProperty(SLC_SUCCESS,
119 testResultPart.getStatus() == TestStatus.PASSED);
120 if (testResultPart.getMessage() != null)
121 resultPartNode.setProperty(SLC_MESSAGE,
122 testResultPart.getMessage());
123 if (testResultPart.getExceptionMessage() != null)
124 resultPartNode.setProperty(SLC_ERROR_MESSAGE,
125 testResultPart.getExceptionMessage());
126 JcrUtils.updateLastModified(node);
127 node.getSession().save();
128 } catch (Exception e) {
129 JcrUtils.discardUnderlyingSessionQuietly(node);
130 throw new SlcException("Cannot get UUID from " + node, e);
131 }
132
133 }
134
135 public String getUuid() {
136 Node node = getNode();
137 try {
138 return node.getProperty(SLC_UUID).getString();
139 } catch (Exception e) {
140 throw new SlcException("Cannot get UUID from " + node, e);
141 }
142 }
143
144 public void close() {
145 Node node = getNode();
146 try {
147 if (node.hasNode(SLC_COMPLETED))
148 return;
149 node.setProperty(SLC_COMPLETED, new GregorianCalendar());
150 JcrUtils.updateLastModified(node);
151 node.getSession().save();
152 } catch (Exception e) {
153 JcrUtils.discardUnderlyingSessionQuietly(node);
154 throw new SlcException("Cannot get close date from " + node, e);
155 }
156 if (logoutWhenDestroyed)
157 JcrUtils.logoutQuietly(session);
158 }
159
160 public Date getCloseDate() {
161 Node node = getNode();
162 try {
163 if (!node.hasNode(SLC_COMPLETED))
164 return null;
165 return node.getProperty(SLC_COMPLETED).getDate().getTime();
166 } catch (Exception e) {
167 throw new SlcException("Cannot get close date from " + node, e);
168 }
169 }
170
171 public Map<String, String> getAttributes() {
172 Node node = getNode();
173 try {
174 Map<String, String> map = new HashMap<String, String>();
175 PropertyIterator pit = node.getProperties();
176 while (pit.hasNext()) {
177 Property p = pit.nextProperty();
178 if (!p.isMultiple())
179 map.put(p.getName(), p.getValue().getString());
180 }
181 return map;
182 } catch (Exception e) {
183 throw new SlcException("Cannot get close date from " + node, e);
184 }
185 }
186
187 public void addAttachment(Attachment attachment) {
188 // TODO implement it
189 }
190
191 public void setUuid(String uuid) {
192 this.uuid = uuid;
193 }
194
195 public void setRepository(Repository repository) {
196 this.repository = repository;
197 }
198
199 public void setResultType(String resultType) {
200 this.resultType = resultType;
201 }
202
203 public void setAttributes(Map<String, String> attributes) {
204 if (uuid != null)
205 throw new SlcException(
206 "Attributes cannot be set on an already initialized test result."
207 + " Update the related JCR node directly instead.");
208 this.attributes = attributes;
209 }
210
211 // public void setLogoutWhenDestroyed(Boolean logoutWhenDestroyed) {
212 // this.logoutWhenDestroyed = logoutWhenDestroyed;
213 // }
214
215 }