View Javadoc
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  
22  package org.dbunit.database.search;
23  
24  import java.sql.ResultSet;
25  
26  import org.dbunit.database.IDatabaseConnection;
27  import org.dbunit.database.PrimaryKeyFilter;
28  import org.dbunit.database.PrimaryKeyFilter.PkTableMap;
29  import org.dbunit.dataset.filter.ITableFilter;
30  import org.dbunit.util.search.IEdge;
31  import org.dbunit.util.search.SearchException;
32  import org.slf4j.Logger;
33  import org.slf4j.LoggerFactory;
34  
35  /**
36   * Extension of the ImportedKeysSearchCallback, where each new edge is added to a 
37   * PrimaryKeyFilter.
38   * @author Felipe Leme (dbunit@felipeal.net)
39   * @version $Revision$
40   * @since Sep 9, 2005
41   */
42  public class ImportedKeysSearchCallbackFilteredByPKs extends ImportedKeysSearchCallback {
43  
44      /**
45       * Logger for this class
46       */
47      private static final Logger logger = LoggerFactory.getLogger(ImportedKeysSearchCallbackFilteredByPKs.class);
48  
49    // primary key filter associated with the call back
50    private final PrimaryKeyFilter pksFilter;
51       
52    /**
53     * Default constructor.
54     * @param connection database connection
55     * @param allowedPKs map of allowed rows, based on the primary keys (key is the name
56     * of a table; value is a Set with allowed primary keys for that table)
57     */
58    public ImportedKeysSearchCallbackFilteredByPKs(IDatabaseConnection connection, PkTableMap allowedPKs) {
59      super(connection);
60      this.pksFilter = new PrimaryKeyFilter(connection, allowedPKs, false);
61    }
62    
63    /**
64     * Get the primary key filter associated with the call back
65     * @return primary key filter associated with the call back
66     */
67    public ITableFilter getFilter() {
68      return this.pksFilter;
69    }
70    
71    
72    public void nodeAdded(Object node) throws SearchException {
73          logger.debug("nodeAdded(node={}) - start", node);
74  
75      this.pksFilter.nodeAdded( node );
76    }
77    
78    protected IEdge newEdge(ResultSet rs, int type, String from, String to, String fkColumn, String pkColumn) throws SearchException {
79  	  if (logger.isDebugEnabled())
80  	  {
81          logger.debug("newEdge(rs={}, type={}, from={}, to={}, fkColumn={}, pkColumn={}) - start",
82          		new Object[]{rs, String.valueOf(type), from, to, fkColumn, pkColumn});
83  	  }
84      ForeignKeyRelationshipEdge edge = createFKEdge( rs, type, from, to, fkColumn, pkColumn );
85      this.pksFilter.edgeAdded( edge );
86      return edge;
87    }
88  
89  
90  }