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