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/10/26 15:58:51
    6    */
    7   package org.asyrinx.joey.gen.command.rdb;
    8
    9   import java.util.Iterator;
    10
    11  import org.apache.commons.lang.StringUtils;
    12  import org.asyrinx.joey.gen.model.rdb.Column;
    13  import org.asyrinx.joey.gen.model.rdb.ForeignKey;
    14  import org.asyrinx.joey.gen.model.rdb.ForeignKeyEntry;
    15  import org.asyrinx.joey.gen.model.rdb.ForeignKeyType;
    16  import org.asyrinx.joey.gen.model.rdb.Table;
    17
    18  /***
    19   * @author takeshi
    20   */
    21  public class ExtendsToFk extends RdbCommand {
    22
    23      /***
    24       *
    25       */
    26      public ExtendsToFk() {
    27          super();
    28      }
    29
    30      /*
    31       * (non-Javadoc)
    32       *
    33       * @see org.asyrinx.joey.gen.model.rdb.visitor.RdbVisitorMock#visit(org.asyrinx.joey.gen.model.rdb.Table)
    34       */
    35      public void visit(Table table) {
    36          if (StringUtils.isEmpty(table.getExtends()))
    37              return;
    38          final Table extendsTable = table.getExtendsTable();
    39          if (extendsTable == null) {
    40              addError(table, "could not find table'" + table.getExtends() + "'");
    41          }
    42          final ForeignKey foreignKey = new ForeignKey(table, extendsTable.getName());
    43          foreignKey.setType(ForeignKeyType.EXTENDS);
    44          this.log(foreignKey, "create FK  ");
    45          if (table.hasPrimaryKey()) {
    46              this.log
    47                      .warn("table '"
    48                              + table.getName()
    49                              + "' has primary key. "
    50                              + "if you use hibernate, don't set primary key because primary key will be automatically generated.");
    51          }
    52          for (Iterator i = extendsTable.getPkColumns(); i.hasNext();) {
    53              final Column foreignColumn = (Column) i.next();
    54              final Column localColumn = new Column(table, foreignColumn.getName(), foreignColumn.getType(),
    55                      foreignColumn.getSize(), foreignColumn.isRequired(), !table.hasPrimaryKey(), foreignColumn
    56                              .getDefaultValue());
    57              localColumn.setExtended(true);
    58              this.log(localColumn, "create Col ");
    59              final ForeignKeyEntry foreignKeyEntry = new ForeignKeyEntry(foreignKey, localColumn.getName(),
    60                      foreignColumn.getName());
    61              this.log(foreignKeyEntry, "create FKEntry");
    62          }
    63      }
    64
    65  }