1 23 24 29 30 package com.sun.jdo.api.persistence.model.mapping.impl; 31 32 import java.util.*; 33 import java.beans.PropertyVetoException ; 34 35 import org.netbeans.modules.dbschema.*; 36 import org.netbeans.modules.dbschema.util.NameUtil; 37 38 import com.sun.jdo.api.persistence.model.*; 39 import com.sun.jdo.api.persistence.model.mapping.*; 40 import com.sun.jdo.api.persistence.model.jdo.PersistenceClassElement; 41 import com.sun.jdo.spi.persistence.utility.I18NHelper; 42 import com.sun.jdo.spi.persistence.utility.JavaTypeHelper; 43 44 50 public class MappingClassElementImpl extends MappingElementImpl 51 implements MappingClassElement 52 { 53 public static final int CLONE_FIELDS = 1; 55 public static final int CLONE_DEEP = 2; 56 public static final int CLONE_MASK = 3; 57 public static final int NAVIGABLE = 4; 58 59 PersistenceClassElement _persistenceElement; 60 61 private boolean _isModified; 62 private int _properties; 63 private ArrayList _tables; private ArrayList _fields; 66 70 private static final int CURRENT_VERSION_NO = 5; 71 72 76 private int versionNo = CURRENT_VERSION_NO; 77 78 82 private String _databaseRoot; 83 84 86 private int _consistencyLevel; 87 88 89 99 100 103 107 public MappingClassElementImpl () 108 { 109 this((String )null); 110 } 111 112 115 public MappingClassElementImpl (String name) 116 { 117 super(name); 118 _consistencyLevel = NONE_CONSISTENCY; 119 _properties = _properties | NAVIGABLE; 120 } 121 122 126 public MappingClassElementImpl (PersistenceClassElement element) 127 { 128 this((element != null) ? element.getName() : null); 129 setPersistenceElement(element); 130 } 131 132 137 public int getVersionNumber () { return versionNo; } 138 139 142 private void setVersionNumber (int version) { versionNo = version; } 143 144 149 public boolean hasOldVersionNumber () 150 { 151 return (getVersionNumber() < CURRENT_VERSION_NO); 152 } 153 154 160 public static MappingClassElement forName (String name, Model model) 161 { 162 return model.getMappingClass(name); 163 } 164 165 172 protected final void firePropertyChange (String name, Object o, Object n) 173 { 174 boolean noChange = ((o != null) && (n != null) && o.equals(n)); 178 179 super.firePropertyChange(name, o, n); 180 181 if (!(PROP_MODIFIED.equals(name)) && !noChange) 182 setModified(true); 183 } 184 185 193 protected final void fireVetoableChange (String name, Object o, Object n) 194 throws PropertyVetoException 195 { 196 boolean noChange = ((o != null) && (n != null) && o.equals(n)); 200 201 super.fireVetoableChange(name, o, n); 202 203 if (!(PROP_MODIFIED.equals(name)) && !noChange) 204 fireVetoableChange(PROP_MODIFIED, Boolean.FALSE, Boolean.TRUE); 205 } 206 207 209 public final PersistenceClassElement getPersistenceElement () 210 { 211 return _persistenceElement; 212 } 213 214 217 public void setPersistenceElement (PersistenceClassElement element) 218 { 219 _persistenceElement = element; 220 } 221 222 233 235 239 public boolean isModified () { return _isModified; } 240 241 247 public void setModified (boolean flag) 248 { 249 boolean oldFlag = isModified(); 250 251 if (flag != oldFlag) 252 { 253 _isModified = flag; 254 firePropertyChange(PROP_MODIFIED, JavaTypeHelper.valueOf(oldFlag), 255 JavaTypeHelper.valueOf(flag)); 256 } 257 } 258 259 269 public int getConsistencyLevel () { return _consistencyLevel; } 270 271 281 public void setConsistencyLevel (int level) throws ModelException 282 { 283 Integer old = new Integer (getConsistencyLevel()); 284 Integer newLevel = new Integer (level); 285 286 try 287 { 288 fireVetoableChange(PROP_CONSISTENCY, old, newLevel); 289 _consistencyLevel = level; 290 firePropertyChange(PROP_CONSISTENCY, old, newLevel); 291 } 292 catch (PropertyVetoException e) 293 { 294 throw new ModelVetoException(e); 295 } 296 } 297 298 302 public String getDatabaseRoot () { return _databaseRoot; } 303 304 310 public void setDatabaseRoot (SchemaElement root) throws ModelException 311 { 312 String old = getDatabaseRoot(); 313 String newRoot = ((root != null) ? root.getName().getFullName() : null); 314 315 try 316 { 317 fireVetoableChange(PROP_DATABASE_ROOT, old, newRoot); 318 _databaseRoot = newRoot; 319 firePropertyChange(PROP_DATABASE_ROOT, old, newRoot); 320 } 321 catch (PropertyVetoException e) 322 { 323 throw new ModelVetoException(e); 324 } 325 } 326 327 331 public ArrayList getTables () 332 { 333 if (_tables == null) 334 _tables = new ArrayList(); 335 336 return _tables; 337 } 338 339 344 public MappingTableElement getTable (String name) 345 { 346 Iterator tableIterator = getTables().iterator(); 347 348 while (tableIterator.hasNext()) 349 { 350 MappingTableElement table = 351 (MappingTableElement)tableIterator.next(); 352 353 if (table.getName().equals(name)) 354 return table; 355 } 356 357 return null; 358 } 359 360 367 public void addTable (TableElement table) throws ModelException 368 { 369 if (table != null) 370 { 371 ArrayList tables = getTables(); 372 373 if (tables.isEmpty()) 375 setPrimaryTable(table); 376 else 377 { 378 HashMap newSecondaryTables = new HashMap(); 379 Iterator iterator = tables.iterator(); 380 boolean found = false; 381 382 while (iterator.hasNext()) 384 if (((MappingTableElement)iterator.next()).isEqual(table)) 385 return; 386 387 iterator = tables.iterator(); 390 while (iterator.hasNext()) 391 { 392 MappingTableElement mappingTable = 393 (MappingTableElement)iterator.next(); 394 String absoluteTableName = NameUtil.getAbsoluteTableName( 395 _databaseRoot, mappingTable.getTable()); 396 ForeignKeyElement[] foreignKeys = TableElement.forName( 397 absoluteTableName).getForeignKeys(); 398 int i, count = 399 ((foreignKeys != null) ? foreignKeys.length : 0); 400 401 for (i = 0; i < count; i++) 402 { 403 ForeignKeyElement fk = foreignKeys[i]; 404 405 if (table == fk.getReferencedTable()) 406 { 407 newSecondaryTables.put(mappingTable, fk); 411 found = true; 412 } 413 } 414 } 415 416 if (found) { 418 iterator = newSecondaryTables.keySet().iterator(); 419 420 while (iterator.hasNext()) 421 { 422 MappingTableElement mappingTable = 423 (MappingTableElement)iterator.next(); 424 MappingReferenceKeyElement refKey = 425 addSecondaryTable(mappingTable, table); 426 427 refKey.addColumnPairs(((ForeignKeyElement) 428 newSecondaryTables.get(mappingTable)). 429 getColumnPairs()); 430 } 431 432 } 433 else 434 { 435 throw new ModelException(I18NHelper.getMessage( 436 getMessages(), 437 "mapping.table.foreign_key_not_found", table)); } 439 } 440 } 441 else 442 { 443 throw new ModelException(I18NHelper.getMessage(getMessages(), 444 "mapping.table.null_argument")); } 446 } 447 448 452 public void setPrimaryTable (TableElement table) throws ModelException 453 { 454 ArrayList tables = getTables(); 455 456 if (!tables.isEmpty()) 457 { 458 throw new ModelException(I18NHelper.getMessage(getMessages(), 459 "mapping.table.primary_table_defined", table)); } 461 else 462 { 463 UniqueKeyElement key = table.getPrimaryKey(); 464 MappingTableElement mappingTable = 465 new MappingTableElementImpl(table, this); 466 SchemaElement schema = table.getDeclaringSchema(); 467 String currentRoot = getDatabaseRoot(); 468 469 if (currentRoot == null) setDatabaseRoot(schema); 471 else if (!currentRoot.equals(schema.getName().getFullName())) 472 { 473 throw new ModelException(I18NHelper.getMessage( 475 getMessages(), "mapping.table.schema_mismatch", table.toString(), currentRoot)); 477 } 478 479 try 480 { 481 fireVetoableChange(PROP_TABLES, null, null); 482 tables.add(mappingTable); 483 firePropertyChange(PROP_TABLES, null, null); 484 } 485 catch (PropertyVetoException e) 486 { 487 throw new ModelVetoException(e); 488 } 489 490 if (key == null) 492 { 493 UniqueKeyElement[] uniqueKeys = table.getUniqueKeys(); 494 495 if ((uniqueKeys != null) && (uniqueKeys.length > 0)) 496 key = uniqueKeys[0]; 497 } 498 499 if (key == null) 500 { 501 } 505 else 506 { 507 ColumnElement[] columns = key.getColumns(); 508 int i, count = ((columns != null) ? columns.length : 0); 509 510 for (i = 0; i < count; i++) 511 mappingTable.addKeyColumn(columns[i]); 512 } 513 } 514 } 515 516 524 public MappingReferenceKeyElement addSecondaryTable (MappingTableElement 525 parentTable, TableElement table) throws ModelException 526 { 527 ArrayList tables = getTables(); 528 529 if ((parentTable == null) || (table == null)) 530 { 531 throw new ModelException(I18NHelper.getMessage(getMessages(), 532 "mapping.element.null_argument")); } 534 else if (!tables.contains(parentTable)) 535 { 536 throw new ModelException(I18NHelper.getMessage(getMessages(), 537 "mapping.table.parent_table_not_found", parentTable.getTable())); 539 } 540 else 541 { 542 Iterator iterator = parentTable.getReferencingKeys().iterator(); 546 MappingTableElement mappingTable = 547 new MappingTableElementImpl(table, this); 548 MappingReferenceKeyElement key = 549 new MappingReferenceKeyElementImpl(mappingTable); 550 551 while (iterator.hasNext()) 552 { 553 MappingTableElement compareTable = 554 ((MappingReferenceKeyElement)iterator.next()).getTable(); 555 556 if (compareTable.isEqual(table)) 557 { 558 throw new ModelException(I18NHelper.getMessage( 559 getMessages(), 560 "mapping.table.secondary_table_defined", new Object []{table, parentTable.getTable()})); 562 } 563 } 564 565 try 566 { 567 fireVetoableChange(PROP_TABLES, null, null); 568 parentTable.addReferencingKey(key); 569 tables.add(mappingTable); 570 firePropertyChange(PROP_TABLES, null, null); 571 } 572 catch (PropertyVetoException e) 573 { 574 throw new ModelVetoException(e); 575 } 576 577 return key; 578 } 579 } 580 581 587 public void removeTable (MappingTableElement table) throws ModelException 588 { 589 if (table != null) 590 { 591 Collection tables = getTables(); 592 Iterator iterator = null; 593 boolean found = false; 594 595 try 596 { 597 fireVetoableChange(PROP_TABLES, null, null); 598 found = tables.remove(table); 599 firePropertyChange(PROP_TABLES, null, null); 600 } 601 catch (PropertyVetoException e) 602 { 603 throw new ModelVetoException(e); 604 } 605 606 iterator = tables.iterator(); 608 while (iterator.hasNext()) 609 { 610 MappingTableElement nextTable = 611 (MappingTableElement)iterator.next(); 612 613 nextTable.removeReference(table); 614 } 615 616 if (found) { 618 ArrayList fieldsToRemove = new ArrayList(); 619 620 iterator = getFields().iterator(); 621 while (iterator.hasNext()) 622 { 623 MappingFieldElementImpl mappingField = 624 (MappingFieldElementImpl)iterator.next(); 625 626 if (mappingField.isMappedToTable(table)) 627 fieldsToRemove.add(mappingField); 628 } 629 630 iterator = fieldsToRemove.iterator(); 631 while (iterator.hasNext()) 632 { 633 MappingFieldElement mappingField = 634 (MappingFieldElement)iterator.next(); 635 boolean versionField = mappingField.isVersion(); 636 637 removeField(mappingField); 638 639 if (versionField) 642 { 643 mappingField = new MappingFieldElementImpl( 644 mappingField.getName(), this); 645 mappingField.setVersion(true); 646 addField(mappingField); 647 } 648 } 649 } 650 else 651 { 652 throw new ModelException(I18NHelper.getMessage(getMessages(), 653 "mapping.element.element_not_removed", table)); } 655 } 656 else 657 { 658 throw new ModelException(I18NHelper.getMessage(getMessages(), 659 "mapping.element.null_argument")); } 661 } 662 663 667 public ArrayList getFields () 668 { 669 if (_fields == null) 670 _fields = new ArrayList(); 671 672 return _fields; 673 } 674 675 680 public MappingFieldElement getField (String name) 681 { 682 Iterator fieldIterator = getFields().iterator(); 683 684 while (fieldIterator.hasNext()) 685 { 686 MappingFieldElement field = 687 (MappingFieldElement)fieldIterator.next(); 688 689 if (name.equals(field.getName())) 690 return field; 691 } 692 693 return null; 694 } 695 696 700 public void addField (MappingFieldElement field) throws ModelException 701 { 702 ArrayList fields = getFields(); 703 704 if (!fields.contains(field)) 705 { 706 try 707 { 708 fireVetoableChange(PROP_FIELDS, null, null); 709 fields.add(field); 710 firePropertyChange(PROP_FIELDS, null, null); 711 } 712 catch (PropertyVetoException e) 713 { 714 throw new ModelVetoException(e); 715 } 716 } 717 } 718 719 723 public void removeField (MappingFieldElement field) throws ModelException 724 { 725 try 726 { 727 fireVetoableChange(PROP_FIELDS, null, null); 728 729 if (!getFields().remove(field)) 730 { 731 throw new ModelException(I18NHelper.getMessage(getMessages(), 732 "mapping.element.element_not_removed", field)); } 734 735 firePropertyChange(PROP_FIELDS, null, null); 736 } 737 catch (PropertyVetoException e) 738 { 739 throw new ModelVetoException(e); 740 } 741 } 742 743 748 public List getVersionFields () 749 { 750 List versionFields = new ArrayList(); 751 752 if (VERSION_CONSISTENCY == getConsistencyLevel()) 753 { 754 Iterator iterator = getFields().iterator(); 755 756 while (iterator.hasNext()) 757 { 758 MappingFieldElement fieldCandidate = 759 (MappingFieldElement)iterator.next(); 760 761 if (fieldCandidate.isVersion()) 762 versionFields.add(fieldCandidate); 763 } 764 } 765 766 return versionFields; 767 } 768 769 774 public boolean isNavigable () { return ((_properties & NAVIGABLE) > 0); } 775 776 782 public void setNavigable (boolean flag) throws ModelException 783 { 784 Boolean old = JavaTypeHelper.valueOf(isNavigable()); 785 Boolean newFlag = JavaTypeHelper.valueOf(flag); 786 787 try 788 { 789 fireVetoableChange(PROP_NAVIGABLE, old, newFlag); 790 _properties = (flag) ? 791 (_properties | NAVIGABLE) : (_properties & ~NAVIGABLE); 792 firePropertyChange(PROP_NAVIGABLE, old, newFlag); 793 } 794 catch (PropertyVetoException e) 795 { 796 throw new ModelVetoException(e); 797 } 798 } 799 800 802 810 protected static ArrayList toColumnObjects (String schemaName, 811 ArrayList columnNames) 812 { 813 Iterator iterator = columnNames.iterator(); 814 ArrayList objects = new ArrayList(); 815 816 while (iterator.hasNext()) 817 { 818 String columnName = (String )iterator.next(); 819 String absoluteColumnName = 820 NameUtil.getAbsoluteMemberName(schemaName, columnName); 821 final TableElement table = 822 TableElement.forName(NameUtil.getTableName(absoluteColumnName)); 823 824 objects.add(table.getMember( 825 DBIdentifier.create(absoluteColumnName))); 826 } 827 828 return objects; 829 } 830 831 833 842 public String getKeyClass () 843 { 844 return getPersistenceElement().getKeyClass(); 845 } 846 847 849 854 858 859 864 public void setFields (ArrayList fields) { _fields = fields; } 865 866 public int getProperties () { return _properties; } 867 868 871 878 918 919 924 926 930 934 935 940 950 951 957 963 969 973 1003 1008 1038 1039 1044 public void postUnarchive () throws ModelException 1045 { 1046 switch (versionNo) 1048 { 1049 case 0: case 1: throw new ModelException (I18NHelper.getMessage(getMessages(), 1052 "file.incompatible_version", getName())); case 2: 1054 stripSchemaName(); 1056 break; 1057 case 3: case 4: case MappingClassElementImpl.CURRENT_VERSION_NO: 1060 break; 1062 default: throw new ModelException (I18NHelper.getMessage(getMessages(), 1064 "file.incompatible_version", getName())); } 1066 } 1067 1068 1075 public void preArchive () 1076 { 1077 setVersionNumber(CURRENT_VERSION_NO); 1079 } 1080 1081 1083 1090 protected void stripSchemaName () 1091 { 1092 String schemaName = null; 1093 1094 if (_tables != null && !_tables.isEmpty()) 1096 { 1097 schemaName = NameUtil.getSchemaName( 1098 ((MappingTableElement)_tables.get(0)).getTable()); 1099 } 1100 1101 _databaseRoot = schemaName; 1103 1104 1106 if (_tables != null) 1108 { 1109 Iterator i = _tables.iterator(); 1110 while (i.hasNext()) 1111 ((MappingTableElementImpl)i.next()).stripSchemaName(); 1112 } 1113 1114 if (_fields != null) 1116 { 1117 Iterator i = _fields.iterator(); 1118 while (i.hasNext()) 1119 ((MappingFieldElementImpl)i.next()).stripSchemaName(); 1120 } 1121 } 1122} 1123 | Popular Tags |