KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > torque > engine > database > model > ForeignKey


1 package org.apache.torque.engine.database.model;
2
3 /*
4  * Copyright 2001-2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Hashtable JavaDoc;
21 import java.util.List JavaDoc;
22 import org.xml.sax.Attributes JavaDoc;
23
24 /**
25  * A class for information about foreign keys of a table.
26  *
27  * @author <a HREF="mailto:fedor.karpelevitch@home.com">Fedor</a>
28  * @author <a HREF="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
29  * @version $Id: ForeignKey.java,v 1.4 2004/02/22 06:27:19 jmcnally Exp $
30  */

31 public class ForeignKey
32 {
33     private String JavaDoc foreignTableName;
34     private String JavaDoc name;
35     private String JavaDoc onUpdate;
36     private String JavaDoc onDelete;
37     private Table parentTable;
38     private List JavaDoc localColumns = new ArrayList JavaDoc(3);
39     private List JavaDoc foreignColumns = new ArrayList JavaDoc(3);
40
41     // the uppercase equivalent of the onDelete/onUpdate values in the dtd
42
private static final String JavaDoc NONE = "NONE";
43     private static final String JavaDoc SETNULL = "SETNULL";
44
45     /**
46      * Imports foreign key from an XML specification
47      *
48      * @param attrib the xml attributes
49      */

50     public void loadFromXML(Attributes JavaDoc attrib)
51     {
52         foreignTableName = attrib.getValue("foreignTable");
53         name = attrib.getValue("name");
54         onUpdate = attrib.getValue("onUpdate");
55         onDelete = attrib.getValue("onDelete");
56         onUpdate = normalizeFKey(onUpdate);
57         onDelete = normalizeFKey(onDelete);
58     }
59
60     /**
61      * Normalizes the input of onDelete, onUpdate attributes
62      *
63      * @param attrib the attribute to normalize
64      * @return nomalized form
65      */

66     private String JavaDoc normalizeFKey(String JavaDoc attrib)
67     {
68         if (attrib == null)
69         {
70             attrib = NONE;
71         }
72
73         attrib = attrib.toUpperCase();
74         if (attrib.equals(SETNULL))
75         {
76             attrib = "SET NULL";
77         }
78         return attrib;
79     }
80
81     /**
82      * Returns whether or not the onUpdate attribute is set
83      *
84      * @return true if the onUpdate attribute is set
85      */

86     public boolean hasOnUpdate()
87     {
88        return !onUpdate.equals(NONE);
89     }
90
91     /**
92      * Returns whether or not the onDelete attribute is set
93      *
94      * @return true if the onDelete attribute is set
95      */

96     public boolean hasOnDelete()
97     {
98        return !onDelete.equals(NONE);
99     }
100
101     /**
102      * Returns the onUpdate attribute
103      *
104      * @return the onUpdate attribute
105      */

106     public String JavaDoc getOnUpdate()
107     {
108        return onUpdate;
109     }
110
111     /**
112      * Returns the onDelete attribute
113      *
114      * @return the onDelete attribute
115      */

116     public String JavaDoc getOnDelete()
117     {
118        return onDelete;
119     }
120
121     /**
122      * Sets the onDelete attribute
123      *
124      * @param value the onDelete attribute
125      */

126     public void setOnDelete(String JavaDoc value)
127     {
128        onDelete = normalizeFKey(value);
129     }
130
131     /**
132      * Sets the onUpdate attribute
133      *
134      * @param value the onUpdate attribute
135      */

136     public void setOnUpdate(String JavaDoc value)
137     {
138        onUpdate = normalizeFKey(value);
139     }
140
141     /**
142      * Returns the name attribute.
143      *
144      * @return the name
145      */

146     public String JavaDoc getName()
147     {
148         return name;
149     }
150
151     /**
152      * Sets the name attribute.
153      *
154      * @param name the name
155      */

156     public void setName(String JavaDoc name)
157     {
158         this.name = name;
159     }
160
161     /**
162      * Get the foreignTableName of the FK
163      *
164      * @return the name of the foreign table
165      */

166     public String JavaDoc getForeignTableName()
167     {
168         return foreignTableName;
169     }
170
171     /**
172      * Set the foreignTableName of the FK
173      *
174      * @param tableName the name of the foreign table
175      */

176     public void setForeignTableName(String JavaDoc tableName)
177     {
178         foreignTableName = tableName;
179     }
180
181     /**
182      * Set the parent Table of the foreign key
183      *
184      * @param parent the table
185      */

186     public void setTable(Table parent)
187     {
188         parentTable = parent;
189     }
190
191     /**
192      * Get the parent Table of the foreign key
193      *
194      * @return the parent table
195      */

196     public Table getTable()
197     {
198         return parentTable;
199     }
200
201     /**
202      * Returns the name of the table the foreign key is in
203      *
204      * @return the name of the table
205      */

206     public String JavaDoc getTableName()
207     {
208         return parentTable.getName();
209     }
210
211     /**
212      * Adds a new reference entry to the foreign key
213      *
214      * @param attrib the xml attributes
215      */

216     public void addReference(Attributes JavaDoc attrib)
217     {
218         addReference(attrib.getValue("local"), attrib.getValue("foreign"));
219     }
220
221     /**
222      * Adds a new reference entry to the foreign key
223      *
224      * @param local name of the local column
225      * @param foreign name of the foreign column
226      */

227     public void addReference(String JavaDoc local, String JavaDoc foreign)
228     {
229         localColumns.add(local);
230         foreignColumns.add(foreign);
231     }
232
233     /**
234      * Returns a comma delimited string of local column names
235      *
236      * @return the local column names
237      */

238     public String JavaDoc getLocalColumnNames()
239     {
240         return Column.makeList(getLocalColumns());
241     }
242
243     /**
244      * Returns a comma delimited string of foreign column names
245      *
246      * @return the foreign column names
247      */

248     public String JavaDoc getForeignColumnNames()
249     {
250         return Column.makeList(getForeignColumns());
251     }
252
253     /**
254      * Returns the list of local column names. You should not edit this List.
255      *
256      * @return the local columns
257      */

258     public List JavaDoc getLocalColumns()
259     {
260         return localColumns;
261     }
262
263     /**
264      * Utility method to get local column names to foreign column names
265      * mapping for this foreign key.
266      *
267      * @return table mapping foreign names to local names
268      */

269     public Hashtable JavaDoc getLocalForeignMapping()
270     {
271         Hashtable JavaDoc h = new Hashtable JavaDoc();
272
273         for (int i = 0; i < localColumns.size(); i++)
274         {
275             h.put(localColumns.get(i), foreignColumns.get(i));
276         }
277
278         return h;
279     }
280
281     /**
282      * Returns the list of foreign column names. You should not edit this List.
283      *
284      * @return the foreign columns
285      */

286     public List JavaDoc getForeignColumns()
287     {
288         return foreignColumns;
289     }
290
291     /**
292      * Utility method to get foreign column names to local column names
293      * mapping for this foreign key.
294      *
295      * @return table mapping local names to foreign names
296      */

297     public Hashtable JavaDoc getForeignLocalMapping()
298     {
299         Hashtable JavaDoc h = new Hashtable JavaDoc();
300
301         for (int i = 0; i < localColumns.size(); i++)
302         {
303             h.put(foreignColumns.get(i), localColumns.get(i));
304         }
305
306         return h;
307     }
308
309     /**
310      * String representation of the foreign key. This is an xml representation.
311      *
312      * @return string representation in xml
313      */

314     public String JavaDoc toString()
315     {
316         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
317         result.append(" <foreign-key foreignTable=\"")
318             .append(getForeignTableName())
319             .append("\" name=\"")
320             .append(getName())
321             .append("\">\n");
322
323         for (int i = 0; i < localColumns.size(); i++)
324         {
325             result.append(" <reference local=\"")
326                 .append(localColumns.get(i))
327                 .append("\" foreign=\"")
328                 .append(foreignColumns.get(i))
329                 .append("\"/>\n");
330         }
331         result.append(" </foreign-key>\n");
332         return result.toString();
333     }
334 }
335
Popular Tags