KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: DataVisionViewHandler.java 5462 2005-08-05 18:35:48Z 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.FileNotFoundException JavaDoc;
28 import java.io.IOException JavaDoc;
29 import javax.servlet.ServletContext JavaDoc;
30 import javax.servlet.http.HttpServletRequest JavaDoc;
31 import javax.servlet.http.HttpServletResponse JavaDoc;
32
33 import jimm.datavision.Report;
34 import jimm.datavision.UserCancellationException;
35
36 import org.ofbiz.base.util.Debug;
37 import org.ofbiz.webapp.control.ContextFilter;
38 import org.ofbiz.entity.GenericDelegator;
39 import org.ofbiz.entity.jdbc.ConnectionFactory;
40
41 /**
42  * Handles DataVision type view rendering
43  *
44  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
45  * @version $Rev: 5462 $
46  * @since 2.0
47  */

48 public class DataVisionViewHandler implements ViewHandler {
49     
50     public static final String JavaDoc module = DataVisionViewHandler.class.getName();
51
52     protected ServletContext JavaDoc context;
53
54     public void init(ServletContext JavaDoc context) throws ViewHandlerException {
55         this.context = context;
56     }
57
58     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 {
59         // some containers call filters on EVERY request, even forwarded ones,
60
// so let it know that it came from the control servlet
61

62         if (request == null) {
63             throw new ViewHandlerException("The HttpServletRequest object was null, how did that happen?");
64         }
65         if (page == null || page.length() == 0) {
66             throw new ViewHandlerException("View page was null or empty, but must be specified");
67         }
68         if (info == null || info.length() == 0) {
69             throw new ViewHandlerException("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.");
70         }
71
72         // tell the ContextFilter we are forwarding
73
request.setAttribute(ContextFilter.FORWARDED_FROM_SERVLET, new Boolean JavaDoc(true));
74
75         GenericDelegator delegator = (GenericDelegator) request.getAttribute("delegator");
76
77         if (delegator == null) {
78             throw new ViewHandlerException("The delegator object was null, how did that happen?");
79         }
80
81         try {
82             String JavaDoc datasourceName = delegator.getEntityHelperName(info);
83
84             Report report = new Report();
85             report.setDatabaseConnection(ConnectionFactory.getConnection(datasourceName));
86
87             /* NOTE: this is the old code that is no londer needed because of the new setDatabaseConnection method
88             report.setDatabasePassword(""); // password can be bogus because we are using an OFBiz connection...
89             Debug.logInfo("before creating database", module);
90             DataVisionDatabase dvDb = new DataVisionDatabase(datasourceName, report);
91
92             report.setDatabase(dvDb);
93             */

94
95             Debug.logInfo("before reading file", module);
96             report.readFile(context.getRealPath(page)); // Must be after password
97

98             /* NO support for param file yet... need to pull in page params or something
99              if (there_are_params_in_report) {
100              // This must come after reading the report file
101              report.setParameterXMLFile(param_xml_file_name);
102              } */

103
104             Debug.logInfo("before set layout engine", module);
105             report.setLayoutEngine(new jimm.datavision.layout.HTMLLE(response.getWriter()));
106             Debug.logInfo("before run report", module);
107             report.runReport(); // Run the report in this thread
108
Debug.logInfo("after run report, end", module);
109         } catch (UserCancellationException e) {
110             throw new ViewHandlerException("User cancelled report", e);
111         } catch (FileNotFoundException JavaDoc e) {
112             throw new ViewHandlerException("Report file not found [" + page + "]", e);
113         //} catch (ClassNotFoundException e) {
114
// throw new ViewHandlerException("Class not found in report", e);
115
} catch (IOException JavaDoc ie) {
116             throw new ViewHandlerException("IO Error in region", ie);
117         } catch (java.sql.SQLException JavaDoc e) {
118             throw new ViewHandlerException("Database error while running report", e);
119         } catch (Exception JavaDoc e) {
120             throw new ViewHandlerException("Error in report", e);
121             // } catch (ServletException se) {
122
// throw new ViewHandlerException("Error in region", se.getRootCause());
123
}
124     }
125 }
126
Popular Tags