001: /*
002: * $Id: JasperReportsXmlViewHandler.java,v 1.2 2003/09/14 05:36:48 jonesde Exp $
003: *
004: * Copyright (c) 2001-2003 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.content.webapp.view;
026:
027: import java.io.IOException;
028: import java.io.InputStream;
029: import java.io.PipedInputStream;
030: import java.io.PipedOutputStream;
031: import java.util.Map;
032:
033: import javax.servlet.ServletContext;
034: import javax.servlet.http.HttpServletRequest;
035: import javax.servlet.http.HttpServletResponse;
036:
037: import org.ofbiz.base.util.Debug;
038: import org.ofbiz.base.util.UtilHttp;
039: import org.ofbiz.content.webapp.control.ContextFilter;
040: import org.ofbiz.entity.GenericDelegator;
041: import org.ofbiz.entity.jdbc.ConnectionFactory;
042:
043: import dori.jasper.engine.JREmptyDataSource;
044: import dori.jasper.engine.JasperCompileManager;
045: import dori.jasper.engine.JasperFillManager;
046: import dori.jasper.engine.JasperPrintManager;
047: import dori.jasper.engine.JasperReport;
048:
049: /**
050: * Handles JasperReports PDF view rendering
051: *
052: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
053: * @version $Revision: 1.2 $
054: * @since 2.0
055: */
056: public class JasperReportsXmlViewHandler implements ViewHandler {
057:
058: public static final String module = JasperReportsXmlViewHandler.class
059: .getName();
060:
061: protected ServletContext context;
062:
063: public void init(ServletContext context)
064: throws ViewHandlerException {
065: this .context = context;
066: }
067:
068: public void render(String name, String page, String info,
069: String contentType, String encoding,
070: HttpServletRequest request, HttpServletResponse response)
071: throws ViewHandlerException {
072: // some containers call filters on EVERY request, even forwarded ones,
073: // so let it know that it came from the control servlet
074:
075: if (request == null) {
076: throw new ViewHandlerException(
077: "The HttpServletRequest object was null, how did that happen?");
078: }
079: if (page == null || page.length() == 0) {
080: throw new ViewHandlerException(
081: "View page was null or empty, but must be specified");
082: }
083: if (info == null || info.length() == 0) {
084: Debug
085: .logWarning(
086: "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.",
087: module);
088: }
089:
090: // tell the ContextFilter we are forwarding
091: request.setAttribute(ContextFilter.FORWARDED_FROM_SERVLET,
092: new Boolean(true));
093:
094: GenericDelegator delegator = (GenericDelegator) request
095: .getAttribute("delegator");
096:
097: if (delegator == null) {
098: throw new ViewHandlerException(
099: "The delegator object was null, how did that happen?");
100: }
101:
102: try {
103: String datasourceName = delegator.getEntityHelperName(info);
104: InputStream is = context.getResourceAsStream(page);
105: Map parameters = UtilHttp.getParameterMap(request);
106:
107: JasperReport report = JasperCompileManager
108: .compileReport(is);
109:
110: response.setContentType("text/xml");
111:
112: PipedOutputStream fillToPrintOutputStream = new PipedOutputStream();
113: PipedInputStream fillToPrintInputStream = new PipedInputStream(
114: fillToPrintOutputStream);
115:
116: if (datasourceName != null && datasourceName.length() > 0) {
117: JasperFillManager
118: .fillReportToStream(report,
119: fillToPrintOutputStream, parameters,
120: ConnectionFactory
121: .getConnection(datasourceName));
122: } else {
123: JasperFillManager.fillReportToStream(report,
124: fillToPrintOutputStream, parameters,
125: new JREmptyDataSource());
126: }
127: JasperPrintManager.printReportToXmlStream(
128: fillToPrintInputStream, response.getOutputStream());
129: } catch (IOException ie) {
130: throw new ViewHandlerException("IO Error in region", ie);
131: } catch (java.sql.SQLException e) {
132: throw new ViewHandlerException(
133: "Database error while running report", e);
134: } catch (Exception e) {
135: throw new ViewHandlerException("Error in report", e);
136: // } catch (ServletException se) {
137: // throw new ViewHandlerException("Error in region", se.getRootCause());
138: }
139: }
140: }
|