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/12/09 16:11:51
    6    */
    7   package org.asyrinx.joey.gen.command.rdb;
    8
    9   import java.util.Iterator;
    10
    11  import org.apache.commons.collections.Predicate;
    12  import org.apache.commons.lang.StringUtils;
    13  import org.asyrinx.joey.gen.model.rdb.Column;
    14  import org.asyrinx.joey.gen.model.rdb.Table;
    15
    16  /***
    17   * @author takeshi
    18   */
    19  public class PrepareCaptionColumn extends RdbCommand {
    20
    21      public void visit(Table table) {
    22          for (int i = 0; i < predicates.length; i++) {
    23              final Column column = findColumn(table, predicates[i]);
    24              if (column != null) {
    25                  table.setCaptionColumn(column);
    26                  return;
    27              }
    28          }
    29      }
    30
    31      final Predicate[] predicates = new Predicate[] { //
    32      new Predicate() {
    33          public boolean evaluate(Object obj) {
    34              if (!(obj instanceof Column))
    35                  throw new UnsupportedOperationException();
    36              final Column column = (Column) obj;
    37              return column.isCaption();
    38          }
    39      }, new Predicate() {
    40          public boolean evaluate(Object obj) {
    41              if (!(obj instanceof Column))
    42                  throw new UnsupportedOperationException();
    43              final Column column = (Column) obj;
    44              return "name".equalsIgnoreCase(column.getName());
    45          }
    46      }, new Predicate() {
    47          public boolean evaluate(Object obj) {
    48              if (!(obj instanceof Column))
    49                  throw new UnsupportedOperationException();
    50              final Column column = (Column) obj;
    51              if (StringUtils.isEmpty(column.getName()))
    52                  return false;
    53              return column.getName().toLowerCase().endsWith("name");
    54          }
    55      }
    56
    57      };
    58
    59      private Column findColumn(Table table, Predicate predicate) {
    60          for (Iterator i = table.getColumns().iterator(); i.hasNext();) {
    61              final Column column = (Column) i.next();
    62              if (predicate.evaluate(column))
    63                  return column;
    64          }
    65          return null;
    66      }
    67
    68  }