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.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.UserUtilities;
37  import org.itracker.web.actions.base.ItrackerBaseAction;
38  
39  
40  public class DownloadReportAction extends ItrackerBaseAction {
41  	private static final Logger log = Logger.getLogger(DownloadReportAction.class);
42  	
43      public DownloadReportAction () {
44      }
45  
46      public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
47      	ActionMessages errors = new ActionMessages();
48  
49          String pageTitleKey = "";
50          String pageTitleArg = "";
51  
52          if(! hasPermission(UserUtilities.PERMISSION_USER_ADMIN, request, response)) {
53              return mapping.findForward("unauthorized");
54          }
55  
56          try {
57              Integer reportId = new Integer((request.getParameter("id") == null ? "-1" : request.getParameter("id")));
58              if(reportId == null || reportId.intValue() < 0) {
59                  errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidreport"));
60              } else {
61                  ReportService reportService = getITrackerServices().getReportService();
62  
63                  Report report = reportService.getReportDAO().findByPrimaryKey(reportId);
64                  if(report != null) {
65                      //report.setFileData(reportService.getReportFile(reportId));
66                      response.setContentType("application/x-itracker-report-export");
67                      response.setHeader("Content-Disposition", "attachment; filename=\"ITracker_report_" + report.getId() + ".def\"");
68                      ServletOutputStream out = response.getOutputStream();
69                      log.debug("Attempting export for: " + report);
70                      //out.println( new String((byte[]) report.getFileData()));
71                      out.flush();
72                      out.close();
73                      return null;
74                  }
75                  log.debug("Unknown report " + reportId + " specified for export");
76                  errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.invalidreport"));
77              }
78          } catch(Exception e) {
79          	pageTitleKey = "itracker.web.error.title";
80          	request.setAttribute("pageTitleKey",pageTitleKey); 
81              request.setAttribute("pageTitleArg",pageTitleArg); 
82              log.error("Exception while exporting a report.", e);
83              errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage("itracker.web.error.system"));
84          }
85          if(! errors.isEmpty()) {
86              saveErrors(request, errors);
87          }
88  
89          return mapping.findForward("error");
90      }
91  
92  }
93