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: * SQLReportDataFactory.java
027: * ------------
028: * (C) Copyright 2001-2007, by Object Refinery Ltd, Pentaho Corporation and Contributors.
029: */package org.jfree.report.modules.misc.datafactory.sql;
030:
031: import java.sql.Connection;
032: import java.util.HashMap;
033: import javax.swing.table.TableModel;
034:
035: import org.jfree.report.DataRow;
036: import org.jfree.report.ReportDataFactoryException;
037:
038: /**
039: * Creation-Date: 19.02.2006, 17:37:33
040: *
041: * @author Thomas Morgner
042: */
043: public class SQLReportDataFactory extends SimpleSQLReportDataFactory {
044: private HashMap querymappings;
045:
046: public SQLReportDataFactory(final Connection connection) {
047: super (connection);
048: querymappings = new HashMap();
049: }
050:
051: public SQLReportDataFactory(
052: final ConnectionProvider connectionProvider) {
053: super (connectionProvider);
054: querymappings = new HashMap();
055: }
056:
057: public void setQuery(final String name, final String queryString) {
058: if (queryString == null) {
059: querymappings.remove(name);
060: } else {
061: querymappings.put(name, queryString);
062: }
063: }
064:
065: /**
066: * Queries a datasource. The string 'query' defines the name of the query. The
067: * Parameterset given here may contain more data than actually needed.
068: * <p/>
069: * The dataset may change between two calls, do not assume anything!
070: *
071: * @param query
072: * @param parameters
073: * @return
074: */
075: public synchronized TableModel queryData(final String query,
076: final DataRow parameters) throws ReportDataFactoryException {
077: if (query == null) {
078: throw new NullPointerException("Query is null."); //$NON-NLS-1$
079: }
080: final String realQuery = getQuery(query);
081: if (realQuery == null) {
082: throw new ReportDataFactoryException(
083: "Query '" + query + "' is not recognized."); //$NON-NLS-1$ //$NON-NLS-2$
084: }
085: return super .queryData(realQuery, parameters);
086: }
087:
088: public String getQuery(final String name) {
089: return (String) querymappings.get(name);
090: }
091:
092: public String[] getQueryNames() {
093: return (String[]) querymappings.keySet().toArray(
094: new String[querymappings.size()]);
095: }
096:
097: public Object clone() throws CloneNotSupportedException {
098: final SQLReportDataFactory dataFactory = (SQLReportDataFactory) super
099: .clone();
100: dataFactory.querymappings = (HashMap) querymappings.clone();
101: return dataFactory;
102: }
103: }
|