1 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 type = (String ) PropertyUtils.getSimpleProperty(form, "type"); 63 Integer [] projectIds = (Integer []) PropertyUtils.getSimpleProperty(form, "projectIds"); 64 if("all".equalsIgnoreCase(type)) { 65 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 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 Vector reportDataVector = new Vector(); 85 86 InitialContext ic = new InitialContext(); 87 Object 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 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 reportId = (Integer ) PropertyUtils.getSimpleProperty(form, "reportId"); 122 String reportOutput = (String ) 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 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 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 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 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 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 |