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/Table.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 import java.util.ArrayList; 
    21 import java.util.Collection; 
    22 import java.util.HashSet; 
    23 import java.util.List; 
    24 import java.util.Set; 
    25  
    26 import org.apache.commons.lang.builder.CompareToBuilder; 
    27 import org.apache.commons.lang.builder.EqualsBuilder; 
    28 import org.apache.commons.lang.builder.HashCodeBuilder; 
    29 import org.apache.commons.lang.builder.ToStringBuilder; 
    30 import org.apache.commons.lang.builder.ToStringStyle; 
    31 import org.eobjects.metamodel.MetaModelHelper; 
    32  
    332 
    343/** 
     
    409 * @see Relationship 
    4110 */ 
    42 public class Table implements Serializable, Comparable<Table> { 
    43  
    44         private static final long serialVersionUID = -5094888465714027354L; 
    45         protected String _name; 
    46         protected TableType _type; 
    47         protected String _remarks; 
    48         protected Schema _schema; 
    49         protected List<Column> _columns = new ArrayList<Column>(); 
    50         protected List<Relationship> _relationships = new ArrayList<Relationship>(); 
    51         protected String _quoteString = null; 
    52         private transient String _qualifiedLabel; 
    53  
    54         public Table() { 
    55         } 
    56  
    57         public Table(String name) { 
    58                 this(); 
    59                 setName(name); 
    60         } 
    61  
    62         public Table(String name, TableType type) { 
    63                 this(name); 
    64                 _type = type; 
    65         } 
    66  
    67         public Table(String name, TableType type, Schema schema) { 
    68                 this(name, type); 
    69                 _schema = schema; 
    70         } 
    71  
    72         public Table(String name, TableType type, Schema schema, Column... columns) { 
    73                 this(name, type, schema); 
    74                 setColumns(columns); 
    75         } 
     11public interface Table extends Comparable<Table> { 
    7612 
    7713        /** 
    7814         * @return the name of the table 
    7915         */ 
    80         public String getName() { 
    81                 return _name; 
    82         } 
    83  
    84         public Table setName(String name) { 
    85                 _name = name; 
    86                 fireSchemaChanged(); 
    87                 return this; 
    88         } 
    89  
    90         /** 
    91          * Internal getter for the columns of the table. Overwrite this method to 
    92          * implement column lookup, column lazy-loading or similar. 
    93          */ 
    94         protected List<Column> getColumnsInternal() { 
    95                 return _columns; 
    96         } 
    97  
    98         /** 
    99          * Internal getter for the relationships of the table. Overwrite this method 
    100          * to implement relationship lookup, relationship lazy-loading or similar. 
    101          */ 
    102         protected List<Relationship> getRelationshipsInternal() { 
    103                 return _relationships; 
    104         } 
     16        public String getName(); 
    10517 
    10618        /** 
    10719         * @return the number of columns in this table 
    10820         */ 
    109         public int getColumnCount() { 
    110                 return getColumnsInternal().size(); 
    111         } 
     21        public int getColumnCount(); 
    11222 
    11323        /** 
    11424         * @return the columns of the table 
    11525         */ 
    116         public Column[] getColumns() { 
    117                 List<Column> columns = getColumnsInternal(); 
    118                 return columns.toArray(new Column[columns.size()]); 
    119         } 
    120  
    121         public Table setColumns(Column... columns) { 
    122                 List<Column> existingColumns = getColumnsInternal(); 
    123                 existingColumns.clear(); 
    124                 for (Column column : columns) { 
    125                         existingColumns.add(column); 
    126                 } 
    127                 return this; 
    128         } 
    129  
    130         public Table setColumns(Collection<Column> columns) { 
    131                 List<Column> existingColumns = getColumnsInternal(); 
    132                 existingColumns.clear(); 
    133                 for (Column column : columns) { 
    134                         existingColumns.add(column); 
    135                 } 
    136                 return this; 
    137         } 
    138  
    139         public Table addColumn(Column column) { 
    140                 getColumnsInternal().add(column); 
    141                 return this; 
    142         } 
    143  
    144         public Table removeColumn(Column column) { 
    145                 getColumnsInternal().remove(column); 
    146                 return this; 
    147         } 
     26        public Column[] getColumns(); 
    14827 
    14928        /** 
     
    15534         *         found. 
    15635         */ 
    157         public Column getColumnByName(String columnName) { 
    158                 if (columnName != null) { 
    159                         List<Column> foundColumn = new ArrayList<Column>(); 
    160                         // Search for column matches, case insensitive. 
    161                         for (Column column : getColumnsInternal()) { 
    162                                 if (columnName.equalsIgnoreCase(column.getName())) { 
    163                                         foundColumn.add(column); 
    164                                 } 
    165                         } 
    166                         int numColumns = foundColumn.size(); 
    167                         if (numColumns == 0) { 
    168                                 return null; 
    169                         } else if (numColumns == 1) { 
    170                                 return foundColumn.get(0); 
    171                         } else { 
    172                                 // If more matches are found, search case sensitive 
    173                                 for (Column column : foundColumn) { 
    174                                         if (columnName.equals(column.getName())) { 
    175                                                 return column; 
    176                                         } 
    177                                 } 
    178                         } 
    179                 } 
    180                 return null; 
    181         } 
     36        public Column getColumnByName(String columnName); 
    18237 
    18338        /** 
    18439         * @return the schema that the table belong to 
    18540         */ 
    186         public Schema getSchema() { 
    187                 return _schema; 
    188         } 
     41        public Schema getSchema(); 
    18942 
    190         public Table setSchema(Schema schema) { 
    191                 _schema = schema; 
    192                 fireSchemaChanged(); 
    193                 return this; 
    194         } 
    195  
    196         /** 
    197          * Tells the table object that the schema it belongs to have changed. This 
    198          * is typically done if the schema is being renamed or similar. 
    199          */ 
    200         protected void fireSchemaChanged() { 
    201                 _qualifiedLabel = null; 
    202                 for (Column column : _columns) { 
    203                         column.fireTableChanged(); 
    204                 } 
    205         } 
    206          
    20743        /** 
    20844         * @return the type of table 
    20945         */ 
    210         public TableType getType() { 
    211                 return _type; 
    212         } 
     46        public TableType getType(); 
    21347 
    214         public Table setType(TableType type) { 
    215                 _type = type; 
    216                 return this; 
    217         } 
     48        public Table setType(TableType type); 
    21849 
    21950        /** 
     
    22152         *         TableRelation.createRelation(); 
    22253         */ 
    223         public Relationship[] getRelationships() { 
    224                 List<Relationship> relationships = getRelationshipsInternal(); 
    225                 return relationships.toArray(new Relationship[relationships.size()]); 
    226         } 
     54        public Relationship[] getRelationships(); 
    22755 
    22856        /** 
     
    23058         * @return this tables relationsips to another table 
    23159         */ 
    232         public Relationship[] getRelationships(Table otherTable) { 
    233                 List<Relationship> result = new ArrayList<Relationship>(); 
    234                 List<Relationship> relationships = getRelationshipsInternal(); 
    235                 for (Relationship relation : relationships) { 
    236                         if (relation.getForeignTable() == otherTable 
    237                                         && relation.getPrimaryTable() == this) { 
    238                                 result.add(relation); 
    239                         } else if (relation.getForeignTable() == this 
    240                                         && relation.getPrimaryTable() == otherTable) { 
    241                                 result.add(relation); 
    242                         } 
    243                 } 
    244                 return result.toArray(new Relationship[result.size()]); 
    245         } 
     60        public Relationship[] getRelationships(Table otherTable); 
    24661 
    247         /** 
    248          * Protected method for adding a relationship to this table. Should not be 
    249          * used. Use Relationship.createRelationship(Column[], Column[]) instead. 
    250          */ 
    251         protected Table addRelationship(Relationship relation) { 
    252                 getRelationshipsInternal().add(relation); 
    253                 return this; 
    254         } 
     62        public int getRelationshipCount(); 
    25563 
    256         /** 
    257          * Protected method for removing a relationship from this table. Should not 
    258          * be used. Use Relationship.remove() instead. 
    259          */ 
    260         protected Table removeRelationship(Relationship relation) { 
    261                 getRelationshipsInternal().remove(relation); 
    262                 return this; 
    263         } 
     64        public String getRemarks(); 
    26465 
    265         public int getRelationshipCount() { 
    266                 return getRelationshipsInternal().size(); 
    267         } 
     66        public String getQuote(); 
    26867 
    269         public String getRemarks() { 
    270                 return _remarks; 
    271         } 
    272  
    273         public void setRemarks(String remarks) { 
    274                 _remarks = remarks; 
    275         } 
    276  
    277         public String getQuote() { 
    278                 return _quoteString; 
    279         } 
    280  
    281         public Table setQuote(String quoteString) { 
    282                 _quoteString = quoteString; 
    283                 return this; 
    284         } 
    285  
    286         public String getQuotedName() { 
    287                 if (_quoteString != null) { 
    288                         return _quoteString + getName() + _quoteString; 
    289                 } 
    290                 return getName(); 
    291         } 
     68        public String getQuotedName(); 
    29269 
    29370        /** 
     
    29976         * @return a qualified label 
    30077         */ 
    301         public String getQualifiedLabel() { 
    302                 if (_qualifiedLabel == null) { 
    303                         StringBuilder sb = new StringBuilder(); 
    304                         if (_schema != null && _schema.getName() != null) { 
    305                                 sb.append(_schema.getName()); 
    306                                 sb.append('.'); 
    307                         } 
    308                         sb.append(getName()); 
    309                         _qualifiedLabel = sb.toString(); 
    310                 } 
    311                 return _qualifiedLabel; 
    312         } 
     78        public String getQualifiedLabel(); 
    31379 
    314         public Column[] getNumberColumns() { 
    315                 List<Column> result = new ArrayList<Column>(); 
    316                 Column[] columns = getColumns(); 
    317                 for (int i = 0; i < columns.length; i++) { 
    318                         if (columns[i].getType() != null && columns[i].getType().isNumber()) { 
    319                                 result.add(columns[i]); 
    320                         } 
    321                 } 
    322                 return result.toArray(new Column[result.size()]); 
    323         } 
     80        public Column[] getNumberColumns(); 
    32481 
    325         public Column[] getLiteralColumns() { 
    326                 List<Column> result = new ArrayList<Column>(); 
    327                 Column[] columns = getColumns(); 
    328                 for (int i = 0; i < columns.length; i++) { 
    329                         if (columns[i].getType() != null 
    330                                         && columns[i].getType().isLiteral()) { 
    331                                 result.add(columns[i]); 
    332                         } 
    333                 } 
    334                 return result.toArray(new Column[result.size()]); 
    335         } 
     82        public Column[] getLiteralColumns(); 
    33683 
    337         public Column[] getTimeBasedColumns() { 
    338                 List<Column> result = new ArrayList<Column>(); 
    339                 Column[] columns = getColumns(); 
    340                 for (int i = 0; i < columns.length; i++) { 
    341                         if (columns[i].getType() != null 
    342                                         && columns[i].getType().isTimeBased()) { 
    343                                 result.add(columns[i]); 
    344                         } 
    345                 } 
    346                 return result.toArray(new Column[result.size()]); 
    347         } 
     84        public Column[] getTimeBasedColumns(); 
    34885 
    349         public Column[] getBooleanColumns() { 
    350                 List<Column> result = new ArrayList<Column>(); 
    351                 Column[] columns = getColumns(); 
    352                 for (int i = 0; i < columns.length; i++) { 
    353                         if (columns[i].getType() != null 
    354                                         && columns[i].getType().isBoolean()) { 
    355                                 result.add(columns[i]); 
    356                         } 
    357                 } 
    358                 return result.toArray(new Column[result.size()]); 
    359         } 
     86        public Column[] getBooleanColumns(); 
    36087 
    361         public Column[] getIndexedColumns() { 
    362                 List<Column> result = new ArrayList<Column>(); 
    363                 Column[] columns = getColumns(); 
    364                 for (int i = 0; i < columns.length; i++) { 
    365                         if (columns[i].isIndexed()) { 
    366                                 result.add(columns[i]); 
    367                         } 
    368                 } 
    369                 return result.toArray(new Column[result.size()]); 
    370         } 
    371  
    372         @Override 
    373         public String toString() { 
    374                 return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE) 
    375                                 .append("name", _name).append("type", _type).append("remarks", 
    376                                                 _remarks).toString(); 
    377         } 
    378  
    379         @Override 
    380         public int hashCode() { 
    381                 HashCodeBuilder hcb = new HashCodeBuilder(); 
    382                 hcb.append(_name).append(_type).append(_schema).append(_columns.size()); 
    383                 return hcb.toHashCode(); 
    384         } 
    385  
    386         @Override 
    387         public boolean equals(Object obj) { 
    388                 if (obj == this) { 
    389                         return true; 
    390                 } 
    391                 if (obj instanceof Table) { 
    392                         Table that = (Table) obj; 
    393                         return new EqualsBuilder().append(this.getName(), that.getName()) 
    394                                         .append(this.getType(), that.getType()).append( 
    395                                                         this.getRemarks(), that.getRemarks()).append( 
    396                                                         this.getColumns(), that.getColumns()).append( 
    397                                                         this.getRelationships(), that.getRelationships()) 
    398                                         .isEquals(); 
    399                 } 
    400                 return false; 
    401         } 
    402  
    403         public int compareTo(Table that) { 
    404                 CompareToBuilder ctb = new CompareToBuilder(); 
    405                 ctb.append(this.getName(), that.getName()); 
    406                 ctb.append(this.getSchema(), that.getSchema()); 
    407                 return ctb.toComparison(); 
    408         } 
     88        public Column[] getIndexedColumns(); 
    40989 
    41090        /** 
    41191         * @return the relationships where this table is the foreign table 
    41292         */ 
    413         public Relationship[] getForeignKeyRelationships() { 
    414                 List<Relationship> result = new ArrayList<Relationship>(); 
    415                 Relationship[] relationships = getRelationships(); 
    416                 for (Relationship relationship : relationships) { 
    417                         if (equals(relationship.getForeignTable())) { 
    418                                 result.add(relationship); 
    419                         } 
    420                 } 
    421                 return result.toArray(new Relationship[result.size()]); 
    422         } 
     93        public Relationship[] getForeignKeyRelationships(); 
    42394 
    42495        /** 
    42596         * @return the relationships where this table is the primary table 
    42697         */ 
    427         public Relationship[] getPrimaryKeyRelationships() { 
    428                 List<Relationship> result = new ArrayList<Relationship>(); 
    429                 Relationship[] relationships = getRelationships(); 
    430                 for (Relationship relationship : relationships) { 
    431                         if (equals(relationship.getPrimaryTable())) { 
    432                                 result.add(relationship); 
    433                         } 
    434                 } 
    435                 return result.toArray(new Relationship[result.size()]); 
    436         } 
     98        public Relationship[] getPrimaryKeyRelationships(); 
    43799 
    438         public Column[] getForeignKeys() { 
    439                 Set<Column> columns = new HashSet<Column>(); 
    440                 Relationship[] relationships = getForeignKeyRelationships(); 
    441                 for (Relationship relationship : relationships) { 
    442                         Column[] foreignColumns = relationship.getForeignColumns(); 
    443                         for (Column column : foreignColumns) { 
    444                                 columns.add(column); 
    445                         } 
    446                 } 
    447                 return columns.toArray(new Column[columns.size()]); 
    448         } 
     100        public Column[] getForeignKeys(); 
    449101 
    450         public Column[] getPrimaryKeys() { 
    451                 Set<Column> columns = new HashSet<Column>(); 
    452                 Relationship[] relationships = getPrimaryKeyRelationships(); 
    453                 for (Relationship relationship : relationships) { 
    454                         Column[] foreignColumns = relationship.getPrimaryColumns(); 
    455                         for (Column column : foreignColumns) { 
    456                                 columns.add(column); 
    457                         } 
    458                 } 
    459                 return columns.toArray(new Column[columns.size()]); 
    460         } 
     102        public Column[] getPrimaryKeys(); 
    461103 
    462         public String[] getColumnNames() { 
    463                 ArrayList<String> result = new ArrayList<String>(); 
    464                 Column[] columns = getColumns(); 
    465                 for (Column column : columns) { 
    466                         result.add(column.getName()); 
    467                 } 
    468                 return result.toArray(new String[result.size()]); 
    469         } 
     104        public String[] getColumnNames(); 
    470105 
    471         public Column[] getColumnsOfType(ColumnType columnType) { 
    472                 Column[] columns = getColumns(); 
    473                 return MetaModelHelper.getColumnsByType(columns, columnType); 
    474         } 
     106        public Column[] getColumnsOfType(ColumnType columnType); 
     107 
    475108} 
Note: See TracChangeset for help on using the changeset viewer.