ImportedKeysSearchCallbackFilteredByPKs.java

  1. /*
  2.  *
  3.  * The DbUnit Database Testing Framework
  4.  * Copyright (C)2005, 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.database.search;

  22. import java.sql.ResultSet;

  23. import org.dbunit.database.IDatabaseConnection;
  24. import org.dbunit.database.PrimaryKeyFilter;
  25. import org.dbunit.database.PrimaryKeyFilter.PkTableMap;
  26. import org.dbunit.dataset.filter.ITableFilter;
  27. import org.dbunit.util.search.IEdge;
  28. import org.dbunit.util.search.SearchException;
  29. import org.slf4j.Logger;
  30. import org.slf4j.LoggerFactory;

  31. /**
  32.  * Extension of the ImportedKeysSearchCallback, where each new edge is added to a
  33.  * PrimaryKeyFilter.
  34.  * @author Felipe Leme (dbunit@felipeal.net)
  35.  * @version $Revision$
  36.  * @since Sep 9, 2005
  37.  */
  38. public class ImportedKeysSearchCallbackFilteredByPKs extends ImportedKeysSearchCallback {

  39.     /**
  40.      * Logger for this class
  41.      */
  42.     private static final Logger logger = LoggerFactory.getLogger(ImportedKeysSearchCallbackFilteredByPKs.class);

  43.   // primary key filter associated with the call back
  44.   private final PrimaryKeyFilter pksFilter;
  45.      
  46.   /**
  47.    * Default constructor.
  48.    * @param connection database connection
  49.    * @param allowedPKs map of allowed rows, based on the primary keys (key is the name
  50.    * of a table; value is a Set with allowed primary keys for that table)
  51.    */
  52.   public ImportedKeysSearchCallbackFilteredByPKs(IDatabaseConnection connection, PkTableMap allowedPKs) {
  53.     super(connection);
  54.     this.pksFilter = new PrimaryKeyFilter(connection, allowedPKs, false);
  55.   }
  56.  
  57.   /**
  58.    * Get the primary key filter associated with the call back
  59.    * @return primary key filter associated with the call back
  60.    */
  61.   public ITableFilter getFilter() {
  62.     return this.pksFilter;
  63.   }
  64.  
  65.  
  66.   public void nodeAdded(Object node) throws SearchException {
  67.         logger.debug("nodeAdded(node={}) - start", node);

  68.     this.pksFilter.nodeAdded( node );
  69.   }
  70.  
  71.   protected IEdge newEdge(ResultSet rs, int type, String from, String to, String fkColumn, String pkColumn) throws SearchException {
  72.       if (logger.isDebugEnabled())
  73.       {
  74.         logger.debug("newEdge(rs={}, type={}, from={}, to={}, fkColumn={}, pkColumn={}) - start",
  75.                 new Object[]{rs, String.valueOf(type), from, to, fkColumn, pkColumn});
  76.       }
  77.     ForeignKeyRelationshipEdge edge = createFKEdge( rs, type, from, to, fkColumn, pkColumn );
  78.     this.pksFilter.edgeAdded( edge );
  79.     return edge;
  80.   }


  81. }