001: /*
002: * $Id: DataVisionViewHandler.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.FileNotFoundException;
028: import java.io.IOException;
029:
030: import javax.servlet.ServletContext;
031: import javax.servlet.http.HttpServletRequest;
032: import javax.servlet.http.HttpServletResponse;
033:
034: import jimm.datavision.Report;
035: import jimm.datavision.UserCancellationException;
036:
037: import org.ofbiz.base.util.Debug;
038: import org.ofbiz.content.webapp.control.ContextFilter;
039: import org.ofbiz.entity.GenericDelegator;
040: import org.ofbiz.entity.jdbc.ConnectionFactory;
041:
042: /**
043: * Handles DataVision type view rendering
044: *
045: * @author <a href="mailto:jonesde@ofbiz.org">David E. Jones</a>
046: * @version $Revision: 1.2 $
047: * @since 2.0
048: */
049: public class DataVisionViewHandler implements ViewHandler {
050:
051: public static final String module = DataVisionViewHandler.class
052: .getName();
053:
054: protected ServletContext context;
055:
056: public void init(ServletContext context)
057: throws ViewHandlerException {
058: this .context = context;
059: }
060:
061: public void render(String name, String page, String info,
062: String contentType, String encoding,
063: HttpServletRequest request, HttpServletResponse response)
064: throws ViewHandlerException {
065: // some containers call filters on EVERY request, even forwarded ones,
066: // so let it know that it came from the control servlet
067:
068: if (request == null) {
069: throw new ViewHandlerException(
070: "The HttpServletRequest object was null, how did that happen?");
071: }
072: if (page == null || page.length() == 0) {
073: throw new ViewHandlerException(
074: "View page was null or empty, but must be specified");
075: }
076: if (info == null || info.length() == 0) {
077: throw new ViewHandlerException(
078: "View fnfo 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.");
079: }
080:
081: // tell the ContextFilter we are forwarding
082: request.setAttribute(ContextFilter.FORWARDED_FROM_SERVLET,
083: new Boolean(true));
084:
085: GenericDelegator delegator = (GenericDelegator) request
086: .getAttribute("delegator");
087:
088: if (delegator == null) {
089: throw new ViewHandlerException(
090: "The delegator object was null, how did that happen?");
091: }
092:
093: try {
094: String datasourceName = delegator.getEntityHelperName(info);
095:
096: Report report = new Report();
097: report.setDatabaseConnection(ConnectionFactory
098: .getConnection(datasourceName));
099:
100: /* NOTE: this is the old code that is no londer needed because of the new setDatabaseConnection method
101: report.setDatabasePassword(""); // password can be bogus because we are using an OFBiz connection...
102: Debug.logInfo("before creating database", module);
103: DataVisionDatabase dvDb = new DataVisionDatabase(datasourceName, report);
104:
105: report.setDatabase(dvDb);
106: */
107:
108: Debug.logInfo("before reading file", module);
109: report.readFile(context.getRealPath(page)); // Must be after password
110:
111: /* NO support for param file yet... need to pull in page params or something
112: if (there_are_params_in_report) {
113: // This must come after reading the report file
114: report.setParameterXMLFile(param_xml_file_name);
115: } */
116:
117: Debug.logInfo("before set layout engine", module);
118: report.setLayoutEngine(new jimm.datavision.layout.HTMLLE(
119: response.getWriter()));
120: Debug.logInfo("before run report", module);
121: report.runReport(); // Run the report in this thread
122: Debug.logInfo("after run report, end", module);
123: } catch (UserCancellationException e) {
124: throw new ViewHandlerException("User cancelled report", e);
125: } catch (FileNotFoundException e) {
126: throw new ViewHandlerException("Report file not found ["
127: + page + "]", e);
128: //} catch (ClassNotFoundException e) {
129: // throw new ViewHandlerException("Class not found in report", e);
130: } catch (IOException ie) {
131: throw new ViewHandlerException("IO Error in region", ie);
132: } catch (java.sql.SQLException e) {
133: throw new ViewHandlerException(
134: "Database error while running report", e);
135: } catch (Exception 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: }
|