1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.itracker.web.actions.admin.report;
20
21 import java.io.IOException;
22
23 import javax.servlet.ServletException;
24 import javax.servlet.ServletOutputStream;
25 import javax.servlet.http.HttpServletRequest;
26 import javax.servlet.http.HttpServletResponse;
27
28 import org.apache.log4j.Logger;
29 import org.apache.struts.action.ActionForm;
30 import org.apache.struts.action.ActionForward;
31 import org.apache.struts.action.ActionMapping;
32 import org.apache.struts.action.ActionMessage;
33 import org.apache.struts.action.ActionMessages;
34 import org.itracker.model.Report;
35 import org.itracker.services.ReportService;
36 import org.itracker.services.util.Base64Coder;
37 import org.itracker.services.util.UserUtilities;
38 import org.itracker.web.actions.base.ItrackerBaseAction;
39
40
41 public class ExportReportAction extends ItrackerBaseAction {
42 private static final Logger log = Logger.getLogger(ExportReportAction.class);
43
44 public ExportReportAction () {
45 }
46
47 public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
48 ActionMessages errors = new ActionMessages();
49
50 String pageTitleKey = "";
51 String pageTitleArg = "";
52
53 if(! hasPermission(UserUtilities.PERMISSION_USER_ADMIN, request, response)) {
54 return mapping.findForward("unauthorized");
55 }
56
57 try {
58 Integer reportId = new Integer((request.getParameter("id") == null ? "-1" : request.getParameter("id")));
59 if(reportId == null || reportId.intValue() < 0) {
60 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidreport"));
61 } else {
62 ReportService reportService = getITrackerServices().getReportService();
63
64 Report report = reportService.getReportDAO().findByPrimaryKey(reportId);
65 if(report != null) {
66
67 response.setContentType("application/x-itracker-report-export");
68 response.setHeader("Content-Disposition", "attachment; filename=\"ITracker_report_" + report.getId() + ".itr\"");
69 ServletOutputStream out = response.getOutputStream();
70 log.debug("Attempting export for: " + report);
71 out.println("# Exported ITracker report " + report.getName());
72 out.println("id=" + report.getId());
73 out.println("name=" + report.getName());
74 out.println("namekey=" + report.getNameKey());
75 out.println("dataType=" + report.getDataType());
76 out.println("reportType=" + report.getReportType());
77 if(report.getClassName() != null && ! report.getClassName().equals("")) {
78 out.println("className=" + report.getClassName());
79 }
80 out.println("description=" + Base64Coder.encodeString(report.getDescription()));
81
82
83
84 out.flush();
85 out.close();
86 return null;
87 }
88 log.debug("Unknown report " + reportId + " specified for export");
89 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidreport"));
90 }
91 } catch(RuntimeException e) {
92 pageTitleKey = "itracker.web.error.title";
93 request.setAttribute("pageTitleKey",pageTitleKey);
94 request.setAttribute("pageTitleArg",pageTitleArg);
95 log.error("Exception while exporting a report.", e);
96 errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
97 }
98 if(! errors.isEmpty()) {
99 saveErrors(request, errors);
100 }
101
102 return mapping.findForward("error");
103 }
104
105 }
106