001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: */
013: package org.pentaho.plugin.jfreereport.outputs;
014:
015: import java.io.File;
016: import java.io.IOException;
017: import java.io.OutputStream;
018:
019: import org.jfree.report.JFreeReport;
020: import org.jfree.report.ReportProcessingException;
021: import org.jfree.report.JFreeReportBoot;
022: import org.jfree.report.layout.output.YieldReportListener;
023: import org.jfree.report.modules.output.table.base.StreamReportProcessor;
024: import org.jfree.report.modules.output.table.html.AllItemsHtmlPrinter;
025: import org.jfree.report.modules.output.table.html.HtmlOutputProcessor;
026: import org.jfree.report.modules.output.table.html.HtmlPrinter;
027: import org.jfree.report.modules.output.table.html.StreamHtmlOutputProcessor;
028: import org.jfree.report.modules.output.table.html.URLRewriter;
029: import org.jfree.repository.ContentIOException;
030: import org.jfree.repository.ContentLocation;
031: import org.jfree.repository.DefaultNameGenerator;
032: import org.jfree.repository.NameGenerator;
033: import org.jfree.repository.file.FileRepository;
034: import org.jfree.repository.stream.StreamRepository;
035: import org.jfree.util.Configuration;
036: import org.pentaho.core.repository.IContentLocation;
037: import org.pentaho.core.repository.IContentRepository;
038: import org.pentaho.core.system.IApplicationContext;
039: import org.pentaho.core.system.PentahoSystem;
040: import org.pentaho.messages.Messages;
041: import org.pentaho.plugin.jfreereport.helper.PentahoURLRewriter;
042: import org.pentaho.plugin.jfreereport.repository.ReportContentRepository;
043:
044: /**
045: * Creation-Date: 07.07.2006, 20:42:17
046: *
047: * @author Thomas Morgner
048: */
049: public class JFreeReportHtmlComponent extends
050: AbstractGenerateStreamContentComponent {
051: private static final long serialVersionUID = -4296469329232291213L;
052:
053: private static final boolean DO_NOT_USE_THE_CONTENT_REPOSITORY = true;
054:
055: public JFreeReportHtmlComponent() {
056: }
057:
058: protected String getMimeType() {
059: return "text/html"; //$NON-NLS-1$
060: }
061:
062: protected String getExtension() {
063: return ".html"; //$NON-NLS-1$
064: }
065:
066: protected boolean performExport(final JFreeReport report,
067: final OutputStream outputStream) {
068: try {
069: IContentRepository contentRepository = null;
070: try {
071: contentRepository = PentahoSystem
072: .getContentRepository(getSession());
073: } catch (Throwable t) {
074: debug(
075: Messages
076: .getString("JFreeReportHtmlComponent.DEBUG_0044_PROCESSING_WITHOUT_CONTENT_REPOS"), t); //$NON-NLS-1$
077: }
078:
079: String contentHandlerPattern = getInputStringValue(REPORTHTML_CONTENTHANDLER);
080: if (contentHandlerPattern == null) {
081: final Configuration globalConfig = JFreeReportBoot
082: .getInstance().getGlobalConfig();
083: contentHandlerPattern = globalConfig
084: .getConfigProperty("org.pentaho.web.ContentHandler"); //$NON-NLS-1$
085: }
086:
087: final IApplicationContext ctx = PentahoSystem
088: .getApplicationContext();
089:
090: final URLRewriter rewriter;
091: final ContentLocation dataLocation;
092: final NameGenerator dataNameGenerator;
093: if (contentRepository == null
094: || DO_NOT_USE_THE_CONTENT_REPOSITORY) {
095: debug(Messages
096: .getString("JFreeReportHtmlComponent.DEBUG_0044_PROCESSING_WITHOUT_CONTENT_REPOS")); //$NON-NLS-1$
097: if (ctx != null) {
098: File dataDirectory = new File(ctx
099: .getFileOutputPath("system/tmp/"));//$NON-NLS-1$
100: if (dataDirectory.exists()
101: && dataDirectory.isDirectory() == false) {
102: dataDirectory = dataDirectory.getParentFile();
103: if (dataDirectory.isDirectory() == false) {
104: throw new ReportProcessingException(
105: Messages
106: .getErrorString(
107: "JFreeReportDirectoryComponent.ERROR_0001_INVALID_DIR", dataDirectory.getPath())); //$NON-NLS-1$
108: }
109: } else if (dataDirectory.exists() == false) {
110: dataDirectory.mkdirs();
111: }
112:
113: final FileRepository dataRepository = new FileRepository(
114: dataDirectory);
115: dataLocation = dataRepository.getRoot();
116: dataNameGenerator = new DefaultNameGenerator(
117: dataLocation);
118: rewriter = new PentahoURLRewriter(
119: contentHandlerPattern);
120: } else {
121: dataLocation = null;
122: dataNameGenerator = null;
123: rewriter = new PentahoURLRewriter(
124: contentHandlerPattern);
125: }
126: } else {
127: debug(Messages
128: .getString("JFreeReportHtmlComponent.DEBUG_045_PROCESSING_WITH_CONTENT_REPOS")); //$NON-NLS-1$
129: final String thePath = getSolutionName()
130: + "/" + getSolutionPath() + "/" + getSession().getId();//$NON-NLS-1$//$NON-NLS-2$
131: final IContentLocation pentahoContentLocation = contentRepository
132: .newContentLocation(thePath, getActionName(),
133: getActionTitle(), getSolutionPath(),
134: true);
135: // todo
136: final ReportContentRepository repository = new ReportContentRepository(
137: pentahoContentLocation, getActionName());
138: dataLocation = repository.getRoot();
139: dataNameGenerator = new DefaultNameGenerator(
140: dataLocation);
141: rewriter = new PentahoURLRewriter(contentHandlerPattern);
142: }
143:
144: final StreamRepository targetRepository = new StreamRepository(
145: null, outputStream);
146: final ContentLocation targetRoot = targetRepository
147: .getRoot();
148:
149: final HtmlOutputProcessor outputProcessor = new StreamHtmlOutputProcessor(
150: report.getConfiguration());
151: final HtmlPrinter printer = new AllItemsHtmlPrinter(report
152: .getResourceManager());
153: printer.setContentWriter(targetRoot,
154: new DefaultNameGenerator(targetRoot,
155: "index", "html"));//$NON-NLS-1$//$NON-NLS-2$
156: printer.setDataWriter(dataLocation, dataNameGenerator);
157: printer.setUrlRewriter(rewriter);
158: outputProcessor.setPrinter(printer);
159:
160: final StreamReportProcessor sp = new StreamReportProcessor(
161: report, outputProcessor);
162: final int yieldRate = getYieldRate();
163: if (yieldRate > 0) {
164: sp.addReportProgressListener(new YieldReportListener(
165: yieldRate));
166: }
167: sp.processReport();
168: sp.close();
169:
170: outputStream.flush();
171: close();
172: return true;
173: } catch (ReportProcessingException e) {
174: error(
175: Messages
176: .getString("JFreeReportHtmlComponent.ERROR_0046_FAILED_TO_PROCESS_REPORT"), e); //$NON-NLS-1$
177: return false;
178: } catch (IOException e) {
179: error(
180: Messages
181: .getString("JFreeReportHtmlComponent.ERROR_0046_FAILED_TO_PROCESS_REPORT"), e); //$NON-NLS-1$
182: return false;
183: } catch (ContentIOException e) {
184: error(
185: Messages
186: .getString("JFreeReportHtmlComponent.ERROR_0046_FAILED_TO_PROCESS_REPORT"), e); //$NON-NLS-1$
187: return false;
188: }
189: }
190: }
|