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.apache.commons.lang.builder.EqualsBuilder;
14 import org.asyrinx.brownie.core.lang.NumberUtils;
15 import org.asyrinx.joey.gen.jdbc.JdbcUtils;
16 import org.asyrinx.joey.gen.model.Element;
17
18 /***
19 * @author akima
20 */
21 public class Column extends Element {
22
23 /***
24 */
25 public Column() {
26 this(null, null, null);
27 }
28
29 /***
30 * @param table
31 * @param name
32 * @param type
33 */
34 public Column(Table parent, String name, String type) {
35 super(parent, name);
36 this.type = type;
37 }
38
39 /***
40 * @param table
41 * @param name
42 * @param type
43 * @param size
44 */
45 public Column(Table parent, String name, String type, String size) {
46 this(parent, name, type, size, false, false);
47 }
48
49 /***
50 * @param table
51 * @param name
52 * @param type
53 * @param size
54 * @param required
55 */
56 public Column(Table parent, String name, String type, String size, boolean required) {
57 this(parent, name, type, size, required, false, null);
58 }
59
60 /***
61 * @param table
62 * @param name
63 * @param type
64 * @param size
65 * @param required
66 * @param primaryKey
67 */
68 public Column(Table parent, String name, String type, String size, boolean required,
69 boolean primaryKey) {
70 this(parent, name, type, size, required, primaryKey, null);
71 }
72
73 /***
74 * @param table
75 * @param name
76 * @param type
77 * @param size
78 * @param required
79 * @param primaryKey
80 * @param defaultValue
81 */
82 public Column(Table parent, String name, String type, String size, boolean required,
83 boolean primaryKey, String defaultValue) {
84 super(parent, name);
85 this.type = type;
86 this.size = size;
87 this.required = required;
88 this.primaryKey = primaryKey;
89 this.defaultValue = defaultValue;
90 }
91
92
93
94
95
96
97 public Table getParent() {
98 return (Table) super.getParentElement();
99 }
100
101 private String type = null;
102
103 private String size = null;
104
105 private String decimalSize = null;
106
107 private boolean required = false;
108
109 private boolean primaryKey = false;
110
111 private String defaultValue = null;
112
113 private String enum = null;
114
115 private boolean autoIncrement = false;
116
117 private String idMethod = null;
118
119 private String fk = null;
120
121 private String fkType = ForeignKeyType.NORMAL.getName();
122
123 private String fkCascade = "none";
124
125 private boolean indexed = false;
126
127 private boolean unique = false;
128
129 private boolean extended = false;
130
131 private boolean caption = false;
132
133
134
135
136
137
138 public boolean equals(Object obj) {
139 if (!super.equals(obj))
140 return false;
141 if (!(obj instanceof Column))
142 return false;
143 final Column other = (Column) obj;
144 return new EqualsBuilder()
145 .append(this.getType(), other.getType())
146 .append(this.getSize(), other.getSize())
147 .append(this.isRequired(), other.isRequired())
148 .append(this.isPrimaryKey(), other.isPrimaryKey())
149 .append(this.getDefaultValue(), other.getDefaultValue())
150 .append(this.getEnum(), other.getEnum())
151 .isEquals();
152 }
153
154 /***
155 * @return Returns the defaultValue.
156 */
157 public String getDefault() {
158 return getDefaultValue();
159 }
160
161 public int getSizeAsInt() {
162 return NumberUtils.toInt(getSize(), 0);
163 }
164
165 public int getDeciamlSizeAsInt() {
166 return NumberUtils.toInt(getDecimalSize(), 0);
167 }
168
169 /***
170 * @param defaultValue
171 * The defaultValue to set.
172 */
173 public void setDefault(String defaultValue) {
174 this.setDefaultValue(defaultValue);
175 }
176
177 /***
178 * @return Returns the defaultValue.
179 */
180 public String getDefaultValue() {
181 return defaultValue;
182 }
183
184 /***
185 * @param defaultValue
186 * The defaultValue to set.
187 */
188 public void setDefaultValue(String defaultValue) {
189 this.defaultValue = defaultValue;
190 }
191
192 /***
193 * @return Returns the primaryKey.
194 */
195 public boolean isPrimaryKey() {
196 return primaryKey;
197 }
198
199 /***
200 * @param primaryKey
201 * The primaryKey to set.
202 */
203 public void setPrimaryKey(boolean primaryKey) {
204 this.primaryKey = primaryKey;
205 }
206
207 /***
208 * @return Returns the required.
209 */
210 public boolean isRequired() {
211 return required;
212 }
213
214 /***
215 * @param required
216 * The required to set.
217 */
218 public void setRequired(boolean required) {
219 this.required = required;
220 }
221
222 /***
223 * @return Returns the size.
224 */
225 public String getSize() {
226 return size;
227 }
228
229 /***
230 * @param size
231 * The size to set.
232 */
233 public void setSize(String size) {
234 this.size = size;
235 }
236
237 /***
238 * @return Returns the type.
239 */
240 public String getType() {
241 return type;
242 }
243
244 /***
245 * @param type
246 * The type to set.
247 */
248 public void setType(String type) {
249 this.type = type;
250 }
251
252 public int getJdbcType() {
253 return JdbcUtils.toJdbcType(this.getType());
254 }
255
256 /***
257 * @return Returns the enum.
258 */
259 public String getEnum() {
260 return enum;
261 }
262
263 /***
264 * @param enum
265 * The enum to set.
266 */
267 public void setEnum(String enum) {
268 this.enum = enum;
269 }
270
271 public RdbEnumeration getEnumeration() {
272 final Table table = getParent();
273 if (table == null)
274 return null;
275 final Database database = table.getParent();
276 return database.getEnumerations().getEnumeration(getEnum());
277 }
278
279 /***
280 * @return Returns the autoIncrement.
281 */
282 public boolean isAutoIncrement() {
283 return autoIncrement;
284 }
285
286 /***
287 * @param autoIncrement
288 * The autoIncrement to set.
289 */
290 public void setAutoIncrement(boolean autoIncrement) {
291 this.autoIncrement = autoIncrement;
292 }
293
294 /***
295 * @return Returns the idMethod.
296 */
297 public String getIdMethod() {
298 return idMethod;
299 }
300
301 /***
302 * @param idMethod
303 * The idMethod to set.
304 */
305 public void setIdMethod(String idMethod) {
306 this.idMethod = idMethod;
307 }
308
309 /***
310 * @return Returns the fk.
311 */
312 public String getFk() {
313 return fk;
314 }
315
316 /***
317 * @param fk
318 * The fk to set.
319 */
320 public void setFk(String fk) {
321 this.fk = fk;
322 }
323
324 public String getFkCascade() {
325 return fkCascade;
326 }
327
328 public void setFkCascade(String fkcascade) {
329 this.fkCascade = fkcascade;
330 }
331
332 public String getFkType() {
333 return fkType;
334 }
335
336 public void setFkType(String fkType) {
337 this.fkType = fkType;
338 }
339
340 /***
341 * @deprecated
342 * @return
343 */
344
345
346
347
348 /***
349 * @deprecated
350 * @param fkb
351 */
352 public void setFkb(String fkb) {
353 throw new UnsupportedOperationException();
354 }
355
356 /***
357 * @return Returns the indexed.
358 */
359 public boolean isIndexed() {
360 return indexed;
361 }
362
363 /***
364 * @param indexed
365 * The indexed to set.
366 */
367 public void setIndexed(boolean indexed) {
368 this.indexed = indexed;
369 }
370
371 public boolean isUnique() {
372 return unique;
373 }
374
375 public void setUnique(boolean unique) {
376 this.unique = unique;
377 }
378
379 /***
380 * @return Returns the extended.
381 */
382 public boolean isExtended() {
383 return extended;
384 }
385
386 /***
387 * @param extended
388 * The extended to set.
389 */
390 public void setExtended(boolean extended) {
391 this.extended = extended;
392 }
393
394 public List getForeignKeysContainsAsLocal() {
395 final List result = new ArrayList();
396 final Table table = this.getParent();
397 if (table == null)
398 return result;
399 for (Iterator i = table.getForeignKeys().iterator(); i.hasNext();) {
400 final ForeignKey fk = (ForeignKey) i.next();
401 if (fk.containsAsLocal(this))
402 result.add(fk);
403 }
404 return result;
405 }
406
407 public String getDecimalSize() {
408 return decimalSize;
409 }
410
411 public void setDecimalSize(String decimalSize) {
412 this.decimalSize = decimalSize;
413 }
414
415 public boolean isCaption() {
416 return caption;
417 }
418
419 public void setCaption(boolean caption) {
420 this.caption = caption;
421 }
422
423 }