View Javadoc

1   package ar.com.jiji.kaya.reports;
2   
3   import java.io.InputStream;
4   import java.io.OutputStream;
5   import java.util.Map;
6   
7   import net.sf.jasperreports.engine.JRDataSource;
8   import net.sf.jasperreports.engine.JRException;
9   import net.sf.jasperreports.engine.JasperCompileManager;
10  import net.sf.jasperreports.engine.JasperExportManager;
11  import net.sf.jasperreports.engine.JasperFillManager;
12  import net.sf.jasperreports.engine.JasperPrint;
13  
14  /**
15   * Facade para JasperReports. * TODO: documentar y hacer tests
16   * 
17   * @author ldebole
18   * @author lparra
19   * 
20   */
21  public class JasperFacade {
22  
23  	private JasperFacade() {
24  	}
25  
26  	/**
27  	 * Compila un reporte.
28  	 * 
29  	 * @param fileName
30  	 *            El nombre del archivo, sin extension. Genera un archivo con
31  	 *            igual nombre pero con la extension .jasper
32  	 * @throws ReportException
33  	 */
34  	public static void compile(String fileName) throws ReportException {
35  		try {
36  			JasperCompileManager.compileReportToFile(fileName + ".jrxml",
37  					fileName + ".jasper");
38  		} catch (JRException e) {
39  			throw new ReportException(e);
40  		}
41  
42  	}
43  
44  	/**
45  	 * Genera un reporte a partir de un reporte ya compilado.
46  	 * 
47  	 * @param format
48  	 *            El formato del reporte a generar.
49  	 * @param reportResource
50  	 *            El path dentro del classpath donde esta el reporte compilado.
51  	 * @param params
52  	 *            Los parametros del reporte. Si no tiene se puede enviar null.
53  	 * @param dataSource
54  	 * @param out
55  	 *            El stream donde se escriben el reporte.
56  	 * @throws ReportException
57  	 *             En caso de error.
58  	 */
59  	public static void export(ReportFormat format, String reportResource,
60  			Map params, JRDataSource dataSource, OutputStream out)
61  			throws ReportException {
62  		InputStream reportStream = JasperFacade.class
63  				.getResourceAsStream(reportResource);
64  		if (reportStream == null)
65  			throw new ReportException("Report not found " + reportResource);
66  
67  		try {
68  			JasperPrint jprint = JasperFillManager.fillReport(reportStream,
69  					params, dataSource);
70  			switch (format) {
71  			case PDF:
72  				JasperExportManager.exportReportToPdfStream(jprint, out);
73  			case HTML:
74  				JasperExportManager.exportReportToPdfStream(jprint, out);
75  			}
76  		} catch (JRException e) {
77  			throw new ReportException(e);
78  		}
79  	}
80  
81  	public static void export(ReportFormat tipo, String fileName, Map args,
82  			JRDataSource dataSource) throws ReportException {
83  		try {
84  			switch (tipo) {
85  			case PDF:
86  				exportToPdf(fileName, args, dataSource);
87  			case HTML:
88  				exportToHtml(fileName, args, dataSource);
89  			}
90  		} catch (JRException e) {
91  			throw new ReportException(e);
92  		}
93  
94  	}
95  
96  	public static void export(ReportFormat tipo, String fileName,
97  			JRDataSource dataSource) throws ReportException {
98  		export(tipo, fileName, null, dataSource);
99  	}
100 
101 	private static void exportToPdf(String fileName, Map args,
102 			JRDataSource dataSource) throws JRException {
103 		JasperExportManager.exportReportToPdfFile(JasperFillManager
104 				.fillReportToFile(fileName + ".jasper", args, dataSource),
105 				fileName + ".pdf");
106 	}
107 
108 	private static void exportToHtml(String fileName, Map args,
109 			JRDataSource dataSource) throws JRException {
110 		JasperExportManager.exportReportToHtmlFile(JasperFillManager
111 				.fillReportToFile(fileName + ".jasper", args, dataSource),
112 				fileName + ".html");
113 	}
114 
115 }