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
20
21
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.Hashtable;
25 import java.util.List;
26 import java.util.Map;
27
28 import org.apache.commons.collections.map.ListOrderedMap;
29 import org.xml.sax.Attributes;
30
31
32
33
34
35
36
37
38
39 public class ForeignKey
40 {
41 private String foreignTableName;
42 private String name;
43 private String onUpdate;
44 private String onDelete;
45 private Table parentTable;
46 private List localColumns = new ArrayList(3);
47 private List foreignColumns = new ArrayList(3);
48 private Map options = Collections.synchronizedMap(new ListOrderedMap());
49
50
51
52 private static final String NONE = "NONE";
53 private static final String SETNULL = "SETNULL";
54
55
56
57
58
59
60 public void loadFromXML(Attributes attrib)
61 {
62 foreignTableName = attrib.getValue("foreignTable");
63 name = attrib.getValue("name");
64 onUpdate = attrib.getValue("onUpdate");
65 onDelete = attrib.getValue("onDelete");
66 onUpdate = normalizeFKey(onUpdate);
67 onDelete = normalizeFKey(onDelete);
68 }
69
70
71
72
73
74
75
76 private String normalizeFKey(String attrib)
77 {
78 if (attrib == null)
79 {
80 attrib = NONE;
81 }
82
83 attrib = attrib.toUpperCase();
84 if (attrib.equals(SETNULL))
85 {
86 attrib = "SET NULL";
87 }
88 return attrib;
89 }
90
91
92
93
94
95
96 public boolean hasOnUpdate()
97 {
98 return !onUpdate.equals(NONE);
99 }
100
101
102
103
104
105
106 public boolean hasOnDelete()
107 {
108 return !onDelete.equals(NONE);
109 }
110
111
112
113
114
115
116 public String getOnUpdate()
117 {
118 return onUpdate;
119 }
120
121
122
123
124
125
126 public String getOnDelete()
127 {
128 return onDelete;
129 }
130
131
132
133
134
135
136 public void setOnDelete(String value)
137 {
138 onDelete = normalizeFKey(value);
139 }
140
141
142
143
144
145
146 public void setOnUpdate(String value)
147 {
148 onUpdate = normalizeFKey(value);
149 }
150
151
152
153
154
155
156 public String getName()
157 {
158 return name;
159 }
160
161
162
163
164
165
166 public void setName(String name)
167 {
168 this.name = name;
169 }
170
171
172
173
174
175
176 public String getForeignTableName()
177 {
178 return foreignTableName;
179 }
180
181
182
183
184
185
186 public void setForeignTableName(String tableName)
187 {
188 foreignTableName = tableName;
189 }
190
191
192
193
194
195
196 public void setTable(Table parent)
197 {
198 parentTable = parent;
199 }
200
201
202
203
204
205
206 public Table getTable()
207 {
208 return parentTable;
209 }
210
211
212
213
214
215
216 public String getTableName()
217 {
218 return parentTable.getName();
219 }
220
221
222
223
224
225
226 public void addReference(Attributes attrib)
227 {
228 addReference(attrib.getValue("local"), attrib.getValue("foreign"));
229 }
230
231
232
233
234
235
236
237 public void addReference(String local, String foreign)
238 {
239 localColumns.add(local);
240 foreignColumns.add(foreign);
241 }
242
243
244
245
246
247
248 public String getLocalColumnNames()
249 {
250 return Column.makeList(getLocalColumns());
251 }
252
253
254
255
256
257
258 public String getForeignColumnNames()
259 {
260 return Column.makeList(getForeignColumns());
261 }
262
263
264
265
266
267
268 public List getLocalColumns()
269 {
270 return localColumns;
271 }
272
273
274
275
276
277
278
279 public Hashtable getLocalForeignMapping()
280 {
281 Hashtable h = new Hashtable();
282
283 for (int i = 0; i < localColumns.size(); i++)
284 {
285 h.put(localColumns.get(i), foreignColumns.get(i));
286 }
287
288 return h;
289 }
290
291
292
293
294
295
296 public List getForeignColumns()
297 {
298 return foreignColumns;
299 }
300
301
302
303
304
305
306
307 public Hashtable getForeignLocalMapping()
308 {
309 Hashtable h = new Hashtable();
310
311 for (int i = 0; i < localColumns.size(); i++)
312 {
313 h.put(foreignColumns.get(i), localColumns.get(i));
314 }
315
316 return h;
317 }
318
319
320
321
322
323
324 public String toString()
325 {
326 StringBuffer result = new StringBuffer();
327 result.append(" <foreign-key foreignTable=\"")
328 .append(getForeignTableName())
329 .append("\" name=\"")
330 .append(getName())
331 .append("\">\n");
332
333 for (int i = 0; i < localColumns.size(); i++)
334 {
335 result.append(" <reference local=\"")
336 .append(localColumns.get(i))
337 .append("\" foreign=\"")
338 .append(foreignColumns.get(i))
339 .append("\"/>\n");
340 }
341 result.append(" </foreign-key>\n");
342 return result.toString();
343 }
344
345
346
347
348
349
350
351 public void addOption(String key, String value)
352 {
353 options.put(key, value);
354 }
355
356
357
358
359
360
361
362
363 public String getOption(String key)
364 {
365 return (String) options.get(key);
366 }
367
368
369
370
371
372
373
374
375
376
377
378 public Map getOptions()
379 {
380 return options;
381 }
382 }