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: * SingleValueQueryFunction.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.function.sys;
030:
031: import javax.swing.table.TableModel;
032:
033: import org.jfree.report.DataFactory;
034: import org.jfree.report.ParameterMapping;
035: import org.jfree.report.event.ReportEvent;
036: import org.jfree.report.function.ColumnAggregationExpression;
037: import org.jfree.report.function.Function;
038: import org.jfree.report.states.datarow.ImportedVariablesDataRow;
039: import org.jfree.util.Log;
040:
041: /**
042: * Fires a query against the data-source and returns a single value. The current data-row is used as source for the
043: * parameters of the query. The parameters that should be used must be declared as list of fields.
044: *
045: * @author Thomas Morgner
046: */
047: public class SingleValueQueryFunction extends
048: ColumnAggregationExpression implements Function {
049: /**
050: * The last value queried by the function.
051: */
052: private transient Object value;
053: /**
054: * The name of the query that should be executed.
055: */
056: private String query;
057: /**
058: * The name of the column of the query's result-table that should be used. If undefined, the first column is used.
059: */
060: private String resultColumn;
061:
062: /**
063: * Default Constructor.
064: */
065: public SingleValueQueryFunction() {
066: }
067:
068: /**
069: * Returns the name of the result-column. The result-column specified the name of the column of the query's
070: * result-table that should be used. If undefined, the first column is used.
071: *
072: * @return the result column name.
073: */
074: public String getResultColumn() {
075: return resultColumn;
076: }
077:
078: /**
079: * Defines the name of the result-column. The result-column specified the name of the column of the query's
080: * result-table that should be used. If undefined, the first column is used.
081: *
082: * @param resultColumn the result column name.
083: */
084: public void setResultColumn(final String resultColumn) {
085: this .resultColumn = resultColumn;
086: }
087:
088: /**
089: * Returns the query name.
090: *
091: * @return the query name.
092: */
093: public String getQuery() {
094: return query;
095: }
096:
097: /**
098: * Defines the query name.
099: *
100: * @param query the query name.
101: */
102: public void setQuery(final String query) {
103: this .query = query;
104: }
105:
106: /**
107: * Performs the query by collecting the data from the datarow and executing the query.
108: *
109: * @param event the report event that triggered the query call.
110: */
111: private void performQuery(final ReportEvent event) {
112: if (query == null) {
113: return;
114: }
115: value = null;
116: try {
117: final DataFactory dataFactory = event.getState()
118: .getFlowController().getDataFactory();
119: final String[] fields = getField();
120: final int length = fields.length;
121: final ParameterMapping[] mappings = new ParameterMapping[length];
122: for (int i = 0; i < length; i++) {
123: mappings[i] = new ParameterMapping(fields[i], fields[i]);
124: }
125:
126: final TableModel tableModel = dataFactory
127: .queryData(query, new ImportedVariablesDataRow(
128: getDataRow(), mappings));
129: if (tableModel == null) {
130: return;
131: }
132: final int columnCount = tableModel.getColumnCount();
133: if (tableModel.getRowCount() == 0 || columnCount == 0) {
134: return;
135: }
136: if (resultColumn == null) {
137: value = tableModel.getValueAt(0, 0);
138: return;
139: }
140: for (int i = 0; i < columnCount; i++) {
141: if (resultColumn.equals(tableModel.getColumnName(i))) {
142: value = tableModel.getValueAt(0, i);
143: return;
144: }
145: }
146: // do nothing ..
147: } catch (Exception e) {
148: Log
149: .warn(
150: "SingleValueQueryFunction: Failed to perform query",
151: e);
152: }
153: }
154:
155: /**
156: * Calls the perform-query method.
157: *
158: * @param event the event.
159: */
160: public void reportInitialized(final ReportEvent event) {
161: performQuery(event);
162: }
163:
164: /**
165: * Calls the perform-query method.
166: *
167: * @param event the event.
168: */
169: public void reportStarted(final ReportEvent event) {
170: performQuery(event);
171: }
172:
173: /**
174: * Calls the perform-query method.
175: *
176: * @param event the event.
177: */
178: public void reportFinished(final ReportEvent event) {
179: performQuery(event);
180: }
181:
182: /**
183: * Calls the perform-query method.
184: *
185: * @param event the event.
186: */
187: public void reportDone(final ReportEvent event) {
188: performQuery(event);
189: }
190:
191: /**
192: * Calls the perform-query method.
193: *
194: * @param event the event.
195: */
196: public void groupStarted(final ReportEvent event) {
197: performQuery(event);
198: }
199:
200: /**
201: * Calls the perform-query method.
202: *
203: * @param event the event.
204: */
205: public void groupFinished(final ReportEvent event) {
206: performQuery(event);
207: }
208:
209: /**
210: * Calls the perform-query method.
211: *
212: * @param event the event.
213: */
214: public void itemsStarted(final ReportEvent event) {
215: performQuery(event);
216: }
217:
218: /**
219: * Calls the perform-query method.
220: *
221: * @param event the event.
222: */
223: public void itemsFinished(final ReportEvent event) {
224: performQuery(event);
225: }
226:
227: /**
228: * Calls the perform-query method.
229: *
230: * @param event the event.
231: */
232: public void itemsAdvanced(final ReportEvent event) {
233: performQuery(event);
234: }
235:
236: /**
237: * Returns the query result.
238: *
239: * @return the query result.
240: */
241: public Object getValue() {
242: return value;
243: }
244: }
|