Ignore:
Timestamp:
02/04/10 21:56:24 (2 years ago)
Author:
kasper
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • MetadataBeans/trunk/src/main/java/org/eobjects/metamodel/schema/Relationship.java

    r886 r887  
    1 /** 
    2  *  This file is part of MetaModel. 
    3  * 
    4  *  MetaModel is free software: you can redistribute it and/or modify 
    5  *  it under the terms of the GNU General Public License as published by 
    6  *  the Free Software Foundation, either version 3 of the License, or 
    7  *  (at your option) any later version. 
    8  * 
    9  *  MetaModel is distributed in the hope that it will be useful, 
    10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of 
    11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
    12  *  GNU General Public License for more details. 
    13  * 
    14  *  You should have received a copy of the GNU General Public License 
    15  *  along with MetaModel.  If not, see <http://www.gnu.org/licenses/>. 
    16  */ 
    171package org.eobjects.metamodel.schema; 
    18  
    19 import java.io.Serializable; 
    20  
    21 import org.apache.commons.lang.builder.CompareToBuilder; 
    22 import org.apache.commons.lang.builder.EqualsBuilder; 
    23 import org.apache.commons.lang.builder.HashCodeBuilder; 
    242 
    253/** 
    264 * Represents a relationship between two tables where one set of columns is the 
    275 * primary key, and another set is the foreign key. Relationship is unlike most 
    28  * of the MetaModel an immutable type. The immutability help ensure integrity of 
     6 * of the MetaModel an im type. The immutability help ensure integrity of 
    297 * object-relationships. To create relationsips use the 
    308 * <code>createRelationship</code> method and to remove them use the 
     
    3412 * @see Column 
    3513 */ 
    36 public class Relationship implements Serializable, Comparable<Relationship> { 
     14public interface Relationship extends Comparable<Relationship> { 
    3715 
    38         private static final long serialVersionUID = 238786848828528822L; 
    39         private Table _primaryTable; 
    40         private Column[] _primaryColumns; 
    41         private Table _foreignTable; 
    42         private Column[] _foreignColumns; 
     16        public Table getPrimaryTable(); 
    4317 
    44         /** 
    45          * Factory method to create relations between two tables by specifying which 
    46          * columns from the tables that enforce the relationship. 
    47          *  
    48          * @param primaryColumns 
    49          *            the columns from the primary key table 
    50          * @param foreignColumns 
    51          *            the columns from the foreign key table 
    52          * @return the relation created 
    53          */ 
    54         public static Relationship createRelationship(Column[] primaryColumns, 
    55                         Column[] foreignColumns) { 
    56                 Table primaryTable = checkSameTable(primaryColumns); 
    57                 Table foreignTable = checkSameTable(foreignColumns); 
    58                 Relationship relation = new Relationship(primaryTable, primaryColumns, 
    59                                 foreignTable, foreignColumns); 
    60                 primaryTable.addRelationship(relation); 
     18        public Column[] getPrimaryColumns(); 
    6119 
    62                 // Ticket #144: Some tables have relations with them selves and then the 
    63                 // relationship should only be added once. 
    64                 if (foreignTable != primaryTable) { 
    65                         foreignTable.addRelationship(relation); 
    66                 } 
    67                 return relation; 
    68         } 
     20        public Table getForeignTable(); 
    6921 
    70         public static Relationship createRelationship(Column primaryColumn, 
    71                         Column foreignColumn) { 
    72                 return createRelationship(new Column[] { primaryColumn }, 
    73                                 new Column[] { foreignColumn }); 
    74         } 
     22        public Column[] getForeignColumns(); 
    7523 
    76         private static Table checkSameTable(Column[] columns) { 
    77                 if (columns == null || columns.length == 0) { 
    78                         throw new IllegalArgumentException( 
    79                                         "At least one key-column must exist on both " 
    80                                                         + "primary and foreign side for " 
    81                                                         + "a relation to exist."); 
    82                 } 
    83                 Table table = null; 
    84                 for (int i = 0; i < columns.length; i++) { 
    85                         Column column = columns[i]; 
    86                         if (i == 0) { 
    87                                 table = column.getTable(); 
    88                         } else { 
    89                                 if (table != column.getTable()) { 
    90                                         throw new IllegalArgumentException( 
    91                                                         "Key-columns did not have same table"); 
    92                                 } 
    93                         } 
    94                 } 
    95                 return table; 
    96         } 
    97  
    98         public void remove() { 
    99                 _primaryTable.removeRelationship(this); 
    100                 _foreignTable.removeRelationship(this); 
    101                 _primaryColumns = null; 
    102                 _primaryTable = null; 
    103                 _foreignColumns = null; 
    104                 _foreignTable = null; 
    105         } 
    106  
    107         /** 
    108          * Prevent external instantiation 
    109          */ 
    110         private Relationship(Table primaryTable, Column[] primaryColumns, 
    111                         Table foreignTable, Column[] foreignColumns) { 
    112                 _primaryTable = primaryTable; 
    113                 _primaryColumns = primaryColumns; 
    114                 _foreignTable = foreignTable; 
    115                 _foreignColumns = foreignColumns; 
    116         } 
    117  
    118         public Table getPrimaryTable() { 
    119                 return _primaryTable; 
    120         } 
    121  
    122         public Column[] getPrimaryColumns() { 
    123                 return _primaryColumns; 
    124         } 
    125  
    126         public Table getForeignTable() { 
    127                 return _foreignTable; 
    128         } 
    129  
    130         public Column[] getForeignColumns() { 
    131                 return _foreignColumns; 
    132         } 
    133  
    134         @Override 
    135         public String toString() { 
    136                 StringBuilder sb = new StringBuilder(); 
    137                 sb.append("Relationship["); 
    138                 sb.append("primaryTable=" + _primaryTable.getName()); 
    139                 Column[] columns = getPrimaryColumns(); 
    140                 sb.append(",primaryColumns={"); 
    141                 for (int i = 0; i < columns.length; i++) { 
    142                         if (i != 0) { 
    143                                 sb.append(","); 
    144                         } 
    145                         sb.append(columns[i].getName()); 
    146                 } 
    147                 sb.append("}"); 
    148                 sb.append(",foreignTable=" + _foreignTable.getName()); 
    149                 columns = getForeignColumns(); 
    150                 sb.append(",foreignColumns={"); 
    151                 for (int i = 0; i < columns.length; i++) { 
    152                         if (i != 0) { 
    153                                 sb.append(","); 
    154                         } 
    155                         sb.append(columns[i].getName()); 
    156                 } 
    157                 sb.append("}"); 
    158                 sb.append("]"); 
    159                 return sb.toString(); 
    160         } 
    161  
    162         public int compareTo(Relationship that) { 
    163                 CompareToBuilder ctb = new CompareToBuilder(); 
    164                 ctb.append(this.getPrimaryTable(), that.getPrimaryTable()); 
    165                 ctb.append(this.getForeignTable(), that.getForeignTable()); 
    166                 ctb.append(this.getPrimaryColumns(), that.getPrimaryColumns()); 
    167                 ctb.append(this.getForeignColumns(), that.getForeignColumns()); 
    168                 return ctb.toComparison(); 
    169         } 
    170  
    171         @Override 
    172         public boolean equals(Object obj) { 
    173                 if (obj == this) { 
    174                         return true; 
    175                 } 
    176                 if (obj instanceof Relationship) { 
    177                         Relationship that = (Relationship) obj; 
    178                         EqualsBuilder eb = new EqualsBuilder(); 
    179                         eb.append(this.getPrimaryColumns(), that.getPrimaryColumns()); 
    180                         eb.append(this.getForeignColumns(), that.getForeignColumns()); 
    181                         return eb.isEquals(); 
    182                 } 
    183                 return false; 
    184         } 
    185  
    186         @Override 
    187         public int hashCode() { 
    188                 return new HashCodeBuilder().append(_foreignColumns).append( 
    189                                 _primaryColumns).toHashCode(); 
    190         } 
     24        public int compareTo(Relationship that); 
    19125 
    19226        /** 
     
    19832         *         columns as a part of the relation 
    19933         */ 
    200         public boolean containsColumnPair(Column pkColumn, Column fkColumn) { 
    201                 if (pkColumn != null && fkColumn != null) { 
    202                         for (int i = 0; i < _primaryColumns.length; i++) { 
    203                                 if (pkColumn.equals(_primaryColumns[i]) 
    204                                                 && fkColumn.equals(_foreignColumns[i])) { 
    205                                         return true; 
    206                                 } 
    207                         } 
    208                 } 
    209                 return false; 
    210         } 
     34        public boolean containsColumnPair(Column pkColumn, 
     35                        Column fkColumn); 
     36 
    21137} 
Note: See TracChangeset for help on using the changeset viewer.