Source code: com/dghda/kent/servlet/ReportRunner.java
1 /* Copyright (C) 2001 Duane Griffin <duanegriffin@users.sourceforge.net>
2 This file is part of Kent.
3
4 Kent is free software; you can redistribute it and/or
5 modify it under the terms of the GNU General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 Kent is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 General Public License for more details.
13
14 You should have received a copy of the GNU General Public
15 License along with Kent; see the file COPYING. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA.
18 */
19
20 package com.dghda.kent.servlet;
21
22 import java.io.*;
23 import javax.servlet.*;
24 import javax.servlet.http.*;
25
26 import com.dghda.kent.*;
27
28 /**
29 A servlet which runs a report.
30 */
31 public class ReportRunner extends ReportServlet {
32 public void handleReportRequest (Report report, java.util.Properties config, HttpServletRequest request, HttpServletResponse response) throws IOException {
33 try {
34 String user = request.getRemoteUser();
35
36 // Find the format they want the results in, or use HTML if we can't
37 ReportTransform transform = null;
38 String selectedID = config.getProperty (KentModule.FORMAT_ID);
39 if (selectedID != null)
40 transform = getReportEngine().getTransform (selectedID, user);
41 if (transform == null)
42 transform = new com.dghda.kent.transforms.HTMLTransform (getReportEngine());
43
44 // Run the report
45 String template = report.getReportTemplate (config);
46 String data = report.getReportData (config);
47
48 // Transform & output the results
49 response.setContentType (transform.getContentType (config));
50 transform.transform (response.getOutputStream(),
51 template, data, config);
52
53 // Perform additional actions
54 java.util.List actions = getActions (config, user);
55 java.util.Iterator i = actions.iterator();
56 while (i.hasNext()) {
57
58 // Perform the next action
59 ReportAction action = (ReportAction) i.next();
60 ReportAction.ActionResult result = action.performAction (template, data, config);
61 if (result.getSuccess()) {
62 getReportEngine().getEnvironment().log ("Action " + action.getID() + " successful");
63 } else {
64 StringBuffer msg = new StringBuffer();
65 msg.append ("An error occurred while taking action ");
66 msg.append (action.getID());
67 if (result.getStatus() != null) {
68 msg.append (": ");
69 msg.append (result.getStatus());
70 }
71 if (result.getError() == null)
72 getReportEngine().getEnvironment().log (msg.toString());
73 else
74 getReportEngine().getEnvironment().log (msg.toString(), result.getError());
75 }
76 }
77 } catch (ReportingException exc) {
78 response.reset();
79 getReportEngine().getEnvironment().log ("Error running report", exc);
80 fail (response, "An error occurred while running the report", exc.toString());
81 } catch (ReportTransformException exc) {
82 response.reset();
83 getReportEngine().getEnvironment().log ("Error transforming report output", exc);
84 fail (response, "An error occurred while transforming the report's output", exc.toString());
85 }
86 }
87 }