KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > jasperreports > ebank > ReportBean


1 /*
2  * ============================================================================
3  * GNU Lesser General Public License
4  * ============================================================================
5  *
6  * JasperReports - Free Java report-generating library.
7  * Copyright (C) 2001-2005 Teodor Danciu teodord@users.sourceforge.net
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * Teodor Danciu
24  * 173, Calea Calarasilor, Bl. 42, Sc. 1, Ap. 18
25  * Postal code 030615, Sector 3
26  * Bucharest, ROMANIA
27  * Email: teodord@users.sourceforge.net
28  */

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 JavaDoc;
48
49 import java.text.SimpleDateFormat JavaDoc;
50 import java.util.Locale JavaDoc;
51 import java.util.Map JavaDoc;
52 import java.util.HashMap JavaDoc;
53 import java.util.Date JavaDoc;
54 import java.io.ByteArrayOutputStream JavaDoc;
55 import java.io.File JavaDoc;
56
57
58 public class ReportBean {
59     private ServletContext JavaDoc servletContext;
60     private Locale JavaDoc locale;
61     private AccountHistoryBean accountHistoryBean;
62     private CustomerBean customerBean;
63     private JasperPrint jasperPrint;
64     private Map JavaDoc 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 JavaDoc servletContext) {
78         this.servletContext = servletContext;
79     }
80
81     public void setLocale(Locale JavaDoc 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 JavaDoc 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 JavaDoc reportFile = null;
145             
146             if (displayOption == 2) {
147                 reportFile = new File JavaDoc(servletContext.getRealPath("/reports/BankStatementReport.jasper"));
148             } else {
149                 reportFile = new File JavaDoc(servletContext.getRealPath("/reports/AccountHistoryReport.jasper"));
150             }
151             
152             JasperReport jasperReport = (JasperReport)JRLoader.loadObject(reportFile.getPath());
153             
154             Map JavaDoc parameters = new HashMap JavaDoc();
155             parameters.put(JRParameter.REPORT_LOCALE, locale);
156             parameters.put("baseDir", new File JavaDoc(servletContext.getRealPath("/")));
157             parameters.put("accountHistoryBean", accountHistoryBean);
158             parameters.put("customerBean", customerBean);
159             parameters.put("isFlowDisplay", new Boolean JavaDoc(displayOption == 1));
160             
161             jasperPrint =
162                 JasperFillManager.fillReport(
163                     jasperReport,
164                     parameters,
165                     new JRBeanCollectionDataSource(accountHistoryBean.getTransactions())
166                     );
167         } catch (Exception JavaDoc e) {
168             e.printStackTrace();
169         }
170     }
171
172     public byte[] getPdf() {
173         try {
174             return JasperExportManager.exportReportToPdf(jasperPrint);
175         } catch (Exception JavaDoc e) {
176             e.printStackTrace();
177         }
178         
179         return null;
180     }
181
182     public byte[] getXls() {
183         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
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 JavaDoc e) {
202             e.printStackTrace();
203         }
204         
205         return null;
206     }
207
208     public String JavaDoc getHtml() {
209         if (jasperPrint == null) {
210             return "";
211         } else {
212             StringBuffer JavaDoc sbuffer = new StringBuffer JavaDoc();
213             
214             imagesMap = new HashMap JavaDoc();
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 JavaDoc(pageIndex));
234                 exporter.setParameter(JRHtmlExporterParameter.IS_REMOVE_EMPTY_SPACE_BETWEEN_ROWS, Boolean.FALSE);
235             }
236             
237             try {
238                 exporter.exportReport();
239             } catch (Exception JavaDoc e) {
240                 e.printStackTrace();
241             }
242             
243             return sbuffer.toString();
244         }
245     }
246 }
247
Popular Tags