KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jaspersoft > jasperserver > api > engine > scheduling > hibernate > HibernateReportJobsPersistenceService


1 /*
2  * Copyright (C) 2006 JasperSoft http://www.jaspersoft.com
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed WITHOUT ANY WARRANTY; and without the
10  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11  * See the GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, see http://www.gnu.org/licenses/gpl.txt
15  * or write to:
16  *
17  * Free Software Foundation, Inc.,
18  * 59 Temple Place - Suite 330,
19  * Boston, MA USA 02111-1307
20  */

21 package com.jaspersoft.jasperserver.api.engine.scheduling.hibernate;
22
23 import java.util.ArrayList JavaDoc;
24 import java.util.Iterator JavaDoc;
25 import java.util.List JavaDoc;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.hibernate.criterion.DetachedCriteria;
30 import org.hibernate.criterion.Restrictions;
31 import org.springframework.orm.hibernate3.HibernateTemplate;
32
33 import com.jaspersoft.jasperserver.api.JSException;
34 import com.jaspersoft.jasperserver.api.common.domain.ExecutionContext;
35 import com.jaspersoft.jasperserver.api.engine.common.service.SecurityContextProvider;
36 import com.jaspersoft.jasperserver.api.engine.scheduling.ReportJobsInternalService;
37 import com.jaspersoft.jasperserver.api.engine.scheduling.domain.ReportJob;
38 import com.jaspersoft.jasperserver.api.engine.scheduling.domain.ReportJobIdHolder;
39 import com.jaspersoft.jasperserver.api.engine.scheduling.service.ReportJobsPersistenceService;
40 import com.jaspersoft.jasperserver.api.metadata.common.service.impl.HibernateDaoImpl;
41
42 /**
43  * @author Lucian Chirita (lucianc@users.sourceforge.net)
44  * @version $Id: HibernateReportJobsPersistenceService.java 3801 2006-06-22 08:35:43Z lucian $
45  */

46 public class HibernateReportJobsPersistenceService extends HibernateDaoImpl
47     implements ReportJobsPersistenceService, ReportJobsInternalService {
48     
49     protected static final Log log = LogFactory.getLog(HibernateReportJobsPersistenceService.class);
50     
51     private SecurityContextProvider securityContextProvider;
52
53     public HibernateReportJobsPersistenceService() {
54         super();
55     }
56
57     public SecurityContextProvider getSecurityContextProvider() {
58         return securityContextProvider;
59     }
60
61     public void setSecurityContextProvider(SecurityContextProvider securityContextProvider) {
62         this.securityContextProvider = securityContextProvider;
63     }
64
65     public ReportJob saveJob(ExecutionContext context, final ReportJob job) {
66         return (ReportJob) executeWriteCallback(new DaoCallback() {
67             public Object JavaDoc execute() {
68                 HibernateTemplate hibernateTemplate = getHibernateTemplate();
69                 
70                 PersistentReportJob persistentJob = new PersistentReportJob();
71                 persistentJob.copyFrom(job, hibernateTemplate);
72                 persistentJob.setUsername(getContextUsername());
73                 
74                 persistentJob.cascadeSave(hibernateTemplate);
75                 hibernateTemplate.save(persistentJob);
76                 
77                 hibernateTemplate.flush();//force job id generation
78
ReportJob clientJob = persistentJob.toClient();
79                 
80                 if (log.isDebugEnabled()) {
81                     log.debug("Saved report job " + clientJob.getId() + " for report " + clientJob.getSource().getReportUnitURI());
82                 }
83                 
84                 return clientJob;
85             }
86         });
87     }
88
89     protected String JavaDoc getContextUsername() {
90         return getSecurityContextProvider().getContextUsername();
91     }
92
93     public ReportJob updateJob(ExecutionContext context, final ReportJob job) {
94         return (ReportJob) executeWriteCallback(new DaoCallback() {
95             public Object JavaDoc execute() {
96                 HibernateTemplate hibernateTemplate = getHibernateTemplate();
97                 PersistentReportJob persistentJob = findJob(job.getId(), true);
98                 persistentJob.copyFrom(job, hibernateTemplate);
99                 persistentJob.cascadeSave(hibernateTemplate);
100                 ReportJob clientJob = persistentJob.toClient();
101                 
102                 if (log.isDebugEnabled()) {
103                     log.debug("Updated report job " + clientJob.getId());
104                 }
105                 
106                 return clientJob;
107             }
108         });
109     }
110
111     public ReportJob loadJob(ExecutionContext context, ReportJobIdHolder jobIdHolder) {
112         final long jobId = jobIdHolder.getId();
113         return (ReportJob) executeCallback(new DaoCallback() {
114             public Object JavaDoc execute() {
115                 PersistentReportJob persistentJob = findJob(jobId, false);
116                 ReportJob job;
117                 if (persistentJob == null) {
118                     job = null;
119                 } else {
120                     job = persistentJob.toClient();
121                 }
122                 return job;
123             }
124         });
125     }
126
127     public void deleteJob(ExecutionContext context, ReportJobIdHolder jobIdHolder) {
128         deleteJob(jobIdHolder.getId());
129     }
130
131     public void deleteJob(final long jobId) {
132         executeWriteCallback(new DaoCallback() {
133             public Object JavaDoc execute() {
134                 PersistentReportJob job = findJob(jobId, false);
135                 if (job != null) {
136                     deleteJob(job);
137                 } else {
138                     if (log.isInfoEnabled()) {
139                         log.info("Report job with id " + jobId + " not found for deletion");
140                     }
141                 }
142                 return null;
143             }
144         }, false);
145     }
146
147     public List JavaDoc listJobs(ExecutionContext context, final String JavaDoc reportUnitURI) {
148         return (List JavaDoc) executeCallback(new DaoCallback() {
149             public Object JavaDoc execute() {
150                 List JavaDoc persistentJobs = getReportUnitJobs(reportUnitURI);
151                 List JavaDoc jobs = new ArrayList JavaDoc(persistentJobs.size());
152                 for (Iterator JavaDoc it = persistentJobs.iterator(); it.hasNext();) {
153                     PersistentReportJob persistentJob = (PersistentReportJob) it.next();
154                     jobs.add(persistentJob.toSummary());
155                 }
156                 return jobs;
157             }
158         });
159     }
160
161     protected PersistentReportJob findJob(long jobId, boolean required) {
162         PersistentReportJob job = (PersistentReportJob) getHibernateTemplate().get(PersistentReportJob.class, new Long JavaDoc(jobId));
163         if (job == null && required) {
164             throw new JSException("Job " + job.getId() + " not found");
165         }
166         return job;
167     }
168
169     public String JavaDoc getJobOwner(final long jobId) {
170         return (String JavaDoc) executeCallback(new DaoCallback() {
171             public Object JavaDoc execute() {
172                 PersistentReportJob persistentJob = findJob(jobId, false);
173                 String JavaDoc owner;
174                 if (persistentJob == null) {
175                     owner = null;
176                 } else {
177                     owner = persistentJob.getUsername();
178                 }
179                 return owner;
180             }
181         });
182     }
183
184     public long[] deleteReportUnitJobs(final String JavaDoc reportUnitURI) {
185         return (long[]) executeWriteCallback(new DaoCallback() {
186             public Object JavaDoc execute() {
187                 List JavaDoc jobs = getReportUnitJobs(reportUnitURI);
188                 long[] jobIds;
189                 if (jobs == null || jobs.isEmpty()) {
190                     jobIds = null;
191                 } else {
192                     jobIds = new long[jobs.size()];
193                     int c = 0;
194                     for (Iterator JavaDoc it = jobs.iterator(); it.hasNext(); ++c) {
195                         PersistentReportJob job = (PersistentReportJob) it.next();
196                         jobIds[c] = job.getId();
197                         deleteJob(job);
198                     }
199                 }
200                 return jobIds;
201             }
202         }, false);
203     }
204
205     protected void deleteJob(PersistentReportJob job) {
206         job.delete(getHibernateTemplate());
207         
208         if (log.isDebugEnabled()) {
209             log.debug("Deleted job " + job.getId());
210         }
211     }
212
213     protected List JavaDoc getReportUnitJobs(final String JavaDoc reportUnitURI) {
214         HibernateTemplate hibernateTemplate = getHibernateTemplate();
215         DetachedCriteria crit = DetachedCriteria.forClass(PersistentReportJob.class);
216         crit.add(Restrictions.eq("source.reportUnitURI", reportUnitURI));
217         List JavaDoc persistentJobs = hibernateTemplate.findByCriteria(crit);
218         return persistentJobs;
219     }
220
221 }
222
Popular Tags