1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.gwe.persistence.dao;
18
19 import java.util.List;
20
21 import org.apache.commons.logging.Log;
22 import org.apache.commons.logging.LogFactory;
23 import org.gwe.persistence.model.JobInfo;
24 import org.hibernate.Criteria;
25 import org.hibernate.HibernateException;
26 import org.hibernate.Query;
27 import org.hibernate.Session;
28 import org.hibernate.criterion.Order;
29 import org.hibernate.criterion.ProjectionList;
30 import org.hibernate.criterion.Projections;
31 import org.hibernate.criterion.Restrictions;
32 import org.springframework.orm.hibernate3.HibernateCallback;
33
34
35
36
37
38 public class JobInfoDAO extends BaseInfoDAO<JobInfo, String> {
39
40 private static Log log = LogFactory.getLog(JobInfoDAO.class);
41
42 public void updateJob(final JobInfo job) {
43 getHibernateTemplate().execute(new HibernateCallback() {
44 public Object doInHibernate(Session session) throws HibernateException {
45
46
47 Query query = session.createQuery("update JobInfo set execution = :execution, failures = :failures where id = :id");
48 query.setString("id", job.getId());
49 query.setParameter("execution", job.getExecution());
50 query.setInteger("failures", job.getFailures());
51
52 return query.executeUpdate();
53 }
54 });
55 }
56
57 public List<JobInfo> getNextSchedulableJobsBatch(final int size) {
58 return (List<JobInfo>) getHibernateTemplate().execute(new HibernateCallback() {
59 public Object doInHibernate(Session session) throws HibernateException {
60 List<JobInfo> result = getPendingJobsCriteria(session)
61
62 .addOrder(Order.asc("theOrder.id"))
63 .addOrder(Order.asc("failures"))
64 .addOrder(Order.asc("id"))
65 .setMaxResults(size)
66 .list();
67 return result;
68 }
69 });
70 }
71
72 public int getPendingJobsCount() {
73 return (Integer) getHibernateTemplate().execute(new HibernateCallback() {
74 public Integer doInHibernate(Session session) throws HibernateException {
75 return getCount(getPendingJobsCriteria(session));
76 }
77 });
78 }
79
80 private int getCount(Criteria criteria) {
81 ProjectionList projections = Projections.projectionList()
82 .add(Projections.rowCount());
83 criteria.setProjection(projections);
84 return ((Integer)criteria.list().get(0)).intValue();
85 }
86
87 private Criteria getPendingJobsCriteria(Session session) {
88 return session.createCriteria(JobInfo.class)
89 .createAlias("order", "theOrder")
90
91 .add(Restrictions.eq("theOrder.paused", false))
92 .add(Restrictions.eq("theOrder.aborted", false))
93
94 .add(Restrictions.isNull("execution"));
95 }
96 }
97