1 package org.apache.torque.engine.database.model;
2
3 /* ====================================================================
4 * The Apache Software License, Version 1.1
5 *
6 * Copyright (c) 2001-2003 The Apache Software Foundation. All rights
7 * reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 *
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 *
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 *
21 * 3. The end-user documentation included with the redistribution,
22 * if any, must include the following acknowledgment:
23 * "This product includes software developed by the
24 * Apache Software Foundation (http://www.apache.org/)."
25 * Alternately, this acknowledgment may appear in the software itself,
26 * if and wherever such third-party acknowledgments normally appear.
27 *
28 * 4. The names "Apache" and "Apache Software Foundation" and
29 * "Apache Turbine" must not be used to endorse or promote products
30 * derived from this software without prior written permission. For
31 * written permission, please contact apache@apache.org.
32 *
33 * 5. Products derived from this software may not be called "Apache",
34 * "Apache Turbine", nor may "Apache" appear in their name, without
35 * prior written permission of the Apache Software Foundation.
36 *
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
40 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
41 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
42 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
43 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
44 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
45 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
46 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
47 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
48 * SUCH DAMAGE.
49 * ====================================================================
50 *
51 * This software consists of voluntary contributions made by many
52 * individuals on behalf of the Apache Software Foundation. For more
53 * information on the Apache Software Foundation, please see
54 * <http://www.apache.org/>.
55 */
56
57 import java.util.ArrayList;
58 import java.util.Hashtable;
59 import java.util.List;
60 import org.xml.sax.Attributes;
61
62 /***
63 * A class for information about foreign keys of a table.
64 *
65 * @author <a href="mailto:fedor.karpelevitch@home.com">Fedor</a>
66 * @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
67 * @version $Id: ForeignKey.java,v 1.3 2003/03/21 17:31:08 mpoeschl Exp $
68 */
69 public class ForeignKey
70 {
71 private String foreignTableName;
72 private String name;
73 private String onUpdate;
74 private String onDelete;
75 private Table parentTable;
76 private List localColumns = new ArrayList(3);
77 private List foreignColumns = new ArrayList(3);
78
79 // the uppercase equivalent of the onDelete/onUpdate values in the dtd
80 private static final String NONE = "NONE";
81 private static final String SETNULL = "SETNULL";
82
83 /***
84 * Imports foreign key from an XML specification
85 *
86 * @param attrib the xml attributes
87 */
88 public void loadFromXML(Attributes attrib)
89 {
90 foreignTableName = attrib.getValue("foreignTable");
91 name = attrib.getValue("name");
92 onUpdate = attrib.getValue("onUpdate");
93 onDelete = attrib.getValue("onDelete");
94 onUpdate = normalizeFKey(onUpdate);
95 onDelete = normalizeFKey(onDelete);
96 }
97
98 /***
99 * Normalizes the input of onDelete, onUpdate attributes
100 *
101 * @param attrib the attribute to normalize
102 * @return nomalized form
103 */
104 private String normalizeFKey(String attrib)
105 {
106 if (attrib == null)
107 {
108 attrib = NONE;
109 }
110
111 attrib = attrib.toUpperCase();
112 if (attrib.equals(SETNULL))
113 {
114 attrib = "SET NULL";
115 }
116 return attrib;
117 }
118
119 /***
120 * Returns whether or not the onUpdate attribute is set
121 *
122 * @return true if the onUpdate attribute is set
123 */
124 public boolean hasOnUpdate()
125 {
126 return !onUpdate.equals(NONE);
127 }
128
129 /***
130 * Returns whether or not the onDelete attribute is set
131 *
132 * @return true if the onDelete attribute is set
133 */
134 public boolean hasOnDelete()
135 {
136 return !onDelete.equals(NONE);
137 }
138
139 /***
140 * Returns the onUpdate attribute
141 *
142 * @return the onUpdate attribute
143 */
144 public String getOnUpdate()
145 {
146 return onUpdate;
147 }
148
149 /***
150 * Returns the onDelete attribute
151 *
152 * @return the onDelete attribute
153 */
154 public String getOnDelete()
155 {
156 return onDelete;
157 }
158
159 /***
160 * Sets the onDelete attribute
161 *
162 * @param value the onDelete attribute
163 */
164 public void setOnDelete(String value)
165 {
166 onDelete = normalizeFKey(value);
167 }
168
169 /***
170 * Sets the onUpdate attribute
171 *
172 * @param value the onUpdate attribute
173 */
174 public void setOnUpdate(String value)
175 {
176 onUpdate = normalizeFKey(value);
177 }
178
179 /***
180 * Returns the name attribute.
181 *
182 * @return the name
183 */
184 public String getName()
185 {
186 return name;
187 }
188
189 /***
190 * Sets the name attribute.
191 *
192 * @param name the name
193 */
194 public void setName(String name)
195 {
196 this.name = name;
197 }
198
199 /***
200 * Get the foreignTableName of the FK
201 *
202 * @return the name of the foreign table
203 */
204 public String getForeignTableName()
205 {
206 return foreignTableName;
207 }
208
209 /***
210 * Set the foreignTableName of the FK
211 *
212 * @param tableName the name of the foreign table
213 */
214 public void setForeignTableName(String tableName)
215 {
216 foreignTableName = tableName;
217 }
218
219 /***
220 * Set the parent Table of the foreign key
221 *
222 * @param parent the table
223 */
224 public void setTable(Table parent)
225 {
226 parentTable = parent;
227 }
228
229 /***
230 * Get the parent Table of the foreign key
231 *
232 * @return the parent table
233 */
234 public Table getTable()
235 {
236 return parentTable;
237 }
238
239 /***
240 * Returns the name of the table the foreign key is in
241 *
242 * @return the name of the table
243 */
244 public String getTableName()
245 {
246 return parentTable.getName();
247 }
248
249 /***
250 * Adds a new reference entry to the foreign key
251 *
252 * @param attrib the xml attributes
253 */
254 public void addReference(Attributes attrib)
255 {
256 addReference(attrib.getValue("local"), attrib.getValue("foreign"));
257 }
258
259 /***
260 * Adds a new reference entry to the foreign key
261 *
262 * @param local name of the local column
263 * @param foreign name of the foreign column
264 */
265 public void addReference(String local, String foreign)
266 {
267 localColumns.add(local);
268 foreignColumns.add(foreign);
269 }
270
271 /***
272 * Returns a comma delimited string of local column names
273 *
274 * @return the local column names
275 */
276 public String getLocalColumnNames()
277 {
278 return Column.makeList(getLocalColumns());
279 }
280
281 /***
282 * Returns a comma delimited string of foreign column names
283 *
284 * @return the foreign column names
285 */
286 public String getForeignColumnNames()
287 {
288 return Column.makeList(getForeignColumns());
289 }
290
291 /***
292 * Returns the list of local column names. You should not edit this List.
293 *
294 * @return the local columns
295 */
296 public List getLocalColumns()
297 {
298 return localColumns;
299 }
300
301 /***
302 * Utility method to get local column names to foreign column names
303 * mapping for this foreign key.
304 *
305 * @return table mapping foreign names to local names
306 */
307 public Hashtable getLocalForeignMapping()
308 {
309 Hashtable h = new Hashtable();
310
311 for (int i = 0; i < localColumns.size(); i++)
312 {
313 h.put(localColumns.get(i), foreignColumns.get(i));
314 }
315
316 return h;
317 }
318
319 /***
320 * Returns the list of foreign column names. You should not edit this List.
321 *
322 * @return the foreign columns
323 */
324 public List getForeignColumns()
325 {
326 return foreignColumns;
327 }
328
329 /***
330 * Utility method to get foreign column names to local column names
331 * mapping for this foreign key.
332 *
333 * @return table mapping local names to foreign names
334 */
335 public Hashtable getForeignLocalMapping()
336 {
337 Hashtable h = new Hashtable();
338
339 for (int i = 0; i < localColumns.size(); i++)
340 {
341 h.put(foreignColumns.get(i), localColumns.get(i));
342 }
343
344 return h;
345 }
346
347 /***
348 * String representation of the foreign key. This is an xml representation.
349 *
350 * @return string representation in xml
351 */
352 public String toString()
353 {
354 StringBuffer result = new StringBuffer();
355 result.append(" <foreign-key foreignTable=\"")
356 .append(getForeignTableName())
357 .append("\" name=\"")
358 .append(getName())
359 .append("\">\n");
360
361 for (int i = 0; i < localColumns.size(); i++)
362 {
363 result.append(" <reference local=\"")
364 .append(localColumns.get(i))
365 .append("\" foreign=\"")
366 .append(foreignColumns.get(i))
367 .append("\"/>\n");
368 }
369 result.append(" </foreign-key>\n");
370 return result.toString();
371 }
372 }
This page was automatically generated by Maven