]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - runtime/org.argeo.slc.support.jcr/src/main/java/org/argeo/slc/jcr/dao/SlcExecutionDaoJcr.java
Improve JCR DAO
[gpl/argeo-slc.git] / runtime / org.argeo.slc.support.jcr / src / main / java / org / argeo / slc / jcr / dao / SlcExecutionDaoJcr.java
index 63cada00878e1201f5599084b5f6b18ab2d2651b..2cc32b0a3bf7406dfdd5f782a370d407041f9fdb 100644 (file)
@@ -1,6 +1,6 @@
 package org.argeo.slc.jcr.dao;
 
-import java.io.InputStream;
+import java.util.ArrayList;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.GregorianCalendar;
@@ -8,58 +8,58 @@ import java.util.List;
 
 import javax.jcr.Node;
 import javax.jcr.NodeIterator;
+import javax.jcr.RepositoryException;
 import javax.jcr.Session;
 import javax.jcr.Workspace;
 import javax.jcr.query.Query;
 import javax.jcr.query.QueryManager;
 import javax.jcr.query.QueryResult;
 
-import org.apache.commons.io.IOUtils;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
 import org.argeo.jcr.BeanNodeMapper;
 import org.argeo.jcr.JcrUtils;
 import org.argeo.slc.SlcException;
 import org.argeo.slc.dao.process.SlcExecutionDao;
 import org.argeo.slc.process.SlcExecution;
 import org.argeo.slc.process.SlcExecutionStep;
-import org.springframework.oxm.Marshaller;
-import org.springframework.xml.transform.StringResult;
 
 public class SlcExecutionDaoJcr implements SlcExecutionDao {
+       private final static Log log = LogFactory.getLog(SlcExecutionDaoJcr.class);
        private Session session;
-
-       private Marshaller marshaller;
+       private Workspace workspace;
+       private QueryManager queryManager;
 
        private BeanNodeMapper beanNodeMapper = new BeanNodeMapper();
 
+       public void init() {
+               try {
+                       workspace = session.getWorkspace();
+                       queryManager = workspace.getQueryManager();
+               } catch (RepositoryException e) {
+                       throw new SlcException("Cannot initialize DAO", e);
+               }
+       }
+
        public void addSteps(String slcExecutionId,
                        List<SlcExecutionStep> additionalSteps) {
-               // TODO Auto-generated method stub
+               // TODO: optimize, do one single query
+               SlcExecution slcExecution = getSlcExecution(slcExecutionId);
+               slcExecution.getSteps().addAll(additionalSteps);
+               update(slcExecution);
 
        }
 
        public void create(SlcExecution slcExecution) {
-               StringResult result = new StringResult();
-               InputStream in = null;
+               if (log.isDebugEnabled())
+                       log.debug("create");
+
                try {
-                       beanNodeMapper.saveOrUpdate(getSession(), basePath(slcExecution),
+                       beanNodeMapper.save(getSession(), basePath(slcExecution),
                                        slcExecution);
-
-                       // TODO: optimize with piped streams
-                       // marshaller.marshal(slcExecution, result);
-                       // in = new ByteArrayInputStream(result.toString().getBytes());
-                       //
-                       // String basePath = basePath(slcExecution);
-                       // JcrUtils.mkdirs(getSession(), basePath);
-                       //
-                       // session.importXML(basePath(slcExecution), in,
-                       // ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING);
-
-                       JcrUtils.debug(session.getRootNode());
                        session.save();
                } catch (Exception e) {
                        throw new SlcException("Cannot import " + slcExecution, e);
-               } finally {
-                       IOUtils.closeQuietly(in);
                }
        }
 
@@ -68,27 +68,20 @@ public class SlcExecutionDaoJcr implements SlcExecutionDao {
                cal.setTime(new Date());
                // cal.setTime(slcExecution.getStartDate());
                return "/slc/processes/" + slcExecution.getHost().replace('.', '/')
-                               + '/' + JcrUtils.dateAsPath(cal) + slcExecution.getUuid();
+                               + '/' + JcrUtils.dateAsPath(cal) + "process";
        }
 
        public SlcExecution getSlcExecution(String uuid) {
-               try {
-                       Workspace workspace = session.getWorkspace();
-                       QueryManager qm = workspace.getQueryManager();
-
-                       String queryString = "//*[@uuid='" + uuid + "']";
-                       Query query = qm.createQuery(queryString, Query.XPATH);
-                       QueryResult queryResult = query.execute();
-                       NodeIterator nodeIterator = queryResult.getNodes();
-                       Node node;
-                       if (nodeIterator.hasNext())
-                               node = nodeIterator.nextNode();
-                       else
-                               throw new SlcException("Query returned no node.");
-
-                       if (nodeIterator.hasNext())
-                               throw new SlcException("Query returned more than one node.");
+               if (log.isDebugEnabled())
+                       log.debug("getSlcExecution");
 
+               try {
+                       // TODO: optimize query
+                       String queryString = "//process[@uuid='" + uuid + "']";
+                       Query query = queryManager.createQuery(queryString, Query.XPATH);
+                       Node node = JcrUtils.querySingleNode(query);
+                       if (node == null)
+                               return null;
                        return (SlcExecution) beanNodeMapper.nodeToBean(node);
                } catch (Exception e) {
                        throw new SlcException("Cannot load SLC execution " + uuid, e);
@@ -96,30 +89,59 @@ public class SlcExecutionDaoJcr implements SlcExecutionDao {
        }
 
        public List<SlcExecution> listSlcExecutions() {
-               // TODO Auto-generated method stub
-               return null;
+               if (log.isDebugEnabled())
+                       log.debug("listSlcExecutions");
+               
+               List<SlcExecution> res = new ArrayList<SlcExecution>();
+               // TODO: optimize query
+               String queryString = "//process";
+               try {
+                       Query query = queryManager.createQuery(queryString, Query.XPATH);
+                       QueryResult qr = query.execute();
+                       NodeIterator iterator = qr.getNodes();
+                       while (iterator.hasNext()) {
+                               Node node = iterator.nextNode();
+                               SlcExecution slcExecution = (SlcExecution) beanNodeMapper
+                                               .nodeToBean(node);
+                               res.add(slcExecution);
+                       }
+                       return res;
+               } catch (Exception e) {
+                       throw new SlcException("Cannot list SLC executions", e);
+               }
        }
 
        public void merge(SlcExecution slcExecution) {
-               // TODO Auto-generated method stub
-
+               throw new UnsupportedOperationException();
        }
 
        public void update(SlcExecution slcExecution) {
-               // TODO Auto-generated method stub
+               if (log.isDebugEnabled())
+                       log.debug("update");
 
+               // TODO: optimize query
+               String queryString = "//process[@uuid='" + slcExecution.getUuid()
+                               + "']";
+               try {
+                       Query query = queryManager.createQuery(queryString, Query.XPATH);
+                       Node node = JcrUtils.querySingleNode(query);
+                       beanNodeMapper.update(node, slcExecution);
+                       session.save();
+               } catch (Exception e) {
+                       throw new SlcException("Cannot update " + slcExecution, e);
+               }
        }
 
        public void setSession(Session session) {
                this.session = session;
        }
 
-       public void setMarshaller(Marshaller marshaller) {
-               this.marshaller = marshaller;
-       }
-
        protected Session getSession() {
                return session;
        }
 
+       public void setBeanNodeMapper(BeanNodeMapper beanNodeMapper) {
+               this.beanNodeMapper = beanNodeMapper;
+       }
+
 }