001 package com.sptci.rwt.webui;
002
003 import java.io.IOException;
004 import java.io.OutputStream;
005
006 import nextapp.echo2.app.filetransfer.DownloadProvider;
007
008 import org.apache.poi.hssf.usermodel.HSSFWorkbook;
009
010 import com.sptci.echo2.Configuration;
011 import com.sptci.rwt.BatchQueryExecutor;
012 import com.sptci.rwt.ConnectionManager;
013
014 /**
015 * Action listener for exporting the results of a SQL statement to
016 * Excel. This uses the
017 * <a href='http://poi.apache.org/hssf/index.html'>Apache POI HSSF</a>
018 * library for creating Excel workbooks.
019 *
020 * <p>© Copyright 2007 <a href='http://sptci.com/' target='_new'>Sans Pareil Technologies, Inc.</a></p>
021 * @author Rakesh Vidyadharan 2007-10-09
022 * @version $Id: ExcelDownloadProvider.java 4123 2008-05-25 21:49:01Z rakesh $
023 */
024 public class ExcelDownloadProvider implements DownloadProvider
025 {
026 /** The SQL statement to be executed to fetch the data to be exported. */
027 private final String query;
028
029 /** The connection manager to use to fetch database connection. */
030 private final ConnectionManager manager;
031
032 /**
033 * Create a new instance of the download provider using the specified
034 * values.
035 *
036 * @param query The {@link #query} to execute.
037 * @param manager The {@link #manager} to use to fetch connection.
038 */
039 public ExcelDownloadProvider( final String query,
040 final ConnectionManager manager )
041 {
042 this.query = query;
043 this.manager = manager;
044 }
045
046 /**
047 * Return the <code>content-type</code> for the excel workbook.
048 *
049 * @return The content-type for the workbook.
050 */
051 public String getContentType()
052 {
053 return "application/vnd.ms-excel";
054 }
055
056 /**
057 * Return the <code>content-disposition</code> for the excel workbook.
058 *
059 * @return The content-disposition. Returns <code>attachment</code> to
060 * avoid some issues with full content not being pushed to client
061 * in <code>inline</code> mode.
062 */
063 public String getContentDisposition()
064 {
065 return "attachment";
066 }
067
068 /**
069 * Return the file name for the workbook.
070 *
071 * @return Returns <code>QueryResults.xls</code>.
072 */
073 public String getFileName()
074 {
075 return "QueryResults.xls";
076 }
077
078 /**
079 * Returns the size in bytes of the workbook.
080 *
081 * @return Returns <code>-1</code> to indicate that the size is unknown.
082 */
083 public int getSize()
084 {
085 return -1;
086 }
087
088 /**
089 * The <code>DownloadProvider</code> implementation method. Generates
090 * the excel workbook and streams to the client.
091 *
092 * @param out The {@link java.io.OutputStream} to which the contents of
093 * the Excel workbook is to be written.
094 * @throws IOException If errors are encountered while writing the
095 * contents.
096 */
097 public void writeFile( final OutputStream out ) throws IOException
098 {
099 final BatchQueryExecutor executor = new BatchQueryExecutor( manager );
100 HSSFWorkbook workbook = null;
101
102 try
103 {
104 workbook = executor.export( query );
105 }
106 catch ( Throwable t )
107 {
108 MainController.getController().processFatalException(
109 Configuration.getString( this, "error" ), t );
110 }
111
112 workbook.write( out );
113 out.flush();
114 }
115 }