2 * Copyright (C) 2007-2012 Mathieu Baudier
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org
.argeo
.slc
.hibernate
.process
;
18 import java
.sql
.SQLException
;
19 import java
.util
.List
;
21 import org
.apache
.commons
.logging
.Log
;
22 import org
.apache
.commons
.logging
.LogFactory
;
23 import org
.argeo
.slc
.SlcException
;
24 import org
.argeo
.slc
.dao
.process
.SlcExecutionDao
;
25 import org
.argeo
.slc
.process
.SlcExecution
;
26 import org
.argeo
.slc
.process
.SlcExecutionStep
;
27 import org
.hibernate
.HibernateException
;
28 import org
.hibernate
.Session
;
29 import org
.springframework
.orm
.hibernate3
.HibernateCallback
;
30 import org
.springframework
.orm
.hibernate3
.support
.HibernateDaoSupport
;
32 public class SlcExecutionDaoHibernate
extends HibernateDaoSupport
implements
34 private final static Log log
= LogFactory
35 .getLog(SlcExecutionDaoHibernate
.class);
37 public void create(SlcExecution slcExecution
) {
38 getHibernateTemplate().save(slcExecution
);
41 public void update(final SlcExecution slcExecution
) {
42 getHibernateTemplate().update(slcExecution
);
45 public void merge(final SlcExecution slcExecution
) {
46 getHibernateTemplate().merge(slcExecution
);
49 public SlcExecution
getSlcExecution(String uuid
) {
50 return (SlcExecution
) getHibernateTemplate().get(SlcExecution
.class,
54 @SuppressWarnings("unchecked")
55 public List
<SlcExecution
> listSlcExecutions() {
56 return (List
<SlcExecution
>) getHibernateTemplate().loadAll(
60 public void addSteps(final String slcExecutionId
,
61 final List
<SlcExecutionStep
> additionalSteps
) {
62 getHibernateTemplate().execute(new HibernateCallback() {
64 public Object
doInHibernate(Session session
)
65 throws HibernateException
, SQLException
{
66 SlcExecution slcExecution
= getSlcExecution(session
,
68 slcExecution
.getSteps().addAll(additionalSteps
);
69 session
.update(slcExecution
);
76 @SuppressWarnings("unchecked")
77 public List
<SlcExecutionStep
> tailSteps(final String slcExecutionId
,
78 final Integer nbrOfSteps
) {
79 return (List
<SlcExecutionStep
>) getHibernateTemplate().execute(
80 new HibernateCallback() {
82 public Object
doInHibernate(Session session
)
83 throws HibernateException
, SQLException
{
84 SlcExecution slcExecution
= getSlcExecution(session
,
86 // TODO: do a query count() instead?
87 int stepCount
= slcExecution
.getSteps().size();
88 if (stepCount
> nbrOfSteps
) {
89 return session
.createFilter(
90 slcExecution
.getSteps(), "")
91 .setFirstResult(stepCount
- nbrOfSteps
)
92 .setMaxResults(nbrOfSteps
).list();
94 return slcExecution
.getSteps();
100 @SuppressWarnings("unchecked")
101 public List
<SlcExecutionStep
> tailSteps(final String slcExecutionId
,
102 final String slcExecutionStepId
) {
103 Object
[] values
= { slcExecutionStepId
, slcExecutionId
};
104 List
<Integer
> indexes
= getHibernateTemplate().findByNamedQuery(
105 SlcExecutionStep
.class.getName() + ".stepIndex", values
);
107 Integer index
= indexes
.get(0);
108 if (log
.isTraceEnabled()){
109 log
.trace(indexes
.size());
110 log
.trace("Index " + index
+ " for step " + slcExecutionStepId
111 + " in process " + slcExecutionId
);
113 Object
[] values2
= { slcExecutionId
, index
};
114 return getHibernateTemplate().findByNamedQuery(
115 SlcExecutionStep
.class.getName() + ".stepsAfter", values2
);
118 protected SlcExecution
getSlcExecution(Session session
,
119 String slcExecutionId
) {
120 SlcExecution slcExecution
= (SlcExecution
) session
.get(
121 SlcExecution
.class, slcExecutionId
);
123 if (slcExecution
== null)
124 throw new SlcException("Could not find SLC execution "