1 package org.apache.torque.map; 2 3 21 22 import java.util.Collections ; 23 import java.util.Iterator ; 24 import java.util.Map ; 25 import java.util.StringTokenizer ; 26 27 import org.apache.commons.collections.map.ListOrderedMap; 28 import org.apache.commons.lang.StringUtils; 29 import org.apache.torque.adapter.IDMethod; 30 import org.apache.torque.oid.IdGenerator; 31 32 40 public class TableMap implements IDMethod, java.io.Serializable 41 { 42 43 private static final long serialVersionUID = -9053174532511492818L; 44 45 46 protected static final String [] VALID_ID_METHODS = 47 { 48 NATIVE, AUTO_INCREMENT, SEQUENCE, ID_BROKER, NO_ID_METHOD 49 }; 50 51 52 private Map columns; 53 54 55 private DatabaseMap dbMap; 56 57 58 private String tableName; 59 60 61 private String javaName; 62 63 64 private String prefix; 65 66 67 private String primaryKeyMethod = NO_ID_METHOD; 68 69 70 private String description = ""; 71 72 73 private Class peerClass; 74 75 76 private Class omClass; 77 78 79 private boolean useInheritance = false; 80 81 82 private boolean useManager = false; 83 84 85 private Class managerClass; 86 87 91 private Object pkInfo = null; 92 93 96 public TableMap() 97 { 98 } 99 100 107 public TableMap(String tableName, 108 int numberOfColumns, 109 DatabaseMap containingDB) 110 { 111 this.tableName = tableName; 112 dbMap = containingDB; 113 columns = Collections.synchronizedMap(new ListOrderedMap()); 114 } 115 116 122 public TableMap(String tableName, DatabaseMap containingDB) 123 { 124 this.tableName = tableName; 125 dbMap = containingDB; 126 columns = Collections.synchronizedMap(new ListOrderedMap()); 127 } 128 129 137 public TableMap(String tableName, 138 String prefix, 139 DatabaseMap containingDB) 140 { 141 this.tableName = tableName; 142 this.prefix = prefix; 143 dbMap = containingDB; 144 columns = Collections.synchronizedMap(new ListOrderedMap()); 145 } 146 147 153 public boolean containsColumn(ColumnMap column) 154 { 155 return containsColumn(column.getColumnName()); 156 } 157 158 164 public boolean containsColumn(String name) 165 { 166 if (name.indexOf('.') > 0) 167 { 168 name = name.substring(name.indexOf('.') + 1); 169 } 170 return columns.containsKey(name); 171 } 172 173 178 public DatabaseMap getDatabaseMap() 179 { 180 return dbMap; 181 } 182 183 190 public boolean containsObjectColumn() 191 { 192 synchronized (columns) 193 { 194 Iterator it = columns.values().iterator(); 195 while (it.hasNext()) 196 { 197 Object theType = ((ColumnMap) it.next()).getType(); 198 if (!(theType instanceof String || theType instanceof Number 199 || theType instanceof java.util.Date )) 200 { 201 return true; 202 } 203 } 204 } 205 return false; 206 } 207 208 213 public String getName() 214 { 215 return tableName; 216 } 217 218 223 public String getJavaName() 224 { 225 return javaName; 226 } 227 228 233 public void setJavaName(String value) 234 { 235 this.javaName = value; 236 } 237 238 243 public String getPrefix() 244 { 245 return this.prefix; 246 } 247 248 254 public void setPrefix(String prefix) 255 { 256 this.prefix = prefix; 257 } 258 259 264 public String getPrimaryKeyMethod() 265 { 266 return primaryKeyMethod; 267 } 268 269 275 public IdGenerator getIdGenerator() 276 { 277 return getDatabaseMap().getIdGenerator(primaryKeyMethod); 278 } 279 280 285 public Object getPrimaryKeyMethodInfo() 286 { 287 return pkInfo; 288 } 289 290 295 public ColumnMap[] getColumns() 296 { 297 ColumnMap[] tableColumns = new ColumnMap[columns.size()]; 298 synchronized (columns) 299 { 300 Iterator it = columns.values().iterator(); 301 int i = 0; 302 while (it.hasNext()) 303 { 304 tableColumns[i++] = (ColumnMap) it.next(); 305 } 306 } 307 return tableColumns; 308 } 309 310 316 public ColumnMap getColumn(String name) 317 { 318 try 319 { 320 return (ColumnMap) columns.get(name); 321 } 322 catch (Exception e) 323 { 324 return null; 325 } 326 } 327 328 334 public void addColumn(ColumnMap cmap) 335 { 336 columns.put(cmap.getColumnName(), cmap); 337 } 338 339 348 public void addColumn(String columnName, Object type) 349 { 350 addColumn(columnName, type, false, null, null, 0); 351 } 352 353 364 public void addColumn(String columnName, Object type, int size, int scale) 365 { 366 addColumn(columnName, type, false, null, null, size, scale); 367 } 368 369 379 public void addColumn(String columnName, Object type, int size) 380 { 381 addColumn(columnName, type, false, null, null, size); 382 } 383 384 393 public void addPrimaryKey(String columnName, Object type) 394 { 395 addColumn(columnName, type, true, null, null, 0); 396 } 397 398 408 public void addPrimaryKey(String columnName, Object type, int size) 409 { 410 addColumn(columnName, type, true, null, null, size); 411 } 412 413 424 public void addForeignKey(String columnName, 425 Object type, 426 String fkTable, 427 String fkColumn) 428 { 429 addColumn(columnName, type, false, fkTable, fkColumn, 0); 430 } 431 432 444 public void addForeignKey(String columnName, 445 Object type, 446 String fkTable, 447 String fkColumn, 448 int size) 449 { 450 addColumn(columnName, type, false, fkTable, fkColumn, size); 451 } 452 453 464 public void addForeignPrimaryKey(String columnName, 465 Object type, 466 String fkTable, 467 String fkColumn) 468 { 469 addColumn(columnName, type, true, fkTable, fkColumn, 0); 470 } 471 472 484 public void addForeignPrimaryKey(String columnName, 485 Object type, 486 String fkTable, 487 String fkColumn, 488 int size) 489 { 490 addColumn(columnName, type, true, fkTable, fkColumn, size); 491 } 492 493 506 private void addColumn(String name, 507 Object type, 508 boolean pk, 509 String fkTable, 510 String fkColumn, 511 int size) 512 { 513 addColumn(name, type, pk, fkTable, fkColumn, size, 0); 514 } 515 516 530 private void addColumn(String name, 531 Object type, 532 boolean pk, 533 String fkTable, 534 String fkColumn, 535 int size, 536 int scale) 537 { 538 if (name.indexOf('.') > 0 && name.indexOf(getName()) != -1) 542 { 543 name = name.substring(getName().length() + 1); 544 } 545 if (fkTable != null && fkTable.length() > 0 && fkColumn != null 546 && fkColumn.length() > 0) 547 { 548 if (fkColumn.indexOf('.') > 0 && fkColumn.indexOf(fkTable) != -1) 549 { 550 fkColumn = fkColumn.substring(fkTable.length() + 1); 551 } 552 } 553 ColumnMap col = new ColumnMap(name, this); 554 col.setType(type); 555 col.setPrimaryKey(pk); 556 col.setForeignKey(fkTable, fkColumn); 557 col.setSize(size); 558 col.setScale(scale); 559 columns.put(name, col); 560 } 561 562 569 public void setPrimaryKeyMethod(String method) 570 { 571 primaryKeyMethod = NO_ID_METHOD; 572 573 for (int i = 0; i < VALID_ID_METHODS.length; i++) 575 { 576 if (VALID_ID_METHODS[i].equalsIgnoreCase(method)) 577 { 578 primaryKeyMethod = method; 579 break; 580 } 581 } 582 if (ID_BROKER.equalsIgnoreCase(method)) 583 { 584 getDatabaseMap().startIdBroker(); 585 } 586 } 587 588 593 public void setPrimaryKeyMethodInfo(Object pkInfo) 594 { 595 this.pkInfo = pkInfo; 596 } 597 598 600 606 private boolean hasPrefix(String data) 607 { 608 return (data.indexOf(getPrefix()) != -1); 609 } 610 611 617 private String removePrefix(String data) 618 { 619 return data.substring(getPrefix().length()); 620 } 621 622 631 public final String removeUnderScores(String data) 632 { 633 String tmp = null; 634 StringBuffer out = new StringBuffer (); 635 if (hasPrefix(data)) 636 { 637 tmp = removePrefix(data); 638 } 639 else 640 { 641 tmp = data; 642 } 643 644 StringTokenizer st = new StringTokenizer (tmp, "_"); 645 while (st.hasMoreTokens()) 646 { 647 String element = ((String ) st.nextElement()).toLowerCase(); 648 out.append(StringUtils.capitalize(element)); 649 } 650 return out.toString(); 651 } 652 653 658 public String getDescription() 659 { 660 return description; 661 } 662 663 668 public void setDescription(String description) 669 { 670 this.description = description; 671 } 672 673 678 public Class getOMClass() 679 { 680 return omClass; 681 } 682 683 688 public void setOMClass(Class omClass) 689 { 690 this.omClass = omClass; 691 } 692 693 698 public Class getPeerClass() 699 { 700 return peerClass; 701 } 702 703 708 public void setPeerClass(Class peerClass) 709 { 710 this.peerClass = peerClass; 711 } 712 713 718 public DatabaseMap getDbMap() 719 { 720 return dbMap; 721 } 722 723 728 public boolean isUseInheritance() 729 { 730 return useInheritance; 731 } 732 733 738 public void setUseInheritance(boolean useInheritance) 739 { 740 this.useInheritance = useInheritance; 741 } 742 743 748 public boolean isUseManager() 749 { 750 return useManager; 751 } 752 753 758 public void setUseManager(boolean useManager) 759 { 760 this.useManager = useManager; 761 } 762 763 768 public Class getManagerClass() 769 { 770 return managerClass; 771 } 772 773 778 public void setManagerClass(Class managerClass) 779 { 780 this.managerClass = managerClass; 781 } 782 } 783 | Popular Tags |