View Javadoc

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 org.itracker.web.actions.report;
20  
21  import java.io.IOException;
22  import java.io.PrintWriter;
23  import java.util.ArrayList;
24  import java.util.Collections;
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.Locale;
28  import java.util.Map;
29  import java.util.Set;
30  
31  import javax.servlet.ServletException;
32  import javax.servlet.http.HttpServletRequest;
33  import javax.servlet.http.HttpServletResponse;
34  import javax.servlet.http.HttpSession;
35  
36  import org.apache.commons.beanutils.PropertyUtils;
37  import org.apache.log4j.Logger;
38  import org.apache.struts.action.ActionForm;
39  import org.apache.struts.action.ActionForward;
40  import org.apache.struts.action.ActionMapping;
41  import org.apache.struts.action.ActionMessage;
42  import org.apache.struts.action.ActionMessages;
43  import org.itracker.model.Issue;
44  import org.itracker.model.IssueSearchQuery;
45  import org.itracker.model.PermissionType;
46  import org.itracker.model.Report;
47  import org.itracker.model.SystemConfiguration;
48  import org.itracker.model.User;
49  import org.itracker.services.ConfigurationService;
50  import org.itracker.services.IssueService;
51  import org.itracker.services.ReportService;
52  import org.itracker.services.exceptions.ImportExportException;
53  import org.itracker.services.exceptions.ReportException;
54  import org.itracker.services.util.ImportExportTags;
55  import org.itracker.services.util.ImportExportUtilities;
56  import org.itracker.services.util.IssueUtilities;
57  import org.itracker.services.util.ReportUtilities;
58  import org.itracker.web.actions.base.ItrackerBaseAction;
59  import org.itracker.web.util.Constants;
60  
61  
62  public class DisplayReportAction extends ItrackerBaseAction {
63  	private static final Logger log = Logger.getLogger(DisplayReportAction.class);
64  	
65      public DisplayReportAction() {
66      }
67  
68  	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
69          ActionMessages errors = new ActionMessages();
70  		//  TODO: Action Cleanup
71  
72          try {
73              HttpSession session = request.getSession(false);
74              Locale userLocale = (Locale) session.getAttribute(Constants.LOCALE_KEY);
75              IssueSearchQuery isqm = (IssueSearchQuery) session.getAttribute(Constants.SEARCH_QUERY_KEY);
76  
77              List<Issue> reportingIssues = new ArrayList<Issue>();
78              String reportType = (String) PropertyUtils.getSimpleProperty(form, "type");
79          	log.info("execute: report type was " + reportType);
80          	
81              Integer[] projectIds = (Integer[]) PropertyUtils.getSimpleProperty(form, "projectIds");
82              // TODO All Issues this is huge, remove if possible
83              if("all".equalsIgnoreCase(reportType)) {
84                  // Export all of the issues in the system
85                  User currUser = (User) session.getAttribute(Constants.USER_KEY);
86                  if(! currUser.isSuperUser()) {
87                  	errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.unauthorized"));
88                      throw new ReportException();
89                  }
90  
91                  IssueService issueService = getITrackerServices().getIssueService();
92                  reportingIssues = issueService.getAllIssues();
93                  Collections.sort(reportingIssues, Issue.ID_COMPARATOR);
94              } else if("project".equalsIgnoreCase(reportType)) {
95                  if(projectIds != null && projectIds.length > 0) {
96                      // This wasn't a regular search.  So instead, take all the selected projects and find all the
97                      // issues for them, check which ones the user can see, and then create a new array of issues
98                      List<Issue> reportDataList = new ArrayList<Issue>();
99                      
100                     IssueService issueService = getITrackerServices().getIssueService();
101                     
102                     User currUser = (User) session.getAttribute(Constants.USER_KEY);
103                     Map<Integer, Set<PermissionType>> userPermissions = getUserPermissions(session);
104                     Iterator<Issue> issuesIt = null;
105                     Issue currentIssue = null;
106                     List<Issue> issues;
107                     for(int i = 0; i < projectIds.length; i++) {
108                         issues = issueService.getIssuesByProjectId(projectIds[i]);
109                         issuesIt = issues.iterator();
110                         while (issuesIt.hasNext()) {
111                         	currentIssue = issuesIt.next();
112                             if(IssueUtilities.canViewIssue(currentIssue, currUser, userPermissions)) {
113                                 reportDataList.add(currentIssue);
114                             }
115                         }
116                     }
117                     reportingIssues = reportDataList;
118                     Collections.sort(reportingIssues, Issue.ID_COMPARATOR);
119                     
120                 } else {
121                     throw new ReportException("", "itracker.web.error.projectrequired");
122                 }
123             } else {
124                 // This must be a regular search, look for a search query result.
125                 reportingIssues = (isqm == null || isqm.getResults() == null ? new ArrayList<Issue>() : isqm.getResults());
126             }
127 
128             log.debug("Report data contains " + reportingIssues.size() + " elements.");
129 
130             if(reportingIssues.size() == 0) {
131             	errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.noreportdata"));
132                 throw new ReportException();
133             }
134 
135             Integer reportId = (Integer) PropertyUtils.getSimpleProperty(form, "reportId");
136             String reportOutput = (String) PropertyUtils.getSimpleProperty(form, "reportOutput");
137             if(reportId == null || reportId.intValue() == 0) {
138                 log.debug("Invalid report id: " + reportId + " requested.");
139                 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidreport"));
140                 throw new ReportException();
141             } else if(reportId.intValue() == ReportUtilities.REPORT_EXPORT_XML) {
142                 log.debug("Issue export requested.");
143 
144                 ConfigurationService configurationService = getITrackerServices().getConfigurationService();
145                 SystemConfiguration config = configurationService.getSystemConfiguration(ImportExportTags.EXPORT_LOCALE);
146 
147                 if(! exportIssues(reportingIssues, config, request, response)) {
148                 	errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
149                     throw new ReportException();
150                 }
151                 return null;
152             } else if(reportId.intValue() > 0) {
153                 log.debug("Defined report (" + reportId + ") requested.");
154 
155                 ReportService reportService = getITrackerServices().getReportService();
156                 Report reportModel = reportService.getReportDAO().findByPrimaryKey(reportId);
157 
158                 // probably useless. the dao throws when the report doesn't exists
159                 if(reportModel == null) {
160                     log.debug("Invalid report id: " + reportId + " requested.");
161                     errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidreport"));
162                     throw new ReportException();
163                 }
164                 
165                 log.debug("Report " + reportModel.toString() + " found.");                                                               
166                 
167                 if(ReportUtilities.REPORT_OUTPUT_PDF.equals(reportOutput)) {
168                     log.debug("Processing PDF report.");
169                     reportService.outputPDF(reportingIssues, reportModel, userLocale, reportOutput, session, request, response, mapping);
170                     return null;
171                 } else if(ReportUtilities.REPORT_OUTPUT_XLS.equals(reportOutput)) {
172                     log.debug("Processing XLS report.");
173                     //report.outputXLS(request, response, mapping);
174                     throw new RuntimeException("not working");
175                     //return null;
176                 } else if(ReportUtilities.REPORT_OUTPUT_CSV.equals(reportOutput)) {
177                     log.debug("Processing CSV report.");
178                     //report.outputCSV(request, response, mapping);
179                     throw new RuntimeException("not working");
180                     //return null;
181                 } else if(ReportUtilities.REPORT_OUTPUT_HTML.equals(reportOutput)) {
182                     log.debug("Processing HTML report.");
183                     //report.outputHTML(request, response, mapping);
184                     throw new RuntimeException("not working");
185                     //turn null;
186                 } else {
187                     log.error("Invalid report output format: " + reportOutput);
188                     errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidreportoutput"));
189                     throw new ReportException();
190                 }
191             }
192         } catch(ReportException re) {
193             if(re.getErrorKey() != null) {
194             	errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(re.getErrorKey()));
195             }
196         } catch(Exception e) {
197             log.debug("Error in report processing: " + e.getMessage(), e);
198             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
199             errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.details", e.getMessage()));
200         }
201 
202         if(! errors.isEmpty()) {
203             saveErrors(request, errors);
204         }
205 
206         return mapping.findForward("error");
207     }
208 
209     private boolean exportIssues(List<Issue> issues, SystemConfiguration config, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
210         response.setContentType("text/xml; charset=UTF-8");
211         response.setHeader("Content-Disposition", "attachment; filename=\"issue_export.xml\"");
212         PrintWriter out = response.getWriter();
213 
214         out.println("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n\n");
215         try {
216             String xml = ImportExportUtilities.exportIssues(issues, config);
217             out.print(xml);
218             out.flush();
219         } catch(ImportExportException iee) {
220             log.error("Error exporting issue data. Message: " + iee.getMessage(), iee);
221             return false;
222         }
223         out.flush();
224         out.close();
225         return true;
226     }
227 }
228