1 package org.apache.torque.engine.database.model;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.util.ArrayList;
20 import java.util.Hashtable;
21 import java.util.List;
22 import org.xml.sax.Attributes;
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 239624 2005-08-24 12:18:03Z henning $
30 */
31 public class ForeignKey
32 {
33 private String foreignTableName;
34 private String name;
35 private String onUpdate;
36 private String onDelete;
37 private Table parentTable;
38 private List localColumns = new ArrayList(3);
39 private List foreignColumns = new ArrayList(3);
40
41
42 private static final String NONE = "NONE";
43 private static final String 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 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 normalizeFKey(String 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 getOnUpdate()
107 {
108 return onUpdate;
109 }
110
111 /***
112 * Returns the onDelete attribute
113 *
114 * @return the onDelete attribute
115 */
116 public String 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 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 value)
137 {
138 onUpdate = normalizeFKey(value);
139 }
140
141 /***
142 * Returns the name attribute.
143 *
144 * @return the name
145 */
146 public String 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 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 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 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 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 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 local, String 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 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 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 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 getLocalForeignMapping()
270 {
271 Hashtable h = new Hashtable();
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 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 getForeignLocalMapping()
298 {
299 Hashtable h = new Hashtable();
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 toString()
315 {
316 StringBuffer result = new StringBuffer();
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 }