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.server;
006:
007: import com.sun.portal.ubt.report.data.MinerCollection;
008: import com.sun.portal.ubt.report.data.DataMiner;
009: import com.sun.portal.ubt.report.data.file.FileDataMiner;
010: import com.sun.portal.ubt.report.data.file.parser.BadDelimiterException;
011: import com.sun.portal.ubt.report.view.IllegalMinerException;
012: import com.sun.portal.ubt.report.view.IllegalDataSourceException;
013: import com.sun.portal.ubt.report.view.report.UBTReportI;
014: import com.sun.portal.ubt.report.view.report.UBTReportsFactory;
015: import com.sun.portal.ubt.report.view.report.SupportedReports;
016:
017: import java.io.File;
018: import java.io.IOException;
019: import java.lang.reflect.InvocationTargetException;
020: import java.util.TreeMap;
021: import java.util.Vector;
022: import java.util.Iterator;
023: import java.util.Locale;
024:
025: import net.sf.jasperreports.engine.JasperPrint;
026: import net.sf.jasperreports.engine.JRException;
027:
028: /**
029: * This class generates reports in jrprint format.
030: */
031: public class UBTReportGenerator {
032: String templateDir;
033: String destinationDir;
034: String portalID;
035: Locale clientLocale;
036:
037: public UBTReportGenerator(String templateDir,
038: String destinationDir, String portalID, Locale locale) {
039: this .templateDir = templateDir;
040: this .destinationDir = destinationDir;
041: this .portalID = portalID;
042: this .clientLocale = locale;
043: }
044:
045: public UBTReportGenerator(String templateDir, String portalID,
046: Locale locale) {
047: this .templateDir = templateDir;
048: this .destinationDir = templateDir;
049: this .portalID = portalID;
050: this .clientLocale = locale;
051: }
052:
053: //Should be used for aggregated report generation
054: public JasperPrint[] generateReport(UBTReportI[] reports)
055: throws UBTReportGeneratorException {
056: JasperPrint[] reportPrint = new JasperPrint[reports.length];
057: compileAndReport(reportPrint, reports);
058: return reportPrint;
059: }
060:
061: //Should be used only for localhost/one host for CLI
062: public JasperPrint[] generateReport(String reportName,
063: File[] logFiles) throws UBTReportGeneratorException {
064: JasperPrint[] reportPrint = new JasperPrint[reportName == null ? SupportedReports
065: .get().length
066: : 1];
067: UBTReportI[] reports = makeUBTReportIArray(getUBTReportData(
068: reportName, logFiles));
069: compileAndReport(reportPrint, reports);
070: return reportPrint;
071: }
072:
073: //Should be used only for localhost/one host for consoles
074: public JasperPrint[] generateReport(String[] reportNames,
075: File[] logFiles) throws UBTReportGeneratorException {
076: JasperPrint[] reportPrint = new JasperPrint[reportNames == null ? SupportedReports
077: .get().length
078: : reportNames.length];
079: UBTReportI[] reports = makeUBTReportIArray(getUBTReportData(
080: reportNames, logFiles));
081: compileAndReport(reportPrint, reports);
082: return reportPrint;
083: }
084:
085: protected UBTReportI[] makeUBTReportIArray(Object o) {
086: TreeMap reportDataMap = (TreeMap) o;
087: UBTReportI[] reportI = new UBTReportI[reportDataMap.size()];
088: Iterator reportNamesI = reportDataMap.keySet().iterator();
089: int i = 0;
090: while (reportNamesI.hasNext()) {
091: String reportName = (String) reportNamesI.next();
092: Vector data = (Vector) reportDataMap.get(reportName);
093: reportI[i] = getReportI(reportName, data);
094: i++;
095: }
096: return reportI;
097: }
098:
099: protected UBTReportI getReportI(String reportName, Vector data) {
100: UBTReportI reportI = UBTReportsFactory.getReport(reportName);
101: reportI.setTemplateFolder(templateDir);
102: reportI.setReportFolder(destinationDir);
103: reportI.setLocale(this .clientLocale);
104: DataMiner[] dataMiners = reportI.getDataMiner();
105: for (int i = 0; i < dataMiners.length; i++) {
106: dataMiners[i].setData(data.get(i));
107: dataMiners[i].setPortalID(portalID);
108: }
109: return reportI;
110: }
111:
112: private void compileAndReport(JasperPrint[] reportPrint,
113: UBTReportI[] reports) throws UBTReportGeneratorException {
114: for (int i = 0; i < reports.length; i++) {
115: try {
116: reports[i].compile();
117: reportPrint[i] = reports[i].getReport();
118: } catch (IllegalMinerException e) {
119: throw new UBTReportGeneratorException(e.getMessage());
120: } catch (JRException e) {
121: throw new UBTReportGeneratorException(e.getMessage());
122: } catch (IllegalDataSourceException e) {
123: throw new UBTReportGeneratorException(e.getMessage());
124: }
125: }
126: }
127:
128: private void mine(UBTReportI[] reports, File[] logFiles,
129: String[] reportsToBeGenerated)
130: throws UBTReportGeneratorException {
131: MinerCollection minerCollection = new MinerCollection();
132: for (int i = 0; i < reportsToBeGenerated.length; i++) {
133: reports[i] = UBTReportsFactory
134: .getReport(reportsToBeGenerated[i]);
135: reports[i].setReportFolder(this .destinationDir);
136: reports[i].setTemplateFolder(this .templateDir);
137: minerCollection.add(reports[i].getDataMiner());
138: }
139: FileDataMiner dataMiner = new FileDataMiner();
140: dataMiner.setPortalID(this .portalID);
141: dataMiner.setMinerCollection(minerCollection);
142: for (int i = 0; i < logFiles.length; i++) {
143: dataMiner.setInputFile(logFiles[i]);
144: try {
145: dataMiner.mine(null);
146: } catch (IOException e) {
147: throw new UBTReportGeneratorException(e.getMessage());
148: } catch (IllegalAccessException e) {
149: throw new UBTReportGeneratorException(e.getMessage());
150: } catch (InvocationTargetException e) {
151: throw new UBTReportGeneratorException(e.getMessage());
152: } catch (BadDelimiterException e) {
153: throw new UBTReportGeneratorException(e.getMessage());
154: } catch (Exception e) {
155: throw new UBTReportGeneratorException(e.getMessage());
156: }
157: }
158: }
159:
160: //should be used for a set of reports
161: public TreeMap getUBTReportData(String[] reportNames,
162: File[] logFiles) throws UBTReportGeneratorException {
163: String[] reportsToBeGenerated = reportNames;
164: if (reportNames == null) {
165: reportsToBeGenerated = SupportedReports.get();
166: }
167: UBTReportI[] reports = new UBTReportI[reportsToBeGenerated.length];
168: mine(reports, logFiles, reportsToBeGenerated);
169: TreeMap reportMinerMap = new TreeMap();
170: convertReportItoMap(reportMinerMap, reports);
171: return reportMinerMap;
172: }
173:
174: private void convertReportItoMap(TreeMap reportMinerMap,
175: UBTReportI[] reports) {
176: for (int i = 0; i < reports.length; i++) {
177: reportMinerMap.put(reports[i].getReportName(),
178: getVectorOfData(reports[i].getDataMiner()));
179: }
180: }
181:
182: private Vector getVectorOfData(DataMiner[] dataMiner) {
183: Vector v = new Vector(dataMiner.length);
184: for (int i = 0; i < dataMiner.length; i++) {
185: v.add(i, dataMiner[i].getData());
186: }
187: return v;
188: }
189:
190: //should be user for one or all
191: public TreeMap getUBTReportData(String reportName, File[] logFiles)
192: throws UBTReportGeneratorException {
193: if (logFiles == null || logFiles.length == 0)
194: throw new UBTReportGeneratorException("No log files");
195: String[] reportsToBeGenerated;
196: if (reportName == null) {
197: reportsToBeGenerated = SupportedReports.get();
198: } else {
199: if (!SupportedReports.isSupported(reportName))
200: throw new UBTReportGeneratorException(
201: "Unsupported report");
202: reportsToBeGenerated = new String[1];
203: reportsToBeGenerated[0] = reportName;
204: }
205: UBTReportI[] reports = new UBTReportI[reportsToBeGenerated.length];
206: mine(reports, logFiles, reportsToBeGenerated);
207: TreeMap reportMinerMap = new TreeMap();
208: convertReportItoMap(reportMinerMap, reports);
209: return reportMinerMap;
210: }
211:
212: public static void main(String[] arsg)
213: throws UBTReportGeneratorException {
214: UBTReportGenerator repGen = new UBTReportGenerator(
215: "/space/ws/trunk/ps/ubt/src/com/sun/portal/ubt/report/templates",
216: "/tmp", "EngPortal", Locale.getDefault());
217: repGen.generateReport((String) null, new File[] { new File(
218: "/space/ws/trunk/ps/ubt/sample/ubt.log") });
219: }
220: }
|