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:05
6    */
7   package org.asyrinx.joey.gen.model.rdb;
8   
9   import java.util.ArrayList;
10  import java.util.Collections;
11  import java.util.Iterator;
12  import java.util.List;
13  
14  import org.asyrinx.joey.gen.model.Element;
15  
16  /***
17   * @author akima
18   */
19  public class Table extends Element {
20  
21      /***
22       *  
23       */
24      public Table() {
25          super();
26      }
27  
28      /***
29       *  
30       */
31      public Table(String name) {
32          this(null, name, null);
33      }
34  
35      /***
36       *  
37       */
38      public Table(String name, String label) {
39          this(null, name, label);
40      }
41  
42      /***
43       *  
44       */
45      public Table(Database parent, String name) {
46          this(parent, name, null);
47      }
48  
49      /***
50       *  
51       */
52      public Table(Database parent, String name, String label) {
53          super(parent, name, label);
54      }
55  
56      /*
57       * (non-Javadoc)
58       * 
59       * @see org.asyrinx.joey.gen.model.Element#getParentElement()
60       */
61      public Database getParent() {
62          return (Database) super.getParentElement();
63      }
64  
65      /*
66       * (non-Javadoc)
67       * 
68       * @see org.asyrinx.joey.gen.model.Element#add(org.asyrinx.joey.gen.model.Element)
69       */
70      public void add(Element element) {
71          if (element instanceof Column)
72              columns.add((Column) element);
73          else if (element instanceof Unique)
74              uniques.add((Unique) element);
75          else if (element instanceof Index)
76              indexes.add((Index) element);
77          else if (element instanceof ForeignKey)
78              foreignKeys.add((ForeignKey) element);
79          else
80              super.add(element);
81      }
82  
83      private final ColumnSet columns = new ColumnSet(this);
84  
85      private final IndexSet indexes = new IndexSet(this);
86  
87      private final IndexSet uniques = new IndexSet(this);
88  
89      private final ForeignKeySet foreignKeys = new ForeignKeySet(this);
90  
91      private final PrimaryKey primaryKey = new PrimaryKey(this, "pk");
92  
93      private String _extends = null;
94  
95      private final List referrers = new ArrayList();
96  
97      public List getReferrers() {
98          return referrers;
99      }
100 
101     /***
102      * @return Returns the columns.
103      */
104     public ColumnSet getColumns() {
105         return columns;
106     }
107 
108     /***
109      * @return Returns the foreignKeys.
110      */
111     public ForeignKeySet getForeignKeys() {
112         return foreignKeys;
113     }
114 
115     /***
116      * @return Returns the indexes.
117      */
118     public IndexSet getIndexes() {
119         return indexes;
120     }
121 
122     /***
123      * @return Returns the uniques.
124      */
125     public IndexSet getUniques() {
126         return uniques;
127     }
128 
129     public ForeignKey findForeignKey(Index index) {
130         for (final Iterator iterator = getForeignKeys().iterator(); iterator.hasNext();) {
131             final ForeignKey foreignKey = (ForeignKey) iterator.next();
132             if (foreignKey.getIndex() == index)
133                 return foreignKey;
134         }
135         return null;
136     }
137 
138     public Iterator getPkColumns() {
139         final List result = new ArrayList();
140         for (final Iterator i = columns.iterator(); i.hasNext();) {
141             final Column column = (Column) i.next();
142             if (column.isPrimaryKey())
143                 result.add(column);
144         }
145         return Collections.unmodifiableList(result).iterator();
146     }
147 
148     public boolean hasPrimaryKey() {
149         for (final Iterator i = columns.iterator(); i.hasNext();) {
150             final Column column = (Column) i.next();
151             if (column.isPrimaryKey())
152                 return true;
153         }
154         return false;
155     }
156 
157     protected final Table getTable(String name) {
158         final Database database = getParent();
159         if (database == null)
160             return null;
161         return database.getTables().getTable(name);
162     }
163 
164     public Table getExtendsTable() {
165         return getTable(getExtends());
166     }
167 
168     /***
169      * @return Returns the _extends.
170      */
171     public String getExtends() {
172         return _extends;
173     }
174 
175     /***
176      * @param _extends
177      *            The _extends to set.
178      */
179     public void setExtends(String extendz) {
180         this._extends = extendz;
181     }
182 
183     /***
184      * @return Returns the primaryKey.
185      */
186     public PrimaryKey getPrimaryKey() {
187         return primaryKey;
188     }
189 
190 }