KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > cowsultants > itracker > web > actions > DisplayReportAction


1 /*
2  * This software was designed and created by Jason Carroll.
3  * Copyright (c) 2002, 2003, 2004 Jason Carroll.
4  * The author can be reached at jcarroll@cowsultants.com
5  * ITracker website: http://www.cowsultants.com
6  * ITracker forums: http://www.cowsultants.com/phpBB/index.php
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it only under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  */

18
19 package cowsultants.itracker.web.actions;
20
21 import java.io.*;
22 import java.rmi.*;
23 import java.text.*;
24 import java.util.*;
25 import javax.ejb.*;
26 import javax.rmi.*;
27 import javax.naming.*;
28 import javax.servlet.*;
29 import javax.servlet.http.*;
30
31 import org.apache.commons.beanutils.*;
32 import org.apache.struts.action.*;
33 import org.apache.struts.util.*;
34
35 import cowsultants.itracker.ejb.client.exceptions.*;
36 import cowsultants.itracker.ejb.client.interfaces.*;
37 import cowsultants.itracker.ejb.client.models.*;
38 import cowsultants.itracker.ejb.client.resources.*;
39 import cowsultants.itracker.ejb.client.util.*;
40 import cowsultants.itracker.web.reports.*;
41 import cowsultants.itracker.web.util.*;
42
43
44 public class DisplayReportAction extends ITrackerAction {
45
46     public DisplayReportAction() {
47     }
48
49     public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
50         ActionErrors errors = new ActionErrors();
51
52         if(! isLoggedIn(request, response)) {
53             return mapping.findForward("login");
54         }
55
56         try {
57             HttpSession session = request.getSession(false);
58             Locale userLocale = (Locale) session.getAttribute(Constants.LOCALE_KEY);
59             IssueSearchQueryModel isqm = (IssueSearchQueryModel) session.getAttribute(Constants.SEARCH_QUERY_KEY);
60
61             IssueModel[] reportData = new IssueModel[0];
62             String JavaDoc type = (String JavaDoc) PropertyUtils.getSimpleProperty(form, "type");
63             Integer JavaDoc[] projectIds = (Integer JavaDoc[]) PropertyUtils.getSimpleProperty(form, "projectIds");
64             if("all".equalsIgnoreCase(type)) {
65                 // Export all of the issues in the system
66
UserModel currUser = (UserModel) session.getAttribute(Constants.USER_KEY);
67                 if(! currUser.isSuperUser()) {
68                     errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.unauthorized"));
69                     throw new ReportException();
70                 }
71
72
73                 InitialContext ic = new InitialContext();
74                 Object JavaDoc ihRef = ic.lookup("java:comp/env/" + IssueHandler.JNDI_NAME);
75                 IssueHandlerHome ihHome = (IssueHandlerHome) PortableRemoteObject.narrow(ihRef, IssueHandlerHome.class);
76                 IssueHandler ih = ihHome.create();
77
78                 reportData = ih.getAllIssues();
79                 Arrays.sort(reportData, new IssueModel().new CompareById());
80             } else if("project".equalsIgnoreCase(type)) {
81                 if(projectIds != null && projectIds.length > 0) {
82                     // This wasn't a regular search. So instead, take all the selected projects and find all the
83
// issues for them, check which ones the user can see, and then create a new array of issues
84
Vector reportDataVector = new Vector();
85
86                     InitialContext ic = new InitialContext();
87                     Object JavaDoc ihRef = ic.lookup("java:comp/env/" + IssueHandler.JNDI_NAME);
88                     IssueHandlerHome ihHome = (IssueHandlerHome) PortableRemoteObject.narrow(ihRef, IssueHandlerHome.class);
89                     IssueHandler ih = ihHome.create();
90
91                     UserModel currUser = (UserModel) session.getAttribute(Constants.USER_KEY);
92                     HashMap currPermissions = (HashMap) session.getAttribute(Constants.PERMISSIONS_KEY);
93
94                     for(int i = 0; i < projectIds.length; i++) {
95                         IssueModel[] issues = ih.getIssuesByProjectId(projectIds[i]);
96                         for(int j = 0; j < issues.length; j++) {
97                             if(IssueUtilities.canViewIssue(issues[j], currUser, currPermissions)) {
98                                 reportDataVector.add(issues[j]);
99                             }
100                         }
101                     }
102                     reportData = new IssueModel[reportDataVector.size()];
103                     reportDataVector.copyInto(reportData);
104                     Arrays.sort(reportData, new IssueModel().new CompareById());
105                 } else {
106                     errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.projectrequired"));
107                     throw new ReportException();
108                 }
109             } else {
110                 // This must be a regular search, look for a search query result.
111
reportData = (isqm == null || isqm.getResults() == null ? new IssueModel[0] : isqm.getResults());
112             }
113
114             Logger.logDebug("Report data contains " + reportData.length + " elements.");
115
116             if(reportData.length == 0) {
117                 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.noreportdata"));
118                 throw new ReportException();
119             }
120
121             Integer JavaDoc reportId = (Integer JavaDoc) PropertyUtils.getSimpleProperty(form, "reportId");
122             String JavaDoc reportOutput = (String JavaDoc) PropertyUtils.getSimpleProperty(form, "reportOutput");
123             if(reportId == null || reportId.intValue() == 0) {
124                 Logger.logDebug("Invalid report id: " + reportId + " requested.");
125                 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.invalidreport"));
126                 throw new ReportException();
127             } else if(reportId.intValue() == ReportUtilities.REPORT_EXPORT_XML) {
128                 Logger.logDebug("Issue export requested.");
129
130                 InitialContext ic = new InitialContext();
131                 Object JavaDoc scRef = ic.lookup("java:comp/env/" + SystemConfiguration.JNDI_NAME);
132                 SystemConfigurationHome scHome = (SystemConfigurationHome) PortableRemoteObject.narrow(scRef, SystemConfigurationHome.class);
133                 SystemConfiguration sc = scHome.create();
134                 SystemConfigurationModel config = sc.getSystemConfiguration(ImportExportTags.EXPORT_LOCALE);
135
136                 if(! exportIssues(reportData, config, request, response)) {
137                     errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.system"));
138                     throw new ReportException();
139                 }
140                 return null;
141             } else if(reportId.intValue() > 0) {
142                 Logger.logDebug("Defined report (" + reportId + ") requested.");
143
144                 InitialContext ic = new InitialContext();
145                 Object JavaDoc rhRef = ic.lookup("java:comp/env/" + ReportHandler.JNDI_NAME);
146                 ReportHandlerHome rhHome = (ReportHandlerHome) PortableRemoteObject.narrow(rhRef, ReportHandlerHome.class);
147                 ReportHandler rh = rhHome.create();
148
149                 ReportModel reportModel = rh.getReport(reportId);
150                 if(reportModel == null) {
151                     Logger.logDebug("Invalid report id: " + reportId + " requested.");
152                     errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.invalidreport"));
153                     throw new ReportException();
154                 }
155
156                 reportModel.setFileData(rh.getReportFile(reportModel.getId()));
157                 Logger.logDebug("Report " + reportModel.toString() + " found.");
158
159                 ITrackerReport report = null;
160                 if(reportModel.getClassName() != null && ! reportModel.getClassName().equals("")) {
161                     Logger.logDebug("Creating new class: " + reportModel.getClassName());
162                     Class JavaDoc reportClass = Class.forName(reportModel.getClassName());
163                     report = (ITrackerReport) reportClass.newInstance();
164                 } else if(reportModel.getReportType() == ReportUtilities.REPORTTYPE_JFREE) {
165                     report = (ITrackerReport) new DefaultITrackerJFreeReport();
166                 } else if(reportModel.getReportType() == ReportUtilities.REPORTTYPE_JASPER) {
167                     report = (ITrackerReport) new DefaultITrackerJasperReport();
168                 }
169
170                 if(report == null) {
171                     Logger.logError("Unable to create new instance of report: " + reportModel.getName() + " class: " + reportModel.getClassName() + " type: " + reportModel.getReportType());
172                     errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.invalidreport"));
173                     throw new ReportException();
174                 }
175
176                 Logger.logDebug("Initializing report object.");
177                 report.initializeReport(reportData, reportModel, userLocale, reportOutput, session);
178                 Logger.logDebug("Augmenting report object.");
179                 report.augmentReport();
180
181                 if(ReportUtilities.REPORT_OUTPUT_PDF.equals(reportOutput)) {
182                     Logger.logDebug("Processing PDF report.");
183                     report.outputPDF(request, response, mapping);
184                     return null;
185                 } else if(ReportUtilities.REPORT_OUTPUT_XLS.equals(reportOutput)) {
186                     Logger.logDebug("Processing XLS report.");
187                     report.outputXLS(request, response, mapping);
188                     return null;
189                 } else if(ReportUtilities.REPORT_OUTPUT_CSV.equals(reportOutput)) {
190                     Logger.logDebug("Processing CSV report.");
191                     report.outputCSV(request, response, mapping);
192                     return null;
193                 } else if(ReportUtilities.REPORT_OUTPUT_HTML.equals(reportOutput)) {
194                     Logger.logDebug("Processing HTML report.");
195                     report.outputHTML(request, response, mapping);
196                     return null;
197                 } else {
198                     Logger.logError("Inavlid report output format: " + reportOutput);
199                     errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.invalidreportoutput"));
200                     throw new ReportException();
201                 }
202             }
203         } catch(ReportException re) {
204             if(re.getErrorKey() != null) {
205                 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError(re.getErrorKey()));
206             }
207         } catch(Exception JavaDoc e) {
208             Logger.logDebug("Error in report processing: " + e.getMessage(), e);
209             errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("itracker.web.error.system"));
210         }
211
212         if(! errors.isEmpty()) {
213             saveErrors(request, errors);
214         }
215
216         return mapping.findForward("error");
217     }
218
219     private boolean exportIssues(IssueModel[] issues, SystemConfigurationModel config, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
220         response.setContentType("text/xml; charset=UTF-8");
221         response.setHeader("Content-Disposition", "attachment; filename=\"issue_export.xml\"");
222         PrintWriter out = response.getWriter();
223
224         out.println("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n\n");
225         try {
226             String JavaDoc xml = ImportExportUtilities.exportIssues(issues, config);
227             out.print(xml);
228             out.flush();
229         } catch(ImportExportException iee) {
230             Logger.logError("Error exporting issue data. Message: " + iee.getMessage(), iee);
231             return false;
232         }
233         out.flush();
234         out.close();
235         return true;
236     }
237 }
238   
Popular Tags