1 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 ; 25 import javax.servlet.http.HttpServletRequest ; 26 import javax.servlet.http.HttpServletResponse ; 27 import javax.servlet.http.HttpSession ; 28 29 import org.xml.sax.InputSource ; 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 55 public abstract class AbstractITrackerJFreeReport extends AbstractITrackerReport { 56 protected JFreeReport jreport = null; 57 58 public AbstractITrackerJFreeReport() { 59 } 60 61 65 public void initializeReport(IssueModel[] issues, ReportModel report, Locale locale, String reportOutput, HttpSession 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 (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 ReportConfiguration config = jreport.getReportConfiguration(); 88 89 config.setConfigProperty("com.jrefinery.report.targets.locale", locale.toString()); 90 91 String 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 e) { 99 Logger.logDebug("Error initializing report.", e); 100 jreport = null; 101 throw new ReportException(e.getMessage()); 102 } 103 } 104 105 109 public abstract void augmentReport(); 110 111 115 public void outputPDF(HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) throws ReportException { 116 try { 117 if(jreport == null) { 118 throw new Exception ("The JFreeReport object has not been initialized."); 119 } 120 121 response.setHeader("Content-Type", "application/pdf"); 122 ServletOutputStream 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 e) { 134 Logger.logDebug("Error outputing PDF report.", e); 135 throw new ReportException(e.getMessage()); 136 } 137 } 138 139 143 public void outputHTML(HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) throws ReportException { 144 try { 145 if(jreport == null) { 146 throw new Exception ("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 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 e) { 160 Logger.logDebug("Error outputing PDF report.", e); 161 throw new ReportException(e.getMessage()); 162 } 163 } 164 165 169 public void outputXLS(HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) throws ReportException { 170 try { 171 if(jreport == null) { 172 throw new Exception ("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 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 e) { 186 Logger.logDebug("Error outputing XLS report.", e); 187 throw new ReportException(e.getMessage()); 188 } 189 } 190 191 195 public void outputCSV(HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) throws ReportException { 196 try { 197 if(jreport == null) { 198 throw new Exception ("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 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 e) { 212 Logger.logDebug("Error outputing CSV report.", e); 213 throw new ReportException(e.getMessage()); 214 } 215 } 216 217 221 public Object 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 |