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/08/10 16:59:32
6    */
7   package org.asyrinx.joey.gen.model.rdb;
8   
9   import java.util.Iterator;
10  
11  import org.apache.commons.lang.builder.EqualsBuilder;
12  import org.asyrinx.brownie.core.lang.StringUtils;
13  import org.asyrinx.joey.gen.model.Element;
14  
15  import test.org.asyrinx.joey.gen.model.rdb.Constants;
16  
17  /***
18   * @author akima
19   */
20  public class Database extends Element {
21  
22      public Database() {
23          super();
24      }
25  
26      /***
27       *  
28       */
29      public Database(Databases parent, String name) {
30          super(parent, name);
31      }
32  
33      /*
34       * (non-Javadoc)
35       * 
36       * @see org.asyrinx.joey.gen.model.Element#getParentElement()
37       */
38      public Databases getParent() {
39          return (Databases) super.getParentElement();
40      }
41  
42      /*
43       * (non-Javadoc)
44       * 
45       * @see org.asyrinx.joey.gen.model.Element#add(org.asyrinx.joey.gen.model.Element)
46       */
47      public void add(Element element) {
48          if (element instanceof Table)
49              tables.add((Table) element);
50          else if (element instanceof RdbEnumeration)
51              enumerations.add((RdbEnumeration) element);
52          else
53              super.add(element);
54      }
55  
56      private final TableSet tables = new TableSet(this);
57  
58      /***
59       * @return Returns the tables.
60       */
61      public TableSet getTables() {
62          return tables;
63      }
64  
65      private final RdbEnumerationSet enumerations = new RdbEnumerationSet(this);
66  
67      /***
68       * @return Returns the enumerations.
69       */
70      public RdbEnumerationSet getEnumerations() {
71          return enumerations;
72      }
73  
74      /***
75       * @param committed
76       */
77      public void moveTables(Database dest) {
78          for (final Iterator i = tables.iterator(); i.hasNext();) {
79              final Table table = (Table) i.next();
80              dest.getTables().add(table);
81          }
82      }
83  
84      /***
85       * @param committed
86       */
87      public void moveEnumerations(Database dest) {
88          for (final Iterator i = enumerations.iterator(); i.hasNext();) {
89              final RdbEnumeration enumeration = (RdbEnumeration) i.next();
90              dest.getEnumerations().add(enumeration);
91          }
92      }
93  
94      /***
95       * @param string
96       * @return
97       */
98      public Column getColumn(String columnName) {
99          final String[] nameParts = StringUtils.tokenizeToArray(columnName, Constants.ELEMENTS_DELIMITER);
100         if (nameParts.length == 2) {
101             return getColumn(nameParts[0], nameParts[1]);
102         } else if (nameParts.length == 3) {
103             if (getParent() != null)
104                 return getParent().getColumn(columnName);
105         }
106         return null;
107     }
108 
109     public Column getColumn(String tableName, String columnName) {
110         final Table table = getTables().getTable(tableName);
111         return (table == null) ? null : table.getColumns().getColumn(columnName);
112 
113     }
114 
115     /*
116      * (non-Javadoc)
117      * 
118      * @see java.lang.Object#equals(java.lang.Object)
119      */
120     public boolean equals(Object obj) {
121         if (!super.equals(obj))
122             return false;
123         if (!(obj instanceof Database))
124             return false;
125         final Database other = (Database) obj;
126         return new EqualsBuilder() //
127                 .append(this.getTables(), other.getTables()) //
128                 .append(this.getEnumerations(), other.getEnumerations()) //
129                 .isEquals();
130     }
131 
132 }