1 package xdoclet.modules.ojb.model; 2 3 17 18 import java.util.*; 19 20 import xdoclet.modules.ojb.CommaListIterator; 21 22 25 public class TorqueModelDef extends DefBase 26 { 27 28 private SortedMap _tableDefs = new TreeMap(); 29 30 37 public TorqueModelDef(String dbName, ModelDef ojbModel) 38 { 39 super(dbName); 40 41 ClassDescriptorDef classDef; 42 TableDef tableDef; 43 FieldDescriptorDef fieldDef; 44 ColumnDef columnDef; 45 String name; 46 47 for (Iterator classIt = ojbModel.getClasses(); classIt.hasNext();) 48 { 49 classDef = (ClassDescriptorDef)classIt.next(); 50 if (classDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_OJB_PERSISTENT, false) && 51 classDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_GENERATE_TABLE_INFO, true)) 52 { 53 addTableFor(classDef); 54 } 55 } 56 } 57 58 62 67 private void addTableFor(ClassDescriptorDef classDef) 68 { 69 String name = classDef.getProperty(PropertyHelper.OJB_PROPERTY_TABLE); 70 TableDef tableDef = getTable(name); 71 FieldDescriptorDef fieldDef; 72 ReferenceDescriptorDef refDef; 73 CollectionDescriptorDef collDef; 74 ColumnDef columnDef; 75 IndexDef indexDef; 76 77 if (tableDef == null) 78 { 79 tableDef = new TableDef(name); 80 addTable(tableDef); 81 } 82 if (classDef.hasProperty(PropertyHelper.OJB_PROPERTY_DOCUMENTATION)) 83 { 84 tableDef.setProperty(PropertyHelper.OJB_PROPERTY_DOCUMENTATION, 85 classDef.getProperty(PropertyHelper.OJB_PROPERTY_DOCUMENTATION)); 86 } 87 if (classDef.hasProperty(PropertyHelper.OJB_PROPERTY_TABLE_DOCUMENTATION)) 88 { 89 tableDef.setProperty(PropertyHelper.OJB_PROPERTY_TABLE_DOCUMENTATION, 90 classDef.getProperty(PropertyHelper.OJB_PROPERTY_TABLE_DOCUMENTATION)); 91 } 92 for (Iterator fieldIt = classDef.getFields(); fieldIt.hasNext();) 93 { 94 fieldDef = (FieldDescriptorDef)fieldIt.next(); 95 if (fieldDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_IGNORE, false) || 96 fieldDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_VIRTUAL_FIELD, false)) 97 { 98 continue; 99 } 100 columnDef = addColumnFor(fieldDef, tableDef); 101 if (fieldDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_INDEXED, false)) 102 { 103 indexDef = tableDef.getIndex(null); 105 if (indexDef == null) 106 { 107 indexDef = new IndexDef(null, false); 108 tableDef.addIndex(indexDef); 109 } 110 indexDef.addColumn(columnDef.getName()); 111 } 112 } 113 for (Iterator refIt = classDef.getReferences(); refIt.hasNext();) 114 { 115 refDef = (ReferenceDescriptorDef)refIt.next(); 116 if (!refDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_IGNORE, false)) 117 { 118 addForeignkeys(refDef, tableDef); 119 } 120 } 121 for (Iterator collIt = classDef.getCollections(); collIt.hasNext();) 122 { 123 collDef = (CollectionDescriptorDef)collIt.next(); 124 if (!collDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_IGNORE, false)) 125 { 126 if (collDef.hasProperty(PropertyHelper.OJB_PROPERTY_INDIRECTION_TABLE)) 127 { 128 addIndirectionTable(collDef); 129 } 130 else 131 { 132 addForeignkeys(collDef, tableDef); 133 } 134 } 135 } 136 for (Iterator indexIt = classDef.getIndexDescriptors(); indexIt.hasNext();) 137 { 138 addIndex((IndexDescriptorDef)indexIt.next(), tableDef); 139 } 140 } 141 142 149 private ColumnDef addColumnFor(FieldDescriptorDef fieldDef, TableDef tableDef) 150 { 151 String name = fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_COLUMN); 152 ColumnDef columnDef = tableDef.getColumn(name); 153 154 if (columnDef == null) 155 { 156 columnDef = new ColumnDef(name); 157 tableDef.addColumn(columnDef); 158 } 159 if (!fieldDef.isNested()) 160 { 161 columnDef.setProperty(PropertyHelper.TORQUE_PROPERTY_JAVANAME, fieldDef.getName()); 162 } 163 columnDef.setProperty(PropertyHelper.TORQUE_PROPERTY_TYPE, fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_JDBC_TYPE)); 164 columnDef.setProperty(PropertyHelper.TORQUE_PROPERTY_ID, fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_ID)); 165 if (fieldDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_PRIMARYKEY, false)) 166 { 167 columnDef.setProperty(PropertyHelper.TORQUE_PROPERTY_PRIMARYKEY, "true"); 168 columnDef.setProperty(PropertyHelper.TORQUE_PROPERTY_REQUIRED, "true"); 169 } 170 else if (!fieldDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_NULLABLE, true)) 171 { 172 columnDef.setProperty(PropertyHelper.TORQUE_PROPERTY_REQUIRED, "true"); 173 } 174 if ("database".equals(fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_AUTOINCREMENT))) 175 { 176 columnDef.setProperty(PropertyHelper.TORQUE_PROPERTY_AUTOINCREMENT, "true"); 177 } 178 columnDef.setProperty(PropertyHelper.TORQUE_PROPERTY_SIZE, fieldDef.getSizeConstraint()); 179 if (fieldDef.hasProperty(PropertyHelper.OJB_PROPERTY_DOCUMENTATION)) 180 { 181 columnDef.setProperty(PropertyHelper.OJB_PROPERTY_DOCUMENTATION, 182 fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_DOCUMENTATION)); 183 } 184 if (fieldDef.hasProperty(PropertyHelper.OJB_PROPERTY_COLUMN_DOCUMENTATION)) 185 { 186 columnDef.setProperty(PropertyHelper.OJB_PROPERTY_COLUMN_DOCUMENTATION, 187 fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_COLUMN_DOCUMENTATION)); 188 } 189 return columnDef; 190 } 191 192 198 private void addForeignkeys(ReferenceDescriptorDef refDef, TableDef tableDef) 199 { 200 if (!refDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_DATABASE_FOREIGNKEY, true)) 201 { 202 return; 204 } 205 206 ClassDescriptorDef ownerClassDef = (ClassDescriptorDef)refDef.getOwner(); 213 String targetClassName = refDef.getProperty(PropertyHelper.OJB_PROPERTY_CLASS_REF); 214 ClassDescriptorDef referencedClassDef = ((ModelDef)ownerClassDef.getOwner()).getClass(targetClassName); 215 216 String tableName = getHierarchyTable(referencedClassDef); 219 220 if (tableName == null) 221 { 222 return; 223 } 224 225 try 226 { 227 String name = refDef.getName(); 228 ArrayList localFields = ownerClassDef.getFields(refDef.getProperty(PropertyHelper.OJB_PROPERTY_FOREIGNKEY)); 229 ArrayList remoteFields = referencedClassDef.getPrimaryKeys(); 230 231 tableDef.addForeignkey(name, tableName, getColumns(localFields), getColumns(remoteFields)); 232 } 233 catch (NoSuchFieldException ex) 234 { 235 } 237 } 238 239 245 private void addForeignkeys(CollectionDescriptorDef collDef, TableDef tableDef) 246 { 247 if (!collDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_DATABASE_FOREIGNKEY, true)) 248 { 249 return; 251 } 252 253 ClassDescriptorDef ownerClassDef = (ClassDescriptorDef)collDef.getOwner(); 260 String elementClassName = collDef.getProperty(PropertyHelper.OJB_PROPERTY_ELEMENT_CLASS_REF); 261 ClassDescriptorDef elementClassDef = ((ModelDef)ownerClassDef.getOwner()).getClass(elementClassName); 262 263 for (Iterator it = ownerClassDef.getAllBaseTypes(); it.hasNext();) 266 { 267 if (containsCollectionAndMapsToDifferentTable(collDef, tableDef, (ClassDescriptorDef)it.next())) 268 { 269 return; 270 } 271 } 272 for (Iterator it = ownerClassDef.getAllExtentClasses(); it.hasNext();) 273 { 274 if (containsCollectionAndMapsToDifferentTable(collDef, tableDef, (ClassDescriptorDef)it.next())) 275 { 276 return; 277 } 278 } 279 280 ArrayList candidates = new ArrayList(); 283 284 if (elementClassDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_GENERATE_TABLE_INFO, true)) 285 { 286 candidates.add(elementClassDef); 287 } 288 for (Iterator it = elementClassDef.getAllExtentClasses(); it.hasNext();) 289 { 290 ClassDescriptorDef curSubTypeDef = (ClassDescriptorDef)it.next(); 291 292 if (curSubTypeDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_GENERATE_TABLE_INFO, true)) 293 { 294 candidates.add(curSubTypeDef); 295 } 296 } 297 298 String name = collDef.getName(); 299 ArrayList remoteFields = ownerClassDef.getPrimaryKeys(); 300 HashMap processedTables = new HashMap(); 301 302 for (Iterator it = candidates.iterator(); it.hasNext();) 303 { 304 elementClassDef = (ClassDescriptorDef)it.next(); 305 try 306 { 307 String elementTableName = elementClassDef.getProperty(PropertyHelper.OJB_PROPERTY_TABLE); 309 310 if (!processedTables.containsKey(elementTableName)) 312 { 313 ArrayList localFields = elementClassDef.getFields(collDef.getProperty(PropertyHelper.OJB_PROPERTY_FOREIGNKEY)); 314 TableDef elementTableDef = getTable(elementTableName); 315 316 if (elementTableDef == null) 317 { 318 elementTableDef = new TableDef(elementTableName); 319 addTable(elementTableDef); 320 } 321 elementTableDef.addForeignkey(name, tableDef.getName(), getColumns(localFields), getColumns(remoteFields)); 322 processedTables.put(elementTableName, null); 323 } 324 } 325 catch (NoSuchFieldException ex) 326 { 327 } 329 } 330 } 331 332 338 private List getColumns(List fields) 339 { 340 ArrayList columns = new ArrayList(); 341 342 for (Iterator it = fields.iterator(); it.hasNext();) 343 { 344 FieldDescriptorDef fieldDef = (FieldDescriptorDef)it.next(); 345 346 columns.add(fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_COLUMN)); 347 } 348 return columns; 349 } 350 351 359 private boolean containsCollectionAndMapsToDifferentTable(CollectionDescriptorDef origCollDef, TableDef origTableDef, ClassDescriptorDef classDef) 360 { 361 if (classDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_GENERATE_TABLE_INFO, true) && 362 !origTableDef.getName().equals(classDef.getProperty(PropertyHelper.OJB_PROPERTY_TABLE))) 363 { 364 CollectionDescriptorDef curCollDef = classDef.getCollection(origCollDef.getName()); 365 366 if ((curCollDef != null) && 367 !curCollDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_IGNORE, false)) 368 { 369 return true; 370 } 371 } 372 return false; 373 } 374 375 385 private String getTargetTable(ClassDescriptorDef targetClassDef, String indirectionTable, String foreignKeys) 386 { 387 ModelDef modelDef = (ModelDef)targetClassDef.getOwner(); 388 String tableName = null; 389 390 for (Iterator classIt = modelDef.getClasses(); classIt.hasNext();) 391 { 392 ClassDescriptorDef curClassDef = (ClassDescriptorDef)classIt.next(); 393 394 if (!curClassDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_GENERATE_TABLE_INFO, true)) 395 { 396 continue; 397 } 398 for (Iterator collIt = curClassDef.getCollections(); collIt.hasNext();) 399 { 400 CollectionDescriptorDef curCollDef = (CollectionDescriptorDef)collIt.next(); 401 402 if (!indirectionTable.equals(curCollDef.getProperty(PropertyHelper.OJB_PROPERTY_INDIRECTION_TABLE)) || 403 !CommaListIterator.sameLists(foreignKeys, curCollDef.getProperty(PropertyHelper.OJB_PROPERTY_FOREIGNKEY))) 404 { 405 continue; 406 } 407 if (tableName != null) 409 { 410 if (!tableName.equals(curClassDef.getProperty(PropertyHelper.OJB_PROPERTY_TABLE))) 411 { 412 return null; 414 } 415 } 416 else 417 { 418 tableName = curClassDef.getProperty(PropertyHelper.OJB_PROPERTY_TABLE); 419 } 420 } 421 } 422 if (tableName == null) 423 { 424 return getHierarchyTable(targetClassDef); 427 } 428 else 429 { 430 return tableName; 431 } 432 } 433 434 442 private String getHierarchyTable(ClassDescriptorDef classDef) 443 { 444 ArrayList queue = new ArrayList(); 445 String tableName = null; 446 447 queue.add(classDef); 448 449 while (!queue.isEmpty()) 450 { 451 ClassDescriptorDef curClassDef = (ClassDescriptorDef)queue.get(0); 452 453 queue.remove(0); 454 455 if (curClassDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_GENERATE_TABLE_INFO, true)) 456 { 457 if (tableName != null) 458 { 459 if (!tableName.equals(curClassDef.getProperty(PropertyHelper.OJB_PROPERTY_TABLE))) 460 { 461 return null; 462 } 463 } 464 else 465 { 466 tableName = curClassDef.getProperty(PropertyHelper.OJB_PROPERTY_TABLE); 467 } 468 } 469 for (Iterator it = curClassDef.getExtentClasses(); it.hasNext();) 470 { 471 curClassDef = (ClassDescriptorDef)it.next(); 472 473 if (curClassDef.getReference("super") == null) 474 { 475 queue.add(curClassDef); 476 } 477 } 478 } 479 return tableName; 480 } 481 482 488 private void addIndex(IndexDescriptorDef indexDescDef, TableDef tableDef) 489 { 490 IndexDef indexDef = tableDef.getIndex(indexDescDef.getName()); 491 492 if (indexDef == null) 493 { 494 indexDef = new IndexDef(indexDescDef.getName(), 495 indexDescDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_UNIQUE, false)); 496 tableDef.addIndex(indexDef); 497 } 498 499 try 500 { 501 String fieldNames = indexDescDef.getProperty(PropertyHelper.OJB_PROPERTY_FIELDS); 502 ArrayList fields = ((ClassDescriptorDef)indexDescDef.getOwner()).getFields(fieldNames); 503 FieldDescriptorDef fieldDef; 504 505 for (Iterator it = fields.iterator(); it.hasNext();) 506 { 507 fieldDef = (FieldDescriptorDef)it.next(); 508 indexDef.addColumn(fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_COLUMN)); 509 } 510 } 511 catch (NoSuchFieldException ex) 512 { 513 } 515 } 516 517 522 private void addIndirectionTable(CollectionDescriptorDef collDef) 523 { 524 String tableName = collDef.getProperty(PropertyHelper.OJB_PROPERTY_INDIRECTION_TABLE); 525 TableDef tableDef = getTable(tableName); 526 527 if (tableDef == null) 528 { 529 tableDef = new TableDef(tableName); 530 addTable(tableDef); 531 } 532 if (collDef.hasProperty(PropertyHelper.OJB_PROPERTY_INDIRECTION_TABLE_DOCUMENTATION)) 533 { 534 tableDef.setProperty(PropertyHelper.OJB_PROPERTY_TABLE_DOCUMENTATION, 535 collDef.getProperty(PropertyHelper.OJB_PROPERTY_INDIRECTION_TABLE_DOCUMENTATION)); 536 } 537 538 ClassDescriptorDef ownerClassDef = (ClassDescriptorDef)collDef.getOwner(); 547 ModelDef modelDef = (ModelDef)ownerClassDef.getOwner(); 548 String elementClassName = collDef.getProperty(PropertyHelper.OJB_PROPERTY_ELEMENT_CLASS_REF); 549 ClassDescriptorDef elementClassDef = modelDef.getClass(elementClassName); 550 ArrayList localPrimFields = ownerClassDef.getPrimaryKeys(); 551 ArrayList remotePrimFields = elementClassDef.getPrimaryKeys(); 552 String localKeyList = collDef.getProperty(PropertyHelper.OJB_PROPERTY_FOREIGNKEY); 553 String remoteKeyList = collDef.getProperty(PropertyHelper.OJB_PROPERTY_REMOTE_FOREIGNKEY); 554 String ownerTable = getTargetTable(ownerClassDef, tableName, localKeyList); 555 String elementTable = getTargetTable(elementClassDef, tableName, remoteKeyList); 556 CommaListIterator localKeys = new CommaListIterator(localKeyList); 557 CommaListIterator localKeyDocs = new CommaListIterator(collDef.getProperty(PropertyHelper.OJB_PROPERTY_FOREIGNKEY_DOCUMENTATION)); 558 CommaListIterator remoteKeys = new CommaListIterator(remoteKeyList); 559 CommaListIterator remoteKeyDocs = new CommaListIterator(collDef.getProperty(PropertyHelper.OJB_PROPERTY_REMOTE_FOREIGNKEY_DOCUMENTATION)); 560 ArrayList localColumns = new ArrayList(); 561 ArrayList remoteColumns = new ArrayList(); 562 boolean asPrimarykeys = collDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_INDIRECTION_TABLE_PRIMARYKEYS, false); 563 FieldDescriptorDef fieldDef; 564 ColumnDef columnDef; 565 String relationName; 566 String name; 567 int idx; 568 569 for (idx = 0; localKeys.hasNext(); idx++) 570 { 571 fieldDef = (FieldDescriptorDef)localPrimFields.get(idx); 572 name = localKeys.getNext(); 573 columnDef = tableDef.getColumn(name); 574 if (columnDef == null) 575 { 576 columnDef = new ColumnDef(name); 577 tableDef.addColumn(columnDef); 578 } 579 columnDef.setProperty(PropertyHelper.TORQUE_PROPERTY_TYPE, fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_JDBC_TYPE)); 580 columnDef.setProperty(PropertyHelper.TORQUE_PROPERTY_SIZE, fieldDef.getSizeConstraint()); 581 if (asPrimarykeys) 582 { 583 columnDef.setProperty(PropertyHelper.TORQUE_PROPERTY_PRIMARYKEY, "true"); 584 } 585 if (localKeyDocs.hasNext()) 586 { 587 columnDef.setProperty(PropertyHelper.OJB_PROPERTY_COLUMN_DOCUMENTATION, localKeyDocs.getNext()); 588 } 589 localColumns.add(name); 590 remoteColumns.add(fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_COLUMN)); 591 } 592 if (collDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_DATABASE_FOREIGNKEY, true)) 593 { 594 relationName = collDef.getProperty(PropertyHelper.TORQUE_PROPERTY_RELATION_NAME); 595 if ((relationName != null) && (ownerTable != null)) 596 { 597 tableDef.addForeignkey(relationName, ownerTable, localColumns, remoteColumns); 598 } 599 } 600 localColumns.clear(); 601 remoteColumns.clear(); 602 603 for (idx = 0; remoteKeys.hasNext(); idx++) 604 { 605 fieldDef = (FieldDescriptorDef)remotePrimFields.get(idx); 606 name = remoteKeys.getNext(); 607 608 columnDef = tableDef.getColumn(name); 609 if (columnDef == null) 610 { 611 columnDef = new ColumnDef(name); 612 tableDef.addColumn(columnDef); 613 } 614 columnDef.setProperty(PropertyHelper.TORQUE_PROPERTY_TYPE, fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_JDBC_TYPE)); 615 columnDef.setProperty(PropertyHelper.TORQUE_PROPERTY_SIZE, fieldDef.getSizeConstraint()); 616 if (asPrimarykeys) 617 { 618 columnDef.setProperty(PropertyHelper.TORQUE_PROPERTY_PRIMARYKEY, "true"); 619 } 620 if (remoteKeyDocs.hasNext()) 621 { 622 columnDef.setProperty(PropertyHelper.OJB_PROPERTY_COLUMN_DOCUMENTATION, remoteKeyDocs.getNext()); 623 } 624 localColumns.add(name); 625 remoteColumns.add(fieldDef.getProperty(PropertyHelper.OJB_PROPERTY_COLUMN)); 626 } 627 628 CollectionDescriptorDef elementCollDef = collDef.getRemoteCollection(); 629 630 if (((elementCollDef != null) && elementCollDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_DATABASE_FOREIGNKEY, true)) || 631 ((elementCollDef == null) && collDef.getBooleanProperty(PropertyHelper.OJB_PROPERTY_DATABASE_FOREIGNKEY, true))) 632 { 633 relationName = collDef.getProperty(PropertyHelper.TORQUE_PROPERTY_INV_RELATION_NAME); 634 if ((relationName != null) && (elementTable != null)) 635 { 636 tableDef.addForeignkey(relationName, elementTable, localColumns, remoteColumns); 637 } 638 } 639 } 640 641 643 648 public Iterator getTables() 649 { 650 return _tableDefs.values().iterator(); 651 } 652 653 659 public TableDef getTable(String name) 660 { 661 return (TableDef)_tableDefs.get(name); 662 } 663 664 669 private void addTable(TableDef table) 670 { 671 table.setOwner(this); 672 _tableDefs.put(table.getName(), table); 673 } 674 } 675 | Popular Tags |