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