001: /*
002: * Copyright (C) 2006 Erik Swenson - erik@oreports.com
003: *
004: * This program is free software; you can redistribute it and/or modify it
005: * under the terms of the GNU General Public License as published by the Free
006: * Software Foundation; either version 2 of the License, or (at your option)
007: * any later version.
008: *
009: * This program is distributed in the hope that it will be useful, but WITHOUT
010: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
011: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
012: * more details.
013: *
014: * You should have received a copy of the GNU General Public License along with
015: * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
016: * Place - Suite 330, Boston, MA 02111-1307, USA.
017: *
018: */
019:
020: package org.efs.openreports.engine;
021:
022: import java.io.ByteArrayOutputStream;
023: import java.io.FileInputStream;
024: import java.sql.Connection;
025: import java.util.HashMap;
026: import java.util.List;
027: import java.util.Map;
028:
029: import net.sf.jxls.transformer.XLSTransformer;
030:
031: import org.apache.log4j.Logger;
032: import org.apache.poi.hssf.usermodel.HSSFWorkbook;
033: import org.efs.openreports.ORStatics;
034: import org.efs.openreports.engine.input.ReportEngineInput;
035: import org.efs.openreports.engine.output.QueryEngineOutput;
036: import org.efs.openreports.engine.output.ReportEngineOutput;
037: import org.efs.openreports.objects.Report;
038: import org.efs.openreports.providers.DataSourceProvider;
039: import org.efs.openreports.providers.DirectoryProvider;
040: import org.efs.openreports.providers.PropertiesProvider;
041: import org.efs.openreports.providers.ProviderException;
042:
043: /**
044: * Generates a ReportEngineOutput from a ReportEngineInput. The output consists
045: * of a byte[] containing an XLS spreadsheet.
046: *
047: * @author Erik Swenson
048: *
049: */
050:
051: public class JXLSReportEngine extends ReportEngine {
052: protected static Logger log = Logger
053: .getLogger(JXLSReportEngine.class);
054:
055: public JXLSReportEngine(DataSourceProvider dataSourceProvider,
056: DirectoryProvider directoryProvider,
057: PropertiesProvider propertiesProvider) {
058: super (dataSourceProvider, directoryProvider, propertiesProvider);
059: }
060:
061: public ReportEngineOutput generateReport(ReportEngineInput input)
062: throws ProviderException {
063: Connection conn = null;
064:
065: try {
066: Report report = input.getReport();
067:
068: // create new HashMap to send to JXLS in order to maintain original map of parameters
069: Map<String, Object> jxlsReportMap = new HashMap<String, Object>(
070: input.getParameters());
071:
072: if (report.getQuery() != null
073: && report.getQuery().trim().length() > 0) {
074: QueryReportEngine queryEngine = new QueryReportEngine(
075: dataSourceProvider, directoryProvider,
076: propertiesProvider);
077:
078: QueryEngineOutput output = (QueryEngineOutput) queryEngine
079: .generateReport(input);
080:
081: jxlsReportMap.put(ORStatics.JXLS_REPORT_RESULTS, output
082: .getResults());
083: } else {
084: conn = dataSourceProvider.getConnection(report
085: .getDataSource().getId());
086: JXLSReportManagerImpl rm = new JXLSReportManagerImpl(
087: conn, jxlsReportMap, dataSourceProvider);
088: jxlsReportMap.put("rm", rm);
089: }
090:
091: FileInputStream template = new FileInputStream(
092: directoryProvider.getReportDirectory()
093: + report.getFile());
094:
095: XLSTransformer transformer = new XLSTransformer();
096: HSSFWorkbook workbook = transformer.transformXLS(template,
097: jxlsReportMap);
098:
099: ByteArrayOutputStream out = new ByteArrayOutputStream();
100: workbook.write(out);
101:
102: ReportEngineOutput output = new ReportEngineOutput();
103: output.setContent(out.toByteArray());
104: output.setContentType(ReportEngineOutput.CONTENT_TYPE_XLS);
105:
106: return output;
107: } catch (Exception e) {
108: e.printStackTrace();
109: throw new ProviderException(e);
110: } finally {
111: try {
112: if (conn != null)
113: conn.close();
114: } catch (Exception c) {
115: log.error("Error closing");
116: }
117: }
118: }
119:
120: public List buildParameterList(Report report)
121: throws ProviderException {
122: throw new ProviderException(
123: "JXLSReportEngine: buildParameterList not implemented.");
124: }
125: }
|