1 29 package net.sf.jasperreports.ebank; 30 31 import com.sun.ebank.web.AccountHistoryBean; 32 import com.sun.ebank.web.CustomerBean; 33 34 import net.sf.jasperreports.engine.JasperPrint; 35 import net.sf.jasperreports.engine.JasperReport; 36 import net.sf.jasperreports.engine.JasperExportManager; 37 import net.sf.jasperreports.engine.JRParameter; 38 import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource; 39 import net.sf.jasperreports.engine.util.JRLoader; 40 import net.sf.jasperreports.engine.JasperFillManager; 41 import net.sf.jasperreports.engine.export.JRHtmlExporter; 42 import net.sf.jasperreports.engine.export.JRHtmlExporterParameter; 43 import net.sf.jasperreports.engine.JRExporterParameter; 44 import net.sf.jasperreports.engine.export.JRXlsExporter; 45 import net.sf.jasperreports.engine.export.JRXlsExporterParameter; 46 47 import javax.servlet.ServletContext ; 48 49 import java.text.SimpleDateFormat ; 50 import java.util.Locale ; 51 import java.util.Map ; 52 import java.util.HashMap ; 53 import java.util.Date ; 54 import java.io.ByteArrayOutputStream ; 55 import java.io.File ; 56 57 58 public class ReportBean { 59 private ServletContext servletContext; 60 private Locale locale; 61 private AccountHistoryBean accountHistoryBean; 62 private CustomerBean customerBean; 63 private JasperPrint jasperPrint; 64 private Map imagesMap; 65 private int pageIndex; 66 private int displayOption; 67 68 public ReportBean() { 69 accountHistoryBean = null; 70 customerBean = null; 71 jasperPrint = null; 72 imagesMap = null; 73 pageIndex = 0; 74 displayOption = 0; 75 } 76 77 public void setServletContext(ServletContext servletContext) { 78 this.servletContext = servletContext; 79 } 80 81 public void setLocale(Locale locale) { 82 this.locale = locale; 83 } 84 85 public void setAccountHistoryBean(AccountHistoryBean accountHistoryBean) { 86 this.accountHistoryBean = accountHistoryBean; 87 } 88 89 public void setCustomerBean(CustomerBean customerBean) { 90 this.customerBean = customerBean; 91 } 92 93 public void setDisplayOption(int displayOption) { 94 this.displayOption = displayOption; 95 } 96 97 public int getDisplayOption() { 98 return displayOption; 99 } 100 101 public Map getImagesMap() { 102 return imagesMap; 103 } 104 105 public int getPageIndex() { 106 return pageIndex; 107 } 108 109 public int getLastPage() { 110 if (jasperPrint != null && jasperPrint.getPages() != null) { 111 return jasperPrint.getPages().size() - 1; 112 } else { 113 return -1; 114 } 115 } 116 117 public void setFirstPage() { 118 pageIndex = 0; 119 } 120 121 public void setPreviousPage() { 122 if (pageIndex > 0) { 123 pageIndex--; 124 } 125 } 126 127 public void setNextPage() { 128 if (pageIndex < getLastPage()) { 129 pageIndex++; 130 } 131 } 132 133 public void setLastPage() { 134 pageIndex = getLastPage(); 135 } 136 137 public void fillReport() { 138 139 pageIndex = 0; 140 141 accountHistoryBean.doTx(); 142 143 try { 144 File reportFile = null; 145 146 if (displayOption == 2) { 147 reportFile = new File (servletContext.getRealPath("/reports/BankStatementReport.jasper")); 148 } else { 149 reportFile = new File (servletContext.getRealPath("/reports/AccountHistoryReport.jasper")); 150 } 151 152 JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath()); 153 154 Map parameters = new HashMap (); 155 parameters.put(JRParameter.REPORT_LOCALE, locale); 156 parameters.put("baseDir", new File (servletContext.getRealPath("/"))); 157 parameters.put("accountHistoryBean", accountHistoryBean); 158 parameters.put("customerBean", customerBean); 159 parameters.put("isFlowDisplay", new Boolean (displayOption == 1)); 160 161 jasperPrint = 162 JasperFillManager.fillReport( 163 jasperReport, 164 parameters, 165 new JRBeanCollectionDataSource(accountHistoryBean.getTransactions()) 166 ); 167 } catch (Exception e) { 168 e.printStackTrace(); 169 } 170 } 171 172 public byte[] getPdf() { 173 try { 174 return JasperExportManager.exportReportToPdf(jasperPrint); 175 } catch (Exception e) { 176 e.printStackTrace(); 177 } 178 179 return null; 180 } 181 182 public byte[] getXls() { 183 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 184 185 JRXlsExporter exporter = new JRXlsExporter(); 186 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 187 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, baos); 188 exporter.setParameter(JRXlsExporterParameter.IS_ONE_PAGE_PER_SHEET, Boolean.FALSE); 189 exporter.setParameter(JRXlsExporterParameter.IS_WHITE_PAGE_BACKGROUND, Boolean.FALSE); 190 191 if (displayOption == 1) { 192 exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE); 193 } else { 194 exporter.setParameter(JRXlsExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.FALSE); 195 } 196 197 try { 198 exporter.exportReport(); 199 200 return baos.toByteArray(); 201 } catch (Exception e) { 202 e.printStackTrace(); 203 } 204 205 return null; 206 } 207 208 public String getHtml() { 209 if (jasperPrint == null) { 210 return ""; 211 } else { 212 StringBuffer sbuffer = new StringBuffer (); 213 214 imagesMap = new HashMap (); 215 216 JRHtmlExporter exporter = new JRHtmlExporter(); 217 218 exporter.setParameter(JRExporterParameter.JASPER_PRINT, jasperPrint); 219 exporter.setParameter(JRExporterParameter.OUTPUT_STRING_BUFFER, sbuffer); 220 exporter.setParameter(JRHtmlExporterParameter.IS_USING_IMAGES_TO_ALIGN, Boolean.FALSE); 221 exporter.setParameter(JRHtmlExporterParameter.IMAGES_MAP, imagesMap); 222 exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI, "image?image="); 223 224 if (displayOption == 1) { 225 exporter.setParameter(JRHtmlExporterParameter.HTML_HEADER, ""); 226 exporter.setParameter(JRHtmlExporterParameter.BETWEEN_PAGES_HTML, ""); 227 exporter.setParameter(JRHtmlExporterParameter.HTML_FOOTER, ""); 228 exporter.setParameter(JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.TRUE); 229 } else { 230 exporter.setParameter(JRHtmlExporterParameter.HTML_HEADER, "<table width=\"100%\" cellpadding=\"1\" cellspacing=\"0\" border=\"0\" bgcolor=\"#000000\"><tr><td>"); 231 exporter.setParameter(JRHtmlExporterParameter.BETWEEN_PAGES_HTML, ""); 232 exporter.setParameter(JRHtmlExporterParameter.HTML_FOOTER, "</td></tr></table>"); 233 exporter.setParameter(JRExporterParameter.PAGE_INDEX, new Integer (pageIndex)); 234 exporter.setParameter(JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.FALSE); 235 } 236 237 try { 238 exporter.exportReport(); 239 } catch (Exception e) { 240 e.printStackTrace(); 241 } 242 243 return sbuffer.toString(); 244 } 245 } 246 } 247 | Popular Tags |