01: /*
02: * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
03: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
04: */
05: package com.sun.portal.ubt.report.client;
06:
07: import net.sf.jasperreports.engine.JasperPrint;
08: import net.sf.jasperreports.engine.JRException;
09: import net.sf.jasperreports.engine.JasperExportManager;
10: import net.sf.jasperreports.engine.util.JRSaver;
11: import com.sun.portal.ubt.report.server.UBTReportGenerator;
12: import com.sun.portal.ubt.report.server.UBTReportGeneratorException;
13:
14: import java.io.File;
15:
16: /**
17: * Saves report in a jrprint or given format. Supported formats are pdf, html, xml
18: */
19: public class UBTReportSaver {
20: public static String PDF_FORMAT = "pdf";
21: public static String HTML_FORMAT = "html";
22: public static String XML_FORMAT = "xml";
23: public static String DEFAULT_FORMAT = PDF_FORMAT;
24: private String fs = System.getProperty("file.separator");
25: private JasperPrint[] report;
26: private String destinationDir;
27:
28: public UBTReportSaver(JasperPrint[] report, String destinationDir) {
29: this .report = report;
30: this .destinationDir = destinationDir;
31: }
32:
33: public void save(String format) throws JRException {
34: if (format == null)
35: format = DEFAULT_FORMAT;
36: if (!validateFormat(format)) {
37: throw new JRException("error.ubt.unsupported.format");
38: }
39: if (report != null) {
40: saveJrprint();
41: saveAsFormat(format);
42: }
43: }
44:
45: private void saveJrprint() throws JRException {
46: for (int i = 0; i < report.length; i++) {
47: String name = report[i].getName();
48: String fileName = destinationDir + fs + name + ".jrprint";
49: JRSaver.saveObject(report[i], fileName);
50: }
51: }
52:
53: private void saveAsFormat(String format) throws JRException {
54: for (int i = 0; i < report.length; i++) {
55: String name = report[i].getName();
56: String fileName = destinationDir + fs + name + "." + format;
57: if (HTML_FORMAT.equals(format)) {
58: JasperExportManager.exportReportToHtmlFile(report[i],
59: fileName);
60: } else if (PDF_FORMAT.equals(format)) {
61: JasperExportManager.exportReportToPdfFile(report[i],
62: fileName);
63: } else if (XML_FORMAT.equals(format)) {
64: JasperExportManager.exportReportToXmlFile(report[i],
65: fileName, false);
66: }
67: }
68: }
69:
70: public static boolean validateFormat(String format) {
71: boolean valid = true;
72: if (format != null) {
73: if (!(format.equals(HTML_FORMAT)
74: || format.equals(XML_FORMAT) || format
75: .equals(PDF_FORMAT))) {
76: valid = false;
77: }
78: }
79:
80: return valid;
81: }
82: }
|