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

📄 queryreportaction.java

📁 The ability to create groups of reports, and grant users access to reports by group. The ability to
💻 JAVA
字号:
/*
 * Copyright (C) 2004 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.actions;

import java.sql.*;
import java.util.*;
import java.util.Date;

import com.opensymphony.xwork.ActionContext;
import com.opensymphony.xwork.ActionSupport;

import org.apache.commons.beanutils.DynaProperty;
import org.apache.commons.beanutils.RowSetDynaClass;
import org.apache.log4j.Logger;
import org.efs.openreports.ORStatics;
import org.efs.openreports.objects.*;
import org.efs.openreports.providers.*;
import org.efs.openreports.util.ORUtil;

import net.sf.jasperreports.engine.design.JRDesignQuery;
import net.sf.jasperreports.engine.util.JRQueryExecuter;

public class QueryReportAction
	extends ActionSupport
	implements ReportLogProviderAware, DataSourceProviderAware
{
	protected static Logger log = Logger.getLogger(QueryReportAction.class);

	private DataSourceProvider dataSourceProvider;
	private ReportLogProvider reportLogProvider;
	
	private List results;
	private DynaProperty[] properties;

	public String execute()
	{
		ReportUser user =
			(ReportUser) ActionContext.getContext().getSession().get(
				ORStatics.REPORT_USER);

		Report report =
			(Report) ActionContext.getContext().getSession().get(
				ORStatics.REPORT);

		Map reportParameters = getReportParameterMap(user);

		ReportLog reportLog = new ReportLog(user, report, new Date());

		try
		{
			log.debug("Starting Query Report: " + report.getName());
			log.debug("Query: " + report.getQuery());

			reportLogProvider.insertReportLog(reportLog);

			executeQuery(report, reportParameters);

			reportLog.setEndTime(new Date());
			reportLog.setStatus(ReportLog.STATUS_SUCCESS);
			reportLogProvider.updateReportLog(reportLog);

			log.debug("Finished Query Report: " + report.getName());
		}
		catch (Exception e)
		{

			addActionError(e.getMessage());

			log.error(e.getMessage());

			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());
			}

			return ERROR;
		}

		return SUCCESS;
	}

	private void executeQuery(Report report, Map parameters)
		throws ProviderException
	{
		results = new ArrayList();
		
		Connection conn = null;
		PreparedStatement pStmt = null;
		ResultSet rs = null;

		try
		{
			ReportDataSource dataSource = report.getDataSource();
			conn = dataSourceProvider.getConnection(dataSource.getId());			

			if (parameters == null || parameters.isEmpty())
			{
				pStmt = conn.prepareStatement(report.getQuery());				
			}
			else				
			{				
				// Use JasperReports Query logic to parse parameters in chart
				// queries

				JRDesignQuery query = new JRDesignQuery();
				query.setText(report.getQuery());

				// convert parameters to JRDesignParameters so they can be
				// parsed
				Map jrParameters = ORUtil.buildJRDesignParameters(parameters);
				
				pStmt =
					JRQueryExecuter.getStatement(
						query,
						jrParameters,
						parameters,
						conn);				
			}
			
			rs = pStmt.executeQuery();

			RowSetDynaClass rowSetDynaClass = new RowSetDynaClass(rs);			
			
			results = rowSetDynaClass.getRows();	
			properties = rowSetDynaClass.getDynaProperties();
			
			rs.close();
		}
		catch (Exception e)
		{
			throw new ProviderException(
				"Error executing report query: " + e.getMessage());
		}
		finally
		{
			try
			{
				if (pStmt != null)
					pStmt.close();
				if (conn != null)
					conn.close();
			}
			catch (Exception c)
			{
				log.error("Error closing");
			}
		}
	}

	protected Map getReportParameterMap(ReportUser user)
	{
		Map reportParameters = new HashMap();

		if (ActionContext
			.getContext()
			.getSession()
			.get(ORStatics.REPORT_PARAMETERS)
			!= null)
		{
			reportParameters =
				(Map) ActionContext.getContext().getSession().get(
					ORStatics.REPORT_PARAMETERS);
		}

		// 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());

		return reportParameters;
	}

	public void setReportLogProvider(ReportLogProvider reportLogProvider)
	{
		this.reportLogProvider = reportLogProvider;
	}

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

	public List getResults()
	{
		return results;
	}
	
	public DynaProperty[] getProperties()
	{
		return properties;
	}

}

⌨️ 快捷键说明

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