1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
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
83 if("all".equalsIgnoreCase(reportType)) {
84
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
97
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
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
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
174 throw new RuntimeException("not working");
175
176 } else if(ReportUtilities.REPORT_OUTPUT_CSV.equals(reportOutput)) {
177 log.debug("Processing CSV report.");
178
179 throw new RuntimeException("not working");
180
181 } else if(ReportUtilities.REPORT_OUTPUT_HTML.equals(reportOutput)) {
182 log.debug("Processing HTML report.");
183
184 throw new RuntimeException("not working");
185
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