KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: JasperReportsXmlViewHandler.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.io.PipedInputStream JavaDoc;
30 import java.io.PipedOutputStream JavaDoc;
31 import java.util.Map JavaDoc;
32
33 import javax.servlet.ServletContext JavaDoc;
34 import javax.servlet.http.HttpServletRequest JavaDoc;
35 import javax.servlet.http.HttpServletResponse JavaDoc;
36
37 import net.sf.jasperreports.engine.JREmptyDataSource;
38 import net.sf.jasperreports.engine.JasperCompileManager;
39 import net.sf.jasperreports.engine.JasperExportManager;
40 import net.sf.jasperreports.engine.JasperFillManager;
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.webapp.control.ContextFilter;
46 import org.ofbiz.entity.GenericDelegator;
47 import org.ofbiz.entity.jdbc.ConnectionFactory;
48
49 /**
50  * Handles JasperReports PDF view rendering
51  *
52  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
53  * @version $Rev: 5966 $
54  * @since 2.0
55  */

56 public class JasperReportsXmlViewHandler implements ViewHandler {
57     
58     public static final String JavaDoc module = JasperReportsXmlViewHandler.class.getName();
59
60     protected ServletContext JavaDoc context;
61
62     public void init(ServletContext JavaDoc context) throws ViewHandlerException {
63         this.context = context;
64     }
65
66     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 {
67         // some containers call filters on EVERY request, even forwarded ones,
68
// so let it know that it came from the control servlet
69

70         if (request == null) {
71             throw new ViewHandlerException("The HttpServletRequest object was null, how did that happen?");
72         }
73         if (page == null || page.length() == 0) {
74             throw new ViewHandlerException("View page was null or empty, but must be specified");
75         }
76         if (info == null || info.length() == 0) {
77             Debug.logWarning("View info string was null or empty, but must be used to specify an Entity that is mapped to the Entity Engine datasource that the report will use.", module);
78         }
79
80         // tell the ContextFilter we are forwarding
81
request.setAttribute(ContextFilter.FORWARDED_FROM_SERVLET, new Boolean JavaDoc(true));
82
83         GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
84
85         if (delegator == null) {
86             throw new ViewHandlerException("The delegator object was null, how did that happen?");
87         }
88
89         try {
90             String JavaDoc datasourceName = delegator.getEntityHelperName(info);
91             InputStream JavaDoc is = context.getResourceAsStream(page);
92             Map JavaDoc parameters = UtilHttp.getParameterMap(request);
93
94             JasperReport report = JasperCompileManager.compileReport(is);
95
96             response.setContentType("text/xml");
97
98             PipedOutputStream JavaDoc fillToPrintOutputStream = new PipedOutputStream JavaDoc();
99             PipedInputStream JavaDoc fillToPrintInputStream = new PipedInputStream JavaDoc(fillToPrintOutputStream);
100
101             if (datasourceName != null && datasourceName.length() > 0) {
102                 JasperFillManager.fillReportToStream(report, fillToPrintOutputStream, parameters, ConnectionFactory.getConnection(datasourceName));
103             } else {
104                 JasperFillManager.fillReportToStream(report, fillToPrintOutputStream, parameters, new JREmptyDataSource());
105             }
106             JasperExportManager.exportReportToXmlStream(fillToPrintInputStream, response.getOutputStream());
107         } catch (IOException JavaDoc ie) {
108             throw new ViewHandlerException("IO Error in region", ie);
109         } catch (java.sql.SQLException JavaDoc e) {
110             throw new ViewHandlerException("Database error while running report", e);
111         } catch (Exception JavaDoc e) {
112             throw new ViewHandlerException("Error in report", e);
113             // } catch (ServletException se) {
114
// throw new ViewHandlerException("Error in region", se.getRootCause());
115
}
116     }
117 }
118
Popular Tags