1 package org
.argeo
.slc
.jcr
.execution
;
3 import java
.util
.Collections
;
4 import java
.util
.GregorianCalendar
;
5 import java
.util
.HashMap
;
9 import javax
.jcr
.Property
;
10 import javax
.jcr
.RepositoryException
;
11 import javax
.jcr
.Session
;
12 import javax
.jcr
.query
.Query
;
14 import org
.apache
.commons
.logging
.Log
;
15 import org
.apache
.commons
.logging
.LogFactory
;
16 import org
.argeo
.jcr
.JcrUtils
;
17 import org
.argeo
.slc
.core
.attachment
.Attachment
;
18 import org
.argeo
.slc
.core
.structure
.tree
.TreeSPath
;
19 import org
.argeo
.slc
.core
.test
.tree
.TreeTestResult
;
20 import org
.argeo
.slc
.core
.test
.tree
.TreeTestResultListener
;
21 import org
.argeo
.slc
.jcr
.SlcJcrUtils
;
22 import org
.argeo
.slc
.jcr
.SlcNames
;
23 import org
.argeo
.slc
.jcr
.SlcTypes
;
24 import org
.argeo
.slc
.test
.TestResultPart
;
25 import org
.argeo
.slc
.test
.TestStatus
;
27 /** Persists results in JCR */
28 public class JcrResultListener
implements TreeTestResultListener
, SlcNames
{
29 private final static Log log
= LogFactory
.getLog(JcrResultListener
.class);
31 private Session session
;
33 /** Caches the mapping between SLC uuids and internal JCR identifiers */
34 private Map
<String
, String
> uuidToIdentifier
= Collections
35 .synchronizedMap(new HashMap
<String
, String
>());
37 public void resultPartAdded(TreeTestResult testResult
,
38 TestResultPart testResultPart
) {
40 String uuid
= testResult
.getUuid();
41 Node resultNode
= getResultNode(uuid
);
42 if (resultNode
== null) {
43 resultNode
= createResultNode(testResult
);
46 String partParentPath
;
47 TreeSPath currentPath
= testResult
.getCurrentPath();
48 if (currentPath
!= null) {
49 String subPath
= currentPath
.getAsUniqueString();
50 partParentPath
= resultNode
.getPath() + subPath
;
52 partParentPath
= resultNode
.getPath();
53 // TODO create some depth?
57 if (session
.itemExists(partParentPath
)) {
58 partParentNode
= session
.getNode(partParentPath
);
60 partParentNode
= JcrUtils
.mkdirs(session
, partParentPath
);
64 // TODO find a better name
65 String partNodeName
= Long
.toString(System
.currentTimeMillis());
66 Node resultPartNode
= partParentNode
.addNode(partNodeName
,
68 resultPartNode
.setProperty(SLC_SUCCESS
,
69 testResultPart
.getStatus() == TestStatus
.PASSED
);
70 if (testResultPart
.getMessage() != null)
71 resultPartNode
.setProperty(SLC_MESSAGE
,
72 testResultPart
.getMessage());
73 if (testResultPart
.getExceptionMessage() != null)
74 resultPartNode
.setProperty(SLC_ERROR_MESSAGE
,
75 testResultPart
.getExceptionMessage());
76 // JcrUtils.debug(resultPartNode);
78 JcrUtils
.updateLastModified(resultNode
);
81 } catch (RepositoryException e
) {
82 JcrUtils
.discardQuietly(session
);
83 log
.error("Cannot add result part " + testResultPart
+ " to "
85 // throw new SlcException("Cannot add result part " + testResultPart
86 // + " to " + testResult, e);
91 /** @return null if does not exist */
92 protected Node
getResultNode(String uuid
) throws RepositoryException
{
94 if (uuidToIdentifier
.containsKey(uuid
)) {
95 return session
.getNodeByIdentifier(uuidToIdentifier
.get(uuid
));
101 "select * from [slc:result] where [slc:uuid]='"
102 + uuid
+ "'", Query
.JCR_SQL2
);
103 resultNode
= JcrUtils
.querySingleNode(q
);
104 if (resultNode
!= null)
105 uuidToIdentifier
.put(uuid
, resultNode
.getIdentifier());
110 protected Node
createResultNode(TreeTestResult testResult
)
111 throws RepositoryException
{
112 String uuid
= testResult
.getUuid();
113 String path
= SlcJcrUtils
.createResultPath(uuid
);
114 Node resultNode
= JcrUtils
.mkdirs(session
, path
, SlcTypes
.SLC_RESULT
);
115 resultNode
.setProperty(SLC_UUID
, uuid
);
116 for (Map
.Entry
<String
, String
> entry
: testResult
.getAttributes()
118 resultNode
.setProperty(entry
.getKey(), entry
.getValue());
121 uuidToIdentifier
.put(uuid
, resultNode
.getIdentifier());
125 public void close(TreeTestResult testResult
) {
127 String uuid
= testResult
.getUuid();
128 Node resultNode
= getResultNode(uuid
);
129 if (resultNode
== null)
130 resultNode
= createResultNode(testResult
);
131 JcrUtils
.updateLastModified(resultNode
);
132 GregorianCalendar closeDate
= new GregorianCalendar();
133 closeDate
.setTime(testResult
.getCloseDate());
134 resultNode
.setProperty(SLC_COMPLETED
, closeDate
);
136 uuidToIdentifier
.remove(uuid
);
138 } catch (RepositoryException e
) {
139 JcrUtils
.discardQuietly(session
);
140 log
.error("Cannot close result " + testResult
, e
);
141 // throw new SlcException("Cannot close result " + testResult, e);
146 public void addAttachment(TreeTestResult testResult
, Attachment attachment
) {
150 public void setSession(Session session
) {
151 this.session
= session
;