]> git.argeo.org Git - gpl/argeo-slc.git/blob - SlcExecutionDaoHibernate.java
84c5d215c3d48db932491051e7d1ec11dc36a17d
[gpl/argeo-slc.git] / SlcExecutionDaoHibernate.java
1 /*
2 * Copyright (C) 2010 Mathieu Baudier <mbaudier@argeo.org>
3 *
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
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
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.
15 */
16
17 package org.argeo.slc.hibernate.process;
18
19 import java.sql.SQLException;
20 import java.util.List;
21
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;
32
33 public class SlcExecutionDaoHibernate extends HibernateDaoSupport implements
34 SlcExecutionDao {
35 private final static Log log = LogFactory
36 .getLog(SlcExecutionDaoHibernate.class);
37
38 public void create(SlcExecution slcExecution) {
39 getHibernateTemplate().save(slcExecution);
40 }
41
42 public void update(final SlcExecution slcExecution) {
43 getHibernateTemplate().update(slcExecution);
44 }
45
46 public void merge(final SlcExecution slcExecution) {
47 getHibernateTemplate().merge(slcExecution);
48 }
49
50 public SlcExecution getSlcExecution(String uuid) {
51 return (SlcExecution) getHibernateTemplate().get(SlcExecution.class,
52 uuid);
53 }
54
55 @SuppressWarnings("unchecked")
56 public List<SlcExecution> listSlcExecutions() {
57 return (List<SlcExecution>) getHibernateTemplate().loadAll(
58 SlcExecution.class);
59 }
60
61 public void addSteps(final String slcExecutionId,
62 final List<SlcExecutionStep> additionalSteps) {
63 getHibernateTemplate().execute(new HibernateCallback() {
64
65 public Object doInHibernate(Session session)
66 throws HibernateException, SQLException {
67 SlcExecution slcExecution = getSlcExecution(session,
68 slcExecutionId);
69 slcExecution.getSteps().addAll(additionalSteps);
70 session.update(slcExecution);
71 return slcExecution;
72 }
73 });
74
75 }
76
77 @SuppressWarnings("unchecked")
78 public List<SlcExecutionStep> tailSteps(final String slcExecutionId,
79 final Integer nbrOfSteps) {
80 return (List<SlcExecutionStep>) getHibernateTemplate().execute(
81 new HibernateCallback() {
82
83 public Object doInHibernate(Session session)
84 throws HibernateException, SQLException {
85 SlcExecution slcExecution = getSlcExecution(session,
86 slcExecutionId);
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();
94 } else {
95 return slcExecution.getSteps();
96 }
97 }
98 });
99 }
100
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);
107
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);
113 }
114 Object[] values2 = { slcExecutionId, index };
115 return getHibernateTemplate().findByNamedQuery(
116 SlcExecutionStep.class.getName() + ".stepsAfter", values2);
117 }
118
119 protected SlcExecution getSlcExecution(Session session,
120 String slcExecutionId) {
121 SlcExecution slcExecution = (SlcExecution) session.get(
122 SlcExecution.class, slcExecutionId);
123
124 if (slcExecution == null)
125 throw new SlcException("Could not find SLC execution "
126 + slcExecutionId);
127
128 return slcExecution;
129 }
130
131 }