1
2
3 package net.sourceforge.sql2java;
4
5 import java.util.ArrayList;
6 import java.util.Date;
7 import java.util.HashMap;
8 import java.util.Hashtable;
9 import java.util.List;
10 import java.util.Random;
11 import java.util.Vector;
12
13 public class Table
14 {
15
16 private Hashtable colHash = new Hashtable();
17 private Vector cols = new Vector();
18 private Hashtable indHash = new Hashtable();
19 private Vector indices = new Vector();
20 private Hashtable indUniqueHash = new Hashtable();
21 private Vector uniqueIndices = new Vector();
22 private Hashtable indNonUniHash = new Hashtable();
23 private Vector nonUniqueIndices = new Vector();
24 private Vector priKey = new Vector();
25 private String catalog, schema, name, type, remarks;
26
27 private Vector foreignKeys = new Vector();
28 private Vector importedKeys = new Vector();
29
30 public boolean isRelationTable()
31 {
32 if ("false".equalsIgnoreCase(getTableProperty("nntable")))
33 return false;
34 return foreignKeys.size() == 2;
35 }
36
37 /**
38 * Tells whether if one of this table's columns (imported key)
39 * points to one of the otherTable's pk.
40 */
41 public boolean relationConnectsTo(Table otherTable)
42 {
43 if (this.equals(otherTable))
44 {
45 return false;
46 }
47
48 for (int i = 0; i < importedKeys.size(); i++)
49 {
50 Column c = (Column) importedKeys.get(i);
51 if (c.getTableName().equals(otherTable.getName()))
52 {
53 return true;
54 }
55 }
56 return false;
57 }
58
59 /**
60 * Return, beside the passed table, the tables this table points to.
61 */
62 public Table[] linkedTables(Database pDatabase, Table pTable)
63 {
64 Vector vector = new Vector();
65
66 for (int iIndex = 0; iIndex < importedKeys.size(); iIndex++)
67 {
68 Column pColumn = (Column) importedKeys.get(iIndex);
69 if (pColumn.getTableName().equals(pTable.getName()) == false)
70 {
71 Table pTableToAdd = pDatabase.getTable(pColumn.getTableName());
72 if (vector.contains(pTableToAdd) == false)
73 vector.add(pTableToAdd);
74 }
75 }
76 return (Table[])vector.toArray(new Table[vector.size()]);
77 }
78
79 /**
80 * Return the imported key pointing to the passed table.
81 */
82 public Column getForeignKeyFor(Table pTable)
83 {
84 for (int iIndex = 0; iIndex < importedKeys.size(); iIndex++)
85 {
86 Column pColumn = (Column) importedKeys.get(iIndex);
87 if (pColumn.getTableName().equals(pTable.getName()))
88 return pColumn;
89 }
90 return null;
91 }
92
93 public void setCatalog(String catalog)
94 {
95 this.catalog = catalog;
96 }
97 public void setSchema(String schema)
98 {
99 this.schema = schema;
100 }
101 public void setName(String name)
102 {
103 this.name = name;
104 }
105 public void setType(String type)
106 {
107 this.type = type;
108 }
109 public void setRemarks(String remarks)
110 {
111 if (remarks!=null) {
112 this.remarks = remarks.replaceAll("/\\*", "SLASH*").replaceAll("\\*/", "*SLASH");
113 }
114
115 }
116
117 public String getCatalog()
118 {
119 return catalog;
120 }
121 public String getSchema()
122 {
123 return schema;
124 }
125 public String getName()
126 {
127 return name;
128 }
129 public String getType()
130 {
131 return type;
132 }
133
134 public Column[] getColumns()
135 {
136 return (Column[])cols.toArray(new Column[cols.size()]);
137 }
138
139 public Column getColumn(String columnName)
140 {
141 return (Column) colHash.get(columnName.toLowerCase());
142 }
143
144 public void addColumn(Column column)
145 {
146 colHash.put(column.getName().toLowerCase(), column);
147 cols.addElement(column);
148 }
149
150 public void removeColumn(Column column)
151 {
152 cols.removeElement(column);
153 colHash.remove(column.getName().toLowerCase());
154 }
155
156 public Index[] getUniqueIndices() {
157 return (Index[])uniqueIndices.toArray(new Index[uniqueIndices.size()]);
158 }
159
160 public Index[] getNonUniqueIndices() {
161 return (Index[])nonUniqueIndices.toArray(new Index[nonUniqueIndices.size()]);
162 }
163
164 public Index[] getIndices() {
165 return (Index[])indices.toArray(new Index[indices.size()]);
166 }
167
168 public Index getIndex(String indName)
169 {
170 return (Index) indHash.get(indName.toLowerCase());
171 }
172
173 public void addIndex(Index index) {
174 indHash.put(index.getName().toLowerCase(), index);
175 indices.add(index);
176 if (index.isUnique()) {
177 indUniqueHash.put(index.getName().toLowerCase(), index);
178 uniqueIndices.add(index);
179 } else {
180 indNonUniHash.put(index.getName().toLowerCase(), index);
181 nonUniqueIndices.add(index);
182 }
183 }
184
185 public void removeIndex(Index index) {
186 indices.remove(index);
187 indHash.remove(index.getName().toLowerCase());
188 if (index.isUnique()) {
189 uniqueIndices.remove(index);
190 indUniqueHash.remove(index.getName().toLowerCase());
191 } else {
192 nonUniqueIndices.remove(index);
193 indNonUniHash.remove(index.getName().toLowerCase());
194 }
195 }
196
197 public Column[] getPrimaryKeys()
198 {
199 return (Column[])priKey.toArray(new Column[priKey.size()]);
200 }
201
202 public boolean hasCompositeKey()
203 {
204 if (priKey.size() > 1)
205 return true;
206 return false;
207 }
208
209 public Column getPrimaryKey() throws RuntimeException
210 {
211 if (priKey.size() != 1)
212 throw new RuntimeException("Table " + getName() + " has a composite key, not a unique primary key");
213 return (Column)priKey.get(0);
214 }
215
216 public void addPrimaryKey(Column column)
217 {
218 priKey.addElement(column);
219 column.isPrimaryKey(true);
220 }
221
222 public Column[] getImportedKeys()
223 {
224 return (Column[])importedKeys.toArray(new Column[importedKeys.size()]);
225 }
226
227 public void addImportedKey(Column column)
228 {
229 if (importedKeys.contains(column) == false)
230 importedKeys.addElement(column);
231 }
232
233 public int countColumns()
234 {
235 return cols.size();
236 }
237
238 public int countPrimaryKeys()
239 {
240 return priKey.size();
241 }
242
243 public boolean hasPrimaryKey()
244 {
245 return countPrimaryKeys() > 0;
246 }
247
248 public int countImportedKeys()
249 {
250 return importedKeys.size();
251 }
252
253 public boolean hasImportedKeys()
254 {
255 return countImportedKeys() > 0;
256 }
257
258 public int countForeignKeys()
259 {
260 return foreignKeys.size();
261 }
262
263 public boolean hasForeignKeys()
264 {
265 return countForeignKeys() > 0;
266 }
267
268 public void addForeignKey(Column col)
269 {
270 if (foreignKeys.contains(col) == false)
271 foreignKeys.add(col);
272 }
273
274 public Column[] getForeignKeys()
275 {
276 return (Column[])foreignKeys.toArray(new Column[foreignKeys.size()]);
277 }
278
279 public boolean isForeignKey(Column col)
280 {
281 return foreignKeys.contains(col);
282 }
283
284 public int countManyToManyTables()
285 {
286 return getManyToManyTables().length;
287 }
288
289 public boolean hasManyToManyTables()
290 {
291 return countManyToManyTables() > 0;
292 }
293
294
295 public Table[] getManyToManyTables()
296 {
297 Vector vector = new Vector();
298
299 Table[] linkedTables = getImportedTables();
300 System.out.println(getName() + " getManyToManyTables, linked tables = " +linkedTables.length );
301
302 for (int iIndex = 0; iIndex < linkedTables.length; iIndex++)
303 {
304 System.out.println(getName() + " " + linkedTables[iIndex].getName() + " relation table ?");
305 if (linkedTables[iIndex].isRelationTable())
306 {
307 Table[] relationLinkedTable = linkedTables[iIndex].getForeignTables();
308 System.out.println(getName() + " " + linkedTables[iIndex].getName() + " has " + relationLinkedTable.length + " foreign table");
309 for (int i = 0; i < relationLinkedTable.length; i++)
310 {
311 System.out.println(getName() + " " + i + " " + relationLinkedTable[i].getName() + " is relation table");
312
313 if (relationLinkedTable[i].equals(this) == false)
314 {
315 if (vector.contains(relationLinkedTable[i]) == false)
316 vector.add(relationLinkedTable[i]);
317 }
318 }
319 }
320 }
321 return (Table[])vector.toArray(new Table[vector.size()]);
322 }
323
324 public int countLinkedTables()
325 {
326 return getLinkedTables().length;
327 }
328
329 public boolean hasLinkedTables()
330 {
331 return countLinkedTables() > 0;
332 }
333
334 public Table[] getLinkedTables()
335 {
336 Vector vector = new Vector();
337
338 for (int iIndex = 0; iIndex < importedKeys.size(); iIndex++)
339 {
340 Column column = (Column) importedKeys.get(iIndex);
341 if (column.getTableName().equals(getName()) == false)
342 {
343 Table pTableToAdd = column.getTable();
344 if (vector.contains(pTableToAdd) == false)
345 vector.add(pTableToAdd);
346 }
347 }
348
349 for (int iIndex = 0; iIndex < foreignKeys.size(); iIndex++)
350 {
351 Column column = (Column) foreignKeys.get(iIndex);
352 column = column.getForeignColumn();
353 if (column.getTableName().equals(getName()) == false)
354 {
355 Table pTableToAdd = column.getTable();
356 if (vector.contains(pTableToAdd) == false)
357 vector.add(pTableToAdd);
358 }
359 }
360 return (Table[])vector.toArray(new Table[vector.size()]);
361 }
362
363 public int countImportedTables()
364 {
365 return getImportedTables().length;
366 }
367
368 public boolean hasImportedTables()
369 {
370 return countImportedTables() > 0;
371 }
372
373 public Table[] getImportedTables()
374 {
375 Vector vector = new Vector();
376 for (int iIndex = 0; iIndex < importedKeys.size(); iIndex++)
377 {
378 Column column = (Column) importedKeys.get(iIndex);
379 if (column.getTableName().equals(getName()) == false)
380 {
381 Table pTableToAdd = column.getTable();
382 if (vector.contains(pTableToAdd) == false)
383 vector.add(pTableToAdd);
384 }
385 }
386 return (Table[])vector.toArray(new Table[vector.size()]);
387 }
388
389 public int countForeignTables()
390 {
391 return getForeignTables().length;
392 }
393
394 public boolean hasForeignTables()
395 {
396 return countForeignTables() > 0;
397 }
398
399 public Table[] getForeignTables()
400 {
401 Vector vector = new Vector();
402 for (int iIndex = 0; iIndex < foreignKeys.size(); iIndex++)
403 {
404 Column column = ((Column) foreignKeys.get(iIndex)).getForeignColumn();
405 if (column.getTableName().equals(getName()) == false)
406 {
407 Table pTableToAdd = column.getTable();
408 if (vector.contains(pTableToAdd) == false)
409 vector.add(pTableToAdd);
410 }
411 }
412 return (Table[])vector.toArray(new Table[vector.size()]);
413 }
414
415 public Table getRelationTable(Table targetTable)
416 {
417 System.out.println("getRelationTable " + getName() + "<->" + targetTable.getName()+ ")");
418 Table[] importedTables = getImportedTables();
419 for (int iIndex = 0; iIndex < importedTables.length; iIndex++)
420 {
421
422 Table[] foreignTables = importedTables[iIndex].getForeignTables();
423 for (int iIndex2 = 0; iIndex2 < foreignTables.length; iIndex2++)
424 {
425
426 if (foreignTables[iIndex2].getName().equalsIgnoreCase(getName()))
427 return importedTables[iIndex];
428 }
429 }
430 return targetTable;
431 }
432
433 private List procedures = new ArrayList();
434 private HashMap procHash = new HashMap();
435 public int countProcedures() {
436 return procedures.size();
437 }
438
439 public boolean hasProcedures() {
440 return countProcedures() > 0;
441 }
442
443 public Procedure[] getProcedures() {
444 return (Procedure[]) procedures.toArray(new Procedure[procedures.size()]);
445 }
446
447 public void addProcedure(Procedure procedure) {
448 if (null == procHash.get(procedure.getName())) {
449 procedures.add(procedure);
450 procHash.put(procedure.getName(), procedure);
451 }
452 }
453
454 public String[] getLinkedPackages()
455 {
456 Vector vector = new Vector();
457 Table[] linkedTables = getLinkedTables();
458 for (int iIndex = 0; iIndex < linkedTables.length; iIndex++)
459 {
460 if (vector.contains(linkedTables[iIndex].getPackage()) == false)
461 vector.add(linkedTables[iIndex].getPackage());
462 }
463 return (String[])vector.toArray(new String[vector.size()]);
464 }
465
466 public String getPackage()
467 {
468 String basePackage = CodeWriter.getProperty("codewriter.package");
469 String xmlSubpackage = getTableProperty("subpackage");
470
471 if (null != xmlSubpackage)
472 return basePackage + "." + xmlSubpackage;
473
474
475
476 int iterating = 1;
477 while(true)
478 {
479 String tablesProperty = "subpackage." + iterating + ".tables";
480 String packageNameProperty = "subpackage." + iterating + ".name";
481 String tables[] = CodeWriter.getPropertyExploded(tablesProperty);
482
483
484 if (tables.length == 0)
485 break;
486 for (int i = 0; i < tables.length; i ++)
487 {
488 if (getName().equalsIgnoreCase(tables[i]))
489 {
490 String packageName = CodeWriter.getProperty(packageNameProperty);
491 if (packageName == null)
492 return basePackage;
493 return basePackage + "." + packageName;
494 }
495 }
496 iterating ++;
497 }
498 return basePackage;
499 }
500
501 public String getPackagePath()
502 {
503 return getPackage().replace('.', '/') + "/";
504 }
505
506 public Column[] getColumnsFor(String webElement)
507 {
508 Vector vector = new Vector();
509 for (int i = 0; i < cols.size(); i++)
510 {
511 Column c = (Column)cols.get(i);
512 if (c.columnFor(webElement))
513 vector.add(c);
514 }
515 return (Column[])vector.toArray(new Column[vector.size()]);
516 }
517
518 public Column getFirstColumn()
519 {
520 return (Column) cols.get(0);
521 }
522
523 public String getTableProperty(String property)
524 {
525 return ConfigHelper.getTableProperty(name, property);
526 }
527
528 public String getRemarks()
529 {
530
531 String xmlDefaultValue = getTableProperty("description");
532 if (xmlDefaultValue != null && xmlDefaultValue.equals("") == false)
533 return xmlDefaultValue;
534 return remarks==null?"":remarks;
535 }
536
537 public String getJavaName()
538 {
539 return convertName("");
540 }
541
542 public String convertName(String value)
543 {
544 if (!"".equals(value)) {
545 return StringUtilities.convertName(getName() + "_" + value, false);
546 }
547 return StringUtilities.convertName(getName(), false);
548
549 }
550
551 public String asClass(String suffix) {
552 return convertName(suffix);
553 }
554
555 public String asCoreClass() {
556 return convertName("");
557 }
558
559 public String asBeanClass() {
560 return convertName("Bean");
561 }
562
563 public String asCacheClass() {
564 return convertName("Cache");
565 }
566
567 public String asRelationnalBeanClass() {
568 return convertName("Relationnal_Bean");
569 }
570
571 public String asHibernateManagerClass() {
572 return convertName("Hibernate_Manager");
573 }
574
575 public String asIteratorClass() {
576 return convertName("Iterator");
577 }
578
579 public String asFactoryClass() {
580 return convertName("Factory");
581 }
582
583 public String asHttpFactoryClass() {
584 return convertName("Http_Factory");
585 }
586
587 public String asComparatorClass() {
588 return convertName("Comparator");
589 }
590
591 public String asListenerClass() {
592 return convertName("Listener");
593 }
594
595 public String asRendererClass() {
596 return convertName("Renderer");
597 }
598
599 public String asExceptionClass() {
600 return convertName("Exception");
601 }
602
603 public String asWidgetClass() {
604 return convertName("Widget");
605 }
606
607 public String asWidgetFactoryClass() {
608 return convertName("Widget_Factory");
609 }
610
611 public String asActionClass() {
612 return convertName("Action");
613 }
614
615 public String asActionTestClass() {
616 return convertName("Action_Test");
617 }
618
619 public String asControllerClass() {
620 return convertName("Controller");
621 }
622
623 public String asControllerTestClass() {
624 return convertName("Controller_Test");
625 }
626
627 public String asFormControllerClass() {
628 return convertName("Form_Controller");
629 }
630
631 public String asFormControllerTestClass() {
632 return convertName("Form_Controller_Test");
633 }
634
635
636 public String asDAOClass() {
637 return convertName("D_A_O");
638 }
639
640 public String asDAOTestClass() {
641 return convertName("D_A_O_Test");
642 }
643
644 public String asDAOHibernateClass() {
645 return convertName("D_A_O_Hibernate");
646 }
647
648 public String asManagerClass() {
649 return convertName("Manager");
650 }
651
652 public String asManagerImplClass() {
653 return convertName("Manager_Impl");
654 }
655
656 public String asManagerTestClass() {
657 return convertName("Manager_Test");
658 }
659
660 public String asModelClass() {
661 return convertName("Model");
662 }
663
664 public String asPKClass() {
665 return convertName("P_K");
666 }
667
668 public String asTblClass() {
669 return convertName("Tbl");
670 }
671
672 public Column getVersionColumn()
673 {
674 for (int i = 0; i < cols.size(); i++)
675 {
676 Column c = (Column)cols.get(i);
677 if (c.isVersion())
678 return c;
679 }
680 throw new RuntimeException("No version column for table " + getName());
681 }
682
683 public boolean hasVersionColumn()
684 {
685 try
686 {
687 getVersionColumn();
688 return true;
689 }
690 catch(RuntimeException e)
691 {
692 return false;
693 }
694 }
695
696 private Random aleatorio = new Random(new Date().getTime());
697 public long getSerialVersionUID() {
698 return aleatorio.nextLong();
699 }
700 }