+/*
+ * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
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;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
-import javax.jcr.Session;
-import javax.jcr.Workspace;
+import javax.jcr.RepositoryException;
import javax.jcr.query.Query;
-import javax.jcr.query.QueryManager;
import javax.jcr.query.QueryResult;
-import org.apache.commons.io.IOUtils;
-import org.argeo.jcr.BeanNodeMapper;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
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 Session session;
- private Marshaller marshaller;
-
- private BeanNodeMapper beanNodeMapper = new BeanNodeMapper();
+public class SlcExecutionDaoJcr extends AbstractSlcJcrDao implements
+ SlcExecutionDao {
+ private final static Log log = LogFactory.getLog(SlcExecutionDaoJcr.class);
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;
try {
- beanNodeMapper.saveOrUpdate(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);
+ nodeMapper.save(getSession(), basePath(slcExecution), slcExecution);
+ getSession().save();
+ } catch (RepositoryException e) {
+ throw new SlcException("Cannot create slcExecution" + slcExecution,
+ e);
}
}
Calendar cal = new GregorianCalendar();
cal.setTime(new Date());
// cal.setTime(slcExecution.getStartDate());
- return "/slc/processes/" + slcExecution.getHost().replace('.', '/')
- + '/' + JcrUtils.dateAsPath(cal) + slcExecution.getUuid();
+ String host = slcExecution.getHost();
+ if (host == null)
+ host = "UNKOWNHOST";
+ return "/slc/processes/" + JcrUtils.hostAsPath(host) + '/'
+ + 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.");
-
- return (SlcExecution) beanNodeMapper.nodeToBean(node);
- } catch (Exception e) {
- throw new SlcException("Cannot load SLC execution " + uuid, e);
- }
+ // TODO: optimize query
+ String queryString = "//process[@uuid='" + uuid + "']";
+ Query query = createQuery(queryString, Query.XPATH);
+ Node node = JcrUtils.querySingleNode(query);
+ if (node == null)
+ return null;
+ return (SlcExecution) nodeMapper.load(node);
}
public List<SlcExecution> listSlcExecutions() {
- // TODO Auto-generated method stub
- return null;
+ List<SlcExecution> res = new ArrayList<SlcExecution>();
+ // TODO: optimize query
+ String queryString = "//process";
+ try {
+ Query query = createQuery(queryString, Query.XPATH);
+ QueryResult qr = query.execute();
+ NodeIterator iterator = qr.getNodes();
+ while (iterator.hasNext()) {
+ Node node = iterator.nextNode();
+ SlcExecution slcExecution = (SlcExecution) nodeMapper
+ .load(node);
+ res.add(slcExecution);
+ }
+ return res;
+ } catch (RepositoryException 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
-
- }
-
- public void setSession(Session session) {
- this.session = session;
+ // TODO: optimize query
+ String queryString = "//process[@uuid='" + slcExecution.getUuid()
+ + "']";
+ try {
+ Query query = createQuery(queryString, Query.XPATH);
+ Node node = JcrUtils.querySingleNode(query);
+ nodeMapper.update(node, slcExecution);
+ getSession().save();
+ } catch (RepositoryException e) {
+ throw new SlcException("Cannot update " + slcExecution, e);
+ }
}
- public void setMarshaller(Marshaller marshaller) {
- this.marshaller = marshaller;
+ public List<SlcExecutionStep> tailSteps(String slcExecutionId,
+ Integer nbrOfSteps) {
+ log.error("Method not implemented, returning an empty list.");
+ return new ArrayList<SlcExecutionStep>();
}
- protected Session getSession() {
- return session;
+ public List<SlcExecutionStep> tailSteps(String slcExecutionId,
+ String slcExecutionStepId) {
+ log.error("Method not implemented, returning an empty list.");
+ return new ArrayList<SlcExecutionStep>();
}
}