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      public void visit(Table table) {
    24          if (StringUtils.isEmpty(table.getExtends()))
    25              return;
    26          final Table extendsTable = table.getExtendsTable();
    27          if (extendsTable == null) {
    28              addError(table, "could not find table'" + table.getExtends() + "'");
    29          }
    30          final ForeignKey foreignKey = new ForeignKey(table, extendsTable.getName());
    31          foreignKey.setType(ForeignKeyType.EXTENDS);
    32          this.log(foreignKey, "create FK  ");
    33          if (table.hasPrimaryKey()) {
    34              this.log
    35                      .warn("table '"
    36                              + table.getName()
    37                              + "' has primary key. "
    38                              + "if you use hibernate, don't set primary key because primary key will be automatically generated.");
    39          }
    40          for (Iterator i = extendsTable.getPkColumns(); i.hasNext();) {
    41              final Column foreignColumn = (Column) i.next();
    42              final Column localColumn = new Column(table, foreignColumn.getName(), foreignColumn.getType(),
    43                      foreignColumn.getSize(), foreignColumn.isRequired(), !table.hasPrimaryKey(), foreignColumn
    44                              .getDefaultValue());
    45              localColumn.setExtended(true);
    46              this.log(localColumn, "create Col ");
    47              final ForeignKeyEntry foreignKeyEntry = new ForeignKeyEntry(foreignKey, localColumn.getName(),
    48                      foreignColumn.getName());
    49              this.log(foreignKeyEntry, "create FKEntry");
    50          }
    51      }
    52
    53  }