1 package net.sf.jasperreports.mysql.web; 2 3 import java.io.ByteArrayOutputStream ; 4 import java.io.File ; 5 import java.sql.Connection ; 6 import java.sql.Timestamp ; 7 import java.text.ParseException ; 8 import java.text.SimpleDateFormat ; 9 import java.util.HashMap ; 10 import java.util.Locale ; 11 import java.util.Map ; 12 13 import javax.servlet.ServletContext ; 14 15 import net.sf.jasperreports.engine.JRExporterParameter; 16 import net.sf.jasperreports.engine.JRParameter; 17 import net.sf.jasperreports.engine.JasperExportManager; 18 import net.sf.jasperreports.engine.JasperFillManager; 19 import net.sf.jasperreports.engine.JasperPrint; 20 import net.sf.jasperreports.engine.JasperReport; 21 import net.sf.jasperreports.engine.export.JRHtmlExporter; 22 import net.sf.jasperreports.engine.export.JRHtmlExporterParameter; 23 import net.sf.jasperreports.engine.export.JRXlsExporter; 24 import net.sf.jasperreports.engine.export.JRXlsExporterParameter; 25 import net.sf.jasperreports.engine.util.JRLoader; 26 27 28 public class ReportBean 29 { 30 31 private static final SimpleDateFormat dateFormat = new SimpleDateFormat ("dd-M-yyyy HH:mm:ss"); 32 33 protected ServletContext servletContext; 34 protected Locale locale; 35 protected JasperPrint jasperPrint; 36 protected Map imagesMap; 37 protected int pageIndex; 38 protected String report; 39 protected String error; 40 protected String database; 41 protected String fromTxt; 42 protected String toTxt; 43 44 public ReportBean() { 45 jasperPrint = null; 46 imagesMap = null; 47 pageIndex = 0; 48 } 49 50 public void setServletContext(ServletContext servletContext) { 51 this.servletContext = servletContext; 52 } 53 54 public void setLocale(Locale locale) { 55 this.locale = locale; 56 } 57 58 public void setReport(String report) { 59 this.report = report; 60 } 61 62 public Map getImagesMap() { 63 return imagesMap; 64 } 65 66 public int getPageIndex() { 67 return pageIndex; 68 } 69 70 public int getLastPage() { 71 if (jasperPrint != null && jasperPrint.getPages() != null) { 72 return jasperPrint.getPages().size() - 1; 73 } else { 74 return -1; 75 } 76 } 77 78 public void setFirstPage() { 79 pageIndex = 0; 80 } 81 82 public void setPreviousPage() { 83 if (pageIndex > 0) { 84 pageIndex--; 85 } 86 } 87 88 public void setNextPage() { 89 if (pageIndex < getLastPage()) { 90 pageIndex++; 91 } 92 } 93 94 public void setLastPage() { 95 pageIndex = getLastPage(); 96 } 97 98 public String getError() { 99 return error; 100 } 101 102 public void setDatabase(String database) { 103 this.database = database; 104 } 105 106 public void setFrom(String from) { 107 this.fromTxt = from; 108 } 109 110 public void setTo(String to) { 111 this.toTxt = to; 112 } 113 114 public void fillReport(Connection connection) { 115 116 error = null; 117 118 Timestamp from = null; 119 Timestamp to = null; 120 121 if (fromTxt != null && fromTxt.trim().length() > 0) 122 { 123 try 124 { 125 from = new Timestamp (dateFormat.parse(fromTxt).getTime()); 126 } 127 catch (ParseException e) 128 { 129 error = e.getMessage(); 130 } 131 } 132 else 133 { 134 error = "'From' field is mandatory."; 135 } 136 137 if (toTxt != null && toTxt.trim().length() > 0) 138 { 139 try 140 { 141 to = new Timestamp (dateFormat.parse(toTxt).getTime()); 142 } 143 catch (ParseException e) 144 { 145 error = e.getMessage(); 146 } 147 } 148 else 149 { 150 error = "'To' field is mandatory."; 151 } 152 153 if (error != null) 154 { 155 return; 156 } 157 158 pageIndex = 0; 159 160 try { 161 File reportFile = new File (servletContext.getRealPath("/reports/" + report + ".jasper")); 162 163 JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath()); 164 165 Map parameters = new HashMap (); 166 parameters.put(JRParameter.REPORT_LOCALE, locale); 167 parameters.put("baseDir", new File (servletContext.getRealPath("/"))); 168 if (database != null) 169 { 170 parameters.put("DatabaseName", database); 171 } 172 if (from != null) 173 { 174 parameters.put("MinDate", from); 175 } 176 if (to != null) 177 { 178 parameters.put("MaxDate", to); 179 } 180 181 jasperPrint = 182 JasperFillManager.fillReport( 183 jasperReport, 184 parameters, 185 connection 186 ); 187 } catch (Exception e) { 188 e.printStackTrace(); 189 } 190 } 191 192 public byte[] getPdf() { 193 try { 194 return JasperExportManager.exportReportToPdf(jasperPrint); 195 } catch (Exception e) { 196 e.printStackTrace(); 197 } 198 199 return null; 200 } 201 202 public byte[] getXls() { 203 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 204 205 JRXlsExporter exporter = new JRXlsExporter(); 206 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 207 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos); 208 exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE); 209 exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE); 210 211 try { 212 exporter.exportReport(); 213 214 return baos.toByteArray(); 215 } catch (Exception e) { 216 e.printStackTrace(); 217 } 218 219 return null; 220 } 221 222 public String getHtml() { 223 if (jasperPrint == null) { 224 return ""; 225 } else { 226 StringBuffer sbuffer = new StringBuffer (); 227 228 imagesMap = new HashMap (); 229 230 JRHtmlExporter exporter = new JRHtmlExporter(); 231 232 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 233 exporter.setParameter(JRExporterParameter.OUTPUT_STRING_BUFFER, sbuffer); 234 exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE); 235 exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, imagesMap); 236 exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "image?image="); 237 238 exporter.setParameter(JRHtmlExporterParameter.HTML_HEADER, "<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\"><tr><td>"); 239 exporter.setParameter(JRHtmlExporterParameter.BETWEEN_PAGES_HTML, ""); 240 exporter.setParameter(JRHtmlExporterParameter.HTML_FOOTER, "</td></tr></table>"); 241 exporter.setParameter(JRExporterParameter.PAGE_INDEX, new Integer (pageIndex)); 242 exporter.setParameter(JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.FALSE); 243 244 try { 245 exporter.exportReport(); 246 } catch (Exception e) { 247 e.printStackTrace(); 248 } 249 250 return sbuffer.toString(); 251 } 252 } 253 } 254 | Popular Tags |