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 17:15:16
    6    */
    7   package org.asyrinx.joey.gen.model.rdb;
    8
    9   import java.util.ArrayList;
    10  import java.util.Iterator;
    11  import java.util.List;
    12
    13  import org.asyrinx.brownie.core.lang.StringUtils;
    14  import org.asyrinx.joey.gen.model.ElementSet;
    15
    16  /***
    17   * @author akima
    18   */
    19  public class ForeignKey extends ElementSet {
    20
    21      /***
    22       *
    23       */
    24      public ForeignKey() {
    25          this(null, null, null);
    26      }
    27
    28      /***
    29       * @param parent
    30       * @param name
    31       */
    32      public ForeignKey(String foreignTable) {
    33          this(null, null, foreignTable);
    34      }
    35
    36      /***
    37       * @param parent
    38       * @param name
    39       */
    40      public ForeignKey(String name, String foreignTable) {
    41          this(null, name, foreignTable);
    42      }
    43
    44      /***
    45       * @param parent
    46       * @param name
    47       */
    48      public ForeignKey(Table parent, String foreignTable) {
    49          this(parent, null, foreignTable);
    50      }
    51
    52      /***
    53       * @param parent
    54       * @param name
    55       */
    56      public ForeignKey(Table parent, String name, String foreignTable) {
    57          super(parent, name);
    58          this.foreign = foreignTable;
    59      }
    60
    61      /*
    62       * (non-Javadoc)
    63       *
    64       * @see org.asyrinx.joey.gen.model.ElementSet#isEntity()
    65       */
    66      public boolean isEntity() {
    67          return true;
    68      }
    69
    70      /*
    71       * (non-Javadoc)
    72       *
    73       * @see org.asyrinx.joey.gen.model.ElementSet#add(org.asyrinx.joey.gen.model.Element)
    74       */
    75      public void add(ForeignKeyEntry entry) {
    76          super.add(entry);
    77      }
    78
    79      /*
    80       * (non-Javadoc)
    81       *
    82       * @see org.asyrinx.joey.gen.model.ElementSet#contains(org.asyrinx.joey.gen.model.Element)
    83       */
    84      public boolean contains(ForeignKeyEntry entry) {
    85          return super.contains(entry);
    86      }
    87
    88      /*
    89       * (non-Javadoc)
    90       *
    91       * @see org.asyrinx.joey.gen.model.ElementSet#getElement(int)
    92       */
    93      public ForeignKeyEntry getEntry(int idx) {
    94          return (ForeignKeyEntry) super.getElement(idx);
    95      }
    96
    97      /*
    98       * (non-Javadoc)
    99       *
    100      * @see org.asyrinx.joey.gen.model.ElementSet#get(java.lang.String)
    101      */
    102     public ForeignKeyEntry getEntry(String name) {
    103         return (ForeignKeyEntry) super.getElement(name);
    104     }
    105
    106     /*
    107      * (non-Javadoc)
    108      *
    109      * @see org.asyrinx.joey.gen.model.ElementSet#remove(java.lang.String)
    110      */
    111     public ForeignKeyEntry removeEntry(String name) {
    112         return (ForeignKeyEntry) super.removeElement(name);
    113     }
    114
    115     /*
    116      * (non-Javadoc)
    117      *
    118      * @see org.asyrinx.joey.gen.model.Element#getParentElement()
    119      */
    120     public Table getParent() {
    121         return (Table) super.getParentElement();
    122     }
    123
    124     public Table getLocal() {
    125         return getParent();
    126     }
    127
    128     protected final Table getTable(String name) {
    129         final Table parent = getParent();
    130         if (parent == null)
    131             return null;
    132         final Database database = parent.getParent();
    133         if (database == null)
    134             return null;
    135         return database.getTables().getTable(name);
    136     }
    137
    138     public Table getForeignTable() {
    139         return getTable(getForeign());
    140     }
    141
    142     public String getLocalColumnNames() {
    143         final List list = new ArrayList();
    144         for (final Iterator i = this.iterator(); i.hasNext();) {
    145             final ForeignKeyEntry entry = (ForeignKeyEntry) i.next();
    146             list.add(entry.getLocalColumn().getName());
    147         }
    148         return StringUtils.join(list.iterator(), ",");
    149     }
    150
    151     public String getForeignColumnNames() {
    152         final List list = new ArrayList();
    153         for (final Iterator i = this.iterator(); i.hasNext();) {
    154             final ForeignKeyEntry entry = (ForeignKeyEntry) i.next();
    155             list.add(entry.getForeignColumn().getName());
    156         }
    157         return StringUtils.join(list.iterator(), ",");
    158     }
    159
    160     private String foreign = null;
    161
    162     private boolean indexed = true;
    163
    164     private Index index = null;
    165
    166     private ForeignKeyType type = ForeignKeyType.NORMAL;
    167
    168     private String cascade = CASCADE_NONE;
    169
    170     /***
    171      * @return Returns the foreign.
    172      */
    173     public String getForeign() {
    174         return foreign;
    175     }
    176
    177     /***
    178      * @param foreign
    179      *            The foreign to set.
    180      */
    181     public void setForeign(String foreignTabe) {
    182         this.foreign = foreignTabe;
    183     }
    184
    185     /***
    186      * @return Returns the indexed.
    187      */
    188     public boolean isIndexed() {
    189         return indexed;
    190     }
    191
    192     /***
    193      * @param indexed
    194      *            The indexed to set.
    195      */
    196     public void setIndexed(boolean indexed) {
    197         this.indexed = indexed;
    198     }
    199
    200     /***
    201      * @return Returns the index.
    202      */
    203     public Index getIndex() {
    204         return index;
    205     }
    206
    207     /***
    208      * @param index
    209      *            The index to set.
    210      */
    211     public void setIndex(Index index) {
    212         this.index = index;
    213     }
    214
    215     /***
    216      * @return Returns the type.
    217      */
    218     public ForeignKeyType getType() {
    219         return type;
    220     }
    221
    222     /***
    223      * @param type
    224      *            The type to set.
    225      */
    226     public void setType(ForeignKeyType type) {
    227         this.type = type;
    228     }
    229
    230     /***
    231      * @param column
    232      * @return
    233      */
    234     public boolean containsAsLocal(Column column) {
    235         for (Iterator i = this.iterator(); i.hasNext();) {
    236             final ForeignKeyEntry entry = (ForeignKeyEntry) i.next();
    237             if (entry.getLocalColumn() == column)
    238                 return true;
    239         }
    240         return false;
    241     }
    242
    243     public String getCascade() {
    244         return cascade;
    245     }
    246
    247     public void setCascade(String cascade) {
    248         this.cascade = cascade;
    249     }
    250
    251     public boolean isDeleteCascade() {
    252         return CASCADE_ALL.equals(getCascade()) || CASCADE_DELETE.equals(getCascade());
    253     }
    254
    255     public boolean isUpdateCascade() {
    256         return CASCADE_ALL.equals(getCascade()) || CASCADE_UPDATE.equals(getCascade());
    257     }
    258
    259     public static final String CASCADE_NONE = "none";
    260
    261     public static final String CASCADE_DELETE = "delete";
    262
    263     public static final String CASCADE_UPDATE = "update";
    264
    265     public static final String CASCADE_ALL = "all";
    266
    267     public static final String[] CASCADES = new String[] { CASCADE_NONE, CASCADE_DELETE, CASCADE_UPDATE, CASCADE_ALL };
    268
    269 }