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