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

📄 reportwriteproviderimpl.java

📁 OpenReports是一个完整的基于Web的报表方案
💻 JAVA
字号:
/*
 * Copyright (C) 2002 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.io.OutputStream;
import java.sql.Connection;
import java.util.Map;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import com.opensymphony.xwork.ActionContext;
import com.opensymphony.xwork.interceptor.component.ComponentManager;

import dori.jasper.engine.*;
import dori.jasper.engine.export.*;

import org.apache.log4j.Logger;

import org.efs.openreports.objects.Report;
import org.efs.openreports.objects.ReportDataSource;
import org.efs.openreports.providers.*;

public class ReportWriteProviderImpl
	implements ReportWriteProvider, DataSourceProviderAware, DirectoryProviderAware
{
	protected static Logger log =
		Logger.getLogger(ReportWriteProviderImpl.class.getName());

	private DataSourceProvider dataSourceProvider;
	private DirectoryProvider directoryProvider;

	public ReportWriteProviderImpl() throws ProviderException
	{
		ComponentManager container =
			(ComponentManager) ActionContext.getContext().get(
				"com.opensymphony.xwork.interceptor.component.ComponentManager");

		container.initializeObject(this);

	}

	public JasperPrint fillReport(Report report, Map map)
		throws ProviderException
	{
		Connection conn = null;

		ReportDataSource dataSource = report.getDataSource();

		try
		{
			JasperReport jr =
				JasperManager.loadReport(
					directoryProvider.getReportDirectory() + report.getFile());

			JasperPrint jp = null;

			if (dataSource == null)
			{
				jp = JasperManager.fillReport(jr, map, new JREmptyDataSource());
			}
			else
			{
				if (dataSource.isJndi())
				{
					Context initCtx = new InitialContext();

					DataSource jndiDataSource =
						(DataSource) initCtx.lookup(dataSource.getUrl());

					conn = jndiDataSource.getConnection();
				}
				else
				{
					conn = dataSourceProvider.getConnection(dataSource.getId());
				}

				jp = JasperManager.fillReport(jr, map, conn);
			}

			if (jp == null || jp.getPages().size() < 1)
				throw new ProviderException("Report Empty");

			return jp;
		}
		catch (Exception e)
		{
			log.error("Error creating report: " + e.toString());
			throw new ProviderException(
				"Error creating report: " + e.getMessage());
		}
		finally
		{
			try
			{
				if (conn != null)
					conn.close();
			}
			catch (Exception ex)
			{
				log.error("Error closing connection: " + ex.getMessage());
			}
		}
	}

	public void sendReportToStream(
		JasperPrint jasperPrint,
		OutputStream out,
		int exportType)
		throws ProviderException
	{
		sendReportToStream(jasperPrint, null, out, exportType);
	}

	public void sendReportToStream(
		JasperPrint jasperPrint,
		Map imagesMap,
		OutputStream out,
		int exportType)
		throws ProviderException
	{
		JRAbstractExporter exporter = null;

		try
		{
			if (exportType == EXPORT_PDF)
			{
				byte[] pdfByteArray =
					JasperExportManager.exportReportToPdf(jasperPrint);

				out.write(pdfByteArray, 0, pdfByteArray.length);
				out.flush();
				out.close();

				return;
			}

			if (exportType == EXPORT_XLS)
			{
				exporter = new JRXlsExporter();

				exporter.setParameter(
					JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET,
					Boolean.TRUE);
			}
			else if (exportType == EXPORT_CSV)
			{
				exporter = new JRCsvExporter();
			}
			else
			{
				exporter = new JRHtmlExporter();

				// see ImageLoaderAction for more information
				exporter.setParameter(
					JRHtmlExporterParameter.IMAGES_MAP,
					imagesMap);
				exporter.setParameter(
					JRHtmlExporterParameter.IMAGES_URI,
					"imageLoader.action?imageName=");
			}

			exporter.setParameter(
				JRExporterParameter.JASPER_PRINT,
				jasperPrint);
			exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
			exporter.exportReport();
		}
		catch (Exception e)
		{
			throw new ProviderException(e.toString());
		}
	}

	public void sendPDFToStream(byte[] pdfByteArray, OutputStream out)
		throws ProviderException
	{
		try
		{
			out.write(pdfByteArray, 0, pdfByteArray.length);
			out.flush();
			out.close();
		}
		catch (Exception e)
		{
			throw new ProviderException(e.toString());
		}
	}

	public void setDataSourceProvider(DataSourceProvider dataSourceProvider)
	{
		this.dataSourceProvider = dataSourceProvider;
	}

	public void setDirectoryProvider(DirectoryProvider directoryProvider)
	{
		this.directoryProvider = directoryProvider;
	}

}

⌨️ 快捷键说明

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