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  package org.dbunit.database.search;
22  
23  import org.dbunit.util.search.Edge;
24  import org.slf4j.Logger;
25  import org.slf4j.LoggerFactory;
26  
27  /**
28   * Implementation of an edge representing a foreign key (FK)  relationship between two
29   * tables.<br>
30   * The <code>from</code> node is the table which have the FK, while the 
31   * <code>to</code> node is the table with the PK. In other words, the edge
32   * A->B means FK(A) = PK(B).<br>
33   * 
34   * <strong>NOTE:</strong> only single-column PKs are supported at this moment
35   *  
36   * @author Felipe Leme (dbunit@felipeal.net)
37   * @author Last changed by: $Author$
38   * @version $Revision$ $Date$
39   * @since 2.2 (Sep 9, 2005)
40   */
41  public class ForeignKeyRelationshipEdge extends Edge {
42      private final Logger log = LoggerFactory.getLogger(getClass());
43  
44      private String fkColumn;
45      private String pkColumn;
46  
47      /**
48       * Creates an edge representing a FK.
49       * @param tableFrom table that has the FK
50       * @param tableTo table that has the PK
51       * @param fkColumn name of the FK column on tableFrom
52       * @param pkColumn name of the PK column on tableTo
53       */
54  
55      public ForeignKeyRelationshipEdge(String tableFrom, String tableTo, String fkColumn, String pkColumn) {
56          super(tableFrom, tableTo);
57          this.fkColumn = fkColumn;
58          this.pkColumn = pkColumn;
59      }
60  
61      /**
62       * Gets the name of the foreign key column in the relationship.
63       * @return name of the foreign key column in the relationship.
64       */
65      public String getFKColumn() {
66          return fkColumn;
67      }
68  
69      /**
70       * Gets the name of the primary key column in the relationship.
71       * @return name of the primary key column in the relationship.
72       */
73      public String getPKColumn() {
74          return pkColumn;
75      }
76      
77      public String toString() {
78          return getFrom() + "(" + getFKColumn() + ")->" + getTo() + "(" + getPKColumn() + ")";
79      }
80  
81      public int hashCode() {
82          final int prime = 31;
83          int result = super.hashCode();
84          result = prime * result
85                  + ((fkColumn == null) ? 0 : fkColumn.hashCode());
86          result = prime * result
87                  + ((pkColumn == null) ? 0 : pkColumn.hashCode());
88          return result;
89      }
90  
91      public boolean equals(Object obj) {
92          if (this == obj)
93              return true;
94          if (!super.equals(obj))
95              return false;
96          if (getClass() != obj.getClass())
97              return false;
98          ForeignKeyRelationshipEdge other = (ForeignKeyRelationshipEdge) obj;
99          if (fkColumn == null) {
100             if (other.fkColumn != null)
101                 return false;
102         } else if (!fkColumn.equals(other.fkColumn))
103             return false;
104         if (pkColumn == null) {
105             if (other.pkColumn != null)
106                 return false;
107         } else if (!pkColumn.equals(other.pkColumn))
108             return false;
109         return true;
110     }
111 
112     public int compareTo(Object o)
113     {
114         log.debug("compareTo(o={}) - start", o);
115 
116         ForeignKeyRelationshipEdge that = (ForeignKeyRelationshipEdge) o;
117 
118         int result = super.compareTo(that);
119         if (result == 0)
120         {
121             result = this.pkColumn.compareTo(that.pkColumn);
122         }
123         if (result == 0)
124         {
125             result = this.fkColumn.compareTo(that.fkColumn);
126         }
127         return result;
128     }
129 }