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.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 57 public abstract class AbstractITrackerJasperReport extends AbstractITrackerReport { 58 protected JasperReport jasperReport = null; 59 protected JasperPrint jasperPrint = null; 60 61 public AbstractITrackerJasperReport() { 62 } 63 64 68 public void initializeReport(IssueModel[] issues, ReportModel report, Locale locale, String reportOutput, HttpSession 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 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 105 public abstract void augmentReport() throws ReportException; 106 107 111 public void outputPDF(HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) throws ReportException { 112 try { 113 if(jasperReport == null || jasperPrint == null) { 114 throw new Exception ("The JasperReport object has not been initialized."); 115 } 116 117 response.setHeader("Content-Type", "application/pdf"); 118 ServletOutputStream out = response.getOutputStream(); 119 120 JasperExportManager exportManager = new JasperExportManager(); 121 exportManager.exportReportToPdfStream(jasperPrint, out); 122 123 out.flush(); 124 out.close(); 125 } catch(Exception e) { 126 Logger.logDebug("Error outputing PDF report.", e); 127 throw new ReportException(e.getMessage()); 128 } 129 } 130 131 135 public void outputHTML(HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) throws ReportException { 136 try { 137 if(jasperReport == null || jasperPrint == null) { 138 throw new Exception ("The JasperReport object has not been initialized."); 139 } 140 141 JRHtmlExporter exporter = new JRHtmlExporter(); 142 143 Map imagesMap = new HashMap(); 144 HttpSession 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 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 e) { 162 Logger.logDebug("Error outputing HTML report.", e); 163 throw new ReportException(e.getMessage()); 164 } 165 } 166 167 171 public void outputXLS(HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) throws ReportException { 172 try { 173 if(jasperReport == null || jasperPrint == null) { 174 throw new Exception ("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 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 e) { 190 Logger.logDebug("Error outputing XLS report.", e); 191 throw new ReportException(e.getMessage()); 192 } 193 } 194 195 199 public void outputCSV(HttpServletRequest request, HttpServletResponse response, ActionMapping mapping) throws ReportException { 200 try { 201 if(jasperReport == null || jasperPrint == null) { 202 throw new Exception ("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 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 e) { 218 Logger.logDebug("Error outputing CSV report.", e); 219 throw new ReportException(e.getMessage()); 220 } 221 } 222 223 227 public Object 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 |