001: /**
002: * ===========================================
003: * JFreeReport : a free Java reporting library
004: * ===========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2001-2007, by Object Refinery Ltd, 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: * ExcelExportTask.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.modules.gui.xls;
030:
031: import java.io.BufferedOutputStream;
032: import java.io.File;
033: import java.io.FileOutputStream;
034: import java.io.IOException;
035: import java.io.OutputStream;
036:
037: import org.jfree.report.JFreeReport;
038: import org.jfree.report.ReportInterruptedException;
039: import org.jfree.report.ReportProcessingException;
040: import org.jfree.report.modules.gui.common.StatusListener;
041: import org.jfree.report.modules.gui.commonswing.ReportProgressDialog;
042: import org.jfree.report.modules.gui.commonswing.StatusType;
043: import org.jfree.report.modules.gui.commonswing.SwingGuiContext;
044: import org.jfree.report.modules.output.table.base.FlowReportProcessor;
045: import org.jfree.report.modules.output.table.xls.FlowExcelOutputProcessor;
046: import org.jfree.report.util.i18n.Messages;
047: import org.jfree.util.Log;
048:
049: /**
050: * An export task implementation, which writes a given report into an Excel file.
051: *
052: * @author Thomas Morgner
053: */
054: public class ExcelExportTask implements Runnable {
055: /**
056: * Access to externalized strings
057: */
058: private Messages messages;
059:
060: /**
061: * The progress dialog that will be used to visualize the report progress.
062: */
063: private final ReportProgressDialog progressDialog;
064: /**
065: * The file name of the output file.
066: */
067: private final String fileName;
068: /**
069: * The report which should be exported.
070: */
071: private final JFreeReport report;
072: private StatusListener statusListener;
073:
074: /**
075: * Creates a new export task.
076: *
077: * @param dialog the progress dialog that will monitor the report progress.
078: * @param report the report that should be exported.
079: */
080: public ExcelExportTask(final JFreeReport report,
081: final ReportProgressDialog dialog,
082: final SwingGuiContext swingGuiContext)
083: throws ReportProcessingException {
084: if (report == null) {
085: throw new NullPointerException(
086: "ExcelExportTask(..): Null report parameter not permitted"); //$NON-NLS-1$
087: }
088: this .fileName = report.getConfiguration().getConfigProperty(
089: "org.jfree.report.modules.gui.xls.FileName"); //$NON-NLS-1$
090: if (fileName == null) {
091: throw new ReportProcessingException(
092: "ExcelExportTask(): Filename is not defined"); //$NON-NLS-1$
093: }
094: this .progressDialog = dialog;
095: this .report = report;
096: if (swingGuiContext != null) {
097: this .statusListener = swingGuiContext.getStatusListener();
098: this .messages = new Messages(swingGuiContext.getLocale(),
099: ExcelExportPlugin.BASE_RESOURCE_CLASS);
100: }
101: }
102:
103: /**
104: * Exports the report into an Excel file.
105: */
106: public void run() {
107: OutputStream out = null;
108: File file = null;
109: try {
110: file = new File(fileName).getCanonicalFile();
111: final File directory = file.getParentFile();
112: if (directory != null) {
113: if (directory.exists() == false) {
114: if (directory.mkdirs() == false) {
115: Log
116: .warn("Can't create directories. Hoping and praying now.."); //$NON-NLS-1$
117: }
118: }
119: }
120: out = new BufferedOutputStream(new FileOutputStream(file));
121: final FlowExcelOutputProcessor target = new FlowExcelOutputProcessor(
122: report.getConfiguration(), out);
123: final FlowReportProcessor reportProcessor = new FlowReportProcessor(
124: report, target);
125: if (progressDialog != null) {
126: progressDialog.setModal(false);
127: progressDialog.setVisible(true);
128: reportProcessor
129: .addReportProgressListener(progressDialog);
130: }
131: reportProcessor.processReport();
132: out.close();
133: out = null;
134: if (progressDialog != null) {
135: reportProcessor
136: .removeReportProgressListener(progressDialog);
137: }
138:
139: if (statusListener != null) {
140: statusListener
141: .setStatus(
142: StatusType.INFORMATION,
143: messages
144: .getString("ExcelExportTask.USER_TASK_FINISHED")); //$NON-NLS-1$
145: }
146:
147: } catch (ReportInterruptedException re) {
148: if (statusListener != null) {
149: statusListener
150: .setStatus(
151: StatusType.WARNING,
152: messages
153: .getString("ExcelExportTask.USER_TASK_ABORTED")); //$NON-NLS-1$
154: }
155:
156: try {
157: out.close();
158: out = null;
159: if (file.delete() == false) {
160: Log
161: .warn(new Log.SimpleMessage(
162: "Unable to delete incomplete export:", file)); //$NON-NLS-1$
163: }
164: } catch (SecurityException se) {
165: // ignore me
166: } catch (IOException ioe) {
167: // ignore me...
168: }
169: } catch (Exception re) {
170: Log.error("Excel export failed", re); //$NON-NLS-1$
171: if (statusListener != null) {
172: statusListener.setStatus(StatusType.WARNING, messages
173: .getString("ExcelExportTask.USER_TASK_FAILED")); //$NON-NLS-1$
174: }
175: } finally {
176: try {
177: if (out != null) {
178: out.close();
179: }
180: } catch (Exception e) {
181: Log.error("Unable to close the output stream.", e); //$NON-NLS-1$
182: if (statusListener != null) {
183: statusListener
184: .setStatus(
185: StatusType.WARNING,
186: messages
187: .getString("ExcelExportTask.USER_TASK_FAILED")); //$NON-NLS-1$
188: }
189: // if there is already another error, this exception is
190: // just a minor obstactle. Something big crashed before ...
191: }
192: }
193: if (progressDialog != null) {
194: progressDialog.setVisible(false);
195: }
196: }
197: }
|