KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > jaspersoft > jasperserver > api > engine > scheduling > ReportSchedulingFacade


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;
22
23 import java.util.Iterator JavaDoc;
24 import java.util.List JavaDoc;
25
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.springframework.beans.factory.InitializingBean;
29
30 import com.jaspersoft.jasperserver.api.JSValidationException;
31 import com.jaspersoft.jasperserver.api.common.domain.ExecutionContext;
32 import com.jaspersoft.jasperserver.api.common.domain.ValidationErrors;
33 import com.jaspersoft.jasperserver.api.engine.scheduling.domain.ReportJob;
34 import com.jaspersoft.jasperserver.api.engine.scheduling.domain.ReportJobIdHolder;
35 import com.jaspersoft.jasperserver.api.engine.scheduling.domain.ReportJobRuntimeInformation;
36 import com.jaspersoft.jasperserver.api.engine.scheduling.domain.ReportJobSummary;
37 import com.jaspersoft.jasperserver.api.engine.scheduling.domain.ReportJobTrigger;
38 import com.jaspersoft.jasperserver.api.engine.scheduling.service.ReportJobsPersistenceService;
39 import com.jaspersoft.jasperserver.api.engine.scheduling.service.ReportJobsScheduler;
40 import com.jaspersoft.jasperserver.api.engine.scheduling.service.ReportSchedulerListener;
41 import com.jaspersoft.jasperserver.api.engine.scheduling.service.ReportSchedulingService;
42
43 /**
44  * @author Lucian Chirita (lucianc@users.sourceforge.net)
45  * @version $Id: ReportSchedulingFacade.java 3801 2006-06-22 08:35:43Z lucian $
46  */

47 public class ReportSchedulingFacade
48     implements ReportSchedulingService, ReportSchedulingInternalService, ReportSchedulerListener, InitializingBean {
49     
50     private static final Log log = LogFactory.getLog(ReportSchedulingFacade.class);
51     
52     private ReportJobsPersistenceService persistenceService;
53     private ReportJobsInternalService jobsInternalService;
54     private ReportJobsScheduler scheduler;
55     private ReportJobValidator validator;
56
57     public ReportJobsPersistenceService getPersistenceService() {
58         return persistenceService;
59     }
60
61     public void setPersistenceService(
62             ReportJobsPersistenceService persistenceService) {
63         this.persistenceService = persistenceService;
64     }
65
66     public ReportJobsScheduler getScheduler() {
67         return scheduler;
68     }
69
70     public void setScheduler(ReportJobsScheduler scheduler) {
71         this.scheduler = scheduler;
72     }
73
74     public ReportJobValidator getValidator() {
75         return validator;
76     }
77
78     public void setValidator(ReportJobValidator validator) {
79         this.validator = validator;
80     }
81
82     public ReportJobsInternalService getJobsInternalService() {
83         return jobsInternalService;
84     }
85
86     public void setJobsInternalService(ReportJobsInternalService jobsInternalService) {
87         this.jobsInternalService = jobsInternalService;
88     }
89
90     public void afterPropertiesSet() throws Exception JavaDoc {
91         getScheduler().addReportSchedulerListener(this);
92     }
93
94     public void scheduleJob(ExecutionContext context, ReportJob job) {
95         validate(context, job);
96         ReportJob savedJob = persistenceService.saveJob(context, job);
97         scheduler.scheduleJob(context, savedJob);
98     }
99
100     protected void validate(ExecutionContext context, ReportJob job) {
101         ValidationErrors errors = validator.validateJob(context, job);
102         if (errors.isError()) {
103             throw new JSValidationException(errors);
104         }
105     }
106
107     public List JavaDoc getScheduledJobs(ExecutionContext context, String JavaDoc reportUnitURI) {
108         List JavaDoc jobs = persistenceService.listJobs(context, reportUnitURI);
109         setSummaryRuntimeInformation(context, jobs);
110         return jobs;
111     }
112
113     protected void setSummaryRuntimeInformation(ExecutionContext context, List JavaDoc jobs) {
114         if (jobs != null && !jobs.isEmpty()) {
115             long[] jobIds = new long[jobs.size()];
116             int idx = 0;
117             for (Iterator JavaDoc it = jobs.iterator(); it.hasNext(); ++idx) {
118                 ReportJobSummary job = (ReportJobSummary) it.next();
119                 jobIds[idx] = job.getId();
120             }
121             
122             ReportJobRuntimeInformation[] runtimeInfos = scheduler.getJobsRuntimeInformation(context, jobIds);
123             
124             idx = 0;
125             for (Iterator JavaDoc it = jobs.iterator(); it.hasNext(); ++idx) {
126                 ReportJobSummary job = (ReportJobSummary) it.next();
127                 job.setRuntimeInformation(runtimeInfos[idx]);
128             }
129         }
130     }
131
132     public void removeScheduledJob(ExecutionContext context, long jobId) {
133         deleteJob(context, jobId);
134     }
135
136     public void removeScheduledJobs(ExecutionContext context, long[] jobIds) {
137         for (int i = 0; i < jobIds.length; i++) {
138             long jobId = jobIds[i];
139             deleteJob(context, jobId);
140         }
141     }
142
143     public void removeReportUnitJobs(String JavaDoc reportUnitURI) {
144         long[] deletedJobIds = getJobsInternalService().deleteReportUnitJobs(reportUnitURI);
145         if (deletedJobIds != null && deletedJobIds.length > 0) {
146             for (int i = 0; i < deletedJobIds.length; i++) {
147                 long jobId = deletedJobIds[i];
148                 scheduler.removeScheduledJob(null, jobId);
149             }
150         }
151     }
152
153     protected void deleteJob(ExecutionContext context, long jobId) {
154         scheduler.removeScheduledJob(context, jobId);
155         persistenceService.deleteJob(context, new ReportJobIdHolder(jobId));
156     }
157
158     public ReportJob getScheduledJob(ExecutionContext context, long jobId) {
159         return persistenceService.loadJob(context, new ReportJobIdHolder(jobId));
160     }
161
162     public void reportJobFinalized(long jobId) {
163         if (log.isDebugEnabled()) {
164             log.debug("Job " + jobId + " finalized, deleting data");
165         }
166
167         getJobsInternalService().deleteJob(jobId);
168     }
169
170     public void updateScheduledJob(ExecutionContext context, ReportJob job) {
171         validate(context, job);
172
173         ReportJobTrigger origTrigger = job.getTrigger();
174         long origTriggerId = origTrigger.getId();
175         int origTriggerVersion = origTrigger.getVersion();
176
177         ReportJob savedJob = persistenceService.updateJob(context, job);
178         ReportJobTrigger updatedTrigger = savedJob.getTrigger();
179
180         if (updatedTrigger.getId() != origTriggerId || updatedTrigger.getVersion() != origTriggerVersion) {
181             scheduler.rescheduleJob(context, savedJob);
182         } else {
183             if (log.isDebugEnabled()) {
184                 log.debug("Trigger attributes not changed for job " + job.getId() + ", the job will not be rescheduled");
185             }
186         }
187     }
188
189     public ValidationErrors validateJob(ExecutionContext context, ReportJob job) {
190         return validator.validateJob(context, job);
191     }
192 }
193
Popular Tags