1
2
3
4
5
6
7 package org.asyrinx.joey.gen.model.rdb;
8
9 import java.util.ArrayList;
10 import java.util.Iterator;
11 import java.util.List;
12
13 import org.asyrinx.brownie.core.lang.StringUtils;
14 import org.asyrinx.joey.gen.model.ElementSet;
15
16 /***
17 * @author akima
18 */
19 public class ForeignKey extends ElementSet {
20
21 /***
22 *
23 */
24 public ForeignKey() {
25 this(null, null, null);
26 }
27
28 /***
29 * @param parent
30 * @param name
31 */
32 public ForeignKey(String foreignTable) {
33 this(null, null, foreignTable);
34 }
35
36 /***
37 * @param parent
38 * @param name
39 */
40 public ForeignKey(String name, String foreignTable) {
41 this(null, name, foreignTable);
42 }
43
44 /***
45 * @param parent
46 * @param name
47 */
48 public ForeignKey(Table parent, String foreignTable) {
49 this(parent, null, foreignTable);
50 }
51
52 /***
53 * @param parent
54 * @param name
55 */
56 public ForeignKey(Table parent, String name, String foreignTable) {
57 super(parent, name);
58 this.foreign = foreignTable;
59 }
60
61
62
63
64
65
66 public boolean isEntity() {
67 return true;
68 }
69
70
71
72
73
74
75 public void add(ForeignKeyEntry entry) {
76 super.add(entry);
77 }
78
79
80
81
82
83
84 public boolean contains(ForeignKeyEntry entry) {
85 return super.contains(entry);
86 }
87
88
89
90
91
92
93 public ForeignKeyEntry getEntry(int idx) {
94 return (ForeignKeyEntry) super.getElement(idx);
95 }
96
97
98
99
100
101
102 public ForeignKeyEntry getEntry(String name) {
103 return (ForeignKeyEntry) super.getElement(name);
104 }
105
106
107
108
109
110
111 public ForeignKeyEntry removeEntry(String name) {
112 return (ForeignKeyEntry) super.removeElement(name);
113 }
114
115
116
117
118
119
120 public Table getParent() {
121 return (Table) super.getParentElement();
122 }
123
124 public Table getLocal() {
125 return getParent();
126 }
127
128 protected final Table getTable(String name) {
129 final Table parent = getParent();
130 if (parent == null)
131 return null;
132 final Database database = parent.getParent();
133 if (database == null)
134 return null;
135 return database.getTables().getTable(name);
136 }
137
138 public Table getForeignTable() {
139 return getTable(getForeign());
140 }
141
142 public String getLocalColumnNames() {
143 final List list = new ArrayList();
144 for (final Iterator i = this.iterator(); i.hasNext();) {
145 final ForeignKeyEntry entry = (ForeignKeyEntry) i.next();
146 list.add(entry.getLocalColumn().getName());
147 }
148 return StringUtils.join(list.iterator(), ",");
149 }
150
151 public String getForeignColumnNames() {
152 final List list = new ArrayList();
153 for (final Iterator i = this.iterator(); i.hasNext();) {
154 final ForeignKeyEntry entry = (ForeignKeyEntry) i.next();
155 list.add(entry.getForeignColumn().getName());
156 }
157 return StringUtils.join(list.iterator(), ",");
158 }
159
160 private String foreign = null;
161
162 private boolean indexed = true;
163
164 private Index index = null;
165
166 private ForeignKeyType type = ForeignKeyType.NORMAL;
167
168 /***
169 * @return Returns the foreign.
170 */
171 public String getForeign() {
172 return foreign;
173 }
174
175 /***
176 * @param foreign
177 * The foreign to set.
178 */
179 public void setForeign(String foreignTabe) {
180 this.foreign = foreignTabe;
181 }
182
183 /***
184 * @return Returns the indexed.
185 */
186 public boolean isIndexed() {
187 return indexed;
188 }
189
190 /***
191 * @param indexed
192 * The indexed to set.
193 */
194 public void setIndexed(boolean indexed) {
195 this.indexed = indexed;
196 }
197
198 /***
199 * @return Returns the index.
200 */
201 public Index getIndex() {
202 return index;
203 }
204
205 /***
206 * @param index
207 * The index to set.
208 */
209 public void setIndex(Index index) {
210 this.index = index;
211 }
212
213 /***
214 * @return Returns the type.
215 */
216 public ForeignKeyType getType() {
217 return type;
218 }
219
220 /***
221 * @param type
222 * The type to set.
223 */
224 public void setType(ForeignKeyType type) {
225 this.type = type;
226 }
227
228 /***
229 * @param column
230 * @return
231 */
232 public boolean containsAsLocal(Column column) {
233 for (Iterator i = this.iterator(); i.hasNext();) {
234 final ForeignKeyEntry entry = (ForeignKeyEntry) i.next();
235 if (entry.getLocalColumn() == column)
236 return true;
237 }
238 return false;
239 }
240 }