KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > riotfamily > riot > hibernate > job > HibernateJobDao


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1
3  * The contents of this file are subject to the Mozilla Public License Version
4  * 1.1 (the "License"); you may not use this file except in compliance with
5  * the License. You may obtain a copy of the License at
6  * http://www.mozilla.org/MPL/
7  *
8  * Software distributed under the License is distributed on an "AS IS" basis,
9  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
10  * for the specific language governing rights and limitations under the
11  * License.
12  *
13  * The Original Code is Riot.
14  *
15  * The Initial Developer of the Original Code is
16  * Neteye GmbH.
17  * Portions created by the Initial Developer are Copyright (C) 2006
18  * the Initial Developer. All Rights Reserved.
19  *
20  * Contributor(s):
21  * Felix Gnass [fgnass at neteye dot de]
22  *
23  * ***** END LICENSE BLOCK ***** */

24 package org.riotfamily.riot.hibernate.job;
25
26 import java.util.Collection JavaDoc;
27 import java.util.List JavaDoc;
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 JavaDoc getJobDetails() {
38         return createQuery("from JobDetail job " +
39                 "order by job.endDate desc").list();
40     }
41     
42     public Collection JavaDoc 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 JavaDoc type, String JavaDoc 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 JavaDoc jobs = query.list();
61         if (jobs.isEmpty()) {
62             return null;
63         }
64         return (JobDetail) jobs.get(0);
65     }
66     
67     public JobDetail getLastCompletedJobDetail(String JavaDoc type, String JavaDoc 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 JavaDoc jobs = query.list();
78         if (jobs.isEmpty()) {
79             return null;
80         }
81         return (JobDetail) jobs.get(0);
82     }
83     
84     public int getAverageStepTime(String JavaDoc 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 JavaDoc time = (Number JavaDoc) query.uniqueResult();
90         if (time == null) {
91             return 0;
92         }
93         return time.intValue();
94     }
95     
96     public JobDetail getJobDetail(Long JavaDoc 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 JavaDoc getLogEntries(Long JavaDoc 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