001    /**
002     * ========================================
003     * JFreeReport : a free Java report library
004     * ========================================
005     *
006     * Project Info:  http://reporting.pentaho.org/
007     *
008     * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
009     *
010     * This library is free software; you can redistribute it and/or modify it under the terms
011     * of the GNU Lesser General Public License as published by the Free Software Foundation;
012     * either version 2.1 of the License, or (at your option) any later version.
013     *
014     * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015     * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016     * See the GNU Lesser General Public License for more details.
017     *
018     * You should have received a copy of the GNU Lesser General Public License along with this
019     * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020     * Boston, MA 02111-1307, USA.
021     *
022     * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023     * in the United States and other countries.]
024     *
025     * ------------
026     * $Id: PdfExportTask.java 2890 2007-06-10 15:54:22Z taqua $
027     * ------------
028     * (C) Copyright 2000-2005, by Object Refinery Limited.
029     * (C) Copyright 2005-2007, by Pentaho Corporation.
030     */
031    
032    package org.jfree.report.modules.gui.swing.pdf;
033    
034    import java.io.File;
035    import java.io.FileOutputStream;
036    
037    import org.jfree.layouting.modules.output.pdf.PdfOutputProcessor;
038    import org.jfree.report.ReportConfigurationException;
039    import org.jfree.report.flow.ReportJob;
040    import org.jfree.report.flow.streaming.StreamingReportProcessor;
041    import org.jfree.util.Configuration;
042    import org.jfree.util.Log;
043    
044    /**
045     * Creation-Date: 02.12.2006, 15:34:17
046     *
047     * @author Thomas Morgner
048     */
049    public class PdfExportTask implements Runnable
050    {
051      private ReportJob job;
052      private File targetFile;
053    
054      public PdfExportTask(final ReportJob job)
055          throws ReportConfigurationException
056      {
057        if (job == null)
058        {
059          throw new NullPointerException();
060        }
061        this.job = job;
062        final Configuration config = job.getConfiguration();
063        final String targetFileName = config.getConfigProperty("org.jfree.report.modules.gui.common.pdf.TargetFileName");
064    
065        targetFile = new File(targetFileName);
066        if (targetFile.exists())
067        {
068          if (targetFile.delete())
069          {
070            throw new ReportConfigurationException("Target-File exists, but cannot be removed.");
071          }
072        }
073      }
074    
075      /**
076       * When an object implementing interface <code>Runnable</code> is used to
077       * create a thread, starting the thread causes the object's <code>run</code>
078       * method to be called in that separately executing thread.
079       * <p/>
080       * The general contract of the method <code>run</code> is that it may take any
081       * action whatsoever.
082       *
083       * @see Thread#run()
084       */
085      public void run()
086      {
087        try
088        {
089          final FileOutputStream fout = new FileOutputStream(targetFile);
090          final StreamingReportProcessor sp = new StreamingReportProcessor();
091          final PdfOutputProcessor outputProcessor = new PdfOutputProcessor(job.getConfiguration(), fout);
092          sp.setOutputProcessor(outputProcessor);
093          sp.processReport(job);
094    
095        }
096        catch (Exception e)
097        {
098          Log.error ("PDF-Export failed. ", e);
099        }
100        try
101        {
102          job.close();
103          job = null;
104        }
105        catch(Exception e)
106        {
107          // ignore ..
108        }
109      }
110    }