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