ForeignKeyRelationshipEdge.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 org.dbunit.util.search.Edge;
  23. import org.slf4j.Logger;
  24. import org.slf4j.LoggerFactory;

  25. /**
  26.  * Implementation of an edge representing a foreign key (FK)  relationship between two
  27.  * tables.<br>
  28.  * The <code>from</code> node is the table which have the FK, while the
  29.  * <code>to</code> node is the table with the PK. In other words, the edge
  30.  * A->B means FK(A) = PK(B).<br>
  31.  *
  32.  * <strong>NOTE:</strong> only single-column PKs are supported at this moment
  33.  *  
  34.  * @author Felipe Leme (dbunit@felipeal.net)
  35.  * @author Last changed by: $Author$
  36.  * @version $Revision$ $Date$
  37.  * @since 2.2 (Sep 9, 2005)
  38.  */
  39. public class ForeignKeyRelationshipEdge extends Edge {
  40.     private final Logger log = LoggerFactory.getLogger(getClass());

  41.     private String fkColumn;
  42.     private String pkColumn;

  43.     /**
  44.      * Creates an edge representing a FK.
  45.      * @param tableFrom table that has the FK
  46.      * @param tableTo table that has the PK
  47.      * @param fkColumn name of the FK column on tableFrom
  48.      * @param pkColumn name of the PK column on tableTo
  49.      */

  50.     public ForeignKeyRelationshipEdge(String tableFrom, String tableTo, String fkColumn, String pkColumn) {
  51.         super(tableFrom, tableTo);
  52.         this.fkColumn = fkColumn;
  53.         this.pkColumn = pkColumn;
  54.     }

  55.     /**
  56.      * Gets the name of the foreign key column in the relationship.
  57.      * @return name of the foreign key column in the relationship.
  58.      */
  59.     public String getFKColumn() {
  60.         return fkColumn;
  61.     }

  62.     /**
  63.      * Gets the name of the primary key column in the relationship.
  64.      * @return name of the primary key column in the relationship.
  65.      */
  66.     public String getPKColumn() {
  67.         return pkColumn;
  68.     }
  69.    
  70.     public String toString() {
  71.         return getFrom() + "(" + getFKColumn() + ")->" + getTo() + "(" + getPKColumn() + ")";
  72.     }

  73.     public int hashCode() {
  74.         final int prime = 31;
  75.         int result = super.hashCode();
  76.         result = prime * result
  77.                 + ((fkColumn == null) ? 0 : fkColumn.hashCode());
  78.         result = prime * result
  79.                 + ((pkColumn == null) ? 0 : pkColumn.hashCode());
  80.         return result;
  81.     }

  82.     public boolean equals(Object obj) {
  83.         if (this == obj)
  84.             return true;
  85.         if (!super.equals(obj))
  86.             return false;
  87.         if (getClass() != obj.getClass())
  88.             return false;
  89.         ForeignKeyRelationshipEdge other = (ForeignKeyRelationshipEdge) obj;
  90.         if (fkColumn == null) {
  91.             if (other.fkColumn != null)
  92.                 return false;
  93.         } else if (!fkColumn.equals(other.fkColumn))
  94.             return false;
  95.         if (pkColumn == null) {
  96.             if (other.pkColumn != null)
  97.                 return false;
  98.         } else if (!pkColumn.equals(other.pkColumn))
  99.             return false;
  100.         return true;
  101.     }

  102.     public int compareTo(Object o)
  103.     {
  104.         log.debug("compareTo(o={}) - start", o);

  105.         ForeignKeyRelationshipEdge that = (ForeignKeyRelationshipEdge) o;

  106.         int result = super.compareTo(that);
  107.         if (result == 0)
  108.         {
  109.             result = this.pkColumn.compareTo(that.pkColumn);
  110.         }
  111.         if (result == 0)
  112.         {
  113.             result = this.fkColumn.compareTo(that.fkColumn);
  114.         }
  115.         return result;
  116.     }
  117. }