Clover Coverage Report - itracker
Coverage timestamp: Tue May 1 2012 16:42:12 CEST
70   287   18   6.36
2   164   0.26   11
11     1.64  
1    
 
 
  ReportServiceImpl       Line # 59 70 18 0% 0.0
 
No Tests
 
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.services.implementations;
20   
21    import java.io.File;
22    import java.io.IOException;
23    import java.util.HashMap;
24    import java.util.List;
25    import java.util.Locale;
26    import java.util.Map;
27   
28    import javax.servlet.ServletOutputStream;
29    import javax.servlet.http.HttpServletRequest;
30    import javax.servlet.http.HttpServletResponse;
31    import javax.servlet.http.HttpSession;
32    import javax.swing.JFrame;
33   
34    import net.sf.jasperreports.engine.JRDataSource;
35    import net.sf.jasperreports.engine.JRException;
36    import net.sf.jasperreports.engine.JRExporterParameter;
37    import net.sf.jasperreports.engine.JasperExportManager;
38    import net.sf.jasperreports.engine.JasperFillManager;
39    import net.sf.jasperreports.engine.JasperPrint;
40    import net.sf.jasperreports.engine.JasperReport;
41    import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
42    import net.sf.jasperreports.engine.export.JRCsvExporter;
43    import net.sf.jasperreports.engine.export.JRHtmlExporter;
44    import net.sf.jasperreports.engine.export.JRHtmlExporterParameter;
45    import net.sf.jasperreports.engine.export.JRXlsExporter;
46    import net.sf.jasperreports.engine.util.JRLoader;
47    import net.sf.jasperreports.view.JRViewer;
48   
49    import org.apache.struts.action.ActionMapping;
50    import org.apache.struts.util.RequestUtils;
51    import org.itracker.core.resources.ITrackerResources;
52    import org.itracker.model.Issue;
53    import org.itracker.model.Report;
54    import org.itracker.persistence.dao.ReportDAO;
55    import org.itracker.services.ReportService;
56    import org.itracker.services.exceptions.ReportException;
57    import org.itracker.web.util.Constants;
58   
 
59    public class ReportServiceImpl implements ReportService {
60   
61    private final ReportDAO reportDAO;
62   
 
63  0 toggle public ReportServiceImpl(ReportDAO reportDAO) {
64  0 this.reportDAO = reportDAO;
65    }
66   
67    // kill this
 
68  0 toggle public List<Report> getAllReports() {
69  0 return reportDAO.findAll();
70    }
71   
72    // kill this
 
73  0 toggle public int getNumberReports() {
74  0 return reportDAO.findAll().size();
75    }
76   
77    // kill this
 
78  0 toggle public Report createReport(Report report) {
79    // report.setCreateDate(new Date());
80    // report.setLastModifiedDate(report.getCreateDate());
81  0 reportDAO.save(report);
82  0 return report;
83    }
84   
 
85  0 toggle private JasperPrint generateReport(Report report,
86    Map<String, Object> parameters, Object datasource) {
87  0 try {
88   
89  0 JasperReport jasperReport = getJasperReport(report);
90  0 JasperPrint jasperPrint = JasperFillManager.fillReport(
91    jasperReport, parameters, (JRDataSource) datasource);
92  0 return jasperPrint;
93    } catch (JRException e) {
94  0 throw new ReportException(e);
95    }
96   
97    }
98   
 
99  0 toggle private JasperReport getJasperReport(Report report) throws JRException {
100  0 return (JasperReport) JRLoader.loadObject(report.getReportDefinition());
101    }
102   
 
103  0 toggle public ReportDAO getReportDAO() {
104  0 return reportDAO;
105    }
106   
107    /**
108    * break this into stuff that belongs here and stuff that belongs in the web
109    * layer
110    *
111    * @deprecated needs to be moved to static method in a new web-report-utility class
112    */
 
113  0 toggle public void outputPDF(List<Issue> reportDataArray, Report report,
114    Locale userLocale, String reportOutput, HttpSession session,
115    HttpServletRequest request, HttpServletResponse response,
116    ActionMapping mapping) {
117   
118  0 try {
119    // hack, we have to find a more structured way to support
120    // various types of queries
121  0 JRBeanCollectionDataSource beanCollectionDataSource;
122    // JasperReport jr = getJasperReport(report);
123   
124    // TODO: Temporarily commenting out code to remove pnuts dependency
125    // Will need to test for regressions.
126    // if(jr.getQuery() != null) {
127    // List objects = (List)Pnuts.eval(jr.getQuery().getText(), new
128    // Context());
129    // beanCollectionDataSource = new
130    // JRBeanCollectionDataSource(objects);
131    // } else {
132  0 beanCollectionDataSource = new JRBeanCollectionDataSource(
133    reportDataArray);
134    // }
135   
136  0 Map<String, Object> parameters = new HashMap<String, Object>();
137  0 String reportTitle = report.getName();
138  0 if (report.getNameKey() != null) {
139  0 reportTitle = ITrackerResources.getString(report.getNameKey());
140    }
141  0 parameters.put("ReportTitle", reportTitle);
142  0 parameters.put("BaseDir", new File("."));
143  0 JasperPrint jasperPrint = generateReport(report, parameters,
144    beanCollectionDataSource);
145   
146    // test
147  0 JRViewer jrViewer = new JRViewer(jasperPrint);
148  0 JFrame frame = new JFrame();
149  0 frame.add(jrViewer);
150    // frame.show();
151    // test
152  0 response.setHeader("Content-Type", "application/pdf");
153  0 ServletOutputStream out = response.getOutputStream();
154   
155  0 JasperExportManager.exportReportToPdfStream(jasperPrint, out);
156   
157  0 out.flush();
158  0 out.close();
159    } catch (JRException e) {
160    // logger.debug("Error outputing PDF report.", e);
161  0 throw new ReportException(e);
162    } catch (IOException e) {
163  0 throw new ReportException(e);
164    }
165    }
166   
167    /**
168    * @deprecated needs to be moved to static method in a new web-report-utility class
169    * @param request
170    * @param response
171    * @param mapping
172    * @throws ReportException
173    */
 
174  0 toggle public void outputCSV(HttpServletRequest request,
175    HttpServletResponse response, ActionMapping mapping)
176    throws ReportException {
177  0 try {
178    /*
179    * if(jasperReport == null || jasperPrint == null) { throw new
180    * Exception("The JasperReport object has not been initialized."); }
181    */
182   
183  0 JRCsvExporter exporter = new JRCsvExporter();
184   
185  0 response.setHeader("Content-Disposition",
186    "inline; filename=\"itrackerreport.csv\"");
187  0 response.setHeader("Content-Type", "text/csv; charset=UTF-8");
188  0 ServletOutputStream out = response.getOutputStream();
189   
190  0 exporter.setParameter(JRExporterParameter.JASPER_PRINT, null);
191  0 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
192  0 exporter.exportReport();
193   
194  0 out.flush();
195  0 out.close();
196    } catch (Exception e) {
197    // logger.debug("Error outputing CSV report.", e);
198  0 throw new ReportException(e.getMessage());
199    }
200    }
201   
202    /**
203    * Outputs the JFreeReport as an Excel spreadsheet.
204    *
205    * @param out
206    * the OutputStream to send the XLS to.
207    * @deprecated needs to be moved to static method in a new web-report-utility class
208    */
 
209  0 toggle public void outputXLS(HttpServletRequest request,
210    HttpServletResponse response, ActionMapping mapping)
211    throws ReportException {
212  0 try {
213    /*
214    * if(jasperReport == null || jasperPrint == null) { throw new
215    * Exception("The JasperReport object has not been initialized."); }
216    */
217   
218  0 JRXlsExporter exporter = new JRXlsExporter();
219   
220  0 response.setHeader("Content-Disposition",
221    "inline; filename=\"itrackerreport.xls\"");
222  0 response.setHeader("Content-Type",
223    "application/vnd.ms-excel; charset=UTF-8");
224  0 ServletOutputStream out = response.getOutputStream();
225   
226  0 exporter.setParameter(JRExporterParameter.JASPER_PRINT, null);
227  0 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
228  0 exporter.exportReport();
229   
230  0 out.flush();
231  0 out.close();
232    } catch (Exception e) {
233    // logger.debug("Error outputing XLS report.", e);
234  0 throw new ReportException(e.getMessage());
235    }
236    }
237   
238    /**
239    * Outputs the JFreeReport as HTML.
240    *
241    * @param out
242    * the OutputStream to send the HTML to.
243    * @deprecated needs to be moved to static method in a new web-report-utility class
244    */
 
245  0 toggle public void outputHTML(HttpServletRequest request,
246    HttpServletResponse response, ActionMapping mapping)
247    throws ReportException {
248  0 try {
249    /*
250    * if(jasperReport == null || jasperPrint == null) { throw new
251    * Exception("The JasperReport object has not been initialized."); }
252    */
253   
254  0 JRHtmlExporter exporter = new JRHtmlExporter();
255   
256  0 Map<Object, Object> imagesMap = new HashMap<Object, Object>();
257  0 HttpSession session = request.getSession(true);
258  0 session.setAttribute(Constants.REPORT_IMAGEMAP_KEY, imagesMap);
259   
260  0 response.setHeader("Content-Disposition",
261    "inline; filename=\"itrackerreport.html\"");
262  0 response.setHeader("Content-Type", "text/html; charset=UTF-8");
263  0 ServletOutputStream out = response.getOutputStream();
264   
265  0 exporter.setParameter(JRExporterParameter.JASPER_PRINT, null);
266  0 exporter.setParameter(JRExporterParameter.OUTPUT_STREAM, out);
267  0 exporter
268    .setParameter(JRHtmlExporterParameter.IMAGES_MAP, imagesMap);
269    // logger.debug("Image URL=" + RequestUtils.forwardURL(request,
270    // mapping.findForward("imagesurl")));
271  0 exporter.setParameter(JRHtmlExporterParameter.IMAGES_URI,
272    RequestUtils.printableURL(RequestUtils.absoluteURL(request,
273    RequestUtils.forwardURL(request, mapping
274    .findForward("imagesurl"))))
275    + "?image=");
276   
277  0 exporter.exportReport();
278   
279  0 out.flush();
280  0 out.close();
281    } catch (Exception e) {
282    // logger.debug("Error outputing HTML report.", e);
283  0 throw new ReportException(e.getMessage());
284    }
285    }
286   
287    }