001: /**
002: * ===========================================
003: * JFreeReport : a free Java reporting library
004: * ===========================================
005: *
006: * Project Info: http://reporting.pentaho.org/
007: *
008: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
009: *
010: * This library is free software; you can redistribute it and/or modify it under the terms
011: * of the GNU Lesser General Public License as published by the Free Software Foundation;
012: * either version 2.1 of the License, or (at your option) any later version.
013: *
014: * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
015: * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
016: * See the GNU Lesser General Public License for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License along with this
019: * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
020: * Boston, MA 02111-1307, USA.
021: *
022: * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
023: * in the United States and other countries.]
024: *
025: * ------------
026: * DataRowDataSource.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.filter;
030:
031: import org.jfree.report.function.ExpressionRuntime;
032: import org.jfree.report.function.FormulaExpression;
033: import org.jfree.util.Log;
034:
035: /**
036: * A DataSource that can access values from the 'data-row'. The data-row contains all values from the current row of the
037: * report's <code>TableModel</code>, plus the current values of the defined expressions and functions for the report.
038: * <p/>
039: * The DataRowDataSource can either query the data-row directly using the specified field name or it can evaluate a
040: * given formula (which must be compatible to the OpenFormula specifications) to compute the value.
041: * <p/>
042: * Fields and formulas are mutually exclusive; defining a field name autmatically undefines the formula and vice versa.
043: *
044: * @author Thomas Morgner
045: * @see org.jfree.report.DataRow
046: */
047: public class DataRowDataSource implements DataSource {
048: /**
049: * The field name that should be queried.
050: */
051: private String field;
052:
053: /**
054: * The formula-expression that computes the result value, if no field is given.
055: */
056: private FormulaExpression valueExpression;
057:
058: /**
059: * Default constructor.
060: * <p/>
061: * The expression name is empty ("", not null), the value initially null.
062: */
063: public DataRowDataSource() {
064: this (null);
065: }
066:
067: /**
068: * Constructs a new data source.
069: *
070: * @param column the name of the field, function or expression in the data-row.
071: */
072: public DataRowDataSource(final String column) {
073: this .field = column;
074: }
075:
076: /**
077: * Returns the data source column name.
078: *
079: * @return the column name.
080: */
081: public String getDataSourceColumnName() {
082: return field;
083: }
084:
085: /**
086: * Defines the name of the column in the datarow to be queried.
087: *
088: * @param dataSourceColumnName the name of the column in the datarow to be queried.
089: * @throws NullPointerException if the name is <code>null</code>.
090: * @see org.jfree.report.DataRow#get
091: */
092: public void setDataSourceColumnName(
093: final String dataSourceColumnName) {
094: if (dataSourceColumnName == null) {
095: throw new NullPointerException(
096: "The datasource column name must not be null.");
097: }
098: this .field = dataSourceColumnName;
099: if (valueExpression != null) {
100: this .valueExpression.setFormula(null);
101: }
102: }
103:
104: /**
105: * Returns the formula used to compute the value of the data source.
106: *
107: * @return the formula.
108: */
109: public String getFormula() {
110: if (valueExpression == null) {
111: return null;
112: }
113: return valueExpression.getFormula();
114: }
115:
116: /**
117: * Defines the formula used to compute the value of this data source.
118: *
119: * @param formula the formula for the data source.
120: */
121: public void setFormula(final String formula) {
122: if (formula == null) {
123: throw new NullPointerException("Formula must not be null.");
124: }
125:
126: this .field = null;
127: if (valueExpression == null) {
128: valueExpression = new FormulaExpression();
129: }
130: this .valueExpression.setFormula(formula);
131: if ("field".equals(valueExpression.getFormulaNamespace())) {
132: Log
133: .warn("Encountered formula with 'field' prefix. Direct access to field-data should not be done using a formula. Auto-Fixing.");
134: this .field = valueExpression.getFormulaExpression();
135: this .valueExpression.setFormula(null);
136: }
137: }
138:
139: /**
140: * Returns the current value of the data source, obtained from a particular column in the data-row.
141: *
142: * @param runtime the expression runtime that is used to evaluate formulas and expressions when computing the value of
143: * this filter.
144: * @return the value.
145: */
146: public Object getValue(final ExpressionRuntime runtime) {
147: if (runtime == null) {
148: return null;
149: }
150:
151: if (field != null) {
152: return runtime.getDataRow().get(field);
153: }
154: if (valueExpression == null) {
155: return null;
156: }
157:
158: valueExpression.setRuntime(runtime);
159: try {
160: return valueExpression.getValue();
161: } catch (Exception e) {
162: // ignore ..
163: return null;
164: } finally {
165: valueExpression.setRuntime(null);
166: }
167: }
168:
169: /**
170: * Clones the data source. A previously registered report definition is not inherited to the clone.
171: *
172: * @return a clone.
173: * @throws CloneNotSupportedException if the cloning is not supported.
174: */
175: public Object clone() throws CloneNotSupportedException {
176: final DataRowDataSource drs = (DataRowDataSource) super .clone();
177: if (valueExpression != null) {
178: drs.valueExpression = (FormulaExpression) valueExpression
179: .clone();
180: }
181: return drs;
182: }
183: }
|