📄 scheduledreportjob.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.util;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.util.Date;
import java.util.Map;
import org.apache.log4j.Logger;
import org.efs.openreports.ORStatics;
import org.efs.openreports.objects.*;
import org.efs.openreports.providers.*;
import org.quartz.*;
import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.export.*;
public class ScheduledReportJob
implements
Job,
ReportLogProviderAware,
ReportWriteProviderAware,
ChartProviderAware,
DirectoryProviderAware,
MailProviderAware
{
protected static Logger log =
Logger.getLogger(ScheduledReportJob.class.getName());
private ReportWriteProvider reportWriteProvider;
private ReportLogProvider reportLogProvider;
private ChartProvider chartProvider;
private DirectoryProvider directoryProvider;
private MailProvider mailProvider;
public ScheduledReportJob()
{
ORComponentManager.initializeObject(this);
}
public void execute(JobExecutionContext context)
throws JobExecutionException
{
log.debug("Scheduled Report Executing....");
JobDataMap jobDataMap = context.getJobDetail().getJobDataMap();
ReportSchedule reportSchedule =
(ReportSchedule) jobDataMap.get(ORStatics.REPORT_SCHEDULE);
Report report = reportSchedule.getReport();
ReportUser user = reportSchedule.getUser();
Map reportParameters = reportSchedule.getReportParameters();
log.debug("Report: " + report.getName());
log.debug("User: " + user.getName());
ReportLog reportLog = new ReportLog(user, report, new Date());
try
{
reportLogProvider.insertReportLog(reportLog);
// add any charts
if (report.getReportChart() != null)
{
log.debug("Adding chart: " + report.getReportChart().getName());
chartProvider.addCharts(report, reportParameters);
}
// add standard report parameters
reportParameters.put(ORStatics.USER_ID, user.getId());
reportParameters.put(ORStatics.EXTERNAL_ID, user.getExternalId());
reportParameters.put(ORStatics.USER_NAME, user.getName());
reportParameters.put(
ORStatics.IMAGE_DIR,
new File(directoryProvider.getReportImageDirectory()));
JasperPrint jasperPrint =
reportWriteProvider.fillReport(report, reportParameters);
log.debug("Report size: " + jasperPrint.getPages().size());
ByteArrayDataSource byteArrayDataSource =
exportReport(jasperPrint, reportSchedule);
MailMessage mail = new MailMessage();
mail.setSubject(report.getName());
mail.setText(report.getName() + ": Generated on " + new Date());
mail.setByteArrayDataSource(byteArrayDataSource);
mail.setSender(user.getEmail());
mail.parseRecipients(reportSchedule.getRecipients());
mailProvider.sendMail(mail);
log.debug(
byteArrayDataSource.getName()
+ " sent to: "
+ mail.formatRecipients(";"));
reportLog.setEndTime(new Date());
reportLog.setStatus(ReportLog.STATUS_SUCCESS);
reportLogProvider.updateReportLog(reportLog);
log.debug("Scheduled Report Finished...");
}
catch (Exception e)
{
if (e.getMessage() != null && e.getMessage().indexOf("Empty") > 0)
{
reportLog.setStatus(ReportLog.STATUS_EMPTY);
}
else
{
e.printStackTrace();
log.error(e.toString());
reportLog.setMessage(e.getMessage());
reportLog.setStatus(ReportLog.STATUS_FAILURE);
}
reportLog.setEndTime(new Date());
try
{
reportLogProvider.updateReportLog(reportLog);
}
catch (Exception ex)
{
log.error("Unable to create ReportLog: " + ex.getMessage());
}
}
}
protected ByteArrayDataSource exportReport(
JasperPrint jasperPrint,
ReportSchedule reportSchedule)
throws JRException
{
ByteArrayDataSource byteArrayDataSource = null;
ByteArrayOutputStream out = new ByteArrayOutputStream();
JRExporter exporter = null;
switch (reportSchedule.getExportType())
{
case ReportWriteProvider.EXPORT_XLS :
exporter = new JRXlsExporter();
break;
case ReportWriteProvider.EXPORT_CSV :
exporter = new JRCsvExporter();
break;
default :
exporter = new JRPdfExporter();
break;
}
exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
exporter.exportReport();
byte[] byteArray = out.toByteArray();
String reportName = reportSchedule.getReport().getName();
switch (reportSchedule.getExportType())
{
case ReportWriteProvider.EXPORT_XLS :
byteArrayDataSource =
new ByteArrayDataSource(
byteArray,
"text/comma-separated-values");
byteArrayDataSource.setName(reportName + ".xls");
break;
case ReportWriteProvider.EXPORT_CSV :
byteArrayDataSource =
new ByteArrayDataSource(byteArray, "application/pdf");
byteArrayDataSource.setName(reportName + ".csv");
break;
default :
byteArrayDataSource =
new ByteArrayDataSource(byteArray, "application/pdf");
byteArrayDataSource.setName(reportName + ".pdf");
break;
}
return byteArrayDataSource;
}
public void setReportWriteProvider(ReportWriteProvider reportWriteProvider)
{
this.reportWriteProvider = reportWriteProvider;
}
public void setReportLogProvider(ReportLogProvider reportLogProvider)
{
this.reportLogProvider = reportLogProvider;
}
public void setChartProvider(ChartProvider chartProvider)
{
this.chartProvider = chartProvider;
}
public void setDirectoryProvider(DirectoryProvider directoryProvider)
{
this.directoryProvider = directoryProvider;
}
public void setMailProvider(MailProvider mailProvider)
{
this.mailProvider = mailProvider;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -