View Javadoc

1   /*
2    * Copyright 2007-2008 the original author or authors.
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.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   * @author Marco Ruiz
36   * @since Aug 13, 2007
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  				// Update job in DB
46  //		        Query query = session.createQuery("update JobInfo set execution = :execution, failures = :failures, request = :request where id = :id");
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  //		        query.setParameter("request", job.getRequest(), Hibernate.BLOB);
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  //					.addOrder(Order.asc("theOrder.priority"))
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  //	    	.createAlias("order.executionProfile", "theExecutionProfile")
91  			.add(Restrictions.eq("theOrder.paused", false))
92  			.add(Restrictions.eq("theOrder.aborted", false))
93  //			.add(Restrictions.gtProperty("theExecutionProfile.maxRetries", "failures"))
94  			.add(Restrictions.isNull("execution"));
95      }
96  }
97