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/11/25 16:10:30
    6    */
    7   package org.asyrinx.joey.gen.command.rdb;
    8
    9   import java.util.ArrayList;
    10  import java.util.Iterator;
    11  import java.util.List;
    12
    13  import org.asyrinx.joey.gen.model.rdb.Database;
    14  import org.asyrinx.joey.gen.model.rdb.Table;
    15
    16  /***
    17   * @author takeshi
    18   */
    19  public class SortTablesByExtends extends RdbCommand {
    20
    21      /***
    22       *
    23       */
    24      public SortTablesByExtends() {
    25          super();
    26      }
    27
    28      /*
    29       * (non-Javadoc)
    30       *
    31       * @see org.asyrinx.joey.gen.command.rdb.RdbCommand#visit(org.asyrinx.joey.gen.model.rdb.Database)
    32       */
    33      public void visit(Database database) {
    34          final int tableCount = database.getTables().size();
    35          final List list = new ArrayList();
    36          for (Iterator i = database.getTables().iterator(); i.hasNext();) {
    37              final Table table = (Table) i.next();
    38              addTable(table, list);
    39          }
    40          if (tableCount != list.size()) {
    41              addError(database, "failed to sort tables by extends( " + tableCount + " -> " + list.size() + " )");
    42              return;
    43          }
    44          database.getTables().clear();
    45          for (Iterator i = list.iterator(); i.hasNext();) {
    46              final Table table = (Table) i.next();
    47              database.getTables().add(table);
    48          }
    49      }
    50
    51      private void addTable(Table table, List dest) {
    52          if (table.getExtendsTable() != null) {
    53              if (!dest.contains(table.getExtendsTable()))
    54                  addTable(table.getExtendsTable(), dest);
    55          }
    56          if (!dest.contains(table))
    57              dest.add(table);
    58      }
    59
    60  }