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.OutputStream;
016:
017: import org.jfree.report.JFreeReport;
018: import org.pentaho.core.repository.IContentItem;
019: import org.pentaho.messages.Messages;
020:
021: /**
022: * Creation-Date: 07.07.2006, 20:50:22
023: *
024: * @author Thomas Morgner
025: */
026: public abstract class AbstractGenerateStreamContentComponent extends
027: AbstractGenerateContentComponent {
028:
029: private IContentItem contentItem = null;
030:
031: protected AbstractGenerateStreamContentComponent() {
032: }
033:
034: protected boolean validateAction() {
035: if (super .validateAction() == false) {
036: return false;
037: }
038:
039: if (isDefinedOutput(REPORTGENERATESTREAM_REPORT_OUTPUT)) {
040: return true;
041: }
042:
043: if (getOutputNames().size() == 1) {
044: return true;
045: }
046:
047: if (getOutputNames().size() == 0) {
048: warn(Messages.getString("Base.WARN_NO_OUTPUT_STREAM")); //$NON-NLS-1$
049: return true;
050: }
051:
052: warn(Messages
053: .getString("AbstractGenerateStreamContentComponent.ERROR_0038_NO_OUTPUT_DEFINED")); //$NON-NLS-1$
054: return false;
055: }
056:
057: protected abstract String getMimeType();
058:
059: protected abstract String getExtension();
060:
061: protected final boolean performExport(final JFreeReport report) {
062: OutputStream outputStream = createOutputStream();
063: if (outputStream == null) {
064: // We could not get an output stream for the content
065: error(Messages
066: .getErrorString("JFreeReport.ERROR_0008_INVALID_OUTPUT_STREAM")); //$NON-NLS-1$
067: return false;
068: }
069:
070: return performExport(report, outputStream);
071: }
072:
073: protected final void close() {
074: if (contentItem != null) {
075: contentItem.closeOutputStream();
076: }
077: }
078:
079: protected abstract boolean performExport(final JFreeReport report,
080: final OutputStream outputStream);
081:
082: protected OutputStream createOutputStream() {
083: // Try to get the output from the action-sequence document.
084: final String mimeType = getMimeType();
085:
086: if (isDefinedOutput(REPORTGENERATESTREAM_REPORT_OUTPUT)) {
087: contentItem = getOutputItem(
088: REPORTGENERATESTREAM_REPORT_OUTPUT, mimeType,
089: getExtension());
090: try {
091: contentItem.setMimeType(mimeType);
092: return contentItem.getOutputStream(getActionName());
093: } catch (Exception e) {
094: return null;
095: }
096: } else if (getOutputNames().size() == 1) {
097: String outputName = (String) getOutputNames().iterator()
098: .next();
099: contentItem = getOutputContentItem(outputName, mimeType);
100: try {
101: contentItem.setMimeType(mimeType);
102: return contentItem.getOutputStream(getActionName());
103: } catch (Exception e) {
104: return null;
105: }
106: }
107: if (getOutputNames().size() == 0) {
108: // There was no output in the action-sequence document, so make a
109: // default
110: // outputStream.
111: final OutputStream outputStream = getDefaultOutputStream(mimeType);
112: return outputStream;
113: }
114:
115: return null;
116: }
117:
118: protected IContentItem getContentItem() {
119: return contentItem;
120: }
121:
122: }
|