Source for org.jfree.report.modules.data.beans.NamedStaticReportDataFactory

   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: NamedStaticReportDataFactory.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: 
  32: package org.jfree.report.modules.data.beans;
  33: 
  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: import org.jfree.resourceloader.ResourceKey;
  40: 
  41: /**
  42:  * Creation-Date: Jan 12, 2007, 2:16:00 PM
  43:  *
  44:  * @author Thomas Morgner
  45:  */
  46: public class NamedStaticReportDataFactory extends StaticReportDataFactory
  47: {
  48:   private ResourceKey contentBase;
  49:   private HashMap querymappings;
  50: 
  51:   public NamedStaticReportDataFactory()
  52:   {
  53:     querymappings = new HashMap();
  54:   }
  55: 
  56:   public void setQuery(final String name, final String queryString)
  57:   {
  58:     if (queryString == null)
  59:     {
  60:       querymappings.remove(name);
  61:     }
  62:     else
  63:     {
  64:       querymappings.put(name, queryString);
  65:     }
  66:   }
  67: 
  68:   /**
  69:    * Queries a datasource. The string 'query' defines the name of the query. The
  70:    * Parameterset given here may contain more data than actually needed.
  71:    * <p/>
  72:    * The dataset may change between two calls, do not assume anything!
  73:    *
  74:    * @param query
  75:    * @param parameters
  76:    * @return
  77:    */
  78:   public ReportData queryData(final String query, final DataSet parameters)
  79:           throws ReportDataFactoryException
  80:   {
  81:     if (query == null)
  82:     {
  83:       throw new NullPointerException("Query is null.");
  84:     }
  85:     final String realQuery = getQuery(query);
  86:     if (realQuery == null)
  87:     {
  88:       throw new ReportDataFactoryException("Query '" + query + "' is not recognized.");
  89:     }
  90:     return super.queryData(realQuery, parameters);
  91:   }
  92: 
  93:   public String getQuery(final String name)
  94:   {
  95:     return (String) querymappings.get(name);
  96:   }
  97: 
  98:   public String[] getQueryNames()
  99:   {
 100:     return (String[]) querymappings.keySet().toArray(
 101:             new String[querymappings.size()]);
 102:   }
 103: 
 104:   public ResourceKey getContentBase()
 105:   {
 106:     return contentBase;
 107:   }
 108: 
 109:   public void setContentBase(final ResourceKey contentBase)
 110:   {
 111:     this.contentBase = contentBase;
 112:   }
 113: }