/*
* This program is free software; you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License, version 2.1 as published by the Free Software
* Foundation.
*
* You should have received a copy of the GNU Lesser General Public License along with this
* program; if not, you can obtain a copy at http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html
* or from the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
*
* Copyright (c) 2001 - 2009 Object Refinery Ltd, Pentaho Corporation and Contributors.. All rights reserved.
*/
package org.pentaho.reporting.engine.classic.core;
import java.util.HashMap;
import javax.swing.table.TableModel;
import org.pentaho.reporting.libraries.base.config.Configuration;
import org.pentaho.reporting.libraries.resourceloader.ResourceKey;
import org.pentaho.reporting.libraries.resourceloader.ResourceManager;
/**
* The TableDataFactory provides keyed access to predefined tablemodels. The factory does not accept parameters and
* therefore cannot be used for parametrized queries. The queryname is used to lookup the table by its previously
* registered name.
*
* @author Thomas Morgner
*/
public class TableDataFactory implements DataFactory, Cloneable
{
/**
* A unique identifier for long term persistance.
*/
private static final long serialVersionUID = -238954878318943053L;
/**
* The tables for this factory.
*/
private HashMap<String,TableModel> tables;
/**
* Default Constructor.
*/
public TableDataFactory()
{
this.tables = new HashMap<String,TableModel>();
}
/**
* Creates a new TableDataFactory and registers the tablemodel with the given name.
*
* @param name the name of the table.
* @param tableModel the tablemodel.
*/
public TableDataFactory(final String name, final TableModel tableModel)
{
this();
addTable(name, tableModel);
}
/**
* Checks whether the query would be executable by this datafactory. This performs a rough check, not a full query.
*
* @param query
* @param parameters
* @return
*/
public boolean isQueryExecutable(final String query, final DataRow parameters)
{
return tables.containsKey(query);
}
/**
* Registers a tablemodel with the given name. If a different tablemodel has been previously registered with the same
* name, this table will replace the existing one.
*
* @param name the name of the table.
* @param tableModel the tablemodel that should be registered.
*/
public void addTable(final String name, final TableModel tableModel)
{
if (tableModel == null)
{
throw new NullPointerException();
}
if (name == null)
{
throw new NullPointerException();
}
tables.put(name, tableModel);
}
/**
* Removes the table that has been registered by the given name.
*
* @param name the name of the table to be removed.
*/
public void removeTable(final String name)
{
tables.remove(name);
}
public TableModel getTable(final String name)
{
return tables.get(name);
}
/**
* Queries a datasource. The string 'query' defines the name of the query. The Parameterset given here may contain
* more data than actually needed.
* <p/>
* The dataset may change between two calls, do not assume anything!
*
* @param query the name of the table.
* @param parameters are ignored for this factory.
* @return the report data or null.
*/
public TableModel queryData(final String query, final DataRow parameters) throws ReportDataFactoryException
{
final TableModel tableModel = tables.get(query);
if (tableModel == null)
{
throw new ReportDataFactoryException("The specified query '" + query + "' is not recognized.");
}
return tableModel;
}
public void initialize(final Configuration configuration,
final ResourceManager resourceManager,
final ResourceKey contextKey,
final ResourceBundleFactory resourceBundleFactory)
{
}
/**
* This does nothing.
*/
public void open()
{
}
/**
* Closes the data factory. Actually, this one does nothing at all.
*/
public void close()
{
}
/**
* Derives a freshly initialized report data factory, which is independend of the original data factory. Opening or
* Closing one data factory must not affect the other factories.
*
* @return a copy of this factory.
*/
public DataFactory derive()
{
return (DataFactory) clone();
}
/**
* Creates a copy of this data-factory.
*
* @return the copy of the data-factory, never null.
*/
public Object clone()
{
try
{
final TableDataFactory dataFactory = (TableDataFactory) super.clone();
dataFactory.tables = (HashMap<String,TableModel>) tables.clone();
return dataFactory;
}
catch (CloneNotSupportedException e)
{
throw new IllegalStateException(e);
}
}
public String[] getQueryNames()
{
return tables.keySet().toArray(new String[tables.size()]);
}
public void cancelRunningQuery()
{
// TODO implement
}
}