001    /*
002     * JBoss DNA (http://www.jboss.org/dna)
003     * See the COPYRIGHT.txt file distributed with this work for information
004     * regarding copyright ownership.  Some portions may be licensed
005     * to Red Hat, Inc. under one or more contributor license agreements.
006     * See the AUTHORS.txt file in the distribution for a full listing of 
007     * individual contributors. 
008     *
009     * JBoss DNA is free software. Unless otherwise indicated, all code in JBoss DNA
010     * is licensed to you under the terms of the GNU Lesser General Public License as
011     * published by the Free Software Foundation; either version 2.1 of
012     * the License, or (at your option) any later version.
013     *
014     * JBoss DNA is distributed in the hope that it will be useful,
015     * but WITHOUT ANY WARRANTY; without even the implied warranty of
016     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
017     * Lesser General Public License for more details.
018     *
019     * You should have received a copy of the GNU Lesser General Public
020     * License along with this software; if not, write to the Free
021     * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
022     * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
023     */
024    package org.jboss.dna.common.jdbc.model.spi;
025    
026    import java.util.Set;
027    import java.util.HashSet;
028    import org.jboss.dna.common.jdbc.model.api.BestRowIdentifier;
029    import org.jboss.dna.common.jdbc.model.api.BestRowIdentifierScopeType;
030    import org.jboss.dna.common.jdbc.model.api.Catalog;
031    import org.jboss.dna.common.jdbc.model.api.ForeignKey;
032    import org.jboss.dna.common.jdbc.model.api.Index;
033    import org.jboss.dna.common.jdbc.model.api.PrimaryKey;
034    import org.jboss.dna.common.jdbc.model.api.Privilege;
035    import org.jboss.dna.common.jdbc.model.api.Schema;
036    import org.jboss.dna.common.jdbc.model.api.Table;
037    import org.jboss.dna.common.jdbc.model.api.TableColumn;
038    import org.jboss.dna.common.jdbc.model.api.TableType;
039    
040    /**
041     * Provides all core database table specific metadata.
042     * 
043     * @author <a href="mailto:litsenko_sergey@yahoo.com">Sergiy Litsenko</a>
044     */
045    public class TableBean extends SchemaObjectBean implements Table {
046        private static final long serialVersionUID = -1315274844163173964L;
047        private Set<TableColumn> columns = new HashSet<TableColumn>();
048        private Set<ForeignKey> foreignKeys = new HashSet<ForeignKey>();
049        private Set<Index> indexes = new HashSet<Index>();
050        private Set<TableColumn> versionColumns = new HashSet<TableColumn>();
051        private Set<Privilege> privileges = new HashSet<Privilege>();
052        private Set<BestRowIdentifier> bestRowIdentifiers = new HashSet<BestRowIdentifier>();
053        private TableType tableType;
054        private Catalog typeCatalog;
055        private Schema typeSchema;
056        private String typeName;
057        private String selfReferencingColumnName;
058        private String referenceGeneration;
059        private PrimaryKey primaryKey;
060        private Table superTable;
061    
062        /**
063         * Default constructor
064         */
065        public TableBean() {
066        }
067    
068        /**
069         * Returns type of table such as: "TABLE", "VIEW", "SYSTEM TABLE", "GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS", "SYNONYM".
070         * 
071         * @return type of table.
072         */
073        public TableType getTableType() {
074            return tableType;
075        }
076    
077        /**
078         * Sets type of table such as: "TABLE", "VIEW", "SYSTEM TABLE", "GLOBAL TEMPORARY", "LOCAL TEMPORARY", "ALIAS", "SYNONYM".
079         * 
080         * @param tableType the type of table.
081         */
082        public void setTableType( TableType tableType ) {
083            this.tableType = tableType;
084        }
085    
086        /**
087         * Gets type catalog
088         * 
089         * @return types catalog (may be <code>null</code>)
090         */
091        public Catalog getTypeCatalog() {
092            return typeCatalog;
093        }
094    
095        /**
096         * Sets type catalog
097         * 
098         * @param typeCatalog the types catalog (may be <code>null</code>)
099         */
100        public void setTypeCatalog( Catalog typeCatalog ) {
101            this.typeCatalog = typeCatalog;
102        }
103    
104        /**
105         * Gets type schema
106         * 
107         * @return types schema (may be <code>null</code>)
108         */
109        public Schema getTypeSchema() {
110            return typeSchema;
111        }
112    
113        /**
114         * Sets type schema
115         * 
116         * @param typeSchema the types schema (may be <code>null</code>)
117         */
118        public void setTypeSchema( Schema typeSchema ) {
119            this.typeSchema = typeSchema;
120        }
121    
122        /**
123         * Gets type name
124         * 
125         * @return types name (may be <code>null</code>)
126         */
127        public String getTypeName() {
128            return typeName;
129        }
130    
131        /**
132         * Sets type name
133         * 
134         * @param typeName types name (may be <code>null</code>)
135         */
136        public void setTypeName( String typeName ) {
137            this.typeName = typeName;
138        }
139    
140        /**
141         * Gets name of the designated "identifier" column of a typed table (may be <code>null</code>)
142         * 
143         * @return name of the designated "identifier" column of a typed table (may be <code>null</code>)
144         */
145        public String getSelfReferencingColumnName() {
146            return selfReferencingColumnName;
147        }
148    
149        /**
150         * Sets name of the designated "identifier" column of a typed table (may be <code>null</code>)
151         * 
152         * @param selfReferencingColumnName the name of the designated "identifier" column of a typed table (may be <code>null</code>)
153         */
154        public void setSelfReferencingColumnName( String selfReferencingColumnName ) {
155            this.selfReferencingColumnName = selfReferencingColumnName;
156        }
157    
158        /**
159         * specifies how values in getSelfReferencingColumnName () are created. Values are "SYSTEM", "USER", "DERIVED". (may be
160         * <code>null</code>)
161         * 
162         * @return how values in getSelfReferencingColumnName () are created.
163         */
164        public String getReferenceGeneration() {
165            return referenceGeneration;
166        }
167    
168        /**
169         * specifies how values in getSelfReferencingColumnName () are created. Values are "SYSTEM", "USER", "DERIVED". (may be
170         * <code>null</code>)
171         * 
172         * @param referenceGeneration how values in getSelfReferencingColumnName () are created.
173         */
174        public void setReferenceGeneration( String referenceGeneration ) {
175            this.referenceGeneration = referenceGeneration;
176        }
177    
178        /**
179         * Gets a set of table columns
180         * 
181         * @return a set of table columns.
182         */
183        public Set<TableColumn> getColumns() {
184            return columns;
185        }
186    
187        /**
188         * Adds TableColumn
189         * 
190         * @param column the TableColumn
191         */
192        public void addColumn( TableColumn column ) {
193            columns.add(column);
194        }
195    
196        /**
197         * deletes TableColumn
198         * 
199         * @param column the TableColumn
200         */
201        public void deleteColumn( TableColumn column ) {
202            columns.remove(column);
203        }
204    
205        /**
206         * Returns table column for specified column name or null
207         * 
208         * @param columnName the name of column
209         * @return table column for specified column name or null.
210         */
211        public TableColumn findColumnByName( String columnName ) {
212            for (TableColumn c : columns) {
213                if (c.getName().equals(columnName)) {
214                    return c;
215                }
216            }
217            // return nothing
218            return null;
219        }
220    
221        /**
222         * Gets a table primary key
223         * 
224         * @return a table primary key.
225         */
226        public PrimaryKey getPrimaryKey() {
227            return primaryKey;
228        }
229    
230        /**
231         * Sets a table primary key
232         * 
233         * @param primaryKey the table primary key.
234         */
235        public void setPrimaryKey( PrimaryKey primaryKey ) {
236            this.primaryKey = primaryKey;
237        }
238    
239        /**
240         * Gets a set of table foreign key columns
241         * 
242         * @return a set of table foreign keys.
243         */
244        public Set<ForeignKey> getForeignKeys() {
245            return foreignKeys;
246        }
247    
248        /**
249         * adds ForeignKey
250         * 
251         * @param foreignKey the ForeignKey
252         */
253        public void addForeignKey( ForeignKey foreignKey ) {
254            foreignKeys.add(foreignKey);
255        }
256    
257        /**
258         * deletes ForeignKey
259         * 
260         * @param foreignKey the ForeignKey
261         */
262        public void deleteForeignKey( ForeignKey foreignKey ) {
263            foreignKeys.remove(foreignKey);
264        }
265    
266        /**
267         * Returns table foreign key for specified name or null
268         * 
269         * @param fkName the name of foreign key
270         * @return table foreign key for specified name or null.
271         */
272        public ForeignKey findForeignKeyByName( String fkName ) {
273            for (ForeignKey fk : foreignKeys) {
274                if (fk.getName().equals(fkName)) {
275                    return fk;
276                }
277            }
278            // return nothing
279            return null;
280        }
281    
282        /**
283         * Gets a set of table indexes
284         * 
285         * @return a set of table indexes.
286         */
287        public Set<Index> getIndexes() {
288            return indexes;
289        }
290    
291        /**
292         * adds Index
293         * 
294         * @param index the Index
295         */
296        public void addIndex( Index index ) {
297            indexes.add(index);
298        }
299    
300        /**
301         * deletes Index
302         * 
303         * @param index the Index
304         */
305        public void deleteIndex( Index index ) {
306            indexes.remove(index);
307        }
308    
309        /**
310         * Returns table index for specified name or null
311         * 
312         * @param indexName the name of index
313         * @return table index for specified name or null.
314         */
315        public Index findIndexByName( String indexName ) {
316            for (Index i : indexes) {
317                if (i.getName().equals(indexName)) {
318                    return i;
319                }
320            }
321            // return nothing
322            return null;
323        }
324    
325        /**
326         * Gets a set of table version columns
327         * 
328         * @return a set of table version columns.
329         */
330        public Set<TableColumn> getVersionColumns() {
331            return versionColumns;
332        }
333    
334        /**
335         * adds version column
336         * 
337         * @param tableColumn the TableColumn
338         */
339        public void addVersionColumn( TableColumn tableColumn ) {
340            versionColumns.add(tableColumn);
341        }
342    
343        /**
344         * deletes version column
345         * 
346         * @param tableColumn the version column
347         */
348        public void deleteVersionColumn( TableColumn tableColumn ) {
349            versionColumns.remove(tableColumn);
350        }
351    
352        /**
353         * Returns table version column for specified name or null
354         * 
355         * @param columnName the name of Version Column
356         * @return table Version Column for specified name or null.
357         */
358        public TableColumn findVersionColumnByName( String columnName ) {
359            for (TableColumn c : versionColumns) {
360                if (c.getName().equals(columnName)) {
361                    return c;
362                }
363            }
364            // return nothing
365            return null;
366        }
367    
368        /**
369         * Gets table privileges.
370         * 
371         * @return set of table privileges
372         */
373        public Set<Privilege> getPrivileges() {
374            return privileges;
375        }
376    
377        /**
378         * Adds table priviledge
379         * 
380         * @param privilege the table priviledge
381         */
382        public void addPrivilege( Privilege privilege ) {
383            privileges.add(privilege);
384        }
385    
386        /**
387         * Deletes table priviledge
388         * 
389         * @param privilege the table priviledge
390         */
391        public void deletePrivilege( Privilege privilege ) {
392            privileges.remove(privilege);
393        }
394    
395        /**
396         * Searches priviledge by name
397         * 
398         * @param priviledgeName the priviledge name to search
399         * @return priviledge if found, otherwise return null
400         */
401        public Privilege findPriviledgeByName( String priviledgeName ) {
402            for (Privilege p : privileges) {
403                if (p.getName().equals(priviledgeName)) {
404                    return p;
405                }
406            }
407            // return nothing
408            return null;
409        }
410    
411        /**
412         * Retrieves a set of descriptions of a table's optimal set of columns that uniquely identifies a row in temporary scopes.
413         * 
414         * @return BestRowIdentifier set that uniquely identifies a row in scopes.
415         */
416        public Set<BestRowIdentifier> getBestRowIdentifiers() {
417            return bestRowIdentifiers;
418        }
419    
420        /**
421         * Adds BestRowIdentifier
422         * 
423         * @param bestRowIdentifier the BestRowIdentifier
424         */
425        public void addBestRowIdentifier( BestRowIdentifier bestRowIdentifier ) {
426            bestRowIdentifiers.add(bestRowIdentifier);
427        }
428    
429        /**
430         * deletes BestRowIdentifier
431         * 
432         * @param bestRowIdentifier the BestRowIdentifier
433         */
434        public void deleteBestRowIdentifier( BestRowIdentifier bestRowIdentifier ) {
435            bestRowIdentifiers.remove(bestRowIdentifier);
436        }
437    
438        /**
439         * Searches the BestRowIdentifier by scope
440         * 
441         * @param scopeType the scope of best row identifier
442         * @return BestRowIdentifier if any
443         */
444        public BestRowIdentifier findBestRowIdentifierByScopeType( BestRowIdentifierScopeType scopeType ) {
445            for (BestRowIdentifier bri : bestRowIdentifiers) {
446                if (bri.getScopeType().equals(scopeType)) {
447                    return bri;
448                }
449            }
450            // return nothing
451            return null;
452        }
453    
454        // ===============================================================
455        // ------------------- JDBC 3.0 ---------------------------------
456        // ===============================================================
457    
458        /**
459         * Retrieves a description of the table hierarchies defined in a particular schema in this database. Only the immediate super
460         * type/ sub type relationship is modeled.
461         * 
462         * @return super table for this table
463         * @since 1.4 (JDBC 3.0)
464         */
465        public Table getSuperTable() {
466            return superTable;
467        }
468    
469        /**
470         * Sets a description of the table hierarchies defined in a particular schema in this database. Only the immediate super type/
471         * sub type relationship is modeled.
472         * 
473         * @param superTable the super table for this table
474         * @since 1.4 (JDBC 3.0)
475         */
476        public void setSuperTable( Table superTable ) {
477            this.superTable = superTable;
478        }
479    }