KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > webapp > view > JasperReportsPdfViewHandler


1 /*
2  * $Id: JasperReportsPdfViewHandler.java 5966 2005-10-15 05:15:04Z jonesde $
3  *
4  * Copyright (c) 2001-2003 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.webapp.view;
26
27 import java.io.IOException JavaDoc;
28 import java.io.InputStream JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import javax.servlet.ServletContext JavaDoc;
32 import javax.servlet.http.HttpServletRequest JavaDoc;
33 import javax.servlet.http.HttpServletResponse JavaDoc;
34
35 import net.sf.jasperreports.engine.JRDataSource;
36 import net.sf.jasperreports.engine.JREmptyDataSource;
37 import net.sf.jasperreports.engine.JasperCompileManager;
38 import net.sf.jasperreports.engine.JasperExportManager;
39 import net.sf.jasperreports.engine.JasperFillManager;
40 import net.sf.jasperreports.engine.JasperPrint;
41 import net.sf.jasperreports.engine.JasperReport;
42
43 import org.ofbiz.base.util.Debug;
44 import org.ofbiz.base.util.UtilHttp;
45 import org.ofbiz.base.util.cache.UtilCache;
46 import org.ofbiz.webapp.control.ContextFilter;
47 import org.ofbiz.entity.GenericDelegator;
48 import org.ofbiz.entity.jdbc.ConnectionFactory;
49
50 /**
51  * Handles JasperReports PDF view rendering
52  *
53  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
54  * @version $Rev: 5966 $
55  * @since 2.0
56  */

57 public class JasperReportsPdfViewHandler implements ViewHandler {
58     
59     public static final String JavaDoc module = JasperReportsPdfViewHandler.class.getName();
60
61     protected ServletContext JavaDoc context;
62     public static UtilCache jasperReportsCompiledCache = new UtilCache("webapp.JasperReportsCompiled");
63
64     public void init(ServletContext JavaDoc context) throws ViewHandlerException {
65         this.context = context;
66     }
67
68     public void render(String JavaDoc name, String JavaDoc page, String JavaDoc info, String JavaDoc contentType, String JavaDoc encoding, HttpServletRequest JavaDoc request, HttpServletResponse JavaDoc response) throws ViewHandlerException {
69         // some containers call filters on EVERY request, even forwarded ones,
70
// so let it know that it came from the control servlet
71

72         if (request == null) {
73             throw new ViewHandlerException("The HttpServletRequest object was null, how did that happen?");
74         }
75         if (page == null || page.length() == 0) {
76             throw new ViewHandlerException("View page was null or empty, but must be specified");
77         }
78         if (info == null || info.length() == 0) {
79             Debug.logInfo("View info string was null or empty, (optionally used to specify an Entity that is mapped to the Entity Engine datasource that the report will use).", module);
80         }
81
82         // tell the ContextFilter we are forwarding
83
request.setAttribute(ContextFilter.FORWARDED_FROM_SERVLET, new Boolean JavaDoc(true));
84         GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
85         if (delegator == null) {
86             throw new ViewHandlerException("The delegator object was null, how did that happen?");
87         }
88
89         try {
90             JasperReport report = (JasperReport) jasperReportsCompiledCache.get(page);
91             if (report == null) {
92                 synchronized (this) {
93                     report = (JasperReport) jasperReportsCompiledCache.get(page);
94                     if (report == null) {
95                         InputStream JavaDoc is = context.getResourceAsStream(page);
96                         report = JasperCompileManager.compileReport(is);
97                         jasperReportsCompiledCache.put(page, report);
98                     }
99                 }
100             }
101             
102             response.setContentType("application/pdf");
103
104             Map JavaDoc parameters = (Map JavaDoc) request.getAttribute("jrParameters");
105             if (parameters == null) {
106                 parameters = UtilHttp.getParameterMap(request);
107             }
108
109             JRDataSource jrDataSource = (JRDataSource) request.getAttribute("jrDataSource");
110             JasperPrint jp = null;
111             if (jrDataSource == null) {
112                 String JavaDoc datasourceName = delegator.getEntityHelperName(info);
113                 if (datasourceName != null && datasourceName.length() > 0) {
114                     Debug.logInfo("Filling report with connection from datasource: " + datasourceName, module);
115                     jp = JasperFillManager.fillReport(report, parameters, ConnectionFactory.getConnection(datasourceName));
116                 } else {
117                     Debug.logInfo("Filling report with an empty JR datasource", module);
118                     jp = JasperFillManager.fillReport(report, parameters, new JREmptyDataSource());
119                 }
120             } else {
121                 Debug.logInfo("Filling report with a passed in jrDataSource", module);
122                 jp = JasperFillManager.fillReport(report, parameters, jrDataSource);
123             }
124
125             if (jp.getPages().size() < 1) {
126                 throw new ViewHandlerException("Report is Empty (no results?)");
127             } else {
128                 Debug.logInfo("Got report, there are " + jp.getPages().size() + " pages.", module);
129             }
130             JasperExportManager.exportReportToPdfStream(jp, response.getOutputStream());
131         } catch (IOException JavaDoc ie) {
132             throw new ViewHandlerException("IO Error in report", ie);
133         } catch (java.sql.SQLException JavaDoc e) {
134             throw new ViewHandlerException("Database error while running report", e);
135         } catch (Exception JavaDoc e) {
136             throw new ViewHandlerException("Error in report", e);
137             // } catch (ServletException se) {
138
// throw new ViewHandlerException("Error in region", se.getRootCause());
139
}
140     }
141 }
142
Popular Tags