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

📄 reportwriteproviderimpl.java

📁 The ability to create groups of reports, and grant users access to reports by group. The ability to
💻 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.*;
import java.util.List;
import java.util.Map;

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

import org.apache.log4j.Logger;
import org.efs.openreports.objects.*;
import org.efs.openreports.objects.Report;
import org.efs.openreports.objects.ReportDataSource;
import org.efs.openreports.providers.*;

import net.sf.jasperreports.engine.*;
import net.sf.jasperreports.engine.export.*;
import net.sf.jasperreports.engine.util.JRLoader;

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 = (JasperReport) JRLoader.loadObject(directoryProvider
					.getReportDirectory()
					+ report.getFile());
			
			List subReports = report.getSubReportParameters();
			if (subReports != null && subReports.size() > 0)
			{
				Iterator iterator = report.getSubReportParameters().iterator();
				while(iterator.hasNext())
				{
					ReportParameterMap rpMap = (ReportParameterMap) iterator.next();
					
					JasperReport subReport = (JasperReport) JRLoader.loadObject(directoryProvider
							.getReportDirectory()
							+ rpMap.getReportParameter().getData());
					
					map.put(rpMap.getReportParameter().getName(), subReport);
				}
			}

			JasperPrint jp = null;

			if (dataSource == null)
			{
				jp = JasperFillManager.fillReport(jr, map, new JREmptyDataSource());
			}
			else
			{
				conn = dataSourceProvider.getConnection(dataSource.getId());
				jp = JasperFillManager.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 + -