⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 schedulerproviderimpl.java

📁 OpenReports是一个完整的基于Web的报表方案
💻 JAVA
字号:
/*
 * Copyright (C) 2003 Erik Swenson - eswenson@opensourcesoft.net
 * 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
 * more details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
 * Place - Suite 330, Boston, MA 02111-1307, USA.
 *  
 */

package org.efs.openreports.providers.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.quartz.*;

import org.efs.openreports.ORStatics;
import org.efs.openreports.objects.ReportSchedule;
import org.efs.openreports.objects.ReportUser;
import org.efs.openreports.providers.ProviderException;
import org.efs.openreports.providers.SchedulerProvider;
import org.efs.openreports.util.ScheduledReportJob;

public class SchedulerProviderImpl implements SchedulerProvider
{
	protected static Logger log =
		Logger.getLogger(SchedulerProviderImpl.class.getName());

	private Scheduler scheduler;

	public SchedulerProviderImpl()
	{
		SchedulerFactory schedulerFactory =
			new org.quartz.impl.StdSchedulerFactory();

		try
		{
			scheduler = schedulerFactory.getScheduler();
			scheduler.start();

			log.info("SchedulerProviderImpl created");
		}
		catch (SchedulerException se)
		{
			log.error(se.toString());
		}
	}

	public void scheduleReport(ReportSchedule reportSchedule)
		throws ProviderException
	{
		JobDetail jobDetail =
			new JobDetail(
				reportSchedule.getScheduleName(),
				reportSchedule.getScheduleGroup(),
				ScheduledReportJob.class);

		jobDetail.getJobDataMap().put(
			ORStatics.REPORT_SCHEDULE,
			reportSchedule);

		try
		{
			if (reportSchedule.getScheduleType() == ReportSchedule.DAILY)
			{
				StringBuffer cronExpression = new StringBuffer();
				cronExpression.append("0 ");
				cronExpression.append(reportSchedule.getStartMinute());
				cronExpression.append(" ");
				cronExpression.append(reportSchedule.getAbsoluteStartHour());

				if (reportSchedule.getScheduleType() == ReportSchedule.DAILY)
				{
					cronExpression.append(" * * ?");
				}
				else
				{
					cronExpression.append(" ? * ");
					cronExpression.append(reportSchedule.getDayOfWeek());
				}

				CronTrigger cronTrigger =
					new CronTrigger(
						reportSchedule.getScheduleName(),
						reportSchedule.getScheduleGroup());

				cronTrigger.setCronExpression(cronExpression.toString());
				cronTrigger.setStartTime(reportSchedule.getStartDate());

				scheduler.scheduleJob(jobDetail, cronTrigger);
			}
			else
			{
				// default to run once...
				SimpleTrigger trigger =
					new SimpleTrigger(
						reportSchedule.getScheduleName(),
						reportSchedule.getScheduleGroup(),
						reportSchedule.getStartDateTime(),
						null,
						0,
						0L);

				scheduler.scheduleJob(jobDetail, trigger);
			}
		}
		catch (Exception e)
		{
			throw new ProviderException(e);
		}

	}

	public List getScheduledReports(ReportUser reportUser)
		throws ProviderException
	{
		List scheduledReports = new ArrayList();

		String group = reportUser.getId().toString();

		try
		{
			String[] jobNames = scheduler.getJobNames(group);

			for (int i = 0; i < jobNames.length; i++)
			{
				JobDetail jobDetail =
					scheduler.getJobDetail(jobNames[i], group);

				ReportSchedule reportSchedule =
					(ReportSchedule) jobDetail.getJobDataMap().get(
						ORStatics.REPORT_SCHEDULE);

				scheduledReports.add(reportSchedule);
			}
		}
		catch (Exception e)
		{
			throw new ProviderException(e);
		}

		return scheduledReports;
	}

	public void deleteScheduledReport(ReportUser reportUser, String name)
		throws ProviderException
	{
		String group = reportUser.getId().toString();

		try
		{
			scheduler.deleteJob(name, group);
		}
		catch (Exception e)
		{
			throw new ProviderException(e);
		}
	}

	public ReportSchedule getScheduledReport(
		ReportUser reportUser,
		String name)
		throws ProviderException
	{
		String group = reportUser.getId().toString();

		try
		{
			JobDetail jobDetail = scheduler.getJobDetail(name, group);
			
			return (ReportSchedule) jobDetail.getJobDataMap().get(
				ORStatics.REPORT_SCHEDULE);
		}
		catch (Exception e)
		{
			throw new ProviderException(e);
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -