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