]> 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 JCR test result
[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.test.TestResult;
19 import org.argeo.slc.test.TestResultPart;
20 import org.argeo.slc.test.TestRun;
21 import org.argeo.slc.test.TestStatus;
22
23 /** {@link TestResult} wrapping a JCR node of type {@link SlcTypes#SLC_RESULT}. */
24 public class JcrTestResult implements TestResult, SlcNames {
25 /** Should only be set for an already existing result. */
26 private String uuid;
27 private Session session;
28 private String resultType = SlcTypes.SLC_RESULT;
29
30 /** cached for performance purposes */
31 private String nodeIdentifier = null;
32
33 public void init() {
34 try {
35 if (uuid == null) {
36 // create new result
37 uuid = UUID.randomUUID().toString();
38 String path = SlcJcrUtils.createResultPath(uuid);
39 Node resultNode = JcrUtils.mkdirs(session, path, resultType);
40 resultNode.setProperty(SLC_UUID, uuid);
41 session.save();
42 }
43 } catch (Exception e) {
44 JcrUtils.discardQuietly(session);
45 throw new SlcException("Cannot initialize JCR result", e);
46 }
47 }
48
49 public void destroy() {
50
51 }
52
53 public Node getNode() {
54 try {
55 Node resultNode;
56 if (nodeIdentifier != null) {
57 return session.getNodeByIdentifier(nodeIdentifier);
58 } else {
59 QueryManager qm = session.getWorkspace().getQueryManager();
60 Query q = qm.createQuery(
61 "select * from [slc:result] where [slc:uuid]='" + uuid
62 + "'", Query.JCR_SQL2);
63 resultNode = JcrUtils.querySingleNode(q);
64 if (resultNode != null)
65 nodeIdentifier = resultNode.getIdentifier();
66 }
67 return resultNode;
68 } catch (Exception e) {
69 throw new SlcException("Cannot get result node", e);
70 }
71 }
72
73 public void notifyTestRun(TestRun testRun) {
74 }
75
76 public void addResultPart(TestResultPart testResultPart) {
77 Node node = getNode();
78 try {
79 // TODO: find a better way to name it by default
80 String partName = Long.toString(System.currentTimeMillis());
81 Node resultPartNode = node.addNode(partName, SlcTypes.SLC_CHECK);
82 resultPartNode.setProperty(SLC_SUCCESS,
83 testResultPart.getStatus() == TestStatus.PASSED);
84 if (testResultPart.getMessage() != null)
85 resultPartNode.setProperty(SLC_MESSAGE,
86 testResultPart.getMessage());
87 if (testResultPart.getExceptionMessage() != null)
88 resultPartNode.setProperty(SLC_ERROR_MESSAGE,
89 testResultPart.getExceptionMessage());
90 JcrUtils.updateLastModified(node);
91 node.getSession().save();
92 } catch (Exception e) {
93 JcrUtils.discardUnderlyingSessionQuietly(node);
94 throw new SlcException("Cannot get UUID from " + node, e);
95 }
96
97 }
98
99 public String getUuid() {
100 Node node = getNode();
101 try {
102 return node.getProperty(SLC_UUID).getString();
103 } catch (Exception e) {
104 throw new SlcException("Cannot get UUID from " + node, e);
105 }
106 }
107
108 public void close() {
109 Node node = getNode();
110 try {
111 if (node.hasNode(SLC_COMPLETED))
112 return;
113 node.setProperty(SLC_COMPLETED, new GregorianCalendar());
114 JcrUtils.updateLastModified(node);
115 node.getSession().save();
116 } catch (Exception e) {
117 JcrUtils.discardUnderlyingSessionQuietly(node);
118 throw new SlcException("Cannot get close date from " + node, e);
119 }
120 }
121
122 public Date getCloseDate() {
123 Node node = getNode();
124 try {
125 if (!node.hasNode(SLC_COMPLETED))
126 return null;
127 return node.getProperty(SLC_COMPLETED).getDate().getTime();
128 } catch (Exception e) {
129 throw new SlcException("Cannot get close date from " + node, e);
130 }
131 }
132
133 public Map<String, String> getAttributes() {
134 Node node = getNode();
135 try {
136 Map<String, String> map = new HashMap<String, String>();
137 PropertyIterator pit = node.getProperties();
138 while (pit.hasNext()) {
139 Property p = pit.nextProperty();
140 if (!p.isMultiple())
141 map.put(p.getName(), p.getValue().getString());
142 }
143 return map;
144 } catch (Exception e) {
145 throw new SlcException("Cannot get close date from " + node, e);
146 }
147 }
148
149 public void setUuid(String uuid) {
150 this.uuid = uuid;
151 }
152
153 public void setSession(Session session) {
154 this.session = session;
155 }
156
157 public void setResultType(String resultType) {
158 this.resultType = resultType;
159 }
160
161 }