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: * CSVTableExportTask.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.modules.gui.csv;
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.StreamReportProcessor;
045: import org.jfree.report.modules.output.table.csv.StreamCSVOutputProcessor;
046: import org.jfree.report.util.i18n.Messages;
047: import org.jfree.util.Log;
048:
049: /**
050: * An export task implementation that writes an report into a CSV file, and uses the table
051: * target to create layouted content.
052: *
053: * @author Thomas Morgner
054: */
055: public class CSVTableExportTask implements Runnable {
056: /**
057: * Provides access to externalized strings
058: */
059: private Messages messages;
060: /**
061: * The progress dialog that monitors the export process.
062: */
063: private final ReportProgressDialog progressDialog;
064: /**
065: * The name of the output file.
066: */
067: private final String fileName;
068:
069: /**
070: * The report that should be exported.
071: */
072: private final JFreeReport report;
073: private StatusListener statusListener;
074: private String encoding;
075:
076: /**
077: * Creates a new CSV export task.
078: *
079: * @param swingGuiContext the GUI Context
080: * @param dialog the progress monitor
081: * @param report the report that should be exported.
082: */
083: public CSVTableExportTask(final JFreeReport report,
084: final ReportProgressDialog dialog,
085: final SwingGuiContext swingGuiContext)
086: throws ReportProcessingException {
087: if (report == null) {
088: throw new NullPointerException(
089: "CSVTableExportTask(..): Report parameter cannot be null"); //$NON-NLS-1$
090: }
091:
092: final String filename = report.getConfiguration()
093: .getConfigProperty(
094: "org.jfree.report.modules.gui.csv.FileName"); //$NON-NLS-1$
095: if (filename == null) {
096: throw new ReportProcessingException(
097: "CSVTableExportTask(..): Configuration does not contain a valid filename"); //$NON-NLS-1$
098: }
099:
100: this .encoding = report.getConfiguration().getConfigProperty(
101: CSVTableExportDialog.CSV_OUTPUT_ENCODING,
102: CSVTableExportDialog.CSV_OUTPUT_ENCODING_DEFAULT);
103:
104: this .progressDialog = dialog;
105: this .report = report;
106: this .fileName = filename;
107: if (swingGuiContext != null) {
108: this .statusListener = swingGuiContext.getStatusListener();
109: this .messages = new Messages(swingGuiContext.getLocale(),
110: CSVTableExportPlugin.BASE_RESOURCE_CLASS);
111: }
112: }
113:
114: /**
115: * Exports the report into a CSV file.
116: */
117: public void run() {
118: OutputStream out = null;
119: final File file = new File(fileName);
120: try {
121: final File directory = file.getAbsoluteFile()
122: .getParentFile();
123: if (directory != null) {
124: if (directory.exists() == false) {
125: if (directory.mkdirs() == false) {
126: Log
127: .warn("Can't create directories. Hoping and praying now.."); //$NON-NLS-1$
128: }
129: }
130: }
131: out = new BufferedOutputStream(new FileOutputStream(file));
132: final StreamCSVOutputProcessor target = new StreamCSVOutputProcessor(
133: report.getConfiguration(), out);
134: target.setEncoding(encoding);
135:
136: final StreamReportProcessor reportProcessor = new StreamReportProcessor(
137: report, target);
138: if (progressDialog != null) {
139: progressDialog.setModal(false);
140: progressDialog.setVisible(true);
141: reportProcessor
142: .addReportProgressListener(progressDialog);
143: }
144: reportProcessor.processReport();
145: out.close();
146: out = null;
147: if (progressDialog != null) {
148: reportProcessor
149: .removeReportProgressListener(progressDialog);
150: }
151:
152: if (statusListener != null) {
153: statusListener
154: .setStatus(
155: StatusType.INFORMATION,
156: messages
157: .getString("CSVTableExportTask.USER_TASK_COMPLETE")); //$NON-NLS-1$
158: }
159: } catch (ReportInterruptedException re) {
160: if (statusListener != null) {
161: statusListener
162: .setStatus(
163: StatusType.INFORMATION,
164: messages
165: .getString("CSVTableExportTask.USER_TASK_ABORTED")); //$NON-NLS-1$
166: }
167:
168: try {
169: out.close();
170: out = null;
171: if (file.delete() == false) {
172: Log
173: .warn(new Log.SimpleMessage(
174: "Unable to delete incomplete export:", file)); //$NON-NLS-1$
175: }
176: } catch (SecurityException se) {
177: // ignore me
178: } catch (IOException ioe) {
179: // ignore me...
180: }
181: } catch (Exception re) {
182: Log.error("Exporting failed .", re); //$NON-NLS-1$
183: if (statusListener != null) {
184: statusListener
185: .setStatus(
186: StatusType.ERROR,
187: messages
188: .getString("CSVTableExportTask.USER_TASK_FAILED")); //$NON-NLS-1$
189: }
190: } finally {
191: try {
192: if (out != null) {
193: out.close();
194: }
195: } catch (Exception e) {
196: Log.error("Unable to close the output stream.", e); //$NON-NLS-1$
197: // if there is already another error, this exception is
198: // just a minor obstactle. Something big crashed before ...
199: }
200:
201: if (progressDialog != null) {
202: progressDialog.setVisible(false);
203: }
204: }
205: }
206: }
|