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