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: * CSVDataExportTask.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.BufferedWriter;
032: import java.io.File;
033: import java.io.FileOutputStream;
034: import java.io.IOException;
035: import java.io.OutputStreamWriter;
036: import java.io.Writer;
037:
038: import org.jfree.report.JFreeReport;
039: import org.jfree.report.ReportInterruptedException;
040: import org.jfree.report.ReportProcessingException;
041: import org.jfree.report.modules.gui.common.StatusListener;
042: import org.jfree.report.modules.gui.commonswing.ReportProgressDialog;
043: import org.jfree.report.modules.gui.commonswing.StatusType;
044: import org.jfree.report.modules.gui.commonswing.SwingGuiContext;
045: import org.jfree.report.modules.output.csv.CSVProcessor;
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 raw
051: * target to create layouted content.
052: *
053: * @author Thomas Morgner
054: */
055: public class CSVDataExportTask implements Runnable {
056: /**
057: * Provides access to externalized strings
058: */
059: private Messages messages;
060: /**
061: * The name of the output file.
062: */
063: private final String fileName;
064: /**
065: * The encoding to be used for the file.
066: */
067: private final String encoding;
068: /**
069: * The report that should be exported.
070: */
071: private final JFreeReport report;
072: private ReportProgressDialog progressDialog;
073: private StatusListener statusListener;
074:
075: /**
076: * Creates a new CSV export task.
077: *
078: * @param report the report that should be exported.
079: * @param dialog the progress dialog to inform the user about the report progress.
080: * @param swingGuiContext the context connecting the task to the outside UI.
081: */
082: public CSVDataExportTask(final JFreeReport report,
083: final ReportProgressDialog dialog,
084: final SwingGuiContext swingGuiContext)
085: throws ReportProcessingException {
086: if (report == null) {
087: throw new NullPointerException(
088: "CSVDataExportTask(..): Report parameter cannot be null"); //$NON-NLS-1$
089: }
090:
091: final String filename = report.getConfiguration()
092: .getConfigProperty(
093: "org.jfree.report.modules.gui.csv.FileName"); //$NON-NLS-1$
094: if (filename == null) {
095: throw new ReportProcessingException(
096: "CSVDataExportTask(..): Configuration does not contain a valid filename"); //$NON-NLS-1$
097: }
098:
099: this .encoding = report.getConfiguration().getConfigProperty(
100: CSVDataExportDialog.CSV_OUTPUT_ENCODING,
101: CSVDataExportDialog.CSV_OUTPUT_ENCODING_DEFAULT);
102: this .fileName = filename;
103: this .report = report;
104: this .progressDialog = dialog;
105: if (swingGuiContext != null) {
106: this .statusListener = swingGuiContext.getStatusListener();
107: this .messages = new Messages(swingGuiContext.getLocale(),
108: CSVDataExportPlugin.BASE_RESOURCE_CLASS);
109: }
110: }
111:
112: /**
113: * Exports the report into a CSV file.
114: */
115: public void run() {
116: Writer out = null;
117: final File file = new File(fileName);
118: try {
119: final File directory = file.getAbsoluteFile()
120: .getParentFile();
121: if (directory != null) {
122: if (directory.exists() == false) {
123: if (directory.mkdirs() == false) {
124: Log
125: .warn("Can't create directories. Hoping and praying now.."); //$NON-NLS-1$
126: }
127: }
128: }
129:
130: out = new BufferedWriter(new OutputStreamWriter(
131: new FileOutputStream(file), encoding));
132:
133: final CSVProcessor target = new CSVProcessor(report);
134: if (progressDialog != null) {
135: progressDialog.setModal(false);
136: progressDialog.setVisible(true);
137: target.addReportProgressListener(progressDialog);
138: }
139: target.setWriter(out);
140: target.processReport();
141: out.close();
142: out = null;
143:
144: if (progressDialog != null) {
145: target.removeReportProgressListener(progressDialog);
146: }
147:
148: if (statusListener != null) {
149: statusListener
150: .setStatus(
151: StatusType.INFORMATION,
152: messages
153: .getString("CSVRawExportTask.USER_TASK_COMPLETE")); //$NON-NLS-1$
154: }
155: } catch (ReportInterruptedException re) {
156: if (statusListener != null) {
157: statusListener
158: .setStatus(
159: StatusType.INFORMATION,
160: messages
161: .getString("CSVRawExportTask.USER_TASK_ABORTED")); //$NON-NLS-1$
162: }
163: try {
164: out.close();
165: out = null;
166: if (file.delete() == false) {
167: Log
168: .warn(new Log.SimpleMessage(
169: "Unable to delete incomplete export:", file)); //$NON-NLS-1$
170: }
171: } catch (SecurityException se) {
172: // ignore me
173: } catch (IOException ioe) {
174: // ignore me...
175: }
176: } catch (Exception re) {
177: Log.error("Exporting failed .", re); //$NON-NLS-1$
178: if (statusListener != null) {
179: statusListener
180: .setStatus(
181: StatusType.ERROR,
182: messages
183: .getString("CSVRawExportTask.USER_TASK_FAILED")); //$NON-NLS-1$
184: }
185: } finally {
186: try {
187: if (out != null) {
188: out.close();
189: }
190: } catch (Exception e) {
191: if (statusListener != null) {
192: statusListener
193: .setStatus(
194: StatusType.ERROR,
195: messages
196: .getString("CSVRawExportTask.USER_TASK_FAILED")); //$NON-NLS-1$
197: }
198: Log.error("Unable to close the output stream.", e); //$NON-NLS-1$
199: }
200:
201: if (progressDialog != null) {
202: progressDialog.setVisible(false);
203: }
204: }
205: }
206: }
|