Package org.boco.seamUtility

Source Code of org.boco.seamUtility.JasperReportManager

/***************************************************************************
* Copyright (c) 2004 - 2008  Fabrizio Boco fabboco@users.sourceforge.net  *
*                                                                         *
*                                                                         *
*   This is free software; you can redistribute it and/or                 *
*   modify it under the terms of the GNU Library General Public           *
*   License (version 2.1) as published by the Free Software Foundation    *
*                                                                         *
*   This library  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 Library General Public License for more details.                  *
*                                                                         *
*   You should have received a copy of the GNU Library General Public     *
*   License along with this library; see the file COPYING.LIB. If not,    *
*   write to the Free Software Foundation, Inc., 59 Temple Place,         *
*   Suite 330, Boston, MA  02111-1307, USA                                *
*                                                                         *
***************************************************************************/

/**
- $Header: /usr/local/cvslocalrepository/SeamUtility/src/org/boco/seamUtility/JasperReportManager.java,v 1.3.4.3 2008/04/22 06:14:35 fab Exp $
- $Author: fab $
- $Revision: 1.3.4.3 $
- $Date: 2008/04/22 06:14:35 $


- $Log: JasperReportManager.java,v $
- Revision 1.3.4.3  2008/04/22 06:14:35  fab
- Aggiornamento indirizzo di posta
-
- Revision 1.3.4.2  2008/04/19 11:14:40  fab
- Aggiornamento riferimenti licenza e documentazione
-
- Revision 1.3.4.1  2008/03/11 13:31:12  fab
- *** empty log message ***
-
- Revision 1.4  2008/03/11 13:05:14  fab
- Migliore gestione delle segnalazioni di errore
-
- Revision 1.3  2007/10/22 14:23:53  fab
- Fix nella funzione export
-
- Revision 1.2  2007/10/22 15:45:22  fab
- Aggiornamento documentazione
-
- Revision 1.1  2007/09/30 07:45:40  fab
- Nuova versione iniziale del 30/09/2007
-
- Revision 1.2  2007/09/25 16:28:34  fab
- *** empty log message ***
-
- Revision 1.1  2007/09/21 14:53:48  fab
- Versione Iniziale
-
**/
package org.boco.seamUtility;

import java.io.File;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Locale;

import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JRExporterParameter;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.JasperRunManager;
import net.sf.jasperreports.engine.export.JRXlsExporter;
import net.sf.jasperreports.engine.export.JRXlsExporterParameter;


/**
*
* @author Fabrizio Boco
*
*/
public class JasperReportManager
{

  public static enum ReportType
  {
    PDF, XLS
  };

 
  /**
   * Consente l'export di un Report trasferendolo direttamente al browser.
   * <br>
   *
   * @param facesContext
   * @param jndiDataSourceName   nome jndi del data source con cui accedere ai dati
   * @param reportsDirectoryPath path assoluto alla directory che contiene i reports
   * @param reportFileName nome del file che contiene il report che deve essere esportato
   * @param outputFileName       nome del file che deve essere assegnato all'export
   * @param type                 tipo di report (PDF o XLS)
   * @param params        parametri da passare al report
   */
 
 
  public static void export(FacesContext facesContext, String jndiDataSourceName, String reportsDirectoryPath, String reportFileName, String outputFileName, ReportType type, HashMap params) throws JRException, NamingException, SQLException, Exception
  {
    String retMessage = "OK";
    Context initContext = new InitialContext();
    DataSource dataSource = (DataSource) initContext.lookup(jndiDataSourceName);
    Connection conn = dataSource.getConnection();

    if (!reportsDirectoryPath.endsWith(File.separator))
      reportsDirectoryPath += File.separator;

    Locale currentLocale = new java.util.Locale("it", "IT");
    params.put("REPORT_LOCALE", currentLocale);
    params.put("SUBREPORT_DIR", reportsDirectoryPath);

    byte[] data = null;

    String contentType = "";
    String fileExt = "";

    try
    {
      ExternalContext context = facesContext.getExternalContext();
      HttpServletResponse response = (HttpServletResponse) context.getResponse();
      OutputStream out = response.getOutputStream();
     
      switch (type)
      {
        case PDF:
          contentType = "application/pdf";
          fileExt = "pdf";
         
          data = JasperRunManager.runReportToPdf(reportsDirectoryPath + reportFileName + ".jasper", params, conn);

          response.setHeader("Content-Disposition", "attachment;filename=\"" + outputFileName + "." + fileExt + "\"");
          response.setHeader("Cache-Control", "no-cache");
          response.setContentLength((int) data.length);
          response.setContentType(contentType);

          out.write(data, 0, data.length);
          break;
         
        case XLS:
          contentType = "application/xls";
          fileExt = "xls";

          JasperReport report = JasperCompileManager.compileReport(reportsDirectoryPath + reportFileName + ".jrxml");
          JasperPrint print = JasperFillManager.fillReport(report, params, conn);

          JRXlsExporter exporter = new JRXlsExporter();

          exporter.setParameter(JRExporterParameter.JASPER_PRINT, print);
          exporter.setParameter(JRXlsExporterParameter.OUTPUT_STREAM, out);
          exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE);
          exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.TRUE);
          exporter.setParameter(JRXlsExporterParameter.IS_AUTO_DETECT_CELL_TYPE, Boolean.TRUE);
          exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE);
                   
          response.setHeader("Content-disposition", "inline; filename=\"" + outputFileName + "." + fileExt + "\"");
          response.setHeader("Cache-Control", "no-cache");
          response.setContentType(contentType);
         
          exporter.exportReport();         
          break;
      }

      out.flush();
      out.close();
      facesContext.responseComplete();
    }
    catch (Exception ex)
    {
      System.out.println("-----------------------***----");
      System.out.println(ex);
      System.out.println(ex.getCause());
      System.out.println("-----------------------***----");
      throw new Exception(ex.getMessage());
    }
    finally
    {
      if (conn != null)
        conn.close();
    }
  }
}
TOP

Related Classes of org.boco.seamUtility.JasperReportManager

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.