001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
003: * PROPRIETARY/CONFIDENTIAL. Use of this product is subject to license terms.
004: */
005: package com.sun.portal.ubt.report.view.report;
006:
007: import com.sun.portal.ubt.report.data.DataMiner;
008: import com.sun.portal.ubt.report.view.report.UBTReportI;
009: import com.sun.portal.ubt.report.view.IllegalMinerException;
010: import com.sun.portal.ubt.report.view.IllegalDataSourceException;
011: import net.sf.jasperreports.engine.JasperCompileManager;
012: import net.sf.jasperreports.engine.JRException;
013: import net.sf.jasperreports.engine.JasperExportManager;
014: import net.sf.jasperreports.engine.JasperPrint;
015:
016: import java.util.Map;
017: import java.util.HashMap;
018: import java.util.Locale;
019: import java.util.TimeZone;
020: import java.util.Date;
021: import java.io.*;
022: import java.text.SimpleDateFormat;
023: import java.text.DateFormat;
024:
025: /**
026: *
027: */
028: public abstract class UBTReportAdapter implements UBTReportI {
029: protected static String fileSeparator = System
030: .getProperty("file.separator");
031: protected static String templateExtension = ".jrxml";
032: protected static String fillExtension = ".jasper";
033: protected static String printExtension = ".jrprint";
034: protected static String htmlExtension = ".html";
035: protected String templateFolder;
036: protected String reportFolder;
037: protected String reportName;
038: protected DataMiner miner[] = new DataMiner[1];
039: protected Map reportParameters = new HashMap();
040: protected Locale clientLocale;
041:
042: public String getReportName() {
043: return reportName;
044: }
045:
046: public DataMiner[] getDataMiner() {
047: return miner;
048: }
049:
050: public void setDataMiner(DataMiner[] dataMiner) {
051: this .miner = dataMiner;
052: }
053:
054: public void setTemplateFolder(String templateFolder) {
055: this .templateFolder = templateFolder;
056: }
057:
058: public void setReportFolder(String reportFolder) {
059: this .reportFolder = reportFolder;
060: }
061:
062: public void compile() throws JRException {
063: String templateFileName = getReportTemplateName();
064: String jasperFileName = getJasperFileName();
065: String tocFileName = getTocTemplateName();
066: String jasperTocFileName = getTocJasperFileName();
067: //If the template is already compiled ,do n't compile unless there is a change to the template
068: if (!new File(jasperFileName).exists()
069: || (new File(jasperFileName).exists() && new File(
070: jasperFileName).lastModified() < new File(
071: templateFileName).lastModified()))
072: JasperCompileManager.compileReportToFile(templateFileName,
073: jasperFileName);
074: if (!new File(jasperTocFileName).exists()
075: || (new File(jasperTocFileName).exists() && new File(
076: jasperTocFileName).lastModified() < new File(
077: tocFileName).lastModified()))
078: JasperCompileManager.compileReportToFile(tocFileName,
079: jasperTocFileName);
080: }
081:
082: public abstract void fill() throws IllegalMinerException,
083: IllegalDataSourceException, JRException;
084:
085: public abstract JasperPrint getReport()
086: throws IllegalMinerException, JRException,
087: IllegalDataSourceException;
088:
089: public void html() throws JRException {
090: String jrprintFileName = this .getJrprintFileName();
091: String htmlFileName = this .getHtmlFileName();
092: JasperExportManager.exportReportToHtmlFile(jrprintFileName,
093: htmlFileName);
094: copyHelpFile();
095: cleanUpFiles();
096: }
097:
098: private void cleanUpFiles() {
099: //new File(this.getJasperFileName()).deleteOnExit();
100: //new File(this.getJrprintFileName()).deleteOnExit();
101: }
102:
103: private void copyHelpFile() {
104: String helpLink = (String) this .reportParameters
105: .get(UBTReportI.REPORT_HELP_LINK_PROPERTY);
106: String srcHelpFileName = templateFolder + fileSeparator
107: + helpLink.substring(2);
108: String dstHelpFileName = reportFolder + fileSeparator
109: + helpLink.substring(2);
110: try {
111: InputStream in = new FileInputStream(srcHelpFileName);
112: OutputStream out = new FileOutputStream(dstHelpFileName);
113:
114: // Transfer bytes from in to out
115: byte[] buf = new byte[1024];
116: int len;
117: while ((len = in.read(buf)) > 0) {
118: out.write(buf, 0, len);
119: }
120: in.close();
121: out.close();
122: } catch (Exception e) {
123: //drop through
124: }
125: }
126:
127: protected String getReportTemplateName() {
128: return templateFolder + fileSeparator + reportName
129: + templateExtension;
130: }
131:
132: protected String getTocTemplateName() {
133: return templateFolder + fileSeparator
134: + UBTReportI.TABLE_OF_CONTENT + templateExtension;
135: }
136:
137: protected String getTocJasperFileName() {
138: return reportFolder + fileSeparator
139: + UBTReportI.TABLE_OF_CONTENT + fillExtension;
140: }
141:
142: protected String getJasperFileName() {
143: return reportFolder + fileSeparator + reportName
144: + fillExtension;
145: }
146:
147: protected String getJrprintFileName() {
148: return reportFolder + fileSeparator + reportName
149: + printExtension;
150: }
151:
152: protected String getHtmlFileName() {
153: return reportFolder + fileSeparator + reportName
154: + htmlExtension;
155: }
156:
157: protected String getLogo() {
158: return templateFolder + fileSeparator + UBTReportI.REPORT_LOGO;
159: }
160:
161: private synchronized void writeObject(java.io.ObjectOutputStream s)
162: throws IOException {
163: // Write out the length, threshold, loadfactor
164: s.defaultWriteObject();
165: }
166:
167: private void readObject(java.io.ObjectInputStream s)
168: throws IOException, ClassNotFoundException {
169: // Read in the length, threshold, and loadfactor
170: s.defaultReadObject();
171: }
172:
173: public void setLocale(Locale locale) {
174: this .clientLocale = locale;
175: }
176:
177: protected String getLDate() {
178: DateFormat formatter = new SimpleDateFormat(
179: "EEE MMM dd HH:mm:ss zzz yyyy", this .clientLocale);
180: formatter.setTimeZone(TimeZone.getDefault());
181: return formatter.format(new Date());
182: }
183:
184: }
|