X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=runtime%2Forg.argeo.slc.support.hibernate%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fhibernate%2Fprocess%2FSlcExecutionDaoHibernate.java;h=84c5d215c3d48db932491051e7d1ec11dc36a17d;hb=b811ec0603b1e596f26eee8a5378c6294cba495d;hp=5cfde101eff9de9f78a46c5e55cf93c77439b1bd;hpb=2f57b9abf7e5110603e8cf952259509c76c9a162;p=gpl%2Fargeo-slc.git diff --git a/runtime/org.argeo.slc.support.hibernate/src/main/java/org/argeo/slc/hibernate/process/SlcExecutionDaoHibernate.java b/runtime/org.argeo.slc.support.hibernate/src/main/java/org/argeo/slc/hibernate/process/SlcExecutionDaoHibernate.java index 5cfde101e..84c5d215c 100644 --- a/runtime/org.argeo.slc.support.hibernate/src/main/java/org/argeo/slc/hibernate/process/SlcExecutionDaoHibernate.java +++ b/runtime/org.argeo.slc.support.hibernate/src/main/java/org/argeo/slc/hibernate/process/SlcExecutionDaoHibernate.java @@ -1,20 +1,39 @@ +/* + * Copyright (C) 2010 Mathieu Baudier + * + * 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.hibernate.process; import java.sql.SQLException; import java.util.List; -import org.springframework.orm.hibernate3.HibernateCallback; -import org.springframework.orm.hibernate3.support.HibernateDaoSupport; - +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; 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.hibernate.HibernateException; import org.hibernate.Session; +import org.springframework.orm.hibernate3.HibernateCallback; +import org.springframework.orm.hibernate3.support.HibernateDaoSupport; public class SlcExecutionDaoHibernate extends HibernateDaoSupport implements SlcExecutionDao { + private final static Log log = LogFactory + .getLog(SlcExecutionDaoHibernate.class); public void create(SlcExecution slcExecution) { getHibernateTemplate().save(slcExecution); @@ -33,6 +52,7 @@ public class SlcExecutionDaoHibernate extends HibernateDaoSupport implements uuid); } + @SuppressWarnings("unchecked") public List listSlcExecutions() { return (List) getHibernateTemplate().loadAll( SlcExecution.class); @@ -44,13 +64,8 @@ public class SlcExecutionDaoHibernate extends HibernateDaoSupport implements public Object doInHibernate(Session session) throws HibernateException, SQLException { - SlcExecution slcExecution = (SlcExecution) session.get( - SlcExecution.class, slcExecutionId); - - if (slcExecution == null) - throw new SlcException("Could not find SLC execution " - + slcExecutionId); - + SlcExecution slcExecution = getSlcExecution(session, + slcExecutionId); slcExecution.getSteps().addAll(additionalSteps); session.update(slcExecution); return slcExecution; @@ -59,4 +74,58 @@ public class SlcExecutionDaoHibernate extends HibernateDaoSupport implements } + @SuppressWarnings("unchecked") + public List tailSteps(final String slcExecutionId, + final Integer nbrOfSteps) { + return (List) getHibernateTemplate().execute( + new HibernateCallback() { + + public Object doInHibernate(Session session) + throws HibernateException, SQLException { + SlcExecution slcExecution = getSlcExecution(session, + slcExecutionId); + // TODO: do a query count() instead? + int stepCount = slcExecution.getSteps().size(); + if (stepCount > nbrOfSteps) { + return session.createFilter( + slcExecution.getSteps(), "") + .setFirstResult(stepCount - nbrOfSteps) + .setMaxResults(nbrOfSteps).list(); + } else { + return slcExecution.getSteps(); + } + } + }); + } + + @SuppressWarnings("unchecked") + public List tailSteps(final String slcExecutionId, + final String slcExecutionStepId) { + Object[] values = { slcExecutionStepId, slcExecutionId }; + List indexes = getHibernateTemplate().findByNamedQuery( + SlcExecutionStep.class.getName() + ".stepIndex", values); + + Integer index = indexes.get(0); + if (log.isTraceEnabled()){ + log.trace(indexes.size()); + log.trace("Index " + index + " for step " + slcExecutionStepId + + " in process " + slcExecutionId); + } + Object[] values2 = { slcExecutionId, index }; + return getHibernateTemplate().findByNamedQuery( + SlcExecutionStep.class.getName() + ".stepsAfter", values2); + } + + protected SlcExecution getSlcExecution(Session session, + String slcExecutionId) { + SlcExecution slcExecution = (SlcExecution) session.get( + SlcExecution.class, slcExecutionId); + + if (slcExecution == null) + throw new SlcException("Could not find SLC execution " + + slcExecutionId); + + return slcExecution; + } + }