View Javadoc

    1   /*
    2    * joey-gen and its relative products are published under the terms
    3    * of the Apache Software License.
    4    *
    5    * Created on 2004/08/10 16:59:05
    6    */
    7   package org.asyrinx.joey.gen.model.rdb;
    8
    9   import java.util.ArrayList;
    10  import java.util.Collections;
    11  import java.util.Iterator;
    12  import java.util.List;
    13
    14  import org.apache.commons.lang.builder.EqualsBuilder;
    15  import org.asyrinx.joey.gen.model.Element;
    16
    17  /***
    18   * @author akima
    19   */
    20  public class Table extends Element {
    21
    22      /***
    23       *
    24       */
    25      public Table() {
    26          super();
    27      }
    28
    29      /***
    30       *
    31       */
    32      public Table(String name) {
    33          this(null, name, null);
    34      }
    35
    36      /***
    37       *
    38       */
    39      public Table(String name, String label) {
    40          this(null, name, label);
    41      }
    42
    43      /***
    44       *
    45       */
    46      public Table(Database parent, String name) {
    47          this(parent, name, null);
    48      }
    49
    50      /***
    51       *
    52       */
    53      public Table(Database parent, String name, String label) {
    54          super(parent, name, label);
    55      }
    56
    57      /*
    58       * (non-Javadoc)
    59       *
    60       * @see org.asyrinx.joey.gen.model.Element#getParentElement()
    61       */
    62      public Database getParent() {
    63          return (Database) super.getParentElement();
    64      }
    65
    66      /*
    67       * (non-Javadoc)
    68       *
    69       * @see org.asyrinx.joey.gen.model.Element#add(org.asyrinx.joey.gen.model.Element)
    70       */
    71      public void add(Element element) {
    72          if (element instanceof Column)
    73              columns.add((Column) element);
    74          //UniqueはIndexの派生クラスなのでIndexより先にチェックしないとダメ
    75          else if (element instanceof Unique)
    76              uniques.add((Unique) element);
    77          else if (element instanceof Index)
    78              indexes.add((Index) element);
    79          else if (element instanceof ForeignKey)
    80              foreignKeys.add((ForeignKey) element);
    81          else if (element instanceof TablePattern)
    82              patterns.add((TablePattern) element);
    83          else
    84              super.add(element);
    85      }
    86
    87      private final ColumnSet columns = new ColumnSet(this);
    88
    89      private final IndexSet indexes = new IndexSet(this);
    90
    91      private final IndexSet uniques = new IndexSet(this);
    92
    93      private final ForeignKeySet foreignKeys = new ForeignKeySet(this);
    94
    95      private final TablePatternSet patterns = new TablePatternSet(this);
    96
    97      private final PrimaryKey primaryKey = new PrimaryKey(this, "pk");
    98
    99      private String _extends = null;
    100
    101     private final List referrers = new ArrayList();
    102
    103     public List getReferrers() {
    104         return referrers;
    105     }
    106
    107     /***
    108      * @return Returns the columns.
    109      */
    110     public ColumnSet getColumns() {
    111         return columns;
    112     }
    113
    114     /***
    115      * @return Returns the foreignKeys.
    116      */
    117     public ForeignKeySet getForeignKeys() {
    118         return foreignKeys;
    119     }
    120
    121     /***
    122      * @return Returns the indexes.
    123      */
    124     public IndexSet getIndexes() {
    125         return indexes;
    126     }
    127
    128     /***
    129      * @return Returns the uniques.
    130      */
    131     public IndexSet getUniques() {
    132         return uniques;
    133     }
    134
    135     public TablePatternSet getPatterns() {
    136         return patterns;
    137     }
    138
    139     public ForeignKey findForeignKey(Index index) {
    140         for (final Iterator iterator = getForeignKeys().iterator(); iterator.hasNext();) {
    141             final ForeignKey foreignKey = (ForeignKey) iterator.next();
    142             if (foreignKey.getIndex() == index)
    143                 return foreignKey;
    144         }
    145         return null;
    146     }
    147
    148     public Iterator getPkColumns() {
    149         final List result = new ArrayList();
    150         for (final Iterator i = columns.iterator(); i.hasNext();) {
    151             final Column column = (Column) i.next();
    152             if (column.isPrimaryKey())
    153                 result.add(column);
    154         }
    155         return Collections.unmodifiableList(result).iterator();
    156     }
    157
    158     public boolean hasPrimaryKey() {
    159         for (final Iterator i = columns.iterator(); i.hasNext();) {
    160             final Column column = (Column) i.next();
    161             if (column.isPrimaryKey())
    162                 return true;
    163         }
    164         return false;
    165     }
    166
    167     protected final Table getTable(String name) {
    168         final Database database = getParent();
    169         if (database == null)
    170             return null;
    171         return database.getTables().getTable(name);
    172     }
    173
    174     public Table getExtendsTable() {
    175         return getTable(getExtends());
    176     }
    177
    178     /***
    179      * @return Returns the _extends.
    180      */
    181     public String getExtends() {
    182         return _extends;
    183     }
    184
    185     /***
    186      * @param _extends
    187      *            The _extends to set.
    188      */
    189     public void setExtends(String extendz) {
    190         this._extends = extendz;
    191     }
    192
    193     /***
    194      * @return Returns the primaryKey.
    195      */
    196     public PrimaryKey getPrimaryKey() {
    197         return primaryKey;
    198     }
    199
    200     private Column captionColumn = null;
    201
    202     public Column getCaptionColumn() {
    203         return captionColumn;
    204     }
    205
    206     public void setCaptionColumn(Column captionColumn) {
    207         this.captionColumn = captionColumn;
    208     }
    209
    210     /*
    211      * (non-Javadoc)
    212      *
    213      * @see java.lang.Object#equals(java.lang.Object)
    214      */
    215     public boolean equals(Object obj) {
    216         if (!super.equals(obj))
    217             return false;
    218         if (!(obj instanceof Table))
    219             return false;
    220         final Table other = (Table) obj;
    221         return new EqualsBuilder() //
    222                 .append(this.getColumns(), other.getColumns()) //
    223                 .append(this.getIndexes(), other.getIndexes()) //
    224                 .append(this.getUniques(), other.getUniques()) //
    225                 .append(this.getForeignKeys(), other.getForeignKeys()) //
    226                 .append(this.getPatterns(), other.getPatterns()) //
    227                 .append(this.getExtends(), other.getExtends()) //
    228                 .append(this.getCaptionColumn(), other.getCaptionColumn()) //
    229                 .isEquals();
    230     }
    231
    232 }