001: /*
002: * $Id: JasperReportsPdfViewHandler.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.util.Map;
030:
031: import javax.servlet.ServletContext;
032: import javax.servlet.http.HttpServletRequest;
033: import javax.servlet.http.HttpServletResponse;
034:
035: import org.ofbiz.base.util.Debug;
036: import org.ofbiz.base.util.UtilCache;
037: import org.ofbiz.base.util.UtilHttp;
038: import org.ofbiz.content.webapp.control.ContextFilter;
039: import org.ofbiz.entity.GenericDelegator;
040: import org.ofbiz.entity.jdbc.ConnectionFactory;
041:
042: import dori.jasper.engine.JRDataSource;
043: import dori.jasper.engine.JREmptyDataSource;
044: import dori.jasper.engine.JasperCompileManager;
045: import dori.jasper.engine.JasperManager;
046: import dori.jasper.engine.JasperPrint;
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 JasperReportsPdfViewHandler implements ViewHandler {
057:
058: public static final String module = JasperReportsPdfViewHandler.class
059: .getName();
060:
061: protected ServletContext context;
062: public static UtilCache jasperReportsCompiledCache = new UtilCache(
063: "webapp.JasperReportsCompiled");
064:
065: public void init(ServletContext context)
066: throws ViewHandlerException {
067: this .context = context;
068: }
069:
070: public void render(String name, String page, String info,
071: String contentType, String encoding,
072: HttpServletRequest request, HttpServletResponse response)
073: throws ViewHandlerException {
074: // some containers call filters on EVERY request, even forwarded ones,
075: // so let it know that it came from the control servlet
076:
077: if (request == null) {
078: throw new ViewHandlerException(
079: "The HttpServletRequest object was null, how did that happen?");
080: }
081: if (page == null || page.length() == 0) {
082: throw new ViewHandlerException(
083: "View page was null or empty, but must be specified");
084: }
085: if (info == null || info.length() == 0) {
086: Debug
087: .logInfo(
088: "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).",
089: module);
090: }
091:
092: // tell the ContextFilter we are forwarding
093: request.setAttribute(ContextFilter.FORWARDED_FROM_SERVLET,
094: new Boolean(true));
095: GenericDelegator delegator = (GenericDelegator) request
096: .getAttribute("delegator");
097: if (delegator == null) {
098: throw new ViewHandlerException(
099: "The delegator object was null, how did that happen?");
100: }
101:
102: try {
103: JasperReport report = (JasperReport) jasperReportsCompiledCache
104: .get(page);
105: if (report == null) {
106: synchronized (this ) {
107: report = (JasperReport) jasperReportsCompiledCache
108: .get(page);
109: if (report == null) {
110: InputStream is = context
111: .getResourceAsStream(page);
112: report = JasperCompileManager.compileReport(is);
113: jasperReportsCompiledCache.put(page, report);
114: }
115: }
116: }
117:
118: response.setContentType("application/pdf");
119:
120: Map parameters = (Map) request.getAttribute("jrParameters");
121: if (parameters == null) {
122: parameters = UtilHttp.getParameterMap(request);
123: }
124:
125: JRDataSource jrDataSource = (JRDataSource) request
126: .getAttribute("jrDataSource");
127: JasperPrint jp = null;
128: if (jrDataSource == null) {
129: String datasourceName = delegator
130: .getEntityHelperName(info);
131: if (datasourceName != null
132: && datasourceName.length() > 0) {
133: Debug.logInfo(
134: "Filling report with connection from datasource: "
135: + datasourceName, module);
136: jp = JasperManager.fillReport(report, parameters,
137: ConnectionFactory
138: .getConnection(datasourceName));
139: } else {
140: Debug
141: .logInfo(
142: "Filling report with an empty JR datasource",
143: module);
144: jp = JasperManager.fillReport(report, parameters,
145: new JREmptyDataSource());
146: }
147: } else {
148: Debug.logInfo(
149: "Filling report with a passed in jrDataSource",
150: module);
151: jp = JasperManager.fillReport(report, parameters,
152: jrDataSource);
153: }
154:
155: if (jp.getPages().size() < 1) {
156: throw new ViewHandlerException(
157: "Report is Empty (no results?)");
158: } else {
159: Debug.logInfo("Got report, there are "
160: + jp.getPages().size() + " pages.", module);
161: }
162: JasperManager.printReportToPdfStream(jp, response
163: .getOutputStream());
164: } catch (IOException ie) {
165: throw new ViewHandlerException("IO Error in report", ie);
166: } catch (java.sql.SQLException e) {
167: throw new ViewHandlerException(
168: "Database error while running report", e);
169: } catch (Exception e) {
170: throw new ViewHandlerException("Error in report", e);
171: // } catch (ServletException se) {
172: // throw new ViewHandlerException("Error in region", se.getRootCause());
173: }
174: }
175: }
|