Source for org.jfree.report.modules.data.sql.SQLReportDataFactory

   1: /**
   2:  * ========================================
   3:  * JFreeReport : a free Java report library
   4:  * ========================================
   5:  *
   6:  * Project Info:  http://reporting.pentaho.org/
   7:  *
   8:  * (C) Copyright 2000-2007, by Object Refinery Limited, Pentaho Corporation and Contributors.
   9:  *
  10:  * This library is free software; you can redistribute it and/or modify it under the terms
  11:  * of the GNU Lesser General Public License as published by the Free Software Foundation;
  12:  * either version 2.1 of the License, or (at your option) any later version.
  13:  *
  14:  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  15:  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16:  * See the GNU Lesser General Public License for more details.
  17:  *
  18:  * You should have received a copy of the GNU Lesser General Public License along with this
  19:  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  20:  * Boston, MA 02111-1307, USA.
  21:  *
  22:  * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  23:  * in the United States and other countries.]
  24:  *
  25:  * ------------
  26:  * $Id: SQLReportDataFactory.java 3525 2007-10-16 11:43:48Z tmorgner $
  27:  * ------------
  28:  * (C) Copyright 2000-2005, by Object Refinery Limited.
  29:  * (C) Copyright 2005-2007, by Pentaho Corporation.
  30:  */
  31: package org.jfree.report.modules.data.sql;
  32: 
  33: import java.sql.Connection;
  34: import java.util.HashMap;
  35: 
  36: import org.jfree.report.DataSet;
  37: import org.jfree.report.ReportData;
  38: import org.jfree.report.ReportDataFactoryException;
  39: 
  40: /**
  41:  * Creation-Date: 19.02.2006, 17:37:33
  42:  *
  43:  * @author Thomas Morgner
  44:  */
  45: public class SQLReportDataFactory extends SimpleSQLReportDataFactory
  46: {
  47:   private HashMap querymappings;
  48: 
  49:   public SQLReportDataFactory(final Connection connection)
  50:   {
  51:     super(connection);
  52:     querymappings = new HashMap();
  53:   }
  54: 
  55: 
  56:   public SQLReportDataFactory(final ConnectionProvider connectionProvider)
  57:   {
  58:     super(connectionProvider);
  59:     querymappings = new HashMap();
  60:   }
  61: 
  62:   public void setQuery(final String name, final String queryString)
  63:   {
  64:     if (queryString == null)
  65:     {
  66:       querymappings.remove(name);
  67:     }
  68:     else
  69:     {
  70:       querymappings.put(name, queryString);
  71:     }
  72:   }
  73: 
  74:   /**
  75:    * Queries a datasource. The string 'query' defines the name of the query. The
  76:    * Parameterset given here may contain more data than actually needed.
  77:    * <p/>
  78:    * The dataset may change between two calls, do not assume anything!
  79:    *
  80:    * @param query
  81:    * @param parameters
  82:    * @return
  83:    */
  84:   public ReportData queryData(final String query, final DataSet parameters)
  85:           throws ReportDataFactoryException
  86:   {
  87:     if (query == null)
  88:     {
  89:       throw new NullPointerException("Query is null.");
  90:     }
  91:     final String realQuery = getQuery(query);
  92:     if (realQuery == null)
  93:     {
  94:       throw new ReportDataFactoryException("Query '" + query + "' is not recognized.");
  95:     }
  96:     return super.queryData(realQuery, parameters);
  97:   }
  98: 
  99:   public String getQuery(final String name)
 100:   {
 101:     return (String) querymappings.get(name);
 102:   }
 103: 
 104:   public String[] getQueryNames()
 105:   {
 106:     return (String[]) querymappings.keySet().toArray(
 107:             new String[querymappings.size()]);
 108:   }
 109: 
 110: }