KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > web > reports > AbstractITrackerJasperReport


1 /*
2  * This software was designed and created by Jason Carroll.
3  * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4  * The author can be reached at jcarroll@cowsultants.com
5  * ITracker website: http://www.cowsultants.com
6  * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it only under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  */

18
19 package cowsultants.itracker.web.reports;
20
21 import java.io.*;
22 import java.net.*;
23 import java.util.*;
24 import javax.servlet.ServletOutputStream JavaDoc;
25 import javax.servlet.http.HttpServletRequest JavaDoc;
26 import javax.servlet.http.HttpServletResponse JavaDoc;
27 import javax.servlet.http.HttpSession JavaDoc;
28
29 import org.apache.struts.action.ActionMapping;
30 import org.apache.struts.util.RequestUtils;
31
32 import dori.jasper.engine.JasperExportManager;
33 import dori.jasper.engine.JasperFillManager;
34 import dori.jasper.engine.JasperManager;
35 import dori.jasper.engine.JasperPrint;
36 import dori.jasper.engine.JasperReport;
37 import dori.jasper.engine.JRException;
38 import dori.jasper.engine.JRExporterParameter;
39 import dori.jasper.engine.data.JRTableModelDataSource;
40 import dori.jasper.engine.export.JRCsvExporter;
41 import dori.jasper.engine.export.JRHtmlExporter;
42 import dori.jasper.engine.export.JRHtmlExporterParameter;
43 import dori.jasper.engine.export.JRXlsExporter;
44
45 import cowsultants.itracker.ejb.client.exceptions.*;
46 import cowsultants.itracker.ejb.client.models.*;
47 import cowsultants.itracker.ejb.client.resources.*;
48 import cowsultants.itracker.ejb.client.util.*;
49 import cowsultants.itracker.web.util.Constants;
50
51 /**
52   * This class encapsulates a basic ITracker report based on JasperReports. It will load and
53   * process a JasperReport file. This class can be extended to perform custom processing
54   * through the API if needed. Usually this can be done through overriding the
55   * augmentReport method.
56   */

57 public abstract class AbstractITrackerJasperReport extends AbstractITrackerReport {
58     protected JasperReport jasperReport = null;
59     protected JasperPrint jasperPrint = null;
60
61     public AbstractITrackerJasperReport() {
62     }
63
64     /**
65       * Initializes the JasperReport. This loads the appropriate jasper file, sets the data
66       * for the report and sets up the report locale.
67       */

68     public void initializeReport(IssueModel[] issues, ReportModel report, Locale locale, String JavaDoc reportOutput, HttpSession JavaDoc session) throws ReportException {
69         super.initializeReport(issues, report, locale, reportOutput, session);
70
71         JRTableModelDataSource data = null;
72
73         try {
74             if(report.getFileData().length == 0) {
75                 Logger.logDebug("Requested report did not contain a valid report definition.");
76                 throw new ReportException("Requested report did not contain a valid report definition.", "itracker.web.error.invalidreport");
77             }
78             jasperReport = JasperManager.loadReport(new ByteArrayInputStream(report.getFileData()));
79
80             Map parameters = new HashMap();
81             String JavaDoc reportTitle = report.getName();
82             if(report.getNameKey() != null) {
83                 reportTitle = ITrackerResources.getString(report.getNameKey(), locale);
84             }
85             parameters.put("ReportTitle", reportTitle);
86             parameters.put("BaseDir", new File("."));
87
88             if(report.getDataType() == ReportUtilities.DATATYPE_PROJECT) {
89                 data = new JRTableModelDataSource(new ProjectTableModel(issues, locale));
90             } else {
91                 data = new JRTableModelDataSource(new IssueTableModel(issues, locale));
92             }
93             jasperPrint = JasperFillManager.fillReport(jasperReport, parameters, data);
94         } catch(JRException jre) {
95             Logger.logDebug("Error initializing report.", jre);
96             jasperPrint = null;
97             jasperReport = null;
98         }
99     }
100
101     /**
102       * This method must be overriden by subclasses to perform any customization that can only
103       * be performed through the API. This method should be called after initializeReport.
104       */

105     public abstract void augmentReport() throws ReportException;
106
107     /**
108       * Outputs the JFreeReport as a PDF.
109       * @param out the OutputStream to send the PDF to.
110       */

111     public void outputPDF(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, ActionMapping mapping) throws ReportException {
112         try {
113             if(jasperReport == null || jasperPrint == null) {
114                 throw new Exception JavaDoc("The JasperReport object has not been initialized.");
115             }
116
117             response.setHeader("Content-Type", "application/pdf");
118             ServletOutputStream JavaDoc out = response.getOutputStream();
119
120             JasperExportManager exportManager = new JasperExportManager();
121             exportManager.exportReportToPdfStream(jasperPrint, out);
122
123             out.flush();
124             out.close();
125         } catch(Exception JavaDoc e) {
126             Logger.logDebug("Error outputing PDF report.", e);
127             throw new ReportException(e.getMessage());
128         }
129     }
130
131     /**
132       * Outputs the JFreeReport as HTML.
133       * @param out the OutputStream to send the HTML to.
134       */

135     public void outputHTML(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, ActionMapping mapping) throws ReportException {
136         try {
137             if(jasperReport == null || jasperPrint == null) {
138                 throw new Exception JavaDoc("The JasperReport object has not been initialized.");
139             }
140
141             JRHtmlExporter exporter = new JRHtmlExporter();
142
143             Map imagesMap = new HashMap();
144             HttpSession JavaDoc session = request.getSession(true);
145             session.setAttribute(Constants.REPORT_IMAGEMAP_KEY, imagesMap);
146
147             response.setHeader("Content-Disposition", "inline; filename=\"itrackerreport.html\"");
148             response.setHeader("Content-Type", "text/html; charset=UTF-8");
149             ServletOutputStream JavaDoc out = response.getOutputStream();
150
151             exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
152             exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
153             exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, imagesMap);
154             Logger.logDebug("Image URL=" + RequestUtils.forwardURL(request, mapping.findForward("imagesurl")));
155             exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, RequestUtils.printableURL(RequestUtils.absoluteURL(request, RequestUtils.forwardURL(request, mapping.findForward("imagesurl")))) + "?image=");
156
157             exporter.exportReport();
158
159             out.flush();
160             out.close();
161         } catch(Exception JavaDoc e) {
162             Logger.logDebug("Error outputing HTML report.", e);
163             throw new ReportException(e.getMessage());
164         }
165     }
166
167     /**
168       * Outputs the JFreeReport as an Excel spreadsheet.
169       * @param out the OutputStream to send the XLS to.
170       */

171     public void outputXLS(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, ActionMapping mapping) throws ReportException {
172         try {
173             if(jasperReport == null || jasperPrint == null) {
174                 throw new Exception JavaDoc("The JasperReport object has not been initialized.");
175             }
176
177             JRXlsExporter exporter = new JRXlsExporter();
178
179             response.setHeader("Content-Disposition", "inline; filename=\"itrackerreport.xls\"");
180             response.setHeader("Content-Type", "application/vnd.ms-excel; charset=UTF-8");
181             ServletOutputStream JavaDoc out = response.getOutputStream();
182
183             exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
184             exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
185             exporter.exportReport();
186
187             out.flush();
188             out.close();
189         } catch(Exception JavaDoc e) {
190             Logger.logDebug("Error outputing XLS report.", e);
191             throw new ReportException(e.getMessage());
192         }
193     }
194
195     /**
196       * Outputs the JFreeReport as a csv file.
197       * @param out the OutputStream to send the XLS to.
198       */

199     public void outputCSV(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, ActionMapping mapping) throws ReportException {
200         try {
201             if(jasperReport == null || jasperPrint == null) {
202                 throw new Exception JavaDoc("The JasperReport object has not been initialized.");
203             }
204
205             JRCsvExporter exporter = new JRCsvExporter();
206
207             response.setHeader("Content-Disposition", "inline; filename=\"itrackerreport.csv\"");
208             response.setHeader("Content-Type", "text/csv; charset=UTF-8");
209             ServletOutputStream JavaDoc out = response.getOutputStream();
210
211             exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint);
212             exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
213             exporter.exportReport();
214
215             out.flush();
216             out.close();
217         } catch(Exception JavaDoc e) {
218             Logger.logDebug("Error outputing CSV report.", e);
219             throw new ReportException(e.getMessage());
220         }
221     }
222
223     /**
224       * Returns the current JasperReport object.
225       * @exception ReportException thrown if the report has not been properly initialized
226       */

227     public Object JavaDoc getReport() throws ReportException {
228         if(jasperReport == null) {
229             throw new ReportException("The JasperReport object has not been initialized.");
230         }
231
232         return jasperReport;
233     }
234 }
235
Popular Tags