1 package org
.argeo
.slc
.jcr
.dao
;
3 import java
.io
.InputStream
;
4 import java
.util
.Calendar
;
6 import java
.util
.GregorianCalendar
;
10 import javax
.jcr
.NodeIterator
;
11 import javax
.jcr
.Session
;
12 import javax
.jcr
.Workspace
;
13 import javax
.jcr
.query
.Query
;
14 import javax
.jcr
.query
.QueryManager
;
15 import javax
.jcr
.query
.QueryResult
;
17 import org
.apache
.commons
.io
.IOUtils
;
18 import org
.argeo
.jcr
.BeanNodeMapper
;
19 import org
.argeo
.jcr
.JcrUtils
;
20 import org
.argeo
.slc
.SlcException
;
21 import org
.argeo
.slc
.dao
.process
.SlcExecutionDao
;
22 import org
.argeo
.slc
.process
.SlcExecution
;
23 import org
.argeo
.slc
.process
.SlcExecutionStep
;
24 import org
.springframework
.oxm
.Marshaller
;
25 import org
.springframework
.xml
.transform
.StringResult
;
27 public class SlcExecutionDaoJcr
implements SlcExecutionDao
{
28 private Session session
;
30 private Marshaller marshaller
;
32 private BeanNodeMapper beanNodeMapper
= new BeanNodeMapper();
34 public void addSteps(String slcExecutionId
,
35 List
<SlcExecutionStep
> additionalSteps
) {
36 // TODO Auto-generated method stub
40 public void create(SlcExecution slcExecution
) {
41 StringResult result
= new StringResult();
42 InputStream in
= null;
44 beanNodeMapper
.saveOrUpdate(getSession(), basePath(slcExecution
),
47 // TODO: optimize with piped streams
48 // marshaller.marshal(slcExecution, result);
49 // in = new ByteArrayInputStream(result.toString().getBytes());
51 // String basePath = basePath(slcExecution);
52 // JcrUtils.mkdirs(getSession(), basePath);
54 // session.importXML(basePath(slcExecution), in,
55 // ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING);
57 JcrUtils
.debug(session
.getRootNode());
59 } catch (Exception e
) {
60 throw new SlcException("Cannot import " + slcExecution
, e
);
62 IOUtils
.closeQuietly(in
);
66 protected String
basePath(SlcExecution slcExecution
) {
67 Calendar cal
= new GregorianCalendar();
68 cal
.setTime(new Date());
69 // cal.setTime(slcExecution.getStartDate());
70 return "/slc/processes/" + slcExecution
.getHost().replace('.', '/')
71 + '/' + JcrUtils
.dateAsPath(cal
) + slcExecution
.getUuid();
74 public SlcExecution
getSlcExecution(String uuid
) {
76 Workspace workspace
= session
.getWorkspace();
77 QueryManager qm
= workspace
.getQueryManager();
79 String queryString
= "//*[@uuid='" + uuid
+ "']";
80 Query query
= qm
.createQuery(queryString
, Query
.XPATH
);
81 QueryResult queryResult
= query
.execute();
82 NodeIterator nodeIterator
= queryResult
.getNodes();
84 if (nodeIterator
.hasNext())
85 node
= nodeIterator
.nextNode();
87 throw new SlcException("Query returned no node.");
89 if (nodeIterator
.hasNext())
90 throw new SlcException("Query returned more than one node.");
92 return (SlcExecution
) beanNodeMapper
.nodeToBean(node
);
93 } catch (Exception e
) {
94 throw new SlcException("Cannot load SLC execution " + uuid
, e
);
98 public List
<SlcExecution
> listSlcExecutions() {
99 // TODO Auto-generated method stub
103 public void merge(SlcExecution slcExecution
) {
104 // TODO Auto-generated method stub
108 public void update(SlcExecution slcExecution
) {
109 // TODO Auto-generated method stub
113 public void setSession(Session session
) {
114 this.session
= session
;
117 public void setMarshaller(Marshaller marshaller
) {
118 this.marshaller
= marshaller
;
121 protected Session
getSession() {