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/10 9:43:18
6    */
7   package org.asyrinx.joey.gen.command.rdb;
8   
9   import java.util.Map;
10  
11  import org.apache.commons.lang.StringUtils;
12  import org.asyrinx.joey.gen.model.Element;
13  import org.asyrinx.joey.gen.model.rdb.RdbEnumeration;
14  import org.asyrinx.joey.gen.model.rdb.Table;
15  
16  /***
17   * @author takeshi
18   */
19  public class CopyAncestorOption extends RdbCommand {
20  
21      /***
22       *  
23       */
24      public CopyAncestorOption(Object optionKey) {
25          super();
26          this.optionKey = optionKey;
27      }
28  
29      protected final Object optionKey;
30  
31      public void visit(Table table) {
32          table.getOptions().put(optionKey, getOptionValue(table));
33      }
34  
35      public void visit(RdbEnumeration enumeration) {
36          enumeration.getOptions().put(optionKey, getOptionValue(enumeration));
37      }
38  
39      public String getOptionValue(Element element) {
40          final String result = getAncestorOptionValue(element);
41          if (result != null)
42              return result;
43          else
44              return (element.getParentElement() == null) ? null : element.getParentElement().getName();
45      }
46  
47      protected String getAncestorOptionValue(Element element) {
48          String result = getValue(element.getOptions());
49          if (!StringUtils.isEmpty(result)) {
50              return result;
51          } else {
52              if (element.getParentElement() != null)
53                  return getAncestorOptionValue(element.getParentElement());
54              else
55                  return null;
56          }
57      }
58  
59      private String getValue(Map options) {
60          final Object result = options.get(optionKey);
61          return (result != null) ? result.toString() : null;
62      }
63  
64  }