/*
* 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.modules.misc.datafactory.sql;
import java.sql.Connection;
import java.util.HashMap;
import javax.swing.table.TableModel;
import org.pentaho.reporting.engine.classic.core.DataRow;
import org.pentaho.reporting.engine.classic.core.ReportDataFactoryException;
/**
* Creation-Date: 19.02.2006, 17:37:33
*
* @author Thomas Morgner
*/
public class SQLReportDataFactory extends SimpleSQLReportDataFactory
{
private HashMap<String,String> querymappings;
public SQLReportDataFactory(final Connection connection)
{
super(connection);
querymappings = new HashMap<String,String>();
}
public SQLReportDataFactory(final ConnectionProvider connectionProvider)
{
super(connectionProvider);
querymappings = new HashMap<String,String>();
}
/**
* 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 querymappings.containsKey(query);
}
public void setQuery(final String name, final String queryString)
{
if (queryString == null)
{
querymappings.remove(name);
}
else
{
querymappings.put(name, queryString);
}
}
/**
* 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
* @param parameters
* @return
*/
public synchronized TableModel queryData(final String query, final DataRow parameters)
throws ReportDataFactoryException
{
if (query == null)
{
throw new NullPointerException("Query is null."); //$NON-NLS-1$
}
final String realQuery = getQuery(query);
if (realQuery == null)
{
throw new ReportDataFactoryException("Query '" + query + "' is not recognized."); //$NON-NLS-1$ //$NON-NLS-2$
}
return super.queryData(realQuery, parameters);
}
public String getQuery(final String name)
{
return querymappings.get(name);
}
public String[] getQueryNames()
{
return querymappings.keySet().toArray(new String[querymappings.size()]);
}
public Object clone()
{
final SQLReportDataFactory dataFactory = (SQLReportDataFactory) super.clone();
dataFactory.querymappings = (HashMap<String,String>) querymappings.clone();
return dataFactory;
}
public String translateQuery(final String query)
{
return querymappings.get(query);
}
}