001: /*
002: * Copyright (C) 2003 Erik Swenson - erik@oreports.com
003: *
004: * This program is free software; you can redistribute it and/or modify it under
005: * the terms of the GNU General Public License as published by the Free Software
006: * Foundation; either version 2 of the License, or (at your option) any later
007: * version.
008: *
009: * This program is distributed in the hope that it will be useful, but WITHOUT
010: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011: * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
012: * details.
013: *
014: * You should have received a copy of the GNU General Public License along with
015: * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
016: * Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019:
020: package org.efs.openreports.providers.impl;
021:
022: import java.text.ParseException;
023: import java.util.ArrayList;
024: import java.util.List;
025:
026: import org.apache.log4j.Logger;
027: import org.efs.openreports.ORStatics;
028: import org.efs.openreports.objects.ReportSchedule;
029: import org.efs.openreports.objects.ReportUser;
030: import org.efs.openreports.providers.ProviderException;
031: import org.efs.openreports.providers.SchedulerProvider;
032: import org.efs.openreports.util.ScheduledReportJob;
033: import org.quartz.*;
034:
035: public class SchedulerProviderImpl implements SchedulerProvider {
036: protected static Logger log = Logger
037: .getLogger(SchedulerProviderImpl.class.getName());
038:
039: private Scheduler scheduler;
040:
041: public SchedulerProviderImpl() {
042: log.info("SchedulerProviderImpl created.");
043: }
044:
045: public void scheduleReport(ReportSchedule reportSchedule)
046: throws ProviderException {
047: JobDetail jobDetail = new JobDetail(reportSchedule
048: .getScheduleName(), reportSchedule.getScheduleGroup(),
049: ScheduledReportJob.class);
050:
051: jobDetail.getJobDataMap().put(ORStatics.REPORT_SCHEDULE,
052: reportSchedule);
053: jobDetail.setDescription(reportSchedule
054: .getScheduleDescription());
055:
056: if (reportSchedule.getScheduleType() == ReportSchedule.DAILY
057: || reportSchedule.getScheduleType() == ReportSchedule.WEEKLY
058: || reportSchedule.getScheduleType() == ReportSchedule.MONTHLY
059: || reportSchedule.getScheduleType() == ReportSchedule.WEEKDAYS
060: || reportSchedule.getScheduleType() == ReportSchedule.HOURLY
061: || reportSchedule.getScheduleType() == ReportSchedule.CRON) {
062: StringBuffer cronExpression = new StringBuffer();
063:
064: if (reportSchedule.getScheduleType() == ReportSchedule.CRON) {
065: cronExpression.append(reportSchedule
066: .getCronExpression());
067: } else {
068: cronExpression.append("0 ");
069: cronExpression.append(reportSchedule.getStartMinute());
070: cronExpression.append(" ");
071: cronExpression.append(reportSchedule
072: .getAbsoluteStartHour());
073:
074: if (reportSchedule.getScheduleType() == ReportSchedule.HOURLY) {
075: cronExpression.append("-"
076: + reportSchedule.getAbsoluteEndHour());
077: }
078:
079: if (reportSchedule.getScheduleType() == ReportSchedule.WEEKLY) {
080: cronExpression.append(" ? * ");
081: cronExpression
082: .append(reportSchedule.getDayOfWeek());
083: } else if (reportSchedule.getScheduleType() == ReportSchedule.MONTHLY) {
084: cronExpression.append(" "
085: + reportSchedule.getDayOfMonth());
086: cronExpression.append(" * ? ");
087: } else if (reportSchedule.getScheduleType() == ReportSchedule.WEEKDAYS) {
088: cronExpression.append(" ? * MON-FRI");
089: } else {
090: cronExpression.append(" * * ?");
091: }
092: }
093:
094: CronTrigger cronTrigger = new CronTrigger(reportSchedule
095: .getScheduleName(), reportSchedule
096: .getScheduleGroup());
097:
098: try {
099: cronTrigger
100: .setCronExpression(cronExpression.toString());
101: } catch (ParseException pe) {
102: throw new ProviderException(pe);
103: }
104:
105: cronTrigger.setStartTime(reportSchedule.getStartDate());
106: cronTrigger.setPriority(reportSchedule
107: .getSchedulePriority());
108: cronTrigger.getJobDataMap().put(
109: reportSchedule.getScheduleName(),
110: reportSchedule.getRequestId());
111:
112: try {
113: scheduler.scheduleJob(jobDetail, cronTrigger);
114: } catch (SchedulerException e) {
115: throw new ProviderException(e);
116: }
117: } else {
118: // default to run once...
119: SimpleTrigger trigger = new SimpleTrigger(reportSchedule
120: .getScheduleName(), reportSchedule
121: .getScheduleGroup(), reportSchedule
122: .getStartDateTime(), null, 0, 0L);
123: trigger.setPriority(reportSchedule.getSchedulePriority());
124: trigger.getJobDataMap().put(
125: reportSchedule.getScheduleName(),
126: reportSchedule.getRequestId());
127:
128: try {
129: scheduler.scheduleJob(jobDetail, trigger);
130: } catch (SchedulerException e) {
131: throw new ProviderException(e);
132: }
133: }
134: }
135:
136: public List<ReportSchedule> getScheduledReports(
137: ReportUser reportUser) throws ProviderException {
138:
139: List<ReportSchedule> scheduledReports = new ArrayList<ReportSchedule>();
140:
141: try {
142: String group = reportUser.getId().toString();
143:
144: String[] jobNames = scheduler.getJobNames(group);
145:
146: for (int i = 0; i < jobNames.length; i++) {
147: try {
148: JobDetail jobDetail = scheduler.getJobDetail(
149: jobNames[i], group);
150:
151: ReportSchedule reportSchedule = (ReportSchedule) jobDetail
152: .getJobDataMap().get(
153: ORStatics.REPORT_SCHEDULE);
154: reportSchedule.setScheduleDescription(jobDetail
155: .getDescription());
156: reportSchedule
157: .setScheduleState(getTriggerStateName(
158: jobNames[i], group));
159:
160: Trigger trigger = scheduler.getTrigger(jobNames[i],
161: group);
162: if (trigger != null) {
163: reportSchedule.setNextFireDate(trigger
164: .getNextFireTime());
165: }
166:
167: scheduledReports.add(reportSchedule);
168: } catch (ProviderException pe) {
169: log.error(group + " | " + jobNames[i] + " | "
170: + pe.toString());
171: }
172: }
173: } catch (SchedulerException e) {
174: throw new ProviderException(e);
175: }
176:
177: return scheduledReports;
178: }
179:
180: public void deleteScheduledReport(ReportUser reportUser, String name)
181: throws ProviderException {
182: try {
183: String group = reportUser.getId().toString();
184: scheduler.deleteJob(name, group);
185: } catch (SchedulerException e) {
186: throw new ProviderException(e);
187: }
188: }
189:
190: public ReportSchedule getScheduledReport(ReportUser reportUser,
191: String name) throws ProviderException {
192: try {
193: String group = reportUser.getId().toString();
194:
195: JobDetail jobDetail = scheduler.getJobDetail(name, group);
196:
197: ReportSchedule reportSchedule = (ReportSchedule) jobDetail
198: .getJobDataMap().get(ORStatics.REPORT_SCHEDULE);
199: reportSchedule.setScheduleDescription(jobDetail
200: .getDescription());
201: reportSchedule.setScheduleState(getTriggerStateName(name,
202: group));
203:
204: return reportSchedule;
205: } catch (SchedulerException e) {
206: throw new ProviderException(e);
207: }
208: }
209:
210: public void pauseScheduledReport(ReportUser reportUser, String name)
211: throws ProviderException {
212: try {
213: scheduler.pauseJob(name, reportUser.getId().toString());
214: } catch (SchedulerException e) {
215: throw new ProviderException(e);
216: }
217: }
218:
219: public void resumeScheduledReport(ReportUser reportUser, String name)
220: throws ProviderException {
221: try {
222: scheduler.resumeJob(name, reportUser.getId().toString());
223: } catch (SchedulerException e) {
224: throw new ProviderException(e);
225: }
226: }
227:
228: public void setScheduler(Scheduler scheduler) {
229: this .scheduler = scheduler;
230: }
231:
232: private String getTriggerStateName(String name, String group)
233: throws ProviderException {
234: int state = -1;
235:
236: try {
237: state = scheduler.getTriggerState(name, group);
238: } catch (SchedulerException e) {
239: throw new ProviderException(e);
240: }
241:
242: switch (state) {
243: case Trigger.STATE_BLOCKED:
244: return "Blocked";
245:
246: case Trigger.STATE_COMPLETE:
247: return "Complete";
248:
249: case Trigger.STATE_ERROR:
250: return "ERROR";
251:
252: case Trigger.STATE_NORMAL:
253: return "Normal";
254:
255: case Trigger.STATE_PAUSED:
256: return "Paused";
257:
258: default:
259: return "";
260: }
261: }
262: }
|