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/15 18:29:32
    6    */
    7   package org.asyrinx.joey.gen.command.rdb;
    8
    9   import org.apache.commons.lang.StringUtils;
    10  import org.asyrinx.joey.gen.model.rdb.ForeignKey;
    11  import org.asyrinx.joey.gen.model.rdb.Index;
    12  import org.asyrinx.joey.gen.model.rdb.Table;
    13  import org.asyrinx.joey.gen.model.rdb.Unique;
    14
    15  /***
    16   * @author akima
    17   */
    18  public class BasicNaming extends RdbCommand {
    19
    20      private String separater = "_";
    21
    22      private String fkHeader = "f";
    23
    24      private String indexHeader = "i";
    25
    26      private String uniqueHeader = "u";
    27
    28      /***
    29       * @return Returns the fkHeader.
    30       */
    31      public String getFkHeader() {
    32          return fkHeader;
    33      }
    34
    35      /***
    36       * @param fkHeader
    37       *            The fkHeader to set.
    38       */
    39      public void setFkHeader(String fkIndexHeader) {
    40          this.fkHeader = fkIndexHeader;
    41      }
    42
    43      /***
    44       * @return Returns the indexHeader.
    45       */
    46      public String getIndexHeader() {
    47          return indexHeader;
    48      }
    49
    50      /***
    51       * @param indexHeader
    52       *            The indexHeader to set.
    53       */
    54      public void setIndexHeader(String indexHeader) {
    55          this.indexHeader = indexHeader;
    56      }
    57
    58      /***
    59       * @return Returns the separater.
    60       */
    61      public String getSeparater() {
    62          return separater;
    63      }
    64
    65      /***
    66       * @param separater
    67       *            The separater to set.
    68       */
    69      public void setSeparater(String separater) {
    70          this.separater = separater;
    71      }
    72
    73      public String getUniqueHeader() {
    74          return uniqueHeader;
    75      }
    76
    77      public void setUniqueHeader(String uniqueHeader) {
    78          this.uniqueHeader = uniqueHeader;
    79      }
    80
    81      /*
    82       * (non-Javadoc)
    83       *
    84       * @see org.asyrinx.joey.gen.model.rdb.RdbVisitor#visit(org.asyrinx.joey.gen.model.rdb.ForeignKey)
    85       */
    86      public void visit(ForeignKey foreignKey) {
    87          if (foreignKey == null)
    88              return;
    89          if (!StringUtils.isEmpty(foreignKey.getName()))
    90              return;
    91          final Table table = foreignKey.getParent();
    92          if (table == null)
    93              errors.add("foreignKey has no parent table.");
    94          final int index = table.getForeignKeys().indexOf(foreignKey) + 1;
    95          final String name = getFkHeader() + getSeparater() + table.getName() + getSeparater() + index;
    96          foreignKey.setName(name);
    97      }
    98
    99      /*
    100      * (non-Javadoc)
    101      *
    102      * @see org.asyrinx.joey.gen.model.rdb.RdbVisitor#visit(org.asyrinx.joey.gen.model.rdb.Index)
    103      */
    104     public void visit(Index index) {
    105         if (index == null)
    106             return;
    107         if (!StringUtils.isEmpty(index.getName()))
    108             return;
    109         final Table table = index.getParent();
    110         if (table == null)
    111             addError(index, "index has no parent table.");
    112         final ForeignKey foreignKey = table.findForeignKey(index);
    113         final String name;
    114         if (foreignKey != null) {
    115             if (StringUtils.isEmpty(foreignKey.getName()))
    116                 this.visit(foreignKey);
    117             name = getIndexHeader() + getSeparater() + foreignKey.getName();
    118         } else {
    119             final int idx = table.getIndexes().indexOf(index) + 1;
    120             name = getIndexHeader() + getSeparater() + table.getName() + getSeparater() + idx;
    121         }
    122         index.setName(name);
    123     }
    124
    125     /*
    126      * (non-Javadoc)
    127      *
    128      * @see org.asyrinx.joey.gen.command.rdb.RdbCommand#visit(org.asyrinx.joey.gen.model.rdb.Unique)
    129      */
    130     public void visit(Unique unique) {
    131         if (unique == null)
    132             return;
    133         if (!StringUtils.isEmpty(unique.getName()))
    134             return;
    135         final Table table = unique.getParent();
    136         if (table == null)
    137             addError(unique, "unique has no parent table.");
    138         final int idx = table.getUniques().indexOf(unique) + 1;
    139         final String name = getUniqueHeader() + getSeparater() + table.getName() + getSeparater() + idx;
    140         unique.setName(name);
    141     }
    142
    143 }