001    /*
002      Copyright (C) 2002-2003 Laurent Martelli <laurent@aopsys.com>
003      
004      This program is free software; you can redistribute it and/or modify
005      it under the terms of the GNU Lesser General Public License as
006      published by the Free Software Foundation; either version 2 of the
007      License, or (at your option) any later version.
008    
009      This program is distributed in the hope that it will be useful, but
010      WITHOUT ANY WARRANTY; without even the implied warranty of
011      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
012      Lesser General Public License for more details.
013    
014      You should have received a copy of the GNU Lesser General Public
015      License along with this program; if not, write to the Free Software
016      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
017      USA */
018    
019    package org.objectweb.jac.aspects.gui;
020    
021    import dori.jasper.engine.JRDataSource;
022    import dori.jasper.engine.JRException;
023    import dori.jasper.engine.JasperCompileManager;
024    import dori.jasper.engine.JasperExportManager;
025    import dori.jasper.engine.JasperFillManager;
026    import dori.jasper.engine.JasperPrint;
027    import dori.jasper.engine.JasperReport;
028    import java.io.File;
029    import java.io.FileOutputStream;
030    import java.io.IOException;
031    import java.io.OutputStream;
032    import java.util.HashMap;
033    import java.util.Map;
034    import org.apache.log4j.Logger;
035    import org.objectweb.jac.aspects.gui.reports.JacDataSource;
036    import org.objectweb.jac.core.rtti.ClassRepository;
037    
038    public class Reports {
039        static Logger logger = Logger.getLogger("report");
040    
041        /**
042         * Generate a PDF report
043         * @param reportDef resource name of the XML report definition file
044         * @param pdfFile file where to store the resulting PDF document
045         */
046        public static void genReport(String reportDef, File pdfFile) 
047            throws JRException 
048        {
049            genReport(reportDef,pdfFile,new HashMap());
050        }
051    
052        /**
053         * Generate a PDF report
054         * @param reportDef resource name of the XML report definition file
055         * @param pdfFile file where to store the resulting PDF document
056         * @param parameters the parameters to fill the report with
057         */
058        public static void genReport(String reportDef, File pdfFile, Map parameters) 
059            throws JRException 
060        {
061            JasperReport report = getJasperReport(reportDef);
062            logger.debug("Filling report "+report);
063            JasperPrint print = 
064                JasperFillManager.fillReport(
065                    report,
066                    parameters,
067                    new JacDataSource(ClassRepository.get().getClass(Class.class)));
068            logger.debug("Exporting report "+print);
069            JasperExportManager.exportReportToPdfFile(print,pdfFile.getPath());
070            logger.debug("Done");
071        }
072    
073        /**
074         * Generate a PDF report
075         * @param reportDef resource name of the XML report definition file
076         * @param out file where to store the resulting PDF document
077         * @param parameters the parameters to fill the report with
078         * @param dataSource the data source
079         */
080        public static void genReport(String reportDef, OutputStream out, 
081                                     Map parameters, JRDataSource dataSource) 
082            throws JRException 
083        {
084            JasperReport report = getJasperReport(reportDef);
085            logger.debug("Filling report "+report);
086            JasperPrint print = 
087                JasperFillManager.fillReport(
088                    report,
089                    parameters,
090                    dataSource);
091            logger.debug("Exporting report "+print);
092            JasperExportManager.exportReportToPdfStream(print,out);
093            logger.debug("Done");
094        }
095    
096        public static JasperReport getJasperReport(String reportDef) throws JRException {
097            logger.debug("Compiling report file "+reportDef);
098            return
099                JasperCompileManager.compileReport(
100                    Actions.class.getClassLoader().getResourceAsStream(reportDef));
101        }
102    }