1 23 24 package org.dbforms.servlets; 25 26 import net.sf.jasperreports.engine.JRException; 27 import net.sf.jasperreports.engine.JRExporter; 28 import net.sf.jasperreports.engine.JRExporterParameter; 29 import net.sf.jasperreports.engine.JasperCompileManager; 30 import net.sf.jasperreports.engine.JasperFillManager; 31 import net.sf.jasperreports.engine.JasperPrint; 32 import net.sf.jasperreports.engine.export.JRCsvExporter; 33 import net.sf.jasperreports.engine.export.JRPdfExporter; 34 import net.sf.jasperreports.engine.export.JRXlsExporter; 35 36 import org.apache.commons.logging.Log; 37 import org.apache.commons.logging.LogFactory; 38 39 import org.dbforms.config.DbFormsConfig; 40 import org.dbforms.config.DbFormsConfigRegistry; 41 42 import org.dbforms.event.WebEvent; 43 44 import org.dbforms.servlets.reports.JRDataSourceAbstract; 45 import org.dbforms.servlets.reports.ReportParameter; 46 import org.dbforms.servlets.reports.ReportServletAbstract; 47 import org.dbforms.servlets.reports.ReportWriter; 48 49 import org.dbforms.util.ParseUtil; 50 import org.dbforms.util.SqlUtil; 51 import org.dbforms.util.external.FileUtil; 52 53 import java.io.ByteArrayOutputStream ; 54 import java.io.File ; 55 56 import java.sql.Connection ; 57 58 import java.util.HashMap ; 59 import java.util.Map ; 60 61 import javax.servlet.ServletContext ; 62 import javax.servlet.ServletException ; 63 import javax.servlet.http.HttpServletRequest ; 64 import javax.servlet.http.HttpServletResponse ; 65 66 67 68 73 public class JasperReportServlet extends ReportServletAbstract { 74 75 private static Log logCat = LogFactory.getLog(JasperReportServlet.class 76 .getName()); 77 private static final String REPORTFILEEXTENSION = ".jrxml"; 78 79 80 private static final String REPORTNAMEPARAM = "reportname"; 81 82 83 private static final String REPORTTYPEPARAM = "reporttype"; 84 85 90 protected String getReportFileExtension() { 91 return REPORTFILEEXTENSION; 92 } 93 94 95 103 protected void compileReport(ServletContext context, 104 String reportFile) 105 throws Exception { 106 logCat.info("=== user dir " + FileUtil.dirname(reportFile)); 107 System.setProperty("user.dir", FileUtil.dirname(reportFile)); 108 109 File dir = FileUtil.getFile(FileUtil.dirname(reportFile)); 110 File [] list = dir.listFiles(); 111 112 for (int i = 0; i < list.length; i++) { 113 String s = FileUtil.removeExtension(list[i].getPath()); 114 String ext = "." + FileUtil.getExtension(list[i].getPath()); 115 116 if (s.startsWith(reportFile) && (ext.equals(getReportFileExtension()))) { 117 File xmlFile = list[i]; 118 File jasperFile = FileUtil.getFile(s + ".jasper"); 119 120 if (!jasperFile.exists() 121 || (xmlFile.lastModified() > jasperFile.lastModified())) { 122 compileJasper(context, xmlFile.getPath()); 123 } 124 } 125 } 126 } 127 128 129 143 protected ReportWriter processReport(String reportFileFullName, 144 JRDataSourceAbstract dataSource, 145 ServletContext context, 146 HttpServletRequest request, 147 HttpServletResponse response) { 148 156 ReportWriter res = new ReportWriter(); 157 158 try { 159 DbFormsConfig config = null; 161 162 try { 163 config = DbFormsConfigRegistry.instance() 164 .lookup(); 165 } catch (Exception e) { 166 logCat.error("processReport", e); 167 throw new ServletException (e); 168 } 169 170 try { 171 JasperPrint jPrint = null; 173 WebEvent webEvent = (WebEvent) request.getAttribute("webEvent"); 174 Connection con = null; 175 176 if ((webEvent != null) && (webEvent.getTable() == null)) { 177 con = config.getConnection(); 178 } else { 179 con = config.getConnection(getConnectionName(request)); 180 } 181 182 try { 183 ReportParameter repParam = new ReportParameter(context, request, 184 dataSource 185 .getAttributes(), 186 con, 187 FileUtil.dirname(reportFileFullName) 188 + File.separator); 189 Map map = new HashMap (); 190 map.put("PARAM", repParam); 191 jPrint = JasperFillManager.fillReport(reportFileFullName 192 + ".jasper", map, 193 dataSource); 194 } catch (Exception e) { 195 logCat.error("processReport", e); 196 } finally { 197 SqlUtil.closeConnection(con); 198 } 199 200 if ((jPrint == null) || (jPrint.getPages() 201 .size() == 0)) { 202 return null; 203 } else { 204 String outputFormat = ParseUtil.getParameter(request, 205 JasperReportServlet.REPORTTYPEPARAM, 206 "PDF"); 207 208 String fName = ""; 210 211 if ("PDF".equals(outputFormat)) { 212 res.mimeType = "application/pdf"; 213 res.data = exportToPDF(jPrint); 214 fName = FileUtil.filename(reportFileFullName) + ".pdf"; 215 } else if ("XLS".equals(outputFormat)) { 216 res.mimeType = "application/msexcel"; 217 res.data = exportToXLS(jPrint); 218 fName = FileUtil.filename(reportFileFullName) + ".xls"; 219 } else if ("CSV".equalsIgnoreCase(outputFormat)) { 220 res.mimeType = "text/comma-separated-values"; 221 res.data = exportToCSV(jPrint); 222 fName = FileUtil.filename(reportFileFullName) + ".csv"; 223 } 224 225 res.fileName = ParseUtil.getParameter(request, REPORTNAMEPARAM, 226 fName); 227 jPrint = null; 228 229 return res; 230 } 231 } catch (JRException e) { 232 logCat.error("processReport", e); 233 handleException(request, response, e); 234 235 return null; 236 } 237 } catch (Exception e) { 238 handleException(request, response, e); 239 240 return null; 241 } 242 } 243 244 245 252 private static String getConnectionName(HttpServletRequest request) { 253 WebEvent webEvent = (WebEvent) request.getAttribute("webEvent"); 254 String res = null; 255 256 if ((webEvent != null) && (webEvent.getTable() 257 .getId() != -1)) { 258 res = ParseUtil.getParameter(request, 259 "invname_" + webEvent.getTable().getId()); 260 } 261 262 return res; 263 } 264 265 266 private void compileJasper(ServletContext context, 267 String reportFile) 268 throws Exception { 269 logCat.info("=== start to compile " + reportFile); 270 271 String classpath = (String ) context.getAttribute("org.apache.catalina.jsp_classpath"); 273 logCat.info("=== used classpath " + classpath); 274 System.setProperty("jasper.reports.compile.class.path", classpath); 275 276 try { 277 JasperCompileManager.compileReportToFile(reportFile); 278 } catch (Exception e) { 279 logCat.error("compile", e); 280 throw e; 281 } 282 } 283 284 285 private ByteArrayOutputStream exportToCSV(JasperPrint jasperPrint) 286 throws JRException { 287 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 288 JRExporter exporter = new JRCsvExporter(); 289 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 290 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos); 291 exporter.exportReport(); 292 293 return baos; 294 } 295 296 297 private ByteArrayOutputStream exportToPDF(JasperPrint jasperPrint) 298 throws JRException { 299 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 300 JRExporter exporter = new JRPdfExporter(); 301 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 302 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos); 303 exporter.exportReport(); 304 305 return baos; 306 } 307 308 309 private ByteArrayOutputStream exportToXLS(JasperPrint jasperPrint) 310 throws JRException { 311 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 312 JRExporter exporter = new JRXlsExporter(); 313 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 314 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos); 315 exporter.exportReport(); 316 317 return baos; 318 } 319 } 320 | Popular Tags |