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      /***
22       *  
23       */
24      public PrepareCaptionColumn() {
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.Table)
32       */
33      public void visit(Table table) {
34          for (int i = 0; i < predicates.length; i++) {
35              final Column column = findColumn(table, predicates[i]);
36              if (column != null) {
37                  table.setCaptionColumn(column);
38                  return;
39              }
40          }
41      }
42  
43      final Predicate[] predicates = new Predicate[] { //
44      new Predicate() {
45          public boolean evaluate(Object obj) {
46              if (!(obj instanceof Column))
47                  throw new UnsupportedOperationException();
48              final Column column = (Column) obj;
49              return column.isCaption();
50          }
51      }, new Predicate() {
52          public boolean evaluate(Object obj) {
53              if (!(obj instanceof Column))
54                  throw new UnsupportedOperationException();
55              final Column column = (Column) obj;
56              return "name".equalsIgnoreCase(column.getName());
57          }
58      }, new Predicate() {
59          public boolean evaluate(Object obj) {
60              if (!(obj instanceof Column))
61                  throw new UnsupportedOperationException();
62              final Column column = (Column) obj;
63              if (StringUtils.isEmpty(column.getName()))
64                  return false;
65              return column.getName().toLowerCase().endsWith("name");
66          }
67      }
68  
69      };
70  
71      private Column findColumn(Table table, Predicate predicate) {
72          for (Iterator i = table.getColumns().iterator(); i.hasNext();) {
73              final Column column = (Column) i.next();
74              if (predicate.evaluate(column))
75                  return column;
76          }
77          return null;
78      }
79  
80  }