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: * @created Jul 07, 2006
014: * @author Thomas Morgner
015: */
016: package org.pentaho.plugin.jfreereport.outputs;
017:
018: import java.io.File;
019:
020: import org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022: import org.jfree.io.IOUtils;
023: import org.jfree.report.JFreeReport;
024: import org.jfree.report.ResourceBundleFactory;
025: import org.pentaho.messages.Messages;
026: import org.pentaho.plugin.jfreereport.AbstractJFreeReportComponent;
027: import org.pentaho.plugin.jfreereport.helper.PentahoResourceBundleFactory;
028: import org.pentaho.plugin.jfreereport.helper.PentahoTableDataFactory;
029:
030: /**
031: * The base class for all content generating components. This class adds the report-data to the JFreeReport object. If
032: * requested, it creates a private copy of the report before doing any work - cloning is faster than parsing.
033: * <p/>
034: * Sub-Actions of this component are usually the last step in the report processing.
035: *
036: * @author Thomas Morgner
037: */
038: public abstract class AbstractGenerateContentComponent extends
039: AbstractJFreeReportComponent {
040: protected AbstractGenerateContentComponent() {
041: }
042:
043: protected boolean validateAction() {
044: if (isDefinedInput(DATACOMPONENT_REPORTTEMP_OBJINPUT) == false) {
045: warn(Messages
046: .getString("AbstractGenerateContentComponent.ERROR_0038_NO_REPORT_OBJECT_INPUT")); //$NON-NLS-1$
047: return false;
048: }
049:
050: if (isDefinedInput(DATACOMPONENT_REPORTTEMP_DATAINPUT) == false
051: && isDefinedInput(DATACOMPONENT_REPORTTEMP_DATAFACTORY) == false) {
052: warn(Messages
053: .getString("AbstractGenerateContentComponent.ERROR_0039_NO_REPORT_DATA_PARAMETER")); //$NON-NLS-1$
054: return false;
055: }
056:
057: if (isDefinedInput(REPORTGENERATE_YIELDRATE)) {
058: final Object inputValue = getInputValue(REPORTGENERATE_YIELDRATE);
059: if (inputValue instanceof Number) {
060: final Number n = (Number) inputValue;
061: if (n.intValue() < 0) {
062: warn(Messages
063: .getString("AbstractGenerateContentComponent.ERROR_0040_YIELD_RATE_POSITIVE")); //$NON-NLS-1$
064: }
065: } else {
066: warn(Messages
067: .getString("AbstractGenerateContentComponent.ERROR_0041_YIELD_RATE_NUMERIC")); //$NON-NLS-1$
068: return false;
069: }
070: }
071: if (isDefinedInput(REPORTGENERATE_PRIORITYINPUT)) {
072: final String inputValue = getInputStringValue(REPORTGENERATE_PRIORITYINPUT);
073: if (REPORTGENERATE_PRIORITYNORMAL.equals(inputValue) == false
074: && REPORTGENERATE_PRIORITYLOWER.equals(inputValue) == false
075: && REPORTGENERATE_PRIORITYLOWEST.equals(inputValue) == false) {
076: warn(Messages
077: .getString("AbstractGenerateContentComponent.ERROR_0042_PRIORITY_MUST_BE")); //$NON-NLS-1$
078: }
079: }
080: return true;
081: }
082:
083: protected JFreeReport getReport() {
084: final Object maybeJFreeReport = getInputValue(DATACOMPONENT_REPORTTEMP_OBJINPUT);
085: if (maybeJFreeReport instanceof JFreeReport) {
086: return (JFreeReport) maybeJFreeReport;
087: }
088: return null;
089: }
090:
091: /**
092: * This method finds the table data factory in the inputs and returns the object
093: *
094: * @return table factory instance
095: */
096: private PentahoTableDataFactory getTableDataFactory() {
097: if (isDefinedInput(DATACOMPONENT_REPORTTEMP_DATAFACTORY)) {
098: final Object maybeTableDataFactory = getInputValue(DATACOMPONENT_REPORTTEMP_DATAFACTORY);
099: if (maybeTableDataFactory instanceof PentahoTableDataFactory) {
100: return (PentahoTableDataFactory) maybeTableDataFactory;
101: }
102: }
103: return null;
104: }
105:
106: protected boolean validateSystemSettings() {
107: return true;
108: }
109:
110: protected boolean executeAction() throws Throwable {
111: JFreeReport report = getReport();
112: if (report == null) {
113: warn(Messages
114: .getString("AbstractGenerateContentComponent.ERROR_0043_NO_REPORT_FOR_ACTION")); //$NON-NLS-1$
115: return false;
116: }
117:
118: applyThreadPriority();
119:
120: final boolean privateCopy = getInputBooleanValue(
121: REPORTPARAMCOMPONENT_PRIVATEREPORT_OUTPUT, false);
122: if (privateCopy
123: && isDefinedOutput(DATACOMPONENT_REPORTTEMP_OBJINPUT)) {
124: report = (JFreeReport) report.clone();
125: }
126:
127: // this might be invalid in case the action is contained in a sub-directory.
128: final String baseName = IOUtils.getInstance()
129: .stripFileExtension(getActionName());
130: final String path = getSolutionName() + File.separator
131: + getSolutionPath();
132: final PentahoResourceBundleFactory bundleFactory = new PentahoResourceBundleFactory(
133: path, baseName, getSession());
134: report.setResourceBundleFactory(bundleFactory);
135: // set the default resourcebundle. This allows users to override the
136: // resource-bundle in case they want to keep common strings in a common
137: // collection.
138: report
139: .getReportConfiguration()
140: .setConfigProperty(
141: ResourceBundleFactory.DEFAULT_RESOURCE_BUNDLE_CONFIG_KEY,
142: baseName);
143:
144: // set the reports data factory, which it uses to get access to the report data.
145: final PentahoTableDataFactory factory = getTableDataFactory();
146: if (factory != null) {
147: report.setDataFactory(factory);
148: }
149:
150: return performExport(report);
151: }
152:
153: private void applyThreadPriority() {
154: if (isDefinedInput(REPORTGENERATE_PRIORITYINPUT)) {
155: try {
156: final String inputValue = getInputStringValue(REPORTGENERATE_PRIORITYINPUT);
157: if (REPORTGENERATE_PRIORITYLOWER.equals(inputValue)) {
158: final int priority = Math.max(Thread
159: .currentThread().getPriority() - 1, 1);
160: Thread.currentThread().setPriority(priority);
161: } else if (REPORTGENERATE_PRIORITYLOWEST
162: .equals(inputValue)) {
163: Thread.currentThread().setPriority(1);
164: }
165: } catch (Exception e) {
166: // Non fatal exception.
167: warn(Messages
168: .getString("AbstractGenerateContentComponent.ERROR_0044_UNABLE_T0_SET_THREAD_PRIORITY")); //$NON-NLS-1$
169: }
170: }
171: }
172:
173: protected abstract boolean performExport(final JFreeReport report);
174:
175: public void done() {
176:
177: }
178:
179: public boolean init() {
180: return true;
181: }
182:
183: public Log getLogger() {
184: return LogFactory.getLog(getClass());
185: }
186:
187: protected int getYieldRate() {
188: if (isDefinedInput(REPORTGENERATE_YIELDRATE)) {
189: final Object inputValue = getInputValue(REPORTGENERATE_YIELDRATE);
190: if (inputValue instanceof Number) {
191: Number n = (Number) inputValue;
192: if (n.intValue() < 1) {
193: return 0;
194: }
195: return n.intValue();
196: }
197: }
198: return 0;
199: }
200:
201: }
|