KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > oracle > toplink > essentials > internal > helper > DatabaseTable


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the "License"). You may not use this file except
5  * in compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * glassfish/bootstrap/legal/CDDLv1.0.txt or
9  * https://glassfish.dev.java.net/public/CDDLv1.0.html.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * HEADER in each file and include the License file at
15  * glassfish/bootstrap/legal/CDDLv1.0.txt. If applicable,
16  * add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your
18  * own identifying information: Portions Copyright [yyyy]
19  * [name of copyright owner]
20  */

21 // Copyright (c) 1998, 2006, Oracle. All rights reserved.
22
package oracle.toplink.essentials.internal.helper;
23
24 import java.io.*;
25 import java.util.Vector JavaDoc;
26 import java.util.Collection JavaDoc;
27 import oracle.toplink.essentials.internal.databaseaccess.*;
28
29 /**
30  * INTERNAL:
31  * <p> <b>Purpose</b>:
32  * Define a fully qualified table name.<p>
33  * <b>Responsibilities</b>: <ul>
34  * <li> Allow specification of a qualifier to the table, i.e. creator or database.
35  * </ul>
36  *@see DatabaseField
37  */

38 public class DatabaseTable implements Cloneable JavaDoc, Serializable {
39     protected String JavaDoc name;
40     protected String JavaDoc tableQualifier;
41     protected String JavaDoc qualifiedName;
42     protected Vector JavaDoc uniqueConstraints;
43
44     /**
45      * Initialize the newly allocated instance of this class.
46      * By default their is no qualifier.
47      */

48     public DatabaseTable() {
49         this("", "");
50     }
51
52     public DatabaseTable(String JavaDoc possiblyQualifiedName) {
53         setPossiblyQualifiedName(possiblyQualifiedName);
54         uniqueConstraints = new Vector JavaDoc();
55     }
56
57     public DatabaseTable(String JavaDoc tableName, String JavaDoc qualifier) {
58         this.name = tableName;
59         this.tableQualifier = qualifier;
60         uniqueConstraints = new Vector JavaDoc();
61     }
62
63     /**
64      * Add the unique constraint for this column name. Used for DDL generation.
65      */

66     public void addUniqueConstraint(String JavaDoc columnName) {
67         uniqueConstraints.add(columnName);
68     }
69     
70     /**
71      * Add the unique constraint for the columns names. Used for DDL generation.
72      */

73     public void addUniqueConstraints(String JavaDoc[] columnNames) {
74         uniqueConstraints.add(columnNames);
75     }
76     
77     /**
78      * Return a shallow copy of the receiver.
79      * @return Object An Object must be returned or the signature of this method
80      * will conflict with the signature in Object.
81      */

82     public Object JavaDoc clone() {
83         try {
84             return super.clone();
85         } catch (CloneNotSupportedException JavaDoc exception) {
86         }
87
88         return null;
89     }
90
91     /**
92      * Two tables are equal if their names and tables are equal,
93      * or their names are equal and one does not have a qualifier assigned.
94      * This allows an unqualified table to equal the same fully qualified one.
95      */

96     public boolean equals(Object JavaDoc object) {
97         if (object instanceof DatabaseTable) {
98             return equals((DatabaseTable)object);
99         }
100         return false;
101     }
102
103     /**
104      * Two tables are equal if their names and tables are equal,
105      * or their names are equal and one does not have a qualifier assigned.
106      * This allows an unqualified table to equal the same fully qualified one.
107      */

108     public boolean equals(DatabaseTable table) {
109         if (this == table) {
110             return true;
111         }
112         if (DatabasePlatform.shouldIgnoreCaseOnFieldComparisons()) {
113             if (getName().equalsIgnoreCase(table.getName())) {
114                 if ((getTableQualifier().length() == 0) || (table.getTableQualifier().length() == 0) || (getTableQualifier().equalsIgnoreCase(table.getTableQualifier()))) {
115                     return true;
116                 }
117             }
118         } else {
119             if (getName().equals(table.getName())) {
120                 if ((getTableQualifier().length() == 0) || (table.getTableQualifier().length() == 0) || (getTableQualifier().equals(table.getTableQualifier()))) {
121                     return true;
122                 }
123             }
124         }
125
126         return false;
127     }
128
129     /**
130      * Get method for table name.
131      */

132     public String JavaDoc getName() {
133         return name;
134     }
135
136     public String JavaDoc getQualifiedName() {
137         if (qualifiedName == null) {
138             if (tableQualifier.equals("")) {
139                 qualifiedName = getName();
140             } else {
141                 qualifiedName = getTableQualifier() + "." + getName();
142             }
143         }
144
145         return qualifiedName;
146     }
147
148     public String JavaDoc getTableQualifier() {
149         return tableQualifier;
150     }
151     
152     /**
153      * Return a vector of the unique constraints for this table.
154      * Used for DDL generation.
155      */

156     public Vector JavaDoc getUniqueConstraints() {
157         return uniqueConstraints;
158     }
159
160     /**
161      * Return the hashcode of the name, because it is fairly unqiue.
162      */

163     public int hashCode() {
164         return getName().hashCode();
165     }
166
167     /**
168      * Determine whether the receiver has any identification information.
169      * Return true if the name or qualifier of the receiver are nonempty.
170      */

171     public boolean hasName() {
172         if ((getName().length() == 0) && (getTableQualifier().length() == 0)) {
173             return false;
174         }
175
176         return true;
177     }
178
179     /**
180      * INTERNAL:
181      * Is this decorated / has an AS OF (some past time) clause.
182      * <b>Example:</b>
183      * SELECT ... FROM EMPLOYEE AS OF TIMESTAMP (exp) t0 ...
184      */

185     public boolean isDecorated() {
186         return false;
187     }
188
189     protected void resetQualifiedName() {
190         this.qualifiedName = null;
191     }
192
193     /**
194      * This method will set the table name regardless if the name has
195      * a qualifier. Used when aliasing table names.
196      *
197      * @param name
198      */

199     public void setName(String JavaDoc name) {
200         this.name = name;
201         resetQualifiedName();
202     }
203     
204     /**
205      * Used to map the project xml. Anytime a string name is read from the
206      * project xml, we must check if it is fully qualified and split the
207      * actual name from the qualifier.
208      *
209      * @param possiblyQualifiedName
210      */

211     public void setPossiblyQualifiedName(String JavaDoc possiblyQualifiedName) {
212         resetQualifiedName();
213         
214         int index = possiblyQualifiedName.lastIndexOf('.');
215
216         if (index == -1) {
217             this.name = possiblyQualifiedName;
218             this.tableQualifier = "";
219         } else {
220             this.name = possiblyQualifiedName.substring(index + 1, possiblyQualifiedName.length());
221             this.tableQualifier = possiblyQualifiedName.substring(0, index);
222         }
223     }
224
225     public void setTableQualifier(String JavaDoc qualifier) {
226         this.tableQualifier = qualifier;
227         resetQualifiedName();
228     }
229
230     public String JavaDoc toString() {
231         return "DatabaseTable(" + getQualifiedName() + ")";
232     }
233 }
234
Popular Tags