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=f581f9261de1203f9381f339ee60963836c4a5ad;hb=a5b25ca3e5efc8de3571993f6bfc1671c0e2f3ba;hp=b60f6f5828a271de4d781ce8c60644764a8dbfb3;hpb=1fdb1b4e7b1d2b0cabb6483238301b857a6392fa;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 b60f6f582..f581f9261 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,5 +1,5 @@ /* - * Copyright (C) 2010 Mathieu Baudier + * Copyright (C) 2007-2012 Mathieu Baudier * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -13,24 +13,26 @@ * 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); @@ -61,13 +63,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; @@ -76,4 +73,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; + } + }