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.components;
014:
015: import java.util.Iterator;
016: import java.util.Set;
017:
018: import javax.swing.table.TableModel;
019:
020: import org.apache.commons.logging.Log;
021: import org.apache.commons.logging.LogFactory;
022: import org.jfree.report.JFreeReport;
023: import org.pentaho.commons.connection.IPentahoResultSet;
024: import org.pentaho.messages.Messages;
025: import org.pentaho.plugin.jfreereport.AbstractJFreeReportComponent;
026:
027: /**
028: * This sets the report parameters. This is another data preparation step and
029: * should be executed before the query is fired. The data-factory may depend on
030: * these parameters.
031: *
032: * @author Thomas Morgner
033: */
034: public class JFreeReportParametersComponent extends
035: AbstractJFreeReportComponent {
036: private static final long serialVersionUID = -3760254131944082953L;
037:
038: public JFreeReportParametersComponent() {
039: }
040:
041: protected boolean validateAction() {
042: return isDefinedInput(DATACOMPONENT_REPORTTEMP_OBJINPUT);
043: }
044:
045: protected boolean validateSystemSettings() {
046: return true;
047: }
048:
049: public void done() {
050:
051: }
052:
053: protected JFreeReport getReport() {
054: Object maybeReport = getInputValue(DATACOMPONENT_REPORTTEMP_OBJINPUT);
055: if (maybeReport instanceof JFreeReport) {
056: return (JFreeReport) maybeReport;
057: }
058: error(Messages
059: .getString("JFreeReportParametersComponent.ERROR_0033_NO_REPORT_BOUND") + maybeReport); //$NON-NLS-1$
060: return null;
061: }
062:
063: private boolean initReportInputs()
064: throws CloneNotSupportedException {
065: JFreeReport report = getReport();
066: if (report == null) {
067: error(Messages
068: .getString("JFreeReportParametersComponent.ERROR_0034_NO_REPORT_DEFINITION")); //$NON-NLS-1$
069: return false;
070: }
071:
072: final boolean privateCopy = getInputBooleanValue(
073: REPORTPARAMCOMPONENT_PRIVATEREPORT_OUTPUT, false);
074: if (privateCopy
075: && isDefinedOutput(DATACOMPONENT_REPORTTEMP_OBJINPUT)) {
076: report = (JFreeReport) report.clone();
077: }
078:
079: // Get input parameters, and set them as properties in the report
080: // object.
081: final Set paramNames = getInputNames();
082: final Iterator it = paramNames.iterator();
083: while (it.hasNext()) {
084: String paramName = (String) it.next();
085: Object paramValue = getInputValue(paramName);
086: if (paramValue == null || "".equals(paramValue)) { //$NON-NLS-1$
087: continue;
088: }
089:
090: // we filter some well-known bad-guys. It is dangerous to have the
091: // report-object (the parsed JFreeReport object), the "report-data"
092: // (the tablemodel) or the "data" reference copied to the report.
093: // also dangerous are result sets and table models.
094:
095: if (paramValue instanceof IPentahoResultSet) {
096: continue;
097: }
098: if (paramValue instanceof TableModel) {
099: continue;
100: }
101: if (DATACOMPONENT_REPORTTEMP_OBJINPUT.equals(paramName)) {
102: continue;
103: }
104: if (DATACOMPONENT_REPORTTEMP_DATAFACTORY.equals(paramName)) {
105: continue;
106: }
107: if (DATACOMPONENT_REPORTTEMP_DATAINPUT.equals(paramName)) {
108: continue;
109: }
110: if (DATACOMPONENT_DATAINPUT.equals(paramName)) {
111: continue;
112: }
113:
114: if (paramValue instanceof Object[]) {
115: Object values[] = (Object[]) paramValue;
116: StringBuffer valuesBuffer = new StringBuffer();
117: // TODO support non-string items
118: for (int i = 0; i < values.length; i++) {
119: if (i == 0) {
120: valuesBuffer.append(values[i].toString());
121: } else {
122: valuesBuffer.append(',').append(
123: values[i].toString());
124: }
125: }
126: report.setProperty(paramName, valuesBuffer.toString());
127: } else {
128: report.setProperty(paramName, paramValue);
129: }
130: }
131:
132: if (privateCopy
133: && isDefinedInput(DATACOMPONENT_REPORTTEMP_OBJINPUT)) {
134: addTempParameterObject(DATACOMPONENT_REPORTTEMP_OBJINPUT,
135: report);
136: }
137: return true;
138: }
139:
140: protected boolean executeAction() throws Throwable {
141: return initReportInputs();
142: }
143:
144: public boolean init() {
145: return true;
146: }
147:
148: public Log getLogger() {
149: return LogFactory.getLog(JFreeReportParametersComponent.class);
150: }
151: }
|