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