1 21 package oracle.toplink.essentials.internal.ejb.cmp3.metadata; 23 24 import java.lang.reflect.Type ; 25 26 import java.util.ArrayList ; 27 import java.util.Collection ; 28 import java.util.HashMap ; 29 import java.util.List ; 30 import java.util.Map ; 31 32 import oracle.toplink.essentials.exceptions.ValidationException; 34 35 import oracle.toplink.essentials.descriptors.ClassDescriptor; 36 import oracle.toplink.essentials.descriptors.RelationalDescriptor; 37 import oracle.toplink.essentials.descriptors.VersionLockingPolicy; 38 39 import oracle.toplink.essentials.internal.ejb.cmp3.base.CMP3Policy; 40 41 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.ClassAccessor; 42 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.MetadataAccessor; 43 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.RelationshipAccessor; 44 45 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.columns.MetadataColumn; 46 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.columns.MetadataJoinColumns; 47 48 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.listeners.MetadataEntityListener; 49 50 import oracle.toplink.essentials.internal.ejb.cmp3.xml.XMLConstants; 51 52 import oracle.toplink.essentials.internal.helper.DatabaseField; 53 import oracle.toplink.essentials.internal.helper.DatabaseTable; 54 55 import oracle.toplink.essentials.mappings.DatabaseMapping; 56 57 64 public class MetadataDescriptor { 65 protected ClassAccessor m_accessor; 66 protected ClassDescriptor m_descriptor; 67 68 protected Boolean m_usesPropertyAccess; 69 70 protected boolean m_ignoreIDs; 71 protected boolean m_ignoreTables; 72 protected boolean m_isCascadePersist; 73 protected boolean m_ignoreAnnotations; protected boolean m_ignoreInheritance; 75 protected boolean m_usesSingleTableInheritanceStrategy; 76 77 protected String m_xmlSchema; protected String m_xmlCatalog; protected String m_xmlAccess; protected String m_primaryTableName; 81 protected String m_embeddedIdAttributeName; 82 83 protected List <String > m_idAttributeNames; 84 protected List <String > m_orderByAttributeNames; 85 protected List <String > m_idOrderByAttributeNames; 86 protected List <MetadataDescriptor> m_aggregateDescriptors; 87 88 protected Map <String , Type > m_pkClassIDs; 89 protected Map <String , MetadataAccessor> m_accessors; 90 protected Map <String , MetadataColumn> m_attributeOverrides; 91 protected Map <String , MetadataJoinColumns> m_associationOverrides; 92 protected Map <String , MetadataAccessor> m_manyToManyAccessors; 93 protected Map <String , RelationshipAccessor> m_relationshipAccessors; 94 95 98 public MetadataDescriptor(Class javaClass) { 99 init(); 100 m_descriptor = new RelationalDescriptor(); 101 m_descriptor.setExistenceChecking("Check database"); 102 m_descriptor.setAlias(""); 103 setJavaClass(javaClass); 104 } 105 106 109 public void addAccessor(MetadataAccessor accessor) { 110 m_accessors.put(accessor.getAttributeName(), accessor); 111 112 if (accessor.isRelationship()) { 113 m_relationshipAccessors.put(accessor.getAttributeName(), (RelationshipAccessor) accessor); 114 } 115 116 if (accessor.isManyToMany()) { 119 m_manyToManyAccessors.put(accessor.getReferenceClassName(), accessor); 120 } 121 } 122 123 126 public void addAggregateDescriptor(MetadataDescriptor aggregateDescriptor) { 127 m_aggregateDescriptors.add(aggregateDescriptor); 128 } 129 130 133 public void addAssociationOverride(String attributeName, MetadataJoinColumns joinColumns) { 134 m_associationOverrides.put(attributeName, joinColumns); 135 } 136 137 140 public void addAttributeOverride(MetadataColumn column) { 141 m_attributeOverrides.put(column.getAttributeName(), column); 142 } 143 144 147 public void addClassIndicator(Class entityClass, String value) { 148 if (m_accessor.isInheritanceSubclass()) { 149 m_accessor.getInheritanceRootDescriptor().addClassIndicator(entityClass, value); 150 } else { 151 m_descriptor.getInheritancePolicy().addClassNameIndicator(entityClass.getName(), value); 152 } 153 } 154 155 158 public void addDefaultEventListener(MetadataEntityListener listener) { 159 m_descriptor.getEventManager().addDefaultEventListener(listener); 160 } 161 162 165 public void addEntityListenerEventListener(MetadataEntityListener listener) { 166 m_descriptor.getEventManager().addEntityListenerEventListener(listener); 167 } 168 169 172 public void addIdAttributeName(String idAttributeName) { 173 m_idAttributeNames.add(idAttributeName); 174 } 175 176 179 public void addMapping(DatabaseMapping mapping) { 180 m_descriptor.addMapping(mapping); 181 } 182 183 186 public void addMultipleTableForeignKeyField(DatabaseField pkField, DatabaseField fkField) { 187 m_descriptor.addMultipleTableForeignKeyField(pkField, fkField); 188 } 189 190 193 public void addMultipleTablePrimaryKeyField(DatabaseField pkField, DatabaseField fkField) { 194 m_descriptor.addMultipleTablePrimaryKeyField(pkField, fkField); 195 } 196 197 202 public void addPKClassId(String attributeName, Type type) { 203 m_pkClassIDs.put(attributeName, type); 204 } 205 206 209 public void addPrimaryKeyField(DatabaseField field) { 210 m_descriptor.addPrimaryKeyField(field); 211 } 212 213 216 public void addTable(DatabaseTable table) { 217 m_descriptor.addTable(table); 218 } 219 220 223 public boolean excludeSuperclassListeners() { 224 return m_descriptor.getEventManager().excludeSuperclassListeners(); 225 } 226 227 237 public MetadataAccessor getAccessorFor(String fieldOrPropertyName) { 238 MetadataAccessor accessor = m_accessors.get(fieldOrPropertyName); 239 240 if (accessor == null) { 241 accessor = m_accessors.get(MetadataHelper.getAttributeNameFromMethodName(fieldOrPropertyName)); 243 244 if (accessor == null && m_accessor.isInheritanceSubclass()) { 247 accessor = m_accessor.getInheritanceRootDescriptor().getAccessorFor(fieldOrPropertyName); 248 } 249 } 250 251 return accessor; 252 } 253 254 257 public String getAlias() { 258 return m_descriptor.getAlias(); 259 } 260 261 264 public MetadataJoinColumns getAssociationOverrideFor(String attributeName) { 265 return m_associationOverrides.get(attributeName); 266 } 267 268 271 public MetadataColumn getAttributeOverrideFor(String attributeName) { 272 return m_attributeOverrides.get(attributeName); 273 } 274 275 278 public DatabaseField getClassIndicatorField() { 279 if (m_accessor.getInheritanceRootDescriptor() != null) { 280 return m_accessor.getInheritanceRootDescriptor().getDescriptor().getInheritancePolicy().getClassIndicatorField(); 281 } else { 282 if (getDescriptor().hasInheritance()) { 283 return getDescriptor().getInheritancePolicy().getClassIndicatorField(); 284 } else { 285 return null; 286 } 287 } 288 } 289 290 296 public String getDefaultTableName() { 297 String defaultTableName = getAlias().toUpperCase(); 298 299 if (m_accessor.isInheritanceSubclass()) { 300 if (m_accessor.getInheritanceRootDescriptor().usesSingleTableInheritanceStrategy()) { 301 defaultTableName = m_accessor.getInheritanceRootDescriptor().getPrimaryTableName(); 302 } 303 } 304 305 return defaultTableName; 306 } 307 308 311 public ClassDescriptor getDescriptor() { 312 return m_descriptor; 313 } 314 315 318 public String getCatalog() { 319 return m_xmlCatalog; 320 } 321 322 325 public ClassAccessor getClassAccessor() { 326 return m_accessor; 327 } 328 329 332 public String getEmbeddedIdAttributeName() { 333 return m_embeddedIdAttributeName; 334 } 335 336 340 public String getIdAttributeName() { 341 if (getIdAttributeNames().isEmpty()) { 342 if (m_accessor.isInheritanceSubclass()) { 343 return m_accessor.getInheritanceRootDescriptor().getIdAttributeName(); 344 } else { 345 return ""; 346 } 347 } else { 348 return (String ) getIdAttributeNames().get(0); 349 } 350 } 351 352 356 public List <String > getIdAttributeNames() { 357 return m_idAttributeNames; 358 } 359 360 371 public List <String > getIdOrderByAttributeNames() { 372 if (m_idOrderByAttributeNames.isEmpty()) { 373 if (m_idAttributeNames.isEmpty()) { 374 if (m_accessor.isInheritanceSubclass()) { 375 m_idOrderByAttributeNames = m_accessor.getInheritanceRootDescriptor().getIdAttributeNames(); 377 } else { 378 m_idOrderByAttributeNames = getAccessorFor(getEmbeddedIdAttributeName()).getReferenceDescriptor().getOrderByAttributeNames(); 380 } 381 } else { 382 m_idOrderByAttributeNames = m_idAttributeNames; 383 } 384 } 385 386 return m_idOrderByAttributeNames; 387 } 388 389 390 393 public MetadataAccessor getManyToManyAccessor(String className) { 394 return m_manyToManyAccessors.get(className); 395 } 396 397 403 public List <String > getOrderByAttributeNames() { 404 if (m_orderByAttributeNames.isEmpty()) { 405 for (DatabaseMapping mapping : getMappings()) { 406 if (mapping.isDirectToFieldMapping()) { 407 m_orderByAttributeNames.add(mapping.getAttributeName()); 408 } 409 } 410 } 411 412 return m_orderByAttributeNames; 413 } 414 415 418 public Class getJavaClass() { 419 return m_descriptor.getJavaClass(); 420 } 421 422 425 public String getJavaClassName() { 426 return m_descriptor.getJavaClassName(); 427 } 428 429 432 public DatabaseMapping getMappingForAttributeName(String attributeName) { 433 MetadataAccessor accessor = (MetadataAccessor) getAccessorFor(attributeName); 434 435 if (accessor != null) { 436 if (accessor.isRelationship()) { 441 ((RelationshipAccessor) accessor).processRelationship(); 442 } 443 444 return m_descriptor.getMappingForAttributeName(attributeName); 445 } 446 447 for (MetadataDescriptor aggregateDmd : m_aggregateDescriptors) { 450 DatabaseMapping mapping = aggregateDmd.getMappingForAttributeName(attributeName); 451 452 if (mapping != null) { 453 return mapping; 454 } 455 } 456 457 if (m_accessor.isInheritanceSubclass()) { 461 return m_accessor.getInheritanceRootDescriptor().getMappingForAttributeName(attributeName); 462 } 463 464 return null; 466 } 467 468 471 public List <DatabaseMapping> getMappings() { 472 return m_descriptor.getMappings(); 473 } 474 475 478 public String getPKClassName() { 479 String pkClassName = null; 480 481 if (m_descriptor.hasCMPPolicy()) { 482 pkClassName = ((CMP3Policy) m_descriptor.getCMPPolicy()).getPKClassName(); 483 } 484 485 return pkClassName; 486 } 487 488 493 public String getPrimaryKeyFieldName() { 494 return ((DatabaseField)(getPrimaryKeyFields().iterator().next())).getName(); 495 } 496 497 503 public List getPrimaryKeyFields() { 504 List primaryKeyFields = m_descriptor.getPrimaryKeyFields(); 505 506 if (primaryKeyFields.isEmpty() && m_accessor.isInheritanceSubclass()) { 507 primaryKeyFields = m_accessor.getInheritanceRootDescriptor().getPrimaryKeyFields(); 508 } 509 510 return primaryKeyFields; 511 } 512 513 516 public String getPrimaryTableName() { 517 if (m_primaryTableName == null && m_accessor.isInheritanceSubclass()) { 518 return m_accessor.getInheritanceRootDescriptor().getPrimaryTableName(); 519 } else { 520 if (m_descriptor.isAggregateDescriptor()) { 521 return ""; 523 } 524 525 return m_primaryTableName; 526 } 527 } 528 529 532 public Collection <RelationshipAccessor> getRelationshipAccessors() { 533 return m_relationshipAccessors.values(); 534 } 535 536 539 public String getSchema() { 540 return m_xmlSchema; 541 } 542 543 546 public DatabaseField getSequenceNumberField() { 547 return m_descriptor.getSequenceNumberField(); 548 } 549 550 553 public boolean hasAssociationOverrideFor(String attributeName) { 554 return m_associationOverrides.containsKey(attributeName); 555 } 556 557 560 public boolean hasAttributeOverrideFor(String attributeName) { 561 return m_attributeOverrides.containsKey(attributeName); 562 } 563 564 567 public boolean hasCompositePrimaryKey() { 568 return getPrimaryKeyFields().size() > 1 || getPKClassName() != null; 569 } 570 571 574 public boolean hasEmbeddedIdAttribute() { 575 return m_embeddedIdAttributeName != null; 576 } 577 578 581 public boolean hasInheritance() { 582 return m_descriptor.hasInheritance(); 583 } 584 585 588 public boolean hasManyToManyAccessorFor(String className) { 589 return m_manyToManyAccessors.containsKey(className); 590 } 591 592 595 public boolean hasMappingForAttributeName(String attributeName) { 596 return m_descriptor.getMappingForAttributeName(attributeName) != null; 597 } 598 599 603 public boolean hasPrimaryKeyFields() { 604 return m_descriptor.getPrimaryKeyFields().size() > 0; 605 } 606 607 610 public boolean ignoreIDs() { 611 return m_ignoreIDs; 612 } 613 614 617 public boolean ignoreInheritance() { 618 return m_ignoreInheritance; 619 } 620 621 624 public boolean ignoreTables() { 625 return m_ignoreTables; 626 } 627 628 631 private void init() { 632 m_xmlAccess = ""; 633 m_xmlSchema = ""; 634 m_xmlCatalog = ""; 635 636 m_ignoreIDs = false; 637 m_ignoreTables = false; 638 m_ignoreInheritance = false; 639 m_isCascadePersist = false; 640 m_ignoreAnnotations = false; 641 642 m_idAttributeNames = new ArrayList <String >(); 643 m_orderByAttributeNames = new ArrayList <String >(); 644 m_idOrderByAttributeNames = new ArrayList <String >(); 645 m_aggregateDescriptors = new ArrayList <MetadataDescriptor>(); 646 647 m_pkClassIDs = new HashMap <String , Type >(); 648 m_accessors = new HashMap <String , MetadataAccessor>(); 649 m_attributeOverrides = new HashMap <String , MetadataColumn>(); 650 m_manyToManyAccessors = new HashMap <String , MetadataAccessor>(); 651 m_associationOverrides = new HashMap <String , MetadataJoinColumns>(); 652 m_relationshipAccessors = new HashMap <String , RelationshipAccessor>(); 653 } 654 655 660 public boolean isCascadePersist() { 661 return m_isCascadePersist; 662 } 663 664 669 public boolean isXmlFieldAccess() { 670 return m_xmlAccess.equals(XMLConstants.FIELD); 671 } 672 673 678 public boolean isXmlPropertyAccess() { 679 return m_xmlAccess.equals(XMLConstants.PROPERTY); 680 } 681 682 685 public boolean pkClassWasNotValidated() { 686 return ! m_pkClassIDs.isEmpty(); 687 } 688 689 692 public void setAccess(String access) { 693 m_xmlAccess = access; 694 } 695 696 699 public void setAlias(String alias) { 700 m_descriptor.setAlias(alias); 701 } 702 703 706 public void setCatalog(String xmlCatalog) { 707 m_xmlCatalog = xmlCatalog; 708 } 709 710 713 public void setClassAccessor(ClassAccessor accessor) { 714 m_accessor = accessor; 715 } 716 717 720 public void setClassIndicatorField(DatabaseField field) { 721 m_descriptor.getInheritancePolicy().setClassIndicatorField(field); 722 } 723 724 727 public void setDescriptor(ClassDescriptor descriptor) { 728 m_descriptor = descriptor; 729 } 730 731 734 public void setDescriptorIsEmbeddable() { 735 m_descriptor.descriptorIsAggregate(); 736 } 737 738 741 public void setEmbeddedIdAttributeName(String embeddedIdAttributeName) { 742 m_embeddedIdAttributeName = embeddedIdAttributeName; 743 } 744 745 748 public void setEntityEventListener(MetadataEntityListener listener) { 749 m_descriptor.getEventManager().setEntityEventListener(listener); 750 } 751 752 755 public void setExcludeDefaultListeners(boolean excludeDefaultListeners) { 756 m_descriptor.getEventManager().setExcludeDefaultListeners(excludeDefaultListeners); 757 } 758 759 762 public void setExcludeSuperclassListeners(boolean excludeSuperclassListeners) { 763 m_descriptor.getEventManager().setExcludeSuperclassListeners(excludeSuperclassListeners); 764 } 765 766 769 public void setIgnoreFlags() { 770 m_ignoreInheritance = m_descriptor.hasInheritance(); 771 m_ignoreTables = m_descriptor.getTableNames().size() > 0; 772 m_ignoreIDs = m_descriptor.getPrimaryKeyFieldNames().size() > 0; 773 } 774 775 779 public void setInheritanceStrategy(String inheritanceStrategy) { 780 if (inheritanceStrategy.equals(MetadataConstants.TABLE_PER_CLASS)) { 781 throw ValidationException.tablePerClassInheritanceNotSupported(getJavaClass()); 782 } 783 784 m_usesSingleTableInheritanceStrategy = (inheritanceStrategy.equals(MetadataConstants.SINGLE_TABLE)); 785 } 786 787 792 public void setIsCascadePersist(boolean isCascadePersist) { 793 m_isCascadePersist = isCascadePersist; 794 } 795 796 800 public void setJavaClass(Class javaClass) { 801 m_descriptor.setJavaClass(javaClass); 802 m_descriptor.setJavaClassName(javaClass.getName()); 803 } 804 805 808 public void setOptimisticLockingPolicy(VersionLockingPolicy policy) { 809 policy.storeInObject(); 810 m_descriptor.setOptimisticLockingPolicy(policy); 811 } 812 813 817 public void setParentClass(Class parent) { 818 m_descriptor.getInheritancePolicy().setParentClassName(parent.getName()); 819 } 820 821 824 public void setPKClass(Class pkClass) { 825 setPKClass(pkClass.getName()); 826 } 827 828 831 public void setPKClass(String pkClassName) { 832 CMP3Policy policy = new CMP3Policy(); 833 policy.setPrimaryKeyClassName(pkClassName); 834 m_descriptor.setCMPPolicy(policy); 835 } 836 837 840 public void setPrimaryTable(DatabaseTable primaryTable) { 841 addTable(primaryTable); 842 843 m_primaryTableName = primaryTable.getQualifiedName(); 844 } 845 846 849 public void setSchema(String xmlSchema) { 850 m_xmlSchema = xmlSchema; 851 } 852 853 856 public void setSequenceNumberField(DatabaseField field) { 857 m_descriptor.setSequenceNumberField(field); 858 } 859 860 863 public void setSequenceNumberName(String name) { 864 m_descriptor.setSequenceNumberName(name); 865 } 866 867 872 public void setIgnoreAnnotations(boolean ignoreAnnotations) { 873 m_ignoreAnnotations = ignoreAnnotations; 874 } 875 876 881 public void setSingleTableInheritanceStrategy() { 882 m_descriptor.getInheritancePolicy().setSingleTableStrategy(); 883 } 884 885 890 public boolean ignoreAnnotations() { 891 return m_ignoreAnnotations; 892 } 893 894 897 public boolean usesOptimisticLocking() { 898 return m_descriptor.usesOptimisticLocking(); 899 } 900 901 908 public boolean usesPropertyAccess() { 909 if (m_accessor.isInheritanceSubclass()) { 910 return m_accessor.getInheritanceRootDescriptor().usesPropertyAccess(); 911 } else { 912 if (m_usesPropertyAccess == null) { 913 if (MetadataHelper.havePersistenceAnnotationsDefined(MetadataHelper.getFields(getJavaClass())) || isXmlFieldAccess()) { 914 if (isXmlPropertyAccess()) { 915 } 917 918 m_usesPropertyAccess = new Boolean (false); 922 } else if (MetadataHelper.havePersistenceAnnotationsDefined(MetadataHelper.getDeclaredMethods(getJavaClass())) || isXmlPropertyAccess()) { 923 if (isXmlFieldAccess()) { 924 } 926 927 m_usesPropertyAccess = new Boolean (true); 931 } else { 932 for (ClassAccessor mappedSuperclass : getClassAccessor().getMappedSuperclasses()) { 933 if (MetadataHelper.havePersistenceAnnotationsDefined(MetadataHelper.getFields(mappedSuperclass.getJavaClass()))) { 934 m_usesPropertyAccess = new Boolean (false); 938 break; 939 } else if (MetadataHelper.havePersistenceAnnotationsDefined(MetadataHelper.getDeclaredMethods(mappedSuperclass.getJavaClass()))) { 940 m_usesPropertyAccess = new Boolean (true); 944 break; 945 } 946 } 947 948 if (m_usesPropertyAccess == null) { 954 m_usesPropertyAccess = new Boolean (true); 955 } 956 } 957 } 958 959 return m_usesPropertyAccess; 960 } 961 } 962 963 970 public void setUsesPropertyAccess(Boolean usesPropertyAccess) { 971 this.m_usesPropertyAccess = usesPropertyAccess; 972 } 973 974 979 public boolean usesSingleTableInheritanceStrategy() { 980 return m_usesSingleTableInheritanceStrategy; 981 } 982 983 988 public void validatePKClassId(String attributeName, Type type) { 989 if (m_pkClassIDs.containsKey(attributeName)) { 990 Type expectedType = m_pkClassIDs.get(attributeName); 991 992 if (type == expectedType) { 993 m_pkClassIDs.remove(attributeName); 994 } else { 995 throw ValidationException.invalidCompositePKAttribute(getJavaClass(), getPKClassName(), attributeName, expectedType, type); 996 } 997 } 998 } 999 } 1000 | Popular Tags |