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:00:19
    6    */
    7   package org.asyrinx.joey.gen.command.rdb;
    8
    9   import org.apache.commons.lang.StringUtils;
    10  import org.asyrinx.brownie.core.lang.ArrayUtils;
    11  import org.asyrinx.joey.gen.hibernate.Cascade;
    12  import org.asyrinx.joey.gen.model.rdb.ForeignKey;
    13  import org.asyrinx.joey.gen.model.rdb.ForeignKeyEntry;
    14  import org.asyrinx.joey.gen.model.rdb.Table;
    15
    16  /***
    17   * @author akima
    18   */
    19  public class CheckForeignKey extends RdbCommand {
    20
    21      public void visit(ForeignKey foreignKey) {
    22          if (StringUtils.isEmpty(foreignKey.getForeign()))
    23              addError(foreignKey, "foreignKey requires foreignTable");
    24          final Table foreignTable = foreignKey.getForeignTable();
    25          if (foreignTable == null)
    26              addError(foreignKey, "table '" + foreignKey.getForeign() + "' not found");
    27          if (!isAvailableCascadeValue(foreignKey.getCascade()))
    28              addError(foreignKey, "illegal cascade value '" + foreignKey.getCascade() + "'."
    29                      + "cascade must be " + ArrayUtils.toString(Cascade.CASCADES));
    30      }
    31
    32      private boolean isAvailableCascadeValue(String cascade) {
    33          if (StringUtils.isEmpty(cascade))
    34              return true;
    35          for (int i = 0; i < Cascade.CASCADES.length; i++)
    36              if (Cascade.CASCADES[i].getName().equals(cascade))
    37                  return true;
    38          return false;
    39      }
    40
    41      public void visit(ForeignKeyEntry entry) {
    42          if (StringUtils.isEmpty(entry.getLocal()))
    43              addError(entry, "foreignKeyEntry requires local");
    44          if (StringUtils.isEmpty(entry.getForeign()))
    45              addError(entry, "foreignKeyEntry requires foreign");
    46          final ForeignKey foreignKey = entry.getParent();
    47          if (entry.getLocalColumn() == null)
    48              addError(entry, "column '" + entry.getLocal() + "' not found in table '"
    49                      + foreignKey.getLocal().getFullName() + "'");
    50          if (entry.getForeignColumn() == null)
    51              addError(entry, "column '" + entry.getForeign() + "' not found in table '"
    52                      + foreignKey.getForeignTable().getFullName() + "'");
    53      }
    54
    55  }