FilteredTableMetaData.java

  1. /*
  2.  *
  3.  * The DbUnit Database Testing Framework
  4.  * Copyright (C)2002-2004, DbUnit.org
  5.  *
  6.  * This library is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU Lesser General Public
  8.  * License as published by the Free Software Foundation; either
  9.  * version 2.1 of the License, or (at your option) any later version.
  10.  *
  11.  * This library is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  * Lesser General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU Lesser General Public
  17.  * License along with this library; if not, write to the Free Software
  18.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  19.  *
  20.  */

  21. package org.dbunit.dataset;

  22. import org.slf4j.Logger;
  23. import org.slf4j.LoggerFactory;

  24. import org.dbunit.dataset.filter.IColumnFilter;

  25. import java.util.List;
  26. import java.util.ArrayList;

  27. /**
  28.  * @author Manuel Laflamme
  29.  * @version $Revision$
  30.  * @since May 11, 2004
  31.  */
  32. public class FilteredTableMetaData extends AbstractTableMetaData
  33. {

  34.     /**
  35.      * Logger for this class
  36.      */
  37.     private static final Logger logger = LoggerFactory.getLogger(FilteredTableMetaData.class);

  38.     private final String _tableName;
  39.     private final Column[] _columns;
  40.     private final Column[] _primaryKeys;

  41.     public FilteredTableMetaData(ITableMetaData metaData,
  42.             IColumnFilter columnFilter) throws DataSetException
  43.     {
  44.         _tableName = metaData.getTableName();
  45.         _columns = getFilteredColumns(_tableName, metaData.getColumns(), columnFilter);
  46.         _primaryKeys = getFilteredColumns(_tableName, metaData.getPrimaryKeys(), columnFilter);
  47.     }

  48.     public static Column[] getFilteredColumns(String tableName,
  49.             Column[] columns, IColumnFilter columnFilter)
  50.     {
  51.         if (logger.isDebugEnabled())
  52.         {
  53.             logger.debug("getFilteredColumns(tableName={}, columns={}, columnFilter={}) - start",
  54.                     new Object[]{ tableName, columns, columnFilter });
  55.         }

  56.         if (columns == null)
  57.         {
  58.             return new Column[0];
  59.         }
  60.        
  61.         List columnList =  new ArrayList();
  62.         for (int i = 0; i < columns.length; i++)
  63.         {
  64.             Column column = columns[i];
  65.             if (columnFilter.accept(tableName, column))
  66.             {
  67.                 columnList.add(column);
  68.             }
  69.         }
  70.         return (Column[])columnList.toArray(new Column[0]);
  71.     }

  72.     ////////////////////////////////////////////////////////////////////////////
  73.     // ITableMetaData interface

  74.     public String getTableName()
  75.     {
  76.         return _tableName;
  77.     }

  78.     public Column[] getColumns() throws DataSetException
  79.     {
  80.         return _columns;
  81.     }

  82.     public Column[] getPrimaryKeys() throws DataSetException
  83.     {
  84.         return _primaryKeys;
  85.     }
  86. }