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      public void visit(Database database) {
    22          final int tableCount = database.getTables().size();
    23          final List list = new ArrayList();
    24          for (Iterator i = database.getTables().iterator(); i.hasNext();) {
    25              final Table table = (Table) i.next();
    26              addTable(table, list);
    27          }
    28          if (tableCount != list.size()) {
    29              addError(database, "failed to sort tables by extends( " + tableCount + " -> " + list.size() + " )");
    30              return;
    31          }
    32          database.getTables().clear();
    33          for (Iterator i = list.iterator(); i.hasNext();) {
    34              final Table table = (Table) i.next();
    35              database.getTables().add(table);
    36          }
    37      }
    38
    39      private void addTable(Table table, List dest) {
    40          if (table.getExtendsTable() != null) {
    41              if (!dest.contains(table.getExtendsTable()))
    42                  addTable(table.getExtendsTable(), dest);
    43          }
    44          if (!dest.contains(table))
    45              dest.add(table);
    46      }
    47
    48  }