001: /* ***** BEGIN LICENSE BLOCK *****
002: * Version: MPL 1.1
003: * The contents of this file are subject to the Mozilla Public License Version
004: * 1.1 (the "License"); you may not use this file except in compliance with
005: * the License. You may obtain a copy of the License at
006: * http://www.mozilla.org/MPL/
007: *
008: * Software distributed under the License is distributed on an "AS IS" basis,
009: * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
010: * for the specific language governing rights and limitations under the
011: * License.
012: *
013: * The Original Code is Riot.
014: *
015: * The Initial Developer of the Original Code is
016: * Neteye GmbH.
017: * Portions created by the Initial Developer are Copyright (C) 2006
018: * the Initial Developer. All Rights Reserved.
019: *
020: * Contributor(s):
021: * Felix Gnass [fgnass at neteye dot de]
022: *
023: * ***** END LICENSE BLOCK ***** */
024: package org.riotfamily.riot.hibernate.job;
025:
026: import java.util.Collection;
027: import java.util.List;
028:
029: import org.hibernate.Query;
030: import org.riotfamily.riot.hibernate.support.HibernateSupport;
031: import org.riotfamily.riot.job.persistence.JobDao;
032: import org.riotfamily.riot.job.persistence.JobDetail;
033: import org.riotfamily.riot.job.persistence.JobLogEntry;
034:
035: public class HibernateJobDao extends HibernateSupport implements JobDao {
036:
037: public Collection getJobDetails() {
038: return createQuery(
039: "from JobDetail job " + "order by job.endDate desc")
040: .list();
041: }
042:
043: public Collection getPendingJobDetails() {
044: return createQuery(
045: "from JobDetail job where " + "job.state != "
046: + JobDetail.CANCELED + " and "
047: + "job.state != " + JobDetail.COMPLETED
048: + "order by job.startDate desc").list();
049: }
050:
051: public JobDetail getPendingJobDetail(String type, String objectId) {
052: Query query = createQuery("from JobDetail job where "
053: + "job.state != " + JobDetail.CANCELED + " and "
054: + "job.state != " + JobDetail.COMPLETED + " and "
055: + "job.type = :type and job.objectId = :objectId "
056: + "order by job.startDate desc");
057:
058: query.setParameter("type", type);
059: query.setParameter("objectId", objectId);
060: query.setMaxResults(1);
061:
062: List jobs = query.list();
063: if (jobs.isEmpty()) {
064: return null;
065: }
066: return (JobDetail) jobs.get(0);
067: }
068:
069: public JobDetail getLastCompletedJobDetail(String type,
070: String objectId) {
071: Query query = createQuery("from JobDetail job where "
072: + "job.state = " + JobDetail.COMPLETED + " and "
073: + "job.type = :type and job.objectId = :objectId "
074: + "order by job.startDate desc");
075:
076: query.setParameter("type", type);
077: query.setParameter("objectId", objectId);
078: query.setMaxResults(1);
079:
080: List jobs = query.list();
081: if (jobs.isEmpty()) {
082: return null;
083: }
084: return (JobDetail) jobs.get(0);
085: }
086:
087: public int getAverageStepTime(String type) {
088: Query query = createQuery("select avg(averageStepTime) from "
089: + "JobDetail where stepsCompleted > 0 and "
090: + "type = :type");
091:
092: query.setParameter("type", type);
093: Number time = (Number) query.uniqueResult();
094: if (time == null) {
095: return 0;
096: }
097: return time.intValue();
098: }
099:
100: public JobDetail getJobDetail(Long id) {
101: return (JobDetail) getSession().load(JobDetail.class, id);
102: }
103:
104: public void saveJobDetail(JobDetail job) {
105: getSession().save(job);
106: }
107:
108: public void updateJobDetail(JobDetail job) {
109: getSession().update(job);
110: }
111:
112: public Collection getLogEntries(Long jobId) {
113: Query query = createQuery("from JobLogEntry e where "
114: + "e.job.id = :jobId order by e.date desc");
115:
116: query.setParameter("jobId", jobId);
117: return query.list();
118: }
119:
120: public void log(JobLogEntry entry) {
121: getSession().save(entry);
122: }
123:
124: }
|