KickJava   Java API By Example, From Geeks To Geeks.

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


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.xml.sax.InputSource JavaDoc;
30
31 import org.apache.struts.action.ActionMapping;
32
33 import org.jfree.report.*;
34 import org.jfree.report.modules.output.csv.CSVProcessor;
35 import org.jfree.report.modules.output.pageable.base.*;
36 import org.jfree.report.modules.output.pageable.pdf.*;
37 import org.jfree.report.modules.output.table.html.*;
38 import org.jfree.report.modules.output.table.xls.ExcelProcessor;
39 import org.jfree.report.modules.parser.base.ReportGenerator;
40 import org.jfree.report.util.*;
41 import org.jfree.xml.*;
42
43
44 import cowsultants.itracker.ejb.client.exceptions.*;
45 import cowsultants.itracker.ejb.client.models.*;
46 import cowsultants.itracker.ejb.client.resources.*;
47 import cowsultants.itracker.ejb.client.util.*;
48
49 /**
50   * This class encapsulates a basic ITracker report based on JFreeReport. It will load
51   * and process a JFreeReport xml file. This class can be extended to perform custom
52   * processing through the API if needed. Usually this can be done through overriding the
53   * augmentReport method.
54   */

55 public abstract class AbstractITrackerJFreeReport extends AbstractITrackerReport {
56     protected JFreeReport jreport = null;
57
58     public AbstractITrackerJFreeReport() {
59     }
60
61     /**
62       * Initializes the JFreeReport. This loads the appropriate xml file, sets the data
63       * for the report and setups up the report locale.
64       */

65     public void initializeReport(IssueModel[] issues, ReportModel report, Locale locale, String JavaDoc reportOutput, HttpSession JavaDoc session) throws ReportException{
66         super.initializeReport(issues, report, locale, reportOutput, session);
67
68         ReportTableModel data = null;
69
70         try {
71             if(report.getFileData().length == 0) {
72                 Logger.logDebug("Requested report did not contain a valid report definition.");
73                 throw new ReportException("Requested report did not contain a valid report definition.", "itracker.web.error.invalidreport");
74             }
75
76             ReportGenerator generator = ReportGenerator.getInstance();
77             jreport = generator.parseReport(new InputSource JavaDoc(new ByteArrayInputStream(report.getFileData())), new File(".").toURL());
78
79             if(report.getDataType() == ReportUtilities.DATATYPE_PROJECT) {
80                 data = new ProjectTableModel(issues, locale);
81             } else {
82                 data = new IssueTableModel(issues, locale);
83             }
84             jreport.setData(data);
85
86             // Set some configuration properties
87
ReportConfiguration config = jreport.getReportConfiguration();
88
89             config.setConfigProperty("com.jrefinery.report.targets.locale", locale.toString());
90
91             String JavaDoc reportTitle = report.getName();
92             if(report.getNameKey() != null) {
93                 reportTitle = ITrackerResources.getString(report.getNameKey(), locale);
94             }
95             config.setConfigProperty("com.jrefinery.report.targets.pageable.pdf.Title", reportTitle);
96             config.setConfigProperty("com.jrefinery.report.targets.table.html.Title", reportTitle);
97
98         } catch(Exception JavaDoc e) {
99             Logger.logDebug("Error initializing report.", e);
100             jreport = null;
101             throw new ReportException(e.getMessage());
102         }
103     }
104
105     /**
106       * This method must be overriden by subclasses to perform any customization that can only
107       * be performed through the API. This method should be called after initalizeReport.
108       */

109     public abstract void augmentReport();
110
111     /**
112       * Outputs the JFreeReport as a PDF.
113       * @param out the OutputStream to send the PDF to.
114       */

115     public void outputPDF(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, ActionMapping mapping) throws ReportException {
116         try {
117             if(jreport == null) {
118                 throw new Exception JavaDoc("The JFreeReport object has not been initialized.");
119             }
120
121             response.setHeader("Content-Type", "application/pdf");
122             ServletOutputStream JavaDoc out = response.getOutputStream();
123
124             final PDFOutputTarget target = new PDFOutputTarget(out, jreport.getDefaultPageFormat(), true);
125             final PageableReportProcessor processor = new PageableReportProcessor(jreport);
126             processor.setOutputTarget(target);
127             target.open();
128             processor.processReport();
129             target.close();
130
131             out.flush();
132             out.close();
133         } catch(Exception JavaDoc e) {
134             Logger.logDebug("Error outputing PDF report.", e);
135             throw new ReportException(e.getMessage());
136         }
137     }
138
139     /**
140       * Outputs the JFreeReport as HTML.
141       * @param out the OutputStream to send the HTML to.
142       */

143     public void outputHTML(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, ActionMapping mapping) throws ReportException {
144         try {
145             if(jreport == null) {
146                 throw new Exception JavaDoc("The JFreeReport object has not been initialized.");
147             }
148
149             response.setHeader("Content-Disposition", "inline; filename=\"itrackerreport.html\"");
150             response.setHeader("Content-Type", "text/html; charset=UTF-8");
151             ServletOutputStream JavaDoc out = response.getOutputStream();
152
153             final HtmlProcessor processor = new HtmlProcessor(jreport);
154             processor.setFilesystem(new StreamHtmlFilesystem(out));
155             processor.processReport();
156
157             out.flush();
158             out.close();
159         } catch(Exception JavaDoc e) {
160             Logger.logDebug("Error outputing PDF report.", e);
161             throw new ReportException(e.getMessage());
162         }
163     }
164
165     /**
166       * Outputs the JFreeReport as an Excel Spreadsheet (XLS).
167       * @param out the OutputStream to send the XLS to.
168       */

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

195     public void outputCSV(HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response, ActionMapping mapping) throws ReportException {
196         try {
197             if(jreport == null) {
198                 throw new Exception JavaDoc("The JFreeReport object has not been initialized.");
199             }
200
201             response.setHeader("Content-Disposition", "inline; filename=\"itrackerreport.csv\"");
202             response.setHeader("Content-Type", "text/csv; charset=UTF-8");
203             ServletOutputStream JavaDoc out = response.getOutputStream();
204
205             final CSVProcessor processor = new CSVProcessor(jreport);
206             processor.setWriter(new OutputStreamWriter(response.getOutputStream()));
207             processor.processReport();
208
209             out.flush();
210             out.close();
211         } catch(Exception JavaDoc e) {
212             Logger.logDebug("Error outputing CSV report.", e);
213             throw new ReportException(e.getMessage());
214         }
215     }
216
217     /**
218       * Returns the current JFreeReport object.
219       * @exception ReportException thrown if the report has not been properly initialized
220       */

221     public Object JavaDoc getReport() throws ReportException {
222         if(jreport == null) {
223             throw new ReportException("The JFreeReport object has not been initialized.");
224         }
225
226         return jreport;
227     }
228
229 }
230
Popular Tags