1 24 package org.riotfamily.riot.hibernate.job; 25 26 import java.util.Collection ; 27 import java.util.List ; 28 29 import org.hibernate.Query; 30 import org.riotfamily.riot.hibernate.support.HibernateSupport; 31 import org.riotfamily.riot.job.persistence.JobDao; 32 import org.riotfamily.riot.job.persistence.JobDetail; 33 import org.riotfamily.riot.job.persistence.JobLogEntry; 34 35 public class HibernateJobDao extends HibernateSupport implements JobDao { 36 37 public Collection getJobDetails() { 38 return createQuery("from JobDetail job " + 39 "order by job.endDate desc").list(); 40 } 41 42 public Collection getPendingJobDetails() { 43 return createQuery("from JobDetail job where " + 44 "job.state != " + JobDetail.CANCELED + " and " + 45 "job.state != " + JobDetail.COMPLETED + 46 "order by job.startDate desc").list(); 47 } 48 49 public JobDetail getPendingJobDetail(String type, String objectId) { 50 Query query = createQuery("from JobDetail job where " + 51 "job.state != " + JobDetail.CANCELED + " and " + 52 "job.state != " + JobDetail.COMPLETED + " and " + 53 "job.type = :type and job.objectId = :objectId " + 54 "order by job.startDate desc"); 55 56 query.setParameter("type", type); 57 query.setParameter("objectId", objectId); 58 query.setMaxResults(1); 59 60 List jobs = query.list(); 61 if (jobs.isEmpty()) { 62 return null; 63 } 64 return (JobDetail) jobs.get(0); 65 } 66 67 public JobDetail getLastCompletedJobDetail(String type, String objectId) { 68 Query query = createQuery("from JobDetail job where " + 69 "job.state = " + JobDetail.COMPLETED + " and " + 70 "job.type = :type and job.objectId = :objectId " + 71 "order by job.startDate desc"); 72 73 query.setParameter("type", type); 74 query.setParameter("objectId", objectId); 75 query.setMaxResults(1); 76 77 List jobs = query.list(); 78 if (jobs.isEmpty()) { 79 return null; 80 } 81 return (JobDetail) jobs.get(0); 82 } 83 84 public int getAverageStepTime(String type) { 85 Query query = createQuery("select avg(averageStepTime) from " + 86 "JobDetail where stepsCompleted > 0 and " + "type = :type"); 87 88 query.setParameter("type", type); 89 Number time = (Number ) query.uniqueResult(); 90 if (time == null) { 91 return 0; 92 } 93 return time.intValue(); 94 } 95 96 public JobDetail getJobDetail(Long id) { 97 return (JobDetail) getSession().load(JobDetail.class, id); 98 } 99 100 public void saveJobDetail(JobDetail job) { 101 getSession().save(job); 102 } 103 104 public void updateJobDetail(JobDetail job) { 105 getSession().update(job); 106 } 107 108 public Collection getLogEntries(Long jobId) { 109 Query query = createQuery("from JobLogEntry e where " + 110 "e.job.id = :jobId order by e.date desc"); 111 112 query.setParameter("jobId", jobId); 113 return query.list(); 114 } 115 116 public void log(JobLogEntry entry) { 117 getSession().save(entry); 118 } 119 120 } 121 | Popular Tags |