2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
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.
17 package org
.argeo
.slc
.hibernate
.process
;
19 import java
.sql
.SQLException
;
20 import java
.util
.List
;
22 import org
.apache
.commons
.logging
.Log
;
23 import org
.apache
.commons
.logging
.LogFactory
;
24 import org
.argeo
.slc
.SlcException
;
25 import org
.argeo
.slc
.dao
.process
.SlcExecutionDao
;
26 import org
.argeo
.slc
.process
.SlcExecution
;
27 import org
.argeo
.slc
.process
.SlcExecutionStep
;
28 import org
.hibernate
.HibernateException
;
29 import org
.hibernate
.Session
;
30 import org
.springframework
.orm
.hibernate3
.HibernateCallback
;
31 import org
.springframework
.orm
.hibernate3
.support
.HibernateDaoSupport
;
33 public class SlcExecutionDaoHibernate
extends HibernateDaoSupport
implements
35 private final static Log log
= LogFactory
36 .getLog(SlcExecutionDaoHibernate
.class);
38 public void create(SlcExecution slcExecution
) {
39 getHibernateTemplate().save(slcExecution
);
42 public void update(final SlcExecution slcExecution
) {
43 getHibernateTemplate().update(slcExecution
);
46 public void merge(final SlcExecution slcExecution
) {
47 getHibernateTemplate().merge(slcExecution
);
50 public SlcExecution
getSlcExecution(String uuid
) {
51 return (SlcExecution
) getHibernateTemplate().get(SlcExecution
.class,
55 @SuppressWarnings("unchecked")
56 public List
<SlcExecution
> listSlcExecutions() {
57 return (List
<SlcExecution
>) getHibernateTemplate().loadAll(
61 public void addSteps(final String slcExecutionId
,
62 final List
<SlcExecutionStep
> additionalSteps
) {
63 getHibernateTemplate().execute(new HibernateCallback() {
65 public Object
doInHibernate(Session session
)
66 throws HibernateException
, SQLException
{
67 SlcExecution slcExecution
= getSlcExecution(session
,
69 slcExecution
.getSteps().addAll(additionalSteps
);
70 session
.update(slcExecution
);
77 @SuppressWarnings("unchecked")
78 public List
<SlcExecutionStep
> tailSteps(final String slcExecutionId
,
79 final Integer nbrOfSteps
) {
80 return (List
<SlcExecutionStep
>) getHibernateTemplate().execute(
81 new HibernateCallback() {
83 public Object
doInHibernate(Session session
)
84 throws HibernateException
, SQLException
{
85 SlcExecution slcExecution
= getSlcExecution(session
,
87 // TODO: do a query count() instead?
88 int stepCount
= slcExecution
.getSteps().size();
89 if (stepCount
> nbrOfSteps
) {
90 return session
.createFilter(
91 slcExecution
.getSteps(), "")
92 .setFirstResult(stepCount
- nbrOfSteps
)
93 .setMaxResults(nbrOfSteps
).list();
95 return slcExecution
.getSteps();
101 @SuppressWarnings("unchecked")
102 public List
<SlcExecutionStep
> tailSteps(final String slcExecutionId
,
103 final String slcExecutionStepId
) {
104 Object
[] values
= { slcExecutionStepId
, slcExecutionId
};
105 List
<Integer
> indexes
= getHibernateTemplate().findByNamedQuery(
106 SlcExecutionStep
.class.getName() + ".stepIndex", values
);
108 Integer index
= indexes
.get(0);
109 if (log
.isTraceEnabled()){
110 log
.trace(indexes
.size());
111 log
.trace("Index " + index
+ " for step " + slcExecutionStepId
112 + " in process " + slcExecutionId
);
114 Object
[] values2
= { slcExecutionId
, index
};
115 return getHibernateTemplate().findByNamedQuery(
116 SlcExecutionStep
.class.getName() + ".stepsAfter", values2
);
119 protected SlcExecution
getSlcExecution(Session session
,
120 String slcExecutionId
) {
121 SlcExecution slcExecution
= (SlcExecution
) session
.get(
122 SlcExecution
.class, slcExecutionId
);
124 if (slcExecution
== null)
125 throw new SlcException("Could not find SLC execution "