Source code: jbreport/interfaces/servlet/BasicReportServlet.java
1 /*
2 * $Id: BasicReportServlet.java,v 1.1.1.1 2000/08/31 13:14:36 grantfin Exp $
3 *
4 * jbReport - A reporting library for Java
5 * Copyright (C) 2000 Grant Finnemore <grantfin@users.sourceforge.net>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21 package jbreport.interfaces.servlet;
22
23 import java.io.InputStream;
24 import java.io.IOException;
25 import java.util.ArrayList;
26 import java.util.Enumeration;
27 import java.util.HashMap;
28 import java.util.Iterator;
29 import java.util.Map;
30 import java.util.Properties;
31 import javax.servlet.ServletException;
32 import javax.servlet.http.HttpServlet;
33 import javax.servlet.http.HttpServletRequest;
34 import javax.servlet.http.HttpServletResponse;
35
36 import jbreport.Constants;
37 import jbreport.ReportException;
38 import jbreport.ReportFacade;
39 import jbreport.ReportSection;
40 import jbreport.Repository;
41
42 /**
43 * This is the controller instance for basic reporting functions. It will
44 * redirect as appropriate to named jsp's for further information capture.
45 *
46 * <p> At the moment, we don't cater for application connection instances,
47 * but will do so in the future.
48 *
49 * <p> All the initialization parameters for this servlet will be read in from
50 * the properties file in this package.
51 *
52 * @author Grant Finnemore
53 * @version $Revision: 1.1.1.1 $
54 */
55 public
56 class BasicReportServlet extends HttpServlet {
57
58 /** The name of the local file used to load the properties */
59 private static final String PROP_FILE = "basic_report.properties";
60
61 /** The location of the servlet - used by the JSP pages */
62 private static String servletURL;
63
64 /** The properties instance that configures this servlet */
65 private Properties properties = new Properties();
66
67 //
68 // Constructors
69 //
70
71 //
72 // Methods overloaded from HttpServlet
73 //
74
75 /**
76 * This method is used to initialize the reporting package prior to first
77 * use.
78 */
79 public void init() throws ServletException {
80 // Initialize the reporting package here
81 try {
82 // Grab the properties
83 InputStream is = getClass().getResourceAsStream(PROP_FILE);
84 properties.load(is);
85 is.close();
86 log("Loaded properties file for servlet");
87
88 // Load the url of the servlet
89 servletURL = properties.getProperty("servlet.url");
90 log("The servlet relative URL is " + servletURL);
91
92 // Which class will be used to load the xml files
93 String prop = properties.getProperty("repository.init.class");
94 Class cls = Class.forName(prop);
95 log("Using " + cls.getName() + " for xml file loading");
96
97 // Load the appropriate xml files
98 Map m = propertiesForKeyPrefix("repository.init.xml.");
99 for(Iterator it = m.values().iterator(); it.hasNext();) {
100 String fileName = (String)it.next();
101 is = cls.getResourceAsStream(fileName);
102 ReportFacade.initialize(is);
103 is.close();
104 log("Initialized report facade with " + fileName);
105 }
106 log("Finished initialization");
107 }
108 catch(Exception e) {
109 e.printStackTrace();
110 throw new ServletException(e);
111 }
112 }
113
114 /**
115 * The controller is passed data through the get mechanism for navigation.
116 *
117 * <p> We don't use the redirect mechanism here, as that assumes too much
118 * knowledge about the running context of the servlet.
119 */
120 public void doGet(HttpServletRequest request, HttpServletResponse response)
121 throws ServletException, IOException {
122
123 try {
124 String reportName = request.getParameter("report");
125 // Show the list of all reports
126 if(reportName == null) {
127 Iterator it = documentNames();
128 if(it.hasNext()) {
129 request.setAttribute("documentNames", it);
130 String jspPageName = properties.getProperty("jsp.index");
131 request.getRequestDispatcher(jspPageName)
132 .forward(request, response);
133 }
134 else {
135 String jspPageName = properties.getProperty("jsp.err.nodocs");
136 request.getRequestDispatcher(jspPageName)
137 .forward(request, response);
138 }
139 }
140 else {
141 // Create the initial document
142 ReportSection doc = ReportFacade.fetchDocument(reportName);
143 request.getSession().setAttribute("document", doc);
144 Iterator it = doc.boundParameters();
145 if(it.hasNext()) {
146 // Show the page that fills in the bound parameters
147 request.setAttribute("boundParameters", it);
148 String jspPageName = properties.getProperty("jsp.param_rep");
149 request.getRequestDispatcher(jspPageName)
150 .forward(request, response);
151 }
152 else {
153 // Render the report
154 doc.loadData();
155 doc.addRenderer(Constants.BASIC_HTML, response.getWriter());
156 doc.render();
157 }
158 }
159 }
160 catch(ReportException e) {
161 throw new ServletException(e);
162 }
163 }
164
165 /**
166 * The controller is passed data through the post mechanism for data capture
167 */
168 public void doPost(HttpServletRequest request, HttpServletResponse response)
169 throws ServletException, IOException {
170 // Get the document
171 ReportSection doc =
172 (ReportSection)request.getSession().getAttribute("document");
173 if(doc == null) {
174 // Try to find name of report as a parameter - this will only work
175 // if the JSP has the bound parameter names hardcoded, since the
176 // 'boundParameters' attribute has not been setup.
177 String reportName = request.getParameter("report");
178 if(reportName != null) {
179 // Fetch the document
180 try {
181 doc = ReportFacade.fetchDocument(reportName);
182 }
183 catch(ReportException e) {
184 throw new ServletException(e);
185 }
186 }
187 else {
188 // We could not find either the document or the name of the
189 // report, so give up.
190 throw new ServletException("Could not find the document");
191 }
192 }
193
194 // Set the bound parameters
195 for(Enumeration e = request.getParameterNames(); e.hasMoreElements();) {
196 String name = (String)e.nextElement();
197 // log("Setting bound parameter " + name + ":"
198 // + request.getParameter(name));
199 doc.setBoundParamValue(name, request.getParameter(name));
200 }
201
202 // Render the document
203 try {
204 doc.loadData();
205 doc.addRenderer(Constants.BASIC_HTML, response.getWriter());
206 doc.render();
207 }
208 catch(ReportException e) {
209 throw new ServletException(e);
210 }
211 }
212
213 //
214 // Static methods for use by the JSP's
215 //
216
217 public static String getServletURL() {
218 return servletURL;
219 }
220
221 //
222 // Implementation methods
223 //
224
225 /**
226 * Returns an iterator over all the document names that are known to the
227 * repositories which are specified in the properties file
228 */
229 private Iterator documentNames() throws ReportException {
230 Map m = propertiesForKeyPrefix("document.repository.");
231 ArrayList docNames = new ArrayList();
232 for(Iterator it = m.values().iterator(); it.hasNext();) {
233 String name = (String)it.next();
234 Repository repository = ReportFacade.fetchRepository(name);
235 for(Iterator i = repository.documentNames(); i.hasNext();) {
236 docNames.add(name + "." + i.next());
237 }
238 }
239 return docNames.iterator();
240 }
241
242 /**
243 * This will return a map of all the properties whose keys' start with the
244 * specified prefix.
245 */
246 private Map propertiesForKeyPrefix(String prefix) {
247 Map result = new HashMap();
248 for(Enumeration e = properties.propertyNames(); e.hasMoreElements();) {
249 String key = (String)e.nextElement();
250 if(key.startsWith(prefix)) {
251 result.put(key, properties.getProperty(key));
252 }
253 }
254 return result;
255 }
256
257 }
258