1 package org.apache.torque.engine.database.model; 2 3 18 19 import java.util.ArrayList ; 20 import java.util.Hashtable ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 24 import org.apache.commons.lang.StringUtils; 25 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 29 import org.apache.torque.engine.EngineException; 30 31 import org.xml.sax.Attributes ; 32 33 44 public class Table implements IDMethod 45 { 46 47 private static Log log = LogFactory.getLog(Table.class); 48 49 private List columnList; 51 private List foreignKeys; 52 private List indices; 53 private List unices; 54 private List idMethodParameters; 55 private String name; 56 private String description; 57 private String javaName; 58 private String idMethod; 59 private String javaNamingMethod; 60 private Database tableParent; 61 private List referrers; 62 private List foreignTableNames; 63 private boolean containsForeignPK; 64 private Column inheritanceColumn; 65 private boolean skipSql; 66 private boolean abstractValue; 67 private String alias; 68 private String enterface; 69 private String pkg; 70 private String baseClass; 71 private String basePeer; 72 private Hashtable columnsByName; 73 private Hashtable columnsByJavaName; 74 private boolean needsTransactionInPostgres; 75 private boolean heavyIndexing; 76 private boolean forReferenceOnly; 77 78 79 82 public Table() 83 { 84 this(null); 85 } 86 87 92 public Table(String name) 93 { 94 this.name = name; 95 columnList = new ArrayList (); 96 foreignKeys = new ArrayList (5); 97 indices = new ArrayList (5); 98 unices = new ArrayList (5); 99 columnsByName = new Hashtable (); 100 columnsByJavaName = new Hashtable (); 101 } 102 103 109 public void loadFromXML(Attributes attrib, String defaultIdMethod) 110 { 111 name = attrib.getValue("name"); 112 javaName = attrib.getValue("javaName"); 113 idMethod = attrib.getValue("idMethod"); 114 115 javaNamingMethod = attrib.getValue("javaNamingMethod"); 118 if (javaNamingMethod == null) 119 { 120 javaNamingMethod = getDatabase().getDefaultJavaNamingMethod(); 121 } 122 123 if ("null".equals(idMethod)) 124 { 125 idMethod = defaultIdMethod; 126 } 127 skipSql = "true".equals(attrib.getValue("skipSql")); 128 abstractValue = "true".equals(attrib.getValue("abstract")); 130 baseClass = attrib.getValue("baseClass"); 131 basePeer = attrib.getValue("basePeer"); 132 alias = attrib.getValue("alias"); 133 heavyIndexing = "true".equals(attrib.getValue("heavyIndexing")) 134 || (!"false".equals(attrib.getValue("heavyIndexing")) 135 && getDatabase().isHeavyIndexing()); 136 description = attrib.getValue("description"); 137 enterface = attrib.getValue("interface"); 138 } 139 140 148 public void doFinalInitialization() 149 { 150 if (heavyIndexing) 153 { 154 doHeavyIndexing(); 155 } 156 157 doNaming(); 160 } 161 162 179 private void doHeavyIndexing() 180 { 181 if (log.isDebugEnabled()) 182 { 183 log.debug("doHeavyIndex() called on table " + name); 184 } 185 186 List pk = getPrimaryKey(); 187 int size = pk.size(); 188 189 try 190 { 191 for (int i = 1; i < size; i++) 195 { 196 addIndex(new Index(this, pk.subList(i, size))); 197 } 198 } 199 catch (EngineException e) 200 { 201 log.error(e, e); 202 } 203 } 204 205 209 private void doNaming() 210 { 211 int i; 212 int size; 213 String name; 214 215 try 217 { 218 for (i = 0, size = foreignKeys.size(); i < size; i++) 219 { 220 ForeignKey fk = (ForeignKey) foreignKeys.get(i); 221 name = fk.getName(); 222 if (StringUtils.isEmpty(name)) 223 { 224 name = acquireConstraintName("FK", i + 1); 225 fk.setName(name); 226 } 227 } 228 229 for (i = 0, size = indices.size(); i < size; i++) 230 { 231 Index index = (Index) indices.get(i); 232 name = index.getName(); 233 if (StringUtils.isEmpty(name)) 234 { 235 name = acquireConstraintName("I", i + 1); 236 index.setName(name); 237 } 238 } 239 240 for (i = 0, size = unices.size(); i < size; i++) 241 { 242 Unique unique = (Unique) unices.get(i); 243 name = unique.getName(); 244 if (StringUtils.isEmpty(name)) 245 { 246 name = acquireConstraintName("U", i + 1); 247 unique.setName(name); 248 } 249 } 250 } 251 catch (EngineException nameAlreadyInUse) 252 { 253 log.error(nameAlreadyInUse, nameAlreadyInUse); 254 } 255 } 256 257 265 private final String acquireConstraintName(String nameType, int nbr) 266 throws EngineException 267 { 268 List inputs = new ArrayList (4); 269 inputs.add(getDatabase()); 270 inputs.add(getName()); 271 inputs.add(nameType); 272 inputs.add(new Integer (nbr)); 273 return NameFactory.generateName(NameFactory.CONSTRAINT_GENERATOR, 274 inputs); 275 } 276 277 282 public String getBaseClass() 283 { 284 if (isAlias() && baseClass == null) 285 { 286 return alias; 287 } 288 else if (baseClass == null) 289 { 290 return getDatabase().getBaseClass(); 291 } 292 else 293 { 294 return baseClass; 295 } 296 } 297 298 302 public void setBaseClass(String v) 303 { 304 this.baseClass = v; 305 } 306 307 311 public String getBasePeer() 312 { 313 if (isAlias() && basePeer == null) 314 { 315 return alias + "Peer"; 316 } 317 else if (basePeer == null) 318 { 319 return getDatabase().getBasePeer(); 320 } 321 else 322 { 323 return basePeer; 324 } 325 } 326 327 331 public void setBasePeer(String v) 332 { 333 this.basePeer = v; 334 } 335 336 343 public Column addColumn(Attributes attrib) 344 { 345 Column col = new Column(); 346 col.setTable(this); 347 col.setCorrectGetters(false); 348 col.loadFromXML(attrib); 349 addColumn(col); 350 return col; 351 } 352 353 359 public void addColumn(Column col) 360 { 361 col.setTable (this); 362 if (col.isInheritance()) 363 { 364 inheritanceColumn = col; 365 } 366 columnList.add(col); 367 columnsByName.put(col.getName(), col); 368 columnsByJavaName.put(col.getJavaName(), col); 369 col.setPosition(columnList.size()); 370 needsTransactionInPostgres |= col.requiresTransactionInPostgres(); 371 } 372 373 380 public ForeignKey addForeignKey(Attributes attrib) 381 { 382 ForeignKey fk = new ForeignKey(); 383 fk.loadFromXML(attrib); 384 addForeignKey(fk); 385 return fk; 386 } 387 388 392 public Column getChildrenColumn() 393 { 394 return inheritanceColumn; 395 } 396 397 400 public List getChildrenNames() 401 { 402 if (inheritanceColumn == null 403 || !inheritanceColumn.isEnumeratedClasses()) 404 { 405 return null; 406 } 407 List children = inheritanceColumn.getChildren(); 408 List names = new ArrayList (children.size()); 409 for (int i = 0; i < children.size(); i++) 410 { 411 names.add(((Inheritance) children.get(i)).getClassName()); 412 } 413 return names; 414 } 415 416 421 public void addReferrer(ForeignKey fk) 422 { 423 if (referrers == null) 424 { 425 referrers = new ArrayList (5); 426 } 427 referrers.add(fk); 428 } 429 430 435 public List getReferrers() 436 { 437 return referrers; 438 } 439 440 445 public void setContainsForeignPK(boolean b) 446 { 447 containsForeignPK = b; 448 } 449 450 453 public boolean getContainsForeignPK() 454 { 455 return containsForeignPK; 456 } 457 458 463 public List getForeignTableNames() 464 { 465 if (foreignTableNames == null) 466 { 467 foreignTableNames = new ArrayList (1); 468 } 469 return foreignTableNames; 470 } 471 472 478 public void addForeignKey(ForeignKey fk) 479 { 480 fk.setTable (this); 481 foreignKeys.add(fk); 482 483 if (foreignTableNames == null) 484 { 485 foreignTableNames = new ArrayList (5); 486 } 487 if (!foreignTableNames.contains(fk.getForeignTableName())) 488 { 489 foreignTableNames.add(fk.getForeignTableName()); 490 } 491 } 492 493 496 public boolean requiresTransactionInPostgres() 497 { 498 return needsTransactionInPostgres; 499 } 500 501 505 public IdMethodParameter addIdMethodParameter(Attributes attrib) 506 { 507 IdMethodParameter imp = new IdMethodParameter(); 508 imp.loadFromXML(attrib); 509 addIdMethodParameter(imp); 510 return imp; 511 } 512 513 514 520 public void addIdMethodParameter(IdMethodParameter imp) 521 { 522 imp.setTable(this); 523 if (idMethodParameters == null) 524 { 525 idMethodParameters = new ArrayList (2); 526 } 527 idMethodParameters.add(imp); 528 } 529 530 534 public void addIndex(Index index) 535 { 536 index.setTable (this); 537 indices.add(index); 538 } 539 540 544 public Index addIndex(Attributes attrib) 545 { 546 Index index = new Index(); 547 index.loadFromXML(attrib); 548 addIndex(index); 549 return index; 550 } 551 552 556 public void addUnique(Unique unique) 557 { 558 unique.setTable(this); 559 unices.add(unique); 560 } 561 562 568 public Unique addUnique(Attributes attrib) 569 { 570 Unique unique = new Unique(); 571 unique.loadFromXML(attrib); 572 addUnique(unique); 573 return unique; 574 } 575 576 579 public String getName() 580 { 581 return name; 582 } 583 584 587 public void setName(String newName) 588 { 589 name = newName; 590 } 591 592 595 public String getDescription() 596 { 597 return description; 598 } 599 600 605 public void setDescription(String newDescription) 606 { 607 description = newDescription; 608 } 609 610 613 public String getJavaName() 614 { 615 if (javaName == null) 616 { 617 List inputs = new ArrayList (2); 618 inputs.add(name); 619 inputs.add(javaNamingMethod); 620 try 621 { 622 javaName = NameFactory.generateName(NameFactory.JAVA_GENERATOR, 623 inputs); 624 } 625 catch (EngineException e) 626 { 627 log.error(e, e); 628 } 629 } 630 return javaName; 631 } 632 633 636 public void setJavaName(String javaName) 637 { 638 this.javaName = javaName; 639 } 640 641 644 public String getIdMethod() 645 { 646 if (idMethod == null) 647 { 648 return IDMethod.NO_ID_METHOD; 649 } 650 else 651 { 652 return idMethod; 653 } 654 } 655 656 659 public void setIdMethod(String idMethod) 660 { 661 this.idMethod = idMethod; 662 } 663 664 669 public boolean isSkipSql() 670 { 671 return (skipSql || isAlias() || isForReferenceOnly()); 672 } 673 674 678 public void setSkipSql(boolean v) 679 { 680 this.skipSql = v; 681 } 682 683 687 public String getAlias() 688 { 689 return alias; 690 } 691 692 697 public boolean isAlias() 698 { 699 return (alias != null); 700 } 701 702 707 public void setAlias(String v) 708 { 709 this.alias = v; 710 } 711 712 713 717 public String getInterface() 718 { 719 return enterface; 720 } 721 722 726 public void setInterface(String v) 727 { 728 this.enterface = v; 729 } 730 731 739 public boolean isAbstract() 740 { 741 return abstractValue; 742 } 743 744 753 public void setAbstract(boolean v) 754 { 755 this.abstractValue = v; 756 } 757 758 763 public String getPackage() 764 { 765 if (pkg != null) 766 { 767 return pkg; 768 } 769 else 770 { 771 return this.getDatabase().getPackage(); 772 } 773 } 774 775 780 public void setPackage(String v) 781 { 782 this.pkg = v; 783 } 784 785 790 public List getColumns() 791 { 792 return columnList; 793 } 794 795 798 public int getNumColumns() 799 { 800 return columnList.size(); 801 } 802 803 808 public List getForeignKeys() 809 { 810 return foreignKeys; 811 } 812 813 817 public List getIdMethodParameters() 818 { 819 return idMethodParameters; 820 } 821 822 827 public String getSequenceName() 828 { 829 String result = null; 830 if (getIdMethod().equals(NATIVE)) 831 { 832 List idMethodParams = getIdMethodParameters(); 833 if (idMethodParams == null) 834 { 835 result = getName() + "_SEQ"; 836 } 837 else 838 { 839 result = ((IdMethodParameter) idMethodParams.get(0)).getValue(); 840 } 841 } 842 return result; 843 } 844 845 850 public List getIndices() 851 { 852 return indices; 853 } 854 855 860 public List getUnices() 861 { 862 return unices; 863 } 864 865 871 public Column getColumn(String name) 872 { 873 return (Column) columnsByName.get(name); 874 } 875 876 882 public Column getColumnByJavaName(String javaName) 883 { 884 return (Column) columnsByJavaName.get(javaName); 885 } 886 887 895 public ForeignKey getForeignKey(String col) 896 { 897 ForeignKey firstFK = null; 898 for (Iterator iter = foreignKeys.iterator(); iter.hasNext();) 899 { 900 ForeignKey key = (ForeignKey) iter.next(); 901 if (key.getLocalColumns().contains(col)) 902 { 903 if (firstFK == null) 904 { 905 firstFK = key; 906 } 907 else 908 { 909 } 914 } 915 } 916 return firstFK; 917 } 918 919 925 public boolean containsColumn(Column col) 926 { 927 return columnList.contains(col); 928 } 929 930 936 public boolean containsColumn(String name) 937 { 938 return (getColumn(name) != null); 939 } 940 941 946 public void setDatabase(Database parent) 947 { 948 tableParent = parent; 949 } 950 951 956 public Database getDatabase() 957 { 958 return tableParent; 959 } 960 961 966 public boolean isForReferenceOnly() 967 { 968 return forReferenceOnly; 969 } 970 971 976 public void setForReferenceOnly(boolean v) 977 { 978 this.forReferenceOnly = v; 979 } 980 981 986 public String toString() 987 { 988 StringBuffer result = new StringBuffer (); 989 990 result.append ("<table name=\"") 991 .append(name) 992 .append('\"'); 993 994 if (javaName != null) 995 { 996 result.append(" javaName=\"") 997 .append(javaName) 998 .append('\"'); 999 } 1000 1001 if (idMethod != null) 1002 { 1003 result.append(" idMethod=\"") 1004 .append(idMethod) 1005 .append('\"'); 1006 } 1007 1008 if (skipSql) 1009 { 1010 result.append(" skipSql=\"") 1011 .append(new Boolean (skipSql)) 1012 .append('\"'); 1013 } 1014 1015 if (abstractValue) 1016 { 1017 result.append(" abstract=\"") 1018 .append(new Boolean (abstractValue)) 1019 .append('\"'); 1020 } 1021 1022 if (baseClass != null) 1023 { 1024 result.append(" baseClass=\"") 1025 .append(baseClass) 1026 .append('\"'); 1027 } 1028 1029 if (basePeer != null) 1030 { 1031 result.append(" basePeer=\"") 1032 .append(basePeer) 1033 .append('\"'); 1034 } 1035 1036 result.append(">\n"); 1037 1038 if (columnList != null) 1039 { 1040 for (Iterator iter = columnList.iterator(); iter.hasNext();) 1041 { 1042 result.append(iter.next()); 1043 } 1044 } 1045 1046 if (foreignKeys != null) 1047 { 1048 for (Iterator iter = foreignKeys.iterator(); iter.hasNext();) 1049 { 1050 result.append(iter.next()); 1051 } 1052 } 1053 1054 if (idMethodParameters != null) 1055 { 1056 Iterator iter = idMethodParameters.iterator(); 1057 while (iter.hasNext()) 1058 { 1059 result.append(iter.next()); 1060 } 1061 } 1062 1063 result.append ("</table>\n"); 1064 1065 return result.toString(); 1066 } 1067 1068 1074 public List getPrimaryKey() 1075 { 1076 List pk = new ArrayList (columnList.size()); 1077 1078 Iterator iter = columnList.iterator(); 1079 while (iter.hasNext()) 1080 { 1081 Column col = (Column) iter.next(); 1082 if (col.isPrimaryKey()) 1083 { 1084 pk.add(col); 1085 } 1086 } 1087 return pk; 1088 } 1089 1090 1095 public boolean hasPrimaryKey() 1096 { 1097 return (getPrimaryKey().size() > 0); 1098 } 1099 1100 1105 public String printPrimaryKey() 1106 { 1107 return printList(columnList); 1108 } 1109 1110 1116 private String printList(List list) 1117 { 1118 StringBuffer result = new StringBuffer (); 1119 boolean comma = false; 1120 for (Iterator iter = list.iterator(); iter.hasNext();) 1121 { 1122 Column col = (Column) iter.next(); 1123 if (col.isPrimaryKey()) 1124 { 1125 if (comma) 1126 { 1127 result.append(','); 1128 } 1129 else 1130 { 1131 comma = true; 1132 } 1133 result.append(col.getName()); 1134 } 1135 } 1136 return result.toString(); 1137 } 1138 1139 1145 public void setCorrectGetters(Boolean value) 1146 { 1147 boolean correctGetters = value != null && value.booleanValue(); 1148 for (Iterator it = columnList.iterator(); it.hasNext(); ) 1149 { 1150 Column col = (Column) it.next(); 1151 col.setCorrectGetters(correctGetters); 1152 } 1153 } 1154} 1155 | Popular Tags |