1 21 package oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors; 23 24 import java.lang.reflect.AnnotatedElement ; 25 import java.lang.reflect.Field ; 26 import java.lang.reflect.Method ; 27 import java.lang.reflect.Modifier ; 28 29 import java.util.ArrayList ; 30 import java.util.List ; 31 import java.util.Map ; 32 33 import javax.persistence.AssociationOverride; 34 import javax.persistence.AssociationOverrides; 35 import javax.persistence.AttributeOverride; 36 import javax.persistence.AttributeOverrides; 37 import javax.persistence.DiscriminatorColumn; 38 import javax.persistence.DiscriminatorValue; 39 import javax.persistence.Entity; 40 import javax.persistence.EntityListeners; 41 import javax.persistence.ExcludeDefaultListeners; 42 import javax.persistence.ExcludeSuperclassListeners; 43 import javax.persistence.IdClass; 44 import javax.persistence.Inheritance; 45 import javax.persistence.InheritanceType; 46 import javax.persistence.JoinColumn; 47 import javax.persistence.MappedSuperclass; 48 import javax.persistence.NamedNativeQueries; 49 import javax.persistence.NamedNativeQuery; 50 import javax.persistence.NamedQueries; 51 import javax.persistence.NamedQuery; 52 import javax.persistence.PostLoad; 53 import javax.persistence.PostPersist; 54 import javax.persistence.PostRemove; 55 import javax.persistence.PostUpdate; 56 import javax.persistence.PrePersist; 57 import javax.persistence.PreRemove; 58 import javax.persistence.PreUpdate; 59 import javax.persistence.SecondaryTable; 60 import javax.persistence.SecondaryTables; 61 import javax.persistence.SqlResultSetMapping; 62 import javax.persistence.SqlResultSetMappings; 63 import javax.persistence.Table; 64 import javax.persistence.Transient; 65 66 import oracle.toplink.essentials.descriptors.ClassDescriptor; 67 68 import oracle.toplink.essentials.exceptions.ValidationException; 70 71 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.MetadataAccessor; 72 73 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.objects.MetadataAccessibleObject; 74 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.objects.MetadataClass; 75 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.objects.MetadataField; 76 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.objects.MetadataMethod; 77 78 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.columns.MetadataColumn; 79 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.columns.MetadataDiscriminatorColumn; 80 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.columns.MetadataJoinColumn; 81 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.columns.MetadataJoinColumns; 82 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.columns.MetadataPrimaryKeyJoinColumn; 83 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.columns.MetadataPrimaryKeyJoinColumns; 84 85 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.listeners.MetadataEntityClassListener; 86 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.listeners.MetadataEntityListener; 87 88 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataHelper; 89 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataConstants; 90 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataProcessor; 91 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataDescriptor; 92 93 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.queries.MetadataEntityResult; 94 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.queries.MetadataFieldResult; 95 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.queries.MetadataNamedNativeQuery; 96 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.queries.MetadataNamedQuery; 97 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.queries.MetadataSQLResultSetMapping; 98 99 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.tables.MetadataSecondaryTable; 100 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.tables.MetadataTable; 101 102 import oracle.toplink.essentials.internal.ejb.cmp3.xml.accessors.XMLMappedSuperclassAccessor; 103 import oracle.toplink.essentials.internal.ejb.cmp3.xml.listeners.XMLEntityListener; 104 import oracle.toplink.essentials.internal.ejb.cmp3.xml.XMLConstants; 105 import oracle.toplink.essentials.internal.ejb.cmp3.xml.XMLHelper; 106 107 import oracle.toplink.essentials.internal.helper.DatabaseField; 108 import oracle.toplink.essentials.internal.helper.DatabaseTable; 109 import oracle.toplink.essentials.internal.helper.Helper; 110 111 import oracle.toplink.essentials.queryframework.ColumnResult; 112 import oracle.toplink.essentials.queryframework.EntityResult; 113 import oracle.toplink.essentials.queryframework.FieldResult; 114 115 import org.w3c.dom.Node ; 116 import org.w3c.dom.NodeList ; 117 118 124 public class ClassAccessor extends NonRelationshipAccessor { 125 protected Class m_inheritanceRootClass; 126 protected Boolean m_isInheritanceSubclass; 127 protected List <ClassAccessor> m_mappedSuperclasses; 128 129 132 public ClassAccessor(MetadataAccessibleObject accessibleObject, MetadataProcessor processor, MetadataDescriptor descriptor) { 133 super(accessibleObject, processor, descriptor); 134 } 135 136 141 protected void addMultipleTableKeyFields(MetadataPrimaryKeyJoinColumns primaryKeyJoinColumns, String PK_CTX, String FK_CTX) { 142 for (MetadataPrimaryKeyJoinColumn primaryKeyJoinColumn : processPrimaryKeyJoinColumns(primaryKeyJoinColumns)) { 146 String defaultPKFieldName = m_descriptor.getPrimaryKeyFieldName(); 150 151 DatabaseField pkField = primaryKeyJoinColumn.getPrimaryKeyField(); 152 pkField.setName(getName(pkField, defaultPKFieldName, PK_CTX)); 153 154 DatabaseField fkField = primaryKeyJoinColumn.getForeignKeyField(); 155 fkField.setName(getName(fkField, pkField.getName(), FK_CTX)); 156 157 if (fkField.getName().equals(pkField.getName())) { 158 m_descriptor.addMultipleTablePrimaryKeyField(pkField, fkField); 160 } else { 161 m_descriptor.addMultipleTableForeignKeyField(pkField, fkField); 163 } 164 } 165 } 166 167 172 protected MetadataAccessor buildAccessor(MetadataAccessibleObject accessibleObject) { 173 MetadataAccessor accessor = m_descriptor.getAccessorFor(accessibleObject.getAttributeName()); 174 175 if (accessor == null) { 176 if (MetadataHelper.isEmbeddedId(accessibleObject, m_descriptor)) { 177 return new EmbeddedAccessor(accessibleObject, this, true); 178 } else if (MetadataHelper.isEmbedded(accessibleObject, m_descriptor)) { 179 return new EmbeddedAccessor(accessibleObject, this, false); 180 } else if (MetadataHelper.isManyToMany(accessibleObject, m_descriptor)) { 181 return new ManyToManyAccessor(accessibleObject, this); 182 } else if (MetadataHelper.isManyToOne(accessibleObject, m_descriptor)) { 183 return new ManyToOneAccessor(accessibleObject, this); 184 } else if (MetadataHelper.isOneToMany(accessibleObject, m_logger, m_descriptor)) { 185 return new OneToManyAccessor(accessibleObject, this); 186 } else if (MetadataHelper.isOneToOne(accessibleObject, m_project, m_logger, m_descriptor)) { 187 return new OneToOneAccessor(accessibleObject, this); 188 } else { 189 return new BasicAccessor(accessibleObject, this); 191 } 192 } else { 193 return accessor; 194 } 195 } 196 197 202 public void clearMappedSuperclasses() { 203 m_mappedSuperclasses = null; 204 } 205 206 210 public String getDiscriminatorValue() { 211 DiscriminatorValue discriminatorValue = getAnnotation(DiscriminatorValue.class); 212 213 if (discriminatorValue == null) { 214 return null; 215 } else { 216 return discriminatorValue.value(); 217 } 218 } 219 220 224 public String getEntityName() { 225 Entity entity = getAnnotation(Entity.class); 226 return (entity == null) ? "" : entity.name(); 227 } 228 229 232 protected Class getIdClass() { 233 IdClass idClass = getAnnotation(IdClass.class); 234 return (idClass == null) ? null : idClass.value(); 235 } 236 237 244 public Class getInheritanceRootClass() { 245 if (m_inheritanceRootClass == null) { 246 Class lastParent = null; 247 Class parent = getJavaClass().getSuperclass(); 248 249 while (parent != Object .class) { 250 if (hasInheritanceTag(parent) || m_project.containsDescriptor(parent)) { 251 lastParent = parent; 252 } 253 254 parent = parent.getSuperclass(); 255 } 256 257 m_inheritanceRootClass = lastParent; 260 } 261 262 return m_inheritanceRootClass; 263 } 264 265 269 public MetadataDescriptor getInheritanceRootDescriptor() { 270 return m_project.getDescriptor(getInheritanceRootClass()); 271 } 272 273 278 protected String getInheritanceStrategy() { 279 Inheritance inheritance = getAnnotation(Inheritance.class); 280 281 if (inheritance == null) { 282 return ""; 283 } else { 284 return inheritance.strategy().name(); 285 } 286 } 287 288 293 public Class getJavaClass() { 294 return (Class ) getAnnotatedElement(); 295 } 296 297 301 public List <ClassAccessor> getMappedSuperclasses() { 302 if (m_mappedSuperclasses == null) { 303 m_mappedSuperclasses = new ArrayList <ClassAccessor>(); 304 305 Class parent = getJavaClass().getSuperclass(); 306 307 while (parent != Object .class) { 308 if (m_project.hasMappedSuperclass(parent)) { 309 Node node = m_project.getMappedSuperclassNode(parent); 310 XMLHelper helper = m_project.getMappedSuperclassHelper(parent); 311 m_mappedSuperclasses.add(new XMLMappedSuperclassAccessor(new MetadataClass(parent), node, helper, m_processor, m_descriptor)); 312 } else if (isAnnotationPresent(MappedSuperclass.class, parent)) { 313 m_mappedSuperclasses.add(new MappedSuperclassAccessor(new MetadataClass(parent), m_processor, m_descriptor)); 314 } 315 316 parent = parent.getSuperclass(); 317 } 318 } 319 320 return m_mappedSuperclasses; 321 } 322 323 326 protected boolean hasEntityTag(Class cls) { 327 return isAnnotationPresent(Entity.class, cls); 328 } 329 330 334 protected boolean hasInheritanceTag(Class entityClass) { 335 return isAnnotationPresent(Inheritance.class, entityClass); 336 } 337 338 341 public boolean isClass() { 342 return true; 343 } 344 345 348 public boolean isInheritanceSubclass() { 349 if (m_isInheritanceSubclass == null) { 350 m_isInheritanceSubclass = new Boolean (getInheritanceRootClass() != null); 351 } 352 353 return m_isInheritanceSubclass; 354 } 355 356 359 protected boolean isTransient(AnnotatedElement annotatedElement, int modifier) { 360 if (isAnnotationPresent(Transient.class, annotatedElement)) { 361 if (MetadataHelper.getDeclaredAnnotationsCount(annotatedElement, m_descriptor) > 1) { 362 m_validator.throwMappingAnnotationsAppliedToTransientAttribute(annotatedElement); 363 } 364 365 return true; 366 } else if (Modifier.isTransient(modifier)) { 367 if (MetadataHelper.getDeclaredAnnotationsCount(annotatedElement, m_descriptor) > 0) { 368 m_validator.throwMappingAnnotationsAppliedToTransientAttribute(annotatedElement); 369 } 370 371 return true; 372 } 373 374 return false; 375 } 376 377 382 protected boolean isValidPersistenceElement(AnnotatedElement annotatedElement, int modifiers) { 383 return ! (isTransient(annotatedElement, modifiers) || Modifier.isStatic(modifiers) || Modifier.isAbstract(modifiers)); 384 } 385 386 391 protected boolean isValidPersistenceField(Field field) { 392 return (isValidPersistenceElement(field, field.getModifiers())); 393 } 394 395 400 protected boolean isValidPersistenceMethod(Method method) { 401 if (isValidPersistenceElement(method, method.getModifiers())) { 403 String methodName = method.getName(); 405 if (MetadataHelper.isValidPersistenceMethodName(methodName)) { 406 if (method.getParameterTypes().length > 0) { 408 return false; 409 } 410 411 Method setMethod = MetadataHelper.getSetMethod(method, getJavaClass()); 412 413 if (setMethod == null) { 414 if (MetadataHelper.getDeclaredAnnotationsCount(method, m_descriptor) > 0) { 415 m_validator.throwNoCorrespondingSetterMethodDefined(getJavaClass(), method); 418 } 419 420 } else { 423 return true; 424 } 425 } 426 } 427 428 return false; 429 } 430 431 439 public void process() { 440 if (hasEntityTag(getJavaClass())) { 441 443 m_descriptor.setIgnoreFlags(); 445 446 processEntity(); 448 449 processClass(); 452 453 processTableAndInheritance(); 455 456 processMappedSuperclasses(); 458 459 processAccessors(); 461 462 validatePrimaryKey(); 464 465 processSecondaryTables(); 467 } else { 468 m_descriptor.setDescriptorIsEmbeddable(); 470 471 processAccessors(); 473 } 474 } 475 476 481 protected void processAccessor(MetadataAccessor accessor) { 482 if (! accessor.isProcessed()) { 483 m_descriptor.addAccessor(accessor); 485 486 if (accessor.isRelationship()) { 487 m_project.addRelationshipDescriptor(m_descriptor); 489 } else { 490 accessor.process(); 491 accessor.setIsProcessed(); 492 } 493 } 494 } 495 496 500 protected void processAccessorFields() { 501 for (Field field : MetadataHelper.getFields(getJavaClass())) { 502 if (isValidPersistenceField(field)) { 503 processAccessor(buildAccessor(new MetadataField(field))); 504 } 505 } 506 } 507 508 512 protected void processAccessorMethods() { 513 for (Method method : MetadataHelper.getDeclaredMethods(getJavaClass())) { 514 if (isValidPersistenceMethod(method)) { 515 processAccessor(buildAccessor(new MetadataMethod(method))); 516 } 517 } 518 } 519 520 524 protected void processAccessors() { 525 if (m_descriptor.usesPropertyAccess()) { 527 processAccessorMethods(); 528 } else { 529 processAccessorFields(); 530 } 531 } 532 533 538 protected void processAssociationOverride(String attributeName, MetadataJoinColumns joinColumns) { 539 if (joinColumns.loadedFromXML()) { 541 m_descriptor.addAssociationOverride(attributeName, joinColumns); 543 } else { 544 MetadataJoinColumns existingJoinColumns = m_descriptor.getAssociationOverrideFor(attributeName); 547 548 if (existingJoinColumns == null || ! existingJoinColumns.loadedFromXML()) { 549 m_descriptor.addAssociationOverride(attributeName, joinColumns); 551 } else { 552 } 554 } 555 } 556 557 564 protected void processAssociationOverrides() { 565 AssociationOverrides associationOverrides = getAnnotation(AssociationOverrides.class); 567 if (associationOverrides != null) { 568 for (AssociationOverride associationOverride : associationOverrides.value()) { 569 processAssociationOverride(associationOverride.name(), new MetadataJoinColumns(associationOverride.joinColumns())); 570 } 571 } 572 573 AssociationOverride associationOverride = getAnnotation(AssociationOverride.class); 575 if (associationOverride != null) { 576 processAssociationOverride(associationOverride.name(), new MetadataJoinColumns(associationOverride.joinColumns())); 577 } 578 } 579 580 585 protected void processAttributeOverride(MetadataColumn column) { 586 String attributeName = column.getAttributeName(); 587 588 if (column.loadedFromXML()) { 590 m_descriptor.addAttributeOverride(column); 591 } else { 592 MetadataColumn existingColumn = m_descriptor.getAttributeOverrideFor(attributeName); 595 596 if (existingColumn == null || ! existingColumn.loadedFromXML()) { 597 m_descriptor.addAttributeOverride(column); 598 } else { 599 } 601 } 602 } 603 604 609 protected void processAttributeOverrides() { 610 AttributeOverrides attributeOverrides = getAnnotation(AttributeOverrides.class); 612 if (attributeOverrides != null) { 613 for (AttributeOverride attributeOverride : attributeOverrides.value()) { 614 processAttributeOverride(new MetadataColumn(attributeOverride, getAnnotatedElement())); 615 } 616 } 617 618 AttributeOverride attributeOverride = getAnnotation(AttributeOverride.class); 620 if (attributeOverride != null) { 621 processAttributeOverride(new MetadataColumn(attributeOverride, getAnnotatedElement())); 622 } 623 } 624 625 630 protected void processCallbackMethods(Method [] candidateMethods, MetadataEntityListener listener) { 631 for (Method method : candidateMethods) { 632 if (isAnnotationPresent(PostLoad.class, method)) { 633 setPostLoad(method, listener); 634 } 635 636 if (isAnnotationPresent(PostPersist.class, method)) { 637 setPostPersist(method, listener); 638 } 639 640 if (isAnnotationPresent(PostRemove.class, method)) { 641 setPostRemove(method, listener); 642 } 643 644 if (isAnnotationPresent(PostUpdate.class, method)) { 645 setPostUpdate(method, listener); 646 } 647 648 if (isAnnotationPresent(PrePersist.class, method)) { 649 setPrePersist(method, listener); 650 } 651 652 if (isAnnotationPresent(PreRemove.class, method)) { 653 setPreRemove(method, listener); 654 } 655 656 if (isAnnotationPresent(PreUpdate.class, method)) { 657 setPreUpdate(method, listener); 658 } 659 } 660 } 661 662 666 protected void processClass() { 667 processAttributeOverrides(); 669 670 processAssociationOverrides(); 672 673 processNamedQueries(); 675 676 processNamedNativeQueries(); 678 679 processSqlResultSetMappings(); 681 682 processTableGenerator(); 684 685 processSequenceGenerator(); 687 688 processIdClass(); 690 691 processExcludeDefaultListeners(); 693 694 processExcludeSuperclassListeners(); 696 } 697 698 708 protected void processDefaultListeners(ClassLoader loader) { 709 Map <XMLHelper, NodeList > defaultListeners = m_project.getDefaultListeners(); 710 711 for (XMLHelper helper : defaultListeners.keySet()) { 712 helper.setLoader(loader); 714 NodeList nodes = defaultListeners.get(helper); 715 716 for (int i = 0; i < nodes.getLength(); i++) { 717 Node node = nodes.item(i); 718 719 XMLEntityListener listener = new XMLEntityListener(helper.getClassForNode(node), getJavaClass()); 721 722 Method [] candidateMethods = MetadataHelper.getCandidateCallbackMethodsForDefaultListener(listener); 724 processLifecycleEvents(listener, node, helper, candidateMethods); 725 726 processCallbackMethods(candidateMethods, listener); 729 730 m_descriptor.addDefaultEventListener(listener); 732 } 733 } 734 } 735 736 741 protected void processDiscriminatorColumn() { 742 DiscriminatorColumn discriminatorColumn = getAnnotation(DiscriminatorColumn.class); 743 processDiscriminatorColumn(new MetadataDiscriminatorColumn(discriminatorColumn)); 744 } 745 746 751 protected void processDiscriminatorColumn(MetadataDiscriminatorColumn discriminatorColumn) { 752 DatabaseField field = new DatabaseField(); 753 754 field.setName(getName(discriminatorColumn.getName(), MetadataDiscriminatorColumn.DEFAULT_NAME, m_logger.DISCRIMINATOR_COLUMN)); 755 field.setLength(discriminatorColumn.getLength()); 756 field.setTableName(m_descriptor.getPrimaryTableName()); 757 field.setColumnDefinition(discriminatorColumn.getColumnDefinition()); 758 field.setType(MetadataHelper.getDiscriminatorType(discriminatorColumn.getDiscriminatorType())); 759 760 m_descriptor.setClassIndicatorField(field); 762 } 763 764 772 protected void processDiscriminatorValue() { 773 if (! Modifier.isAbstract(getJavaClass().getModifiers())) { 774 String discriminatorValue = getDiscriminatorValue(); 777 778 if (discriminatorValue == null) { 779 m_descriptor.addClassIndicator(getJavaClass(), Helper.getShortClassName(getJavaClassName())); 781 } else { 782 m_descriptor.addClassIndicator(getJavaClass(), discriminatorValue); 783 } 784 } 785 } 786 787 791 protected void processEntity() { 792 if (m_descriptor.getAlias().equals("")) { 794 String alias = getEntityName(); 795 796 if (alias.equals("")) { 797 alias = Helper.getShortClassName(getJavaClassName()); 798 m_logger.logConfigMessage(m_logger.ALIAS, m_descriptor, alias); 799 } 800 801 ClassDescriptor existingDescriptor = getProject().getSession().getProject().getDescriptorForAlias(alias); 803 if (existingDescriptor != null) { 804 m_validator.throwNonUniqueEntityName(existingDescriptor.getJavaClassName(), m_descriptor.getJavaClassName(), alias); 805 } 806 807 m_descriptor.setAlias(alias); 809 getProject().getSession().getProject().addAlias(alias, m_descriptor.getDescriptor()); 810 } 811 } 812 813 817 public MetadataEntityListener processEntityEventListener(ClassLoader loader) { 818 MetadataEntityClassListener listener = new MetadataEntityClassListener(getJavaClass()); 819 820 processCallbackMethods(MetadataHelper.getCandidateCallbackMethodsForEntityClass(getJavaClass()), listener); 822 823 return listener; 824 } 825 826 830 public void processEntityListeners(Class entityClass, ClassLoader loader) { 831 EntityListeners entityListeners = getAnnotation(EntityListeners.class); 832 833 if (entityListeners != null) { 834 for (Class entityListener : entityListeners.value()) { 835 MetadataEntityListener listener = new MetadataEntityListener(entityListener, entityClass); 836 837 processCallbackMethods(MetadataHelper.getCandidateCallbackMethodsForEntityListener(listener), listener); 839 840 m_descriptor.addEntityListenerEventListener(listener); 842 } 843 } 844 } 845 846 851 protected void processExcludeDefaultListeners() { 852 if (isAnnotationPresent(ExcludeDefaultListeners.class)) { 855 m_descriptor.setExcludeDefaultListeners(true); 856 } 857 } 858 859 864 protected void processExcludeSuperclassListeners() { 865 if (isAnnotationPresent(ExcludeSuperclassListeners.class)) { 868 m_descriptor.setExcludeSuperclassListeners(true); 869 } 870 } 871 872 882 protected void processIdClass() { 883 Class idClass = getIdClass(); 884 885 if (idClass != null) { 886 if (m_descriptor.ignoreIDs()) { 887 m_logger.logWarningMessage(m_logger.IGNORE_ID_CLASS, m_descriptor, idClass); 888 } else { 889 m_descriptor.setPKClass(idClass); 890 891 if (m_descriptor.usesPropertyAccess()) { 892 for (Method method : MetadataHelper.getDeclaredMethods(idClass)) { 893 String methodName = method.getName(); 894 895 if (MetadataHelper.isValidPersistenceMethodName(methodName)) { 896 m_descriptor.addPKClassId(MetadataHelper.getAttributeNameFromMethodName(methodName), MetadataHelper.getGenericReturnType(method)); 897 } 898 } 899 } else { 900 for (Field field : MetadataHelper.getFields(idClass)) { 901 m_descriptor.addPKClassId(field.getName(), MetadataHelper.getGenericType(field)); 902 } 903 } 904 } 905 } 906 } 907 908 914 protected void processInheritanceRoot() { 915 if (m_descriptor.ignoreInheritance()) { 916 m_logger.logWarningMessage(m_logger.IGNORE_INHERITANCE, m_descriptor); 917 } else { 918 String inheritanceStrategy = getInheritanceStrategy(); 920 if (inheritanceStrategy.equals("")) { 921 inheritanceStrategy = InheritanceType.SINGLE_TABLE.name(); 923 } 924 925 m_descriptor.setInheritanceStrategy(inheritanceStrategy); 926 927 processDiscriminatorColumn(); 929 930 processDiscriminatorValue(); 932 } 933 } 934 935 939 public void processInheritanceSubclass() { 940 if (m_descriptor.ignoreInheritance()) { 941 m_logger.logWarningMessage(m_logger.IGNORE_INHERITANCE, m_descriptor); 942 } else { 943 if (hasInheritanceTag(getJavaClass())) { 945 m_logger.logWarningMessage(m_logger.IGNORE_INHERITANCE, m_descriptor); 946 } 947 948 setInheritanceParentClass(); 950 951 MetadataDescriptor rootDescriptor = getInheritanceRootDescriptor(); 953 954 if (rootDescriptor.usesSingleTableInheritanceStrategy()) { 956 m_descriptor.setSingleTableInheritanceStrategy(); 957 } else { 958 MetadataPrimaryKeyJoinColumns primaryKeyJoinColumns = getPrimaryKeyJoinColumns(rootDescriptor.getPrimaryTableName(), m_descriptor.getPrimaryTableName()); 961 addMultipleTableKeyFields(primaryKeyJoinColumns, m_logger.INHERITANCE_PK_COLUMN, m_logger.INHERITANCE_FK_COLUMN); 962 } 963 964 processDiscriminatorValue(); 966 967 if (rootDescriptor.hasCompositePrimaryKey()) { 970 m_descriptor.setPKClass(rootDescriptor.getPKClassName()); 971 } 972 } 973 } 974 975 979 protected List <MetadataJoinColumn> processJoinColumns(JoinColumn[] joinColumns) { 980 ArrayList <MetadataJoinColumn> list = new ArrayList <MetadataJoinColumn>(); 981 982 for (JoinColumn joinColumn : joinColumns) { 983 list.add(new MetadataJoinColumn(joinColumn)); 984 } 985 986 return list; 987 } 988 989 993 protected void processLifecycleEvent(MetadataEntityListener listener, Node node, String event, XMLHelper helper, Method [] candidateMethods) { 994 Node eventNode = helper.getNode(node, event); 995 996 if (eventNode != null) { 997 String methodName = helper.getNodeValue(eventNode, XMLConstants.ATT_METHOD_NAME); 998 Method method = MetadataHelper.getMethodForName(candidateMethods, methodName); 999 1000 if (method == null) { 1001 throw ValidationException.invalidCallbackMethod(listener.getListenerClass(), methodName); 1003 } else if (event.equals(XMLConstants.PRE_PERSIST)) { 1004 setPrePersist(method, listener); 1005 } else if (event.equals(XMLConstants.POST_PERSIST)) { 1006 setPostPersist(method, listener); 1007 } else if (event.equals(XMLConstants.PRE_REMOVE)) { 1008 setPreRemove(method, listener); 1009 } else if (event.equals(XMLConstants.POST_REMOVE)) { 1010 setPostRemove(method, listener); 1011 } else if (event.equals(XMLConstants.PRE_UPDATE)) { 1012 setPreUpdate(method, listener); 1013 } else if (event.equals(XMLConstants.POST_UPDATE)) { 1014 setPostUpdate(method, listener); 1015 } else if (event.equals(XMLConstants.POST_LOAD)) { 1016 setPostLoad(method, listener); 1017 } 1018 } 1019 } 1020 1021 1025 protected void processLifecycleEvents(MetadataEntityListener listener, Node node, XMLHelper helper, Method [] candidateMethods) { 1026 processLifecycleEvent(listener, node, XMLConstants.PRE_PERSIST, helper, candidateMethods); 1027 processLifecycleEvent(listener, node, XMLConstants.POST_PERSIST, helper, candidateMethods); 1028 processLifecycleEvent(listener, node, XMLConstants.PRE_REMOVE, helper, candidateMethods); 1029 processLifecycleEvent(listener, node, XMLConstants.POST_REMOVE, helper, candidateMethods); 1030 processLifecycleEvent(listener, node, XMLConstants.PRE_UPDATE, helper, candidateMethods); 1031 processLifecycleEvent(listener, node, XMLConstants.POST_UPDATE, helper, candidateMethods); 1032 processLifecycleEvent(listener, node, XMLConstants.POST_LOAD, helper, candidateMethods); 1033 } 1034 1035 1039 public void processListeners(ClassLoader loader) { 1040 processDefaultListeners(loader); 1042 1043 if (! m_descriptor.excludeSuperclassListeners()) { 1048 List <ClassAccessor> mappedSuperclasses = getMappedSuperclasses(); 1049 int mappedSuperclassesSize = mappedSuperclasses.size(); 1050 1051 for (int i = mappedSuperclassesSize - 1; i >= 0; i--) { 1052 mappedSuperclasses.get(i).processEntityListeners(getJavaClass(), loader); 1053 } 1054 } 1055 1056 processEntityListeners(getJavaClass(), loader); 1057 1058 MetadataEntityListener listener = processEntityEventListener(loader); 1061 1062 if (! m_descriptor.excludeSuperclassListeners()) { 1063 for (ClassAccessor mappedSuperclass : getMappedSuperclasses()) { 1064 mappedSuperclass.processMappedSuperclassEventListener(listener, getJavaClass(), loader); 1065 } 1066 } 1067 1068 if (listener.hasCallbackMethods()) { 1070 m_descriptor.setEntityEventListener(listener); 1071 } 1072 } 1073 1074 1079 protected void processMappedSuperclass() { 1080 processClass(); 1083 1084 processAccessors(); 1086 } 1087 1088 1093 protected void processMappedSuperclasses() { 1094 for (ClassAccessor mappedSuperclass : getMappedSuperclasses()) { 1095 mappedSuperclass.process(); 1096 } 1097 } 1098 1099 1103 public void processMappedSuperclassEventListener(MetadataEntityListener listener, Class entityClass, ClassLoader loader) { 1104 processCallbackMethods(MetadataHelper.getCandidateCallbackMethodsForMappedSuperclass(getJavaClass(), entityClass), listener); 1106 } 1107 1108 1115 protected void processNamedNativeQueries() { 1116 NamedNativeQueries namedNativeQueries = getAnnotation(NamedNativeQueries.class); 1118 if (namedNativeQueries != null) { 1119 for (NamedNativeQuery namedNativeQuery : namedNativeQueries.value()) { 1120 processNamedNativeQuery(new MetadataNamedNativeQuery(namedNativeQuery)); 1121 } 1122 } 1123 1124 NamedNativeQuery namedNativeQuery = getAnnotation(NamedNativeQuery.class); 1126 if (namedNativeQuery != null) { 1127 processNamedNativeQuery(new MetadataNamedNativeQuery(namedNativeQuery)); 1128 } 1129 } 1130 1131 1136 protected void processNamedNativeQuery(MetadataNamedNativeQuery namedNativeQuery) { 1137 if (m_project.hasNamedNativeQuery(namedNativeQuery.getName())) { 1138 MetadataNamedNativeQuery existingNamedNativeQuery = m_project.getNamedNativeQuery(namedNativeQuery.getName()); 1139 1140 if (existingNamedNativeQuery.loadedFromAnnotations() && namedNativeQuery.loadedFromXML()) { 1141 m_project.addNamedNativeQuery(namedNativeQuery); 1143 } else { 1144 m_logger.logWarningMessage(m_logger.IGNORE_QUERY, m_descriptor, namedNativeQuery.getName()); 1146 } 1147 } else { 1148 m_project.addNamedNativeQuery(namedNativeQuery); 1149 } 1150 } 1151 1152 1159 protected void processNamedQueries() { 1160 NamedQueries namedQueries = getAnnotation(NamedQueries.class); 1162 1163 if (namedQueries != null) { 1164 for (NamedQuery namedQuery : namedQueries.value()) { 1165 processNamedQuery(new MetadataNamedQuery(namedQuery)); 1166 } 1167 } 1168 1169 NamedQuery namedQuery = getAnnotation(NamedQuery.class); 1171 1172 if (namedQuery != null) { 1173 processNamedQuery(new MetadataNamedQuery(namedQuery)); 1174 } 1175 } 1176 1177 1182 protected void processNamedQuery(MetadataNamedQuery namedQuery) { 1183 if (m_project.hasNamedQuery(namedQuery.getName())) { 1184 MetadataNamedQuery existingNamedQuery = m_project.getNamedQuery(namedQuery.getName()); 1185 1186 if (existingNamedQuery.loadedFromAnnotations() && namedQuery.loadedFromXML()) { 1187 m_project.addNamedQuery(namedQuery); 1189 } else { 1190 m_logger.logWarningMessage(m_logger.IGNORE_QUERY, m_descriptor, namedQuery.getName()); 1192 } 1193 } else { 1194 m_project.addNamedQuery(namedQuery); 1195 } 1196 } 1197 1198 1203 protected void processSecondaryTable(MetadataSecondaryTable secondaryTable) { 1204 String catalog = getName(secondaryTable.getCatalog(), m_descriptor.getCatalog(), m_logger.TABLE_CATALOG); 1207 1208 String schema = getName(secondaryTable.getSchema(), m_descriptor.getSchema(), m_logger.TABLE_SCHEMA); 1211 1212 secondaryTable.setName(MetadataHelper.getFullyQualifiedTableName(secondaryTable.getName(), catalog, schema)); 1214 1215 m_descriptor.addTable(secondaryTable.getDatabaseTable()); 1217 1218 MetadataPrimaryKeyJoinColumns primaryKeyJoinColumns = secondaryTable.getPrimaryKeyJoinColumns(m_descriptor.getPrimaryTableName()); 1220 addMultipleTableKeyFields(primaryKeyJoinColumns, m_logger.SECONDARY_TABLE_PK_COLUMN, m_logger.SECONDARY_TABLE_FK_COLUMN); 1221 } 1222 1223 1230 protected void processSecondaryTables() { 1231 if (m_descriptor.ignoreTables()) { 1232 m_logger.logWarningMessage(m_logger.IGNORE_TABLE, getJavaClass()); 1233 } else { 1234 SecondaryTables secondaryTables = getAnnotation(SecondaryTables.class); 1236 if (secondaryTables != null) { 1237 for (SecondaryTable secondaryTable : secondaryTables.value()) { 1238 processSecondaryTable(new MetadataSecondaryTable(secondaryTable)); 1239 } 1240 } else { 1241 SecondaryTable secondaryTable = getAnnotation(SecondaryTable.class); 1243 if (secondaryTable != null) { 1244 processSecondaryTable(new MetadataSecondaryTable(secondaryTable)); 1245 } 1246 } 1247 } 1248 } 1249 1250 1255 protected void processSqlResultSetMapping(MetadataSQLResultSetMapping sqlResultSetMapping) { 1256 oracle.toplink.essentials.queryframework.SQLResultSetMapping mapping = new oracle.toplink.essentials.queryframework.SQLResultSetMapping(sqlResultSetMapping.getName()); 1258 1259 for (MetadataEntityResult eResult : sqlResultSetMapping.getEntityResults()) { 1261 EntityResult entityResult = new EntityResult(eResult.getEntityClass().getName()); 1262 1263 for (MetadataFieldResult fResult : eResult.getFieldResults()) { 1265 entityResult.addFieldResult(new FieldResult(fResult.getName(), fResult.getColumn())); 1266 } 1267 1268 entityResult.setDiscriminatorColumn(eResult.getDiscriminatorColumn()); 1270 1271 mapping.addResult(entityResult); 1273 } 1274 1275 for (String columnResult : sqlResultSetMapping.getColumnResults()) { 1277 mapping.addResult(new ColumnResult(columnResult)); 1278 } 1279 1280 getProject().getSession().getProject().addSQLResultSetMapping(mapping); 1281 } 1282 1283 1287 protected void processSqlResultSetMappings() { 1288 SqlResultSetMappings sqlResultSetMappings = getAnnotation(SqlResultSetMappings.class); 1290 1291 if (sqlResultSetMappings != null) { 1292 for (SqlResultSetMapping sqlResultSetMapping : sqlResultSetMappings.value()) { 1293 processSqlResultSetMapping(new MetadataSQLResultSetMapping(sqlResultSetMapping)); 1294 } 1295 } else { 1296 SqlResultSetMapping sqlResultSetMapping = getAnnotation(SqlResultSetMapping.class); 1298 1299 if (sqlResultSetMapping != null) { 1300 processSqlResultSetMapping(new MetadataSQLResultSetMapping(sqlResultSetMapping)); 1301 } 1302 } 1303 } 1304 1305 1309 protected void processTable() { 1310 if (m_descriptor.ignoreTables()) { 1311 m_logger.logWarningMessage(m_logger.IGNORE_TABLE, getJavaClass()); 1312 } else { 1313 Table table = getAnnotation(Table.class); 1314 processTable(new MetadataTable(table)); 1315 } 1316 } 1317 1318 1323 protected void processTable(MetadataTable table) { 1324 String name = getName(table.getName(), m_descriptor.getDefaultTableName(), m_logger.TABLE_NAME); 1326 1327 String catalog = getName(table.getCatalog(), m_descriptor.getCatalog(), m_logger.TABLE_CATALOG); 1329 1330 String schema = getName(table.getSchema(), m_descriptor.getSchema(), m_logger.TABLE_SCHEMA); 1332 1333 table.setName(MetadataHelper.getFullyQualifiedTableName(name, catalog, schema)); 1335 1336 m_descriptor.setPrimaryTable(table.getDatabaseTable()); 1338 } 1339 1340 1345 protected void processTableAndInheritance() { 1346 if (isInheritanceSubclass()) { 1349 MetadataDescriptor rootDescriptor = getInheritanceRootDescriptor(); 1350 1351 ClassAccessor rootAccessor = rootDescriptor.getClassAccessor(); 1353 if (rootAccessor == null) { 1354 rootAccessor = processAccessor(rootDescriptor); 1355 } 1356 1357 if (! rootDescriptor.hasInheritance()) { 1360 rootAccessor.processInheritanceRoot(); 1361 } 1362 1363 if (! rootDescriptor.usesSingleTableInheritanceStrategy()) { 1366 processTable(); 1367 } 1368 1369 processInheritanceSubclass(); 1371 } else { 1372 processTable(); 1374 } 1375 } 1376 1377 1382 protected void setInheritanceParentClass() { 1383 Class parent = getJavaClass().getSuperclass(); 1384 1385 while (parent != Object .class) { 1386 if (hasEntityTag(parent) || m_project.containsDescriptor(parent)) { 1387 break; 1388 } 1389 1390 parent = parent.getSuperclass(); 1391 } 1392 1393 m_descriptor.setParentClass(parent); 1394 } 1395 1396 1400 protected void setPostLoad(Method method, MetadataEntityListener listener) { 1401 listener.setPostBuildMethod(method); 1402 listener.setPostCloneMethod(method); 1403 listener.setPostRefreshMethod(method); 1404 } 1405 1406 1410 protected void setPostPersist(Method method, MetadataEntityListener listener) { 1411 listener.setPostInsertMethod(method); 1412 } 1413 1414 1418 protected void setPostRemove(Method method, MetadataEntityListener listener) { 1419 listener.setPostDeleteMethod(method); 1420 } 1421 1422 1426 protected void setPostUpdate(Method method, MetadataEntityListener listener) { 1427 listener.setPostUpdateMethod(method); 1428 } 1429 1430 1434 protected void setPrePersist(Method method, MetadataEntityListener listener) { 1435 listener.setPrePersistMethod(method); 1436 } 1437 1438 1442 protected void setPreRemove(Method method, MetadataEntityListener listener) { 1443 listener.setPreRemoveMethod(method); 1444 } 1445 1446 1450 protected void setPreUpdate(Method method, MetadataEntityListener listener) { 1451 listener.setPreUpdateWithChangesMethod(method); 1452 } 1453 1454 1458 protected void validatePrimaryKey() { 1459 if (m_descriptor.hasCompositePrimaryKey()) { 1462 if (m_descriptor.pkClassWasNotValidated()) { 1463 m_validator.throwInvalidCompositePKSpecification(getJavaClass(), m_descriptor.getPKClassName()); 1464 } 1465 } else { 1466 if (! m_descriptor.hasPrimaryKeyFields() && ! isInheritanceSubclass()) { 1470 m_validator.throwNoPrimaryKeyAnnotationsFound(getJavaClass()); 1471 } 1472 } 1473 } 1474} 1475 | Popular Tags |