1 21 package oracle.toplink.essentials.internal.ejb.cmp3.xml.accessors; 23 24 import java.lang.reflect.Field ; 25 import java.lang.reflect.Method ; 26 27 import oracle.toplink.essentials.exceptions.ValidationException; 29 30 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.ClassAccessor; 31 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.MetadataAccessor; 32 33 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.objects.MetadataAccessibleObject; 34 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.objects.MetadataClass; 35 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.objects.MetadataField; 36 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.objects.MetadataMethod; 37 38 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.columns.MetadataPrimaryKeyJoinColumns; 39 40 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.listeners.MetadataEntityListener; 41 42 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataDescriptor; 43 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataHelper; 44 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataPersistenceUnit; 45 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.MetadataProcessor; 46 47 import oracle.toplink.essentials.internal.ejb.cmp3.xml.accessors.XMLBasicAccessor; 48 import oracle.toplink.essentials.internal.ejb.cmp3.xml.accessors.XMLEmbeddedAccessor; 49 import oracle.toplink.essentials.internal.ejb.cmp3.xml.accessors.XMLManyToManyAccessor; 50 import oracle.toplink.essentials.internal.ejb.cmp3.xml.accessors.XMLManyToOneAccessor; 51 import oracle.toplink.essentials.internal.ejb.cmp3.xml.accessors.XMLOneToManyAccessor; 52 import oracle.toplink.essentials.internal.ejb.cmp3.xml.accessors.XMLOneToOneAccessor; 53 import oracle.toplink.essentials.internal.ejb.cmp3.xml.accessors.XMLTransientAccessor; 54 55 import oracle.toplink.essentials.internal.ejb.cmp3.xml.columns.XMLColumn; 56 import oracle.toplink.essentials.internal.ejb.cmp3.xml.columns.XMLDiscriminatorColumn; 57 import oracle.toplink.essentials.internal.ejb.cmp3.xml.columns.XMLJoinColumns; 58 import oracle.toplink.essentials.internal.ejb.cmp3.xml.columns.XMLPrimaryKeyJoinColumns; 59 60 import oracle.toplink.essentials.internal.ejb.cmp3.xml.listeners.XMLEntityClassListener; 61 import oracle.toplink.essentials.internal.ejb.cmp3.xml.listeners.XMLEntityListener; 62 63 import oracle.toplink.essentials.internal.ejb.cmp3.xml.queries.XMLNamedNativeQuery; 64 import oracle.toplink.essentials.internal.ejb.cmp3.xml.queries.XMLNamedQuery; 65 import oracle.toplink.essentials.internal.ejb.cmp3.xml.queries.XMLSQLResultSetMapping; 66 67 import oracle.toplink.essentials.internal.ejb.cmp3.xml.sequencing.XMLSequenceGenerator; 68 import oracle.toplink.essentials.internal.ejb.cmp3.xml.sequencing.XMLTableGenerator; 69 70 import oracle.toplink.essentials.internal.ejb.cmp3.xml.tables.XMLSecondaryTable; 71 import oracle.toplink.essentials.internal.ejb.cmp3.xml.tables.XMLTable; 72 73 import oracle.toplink.essentials.internal.ejb.cmp3.xml.XMLConstants; 74 import oracle.toplink.essentials.internal.ejb.cmp3.xml.XMLHelper; 75 76 import org.w3c.dom.Node ; 77 import org.w3c.dom.NodeList ; 78 79 85 public class XMLClassAccessor extends ClassAccessor implements XMLAccessor { 86 protected Node m_node; 87 protected XMLHelper m_helper; 88 89 protected static String m_entityMappingsAccess; 92 protected static String m_entityMappingsSchema; 93 protected static String m_entityMappingsCatalog; 94 95 98 public XMLClassAccessor(MetadataAccessibleObject accessibleObject, Node node, XMLHelper helper, MetadataProcessor processor, MetadataDescriptor descriptor) { 99 super(accessibleObject, processor, descriptor); 100 m_node = node; 101 m_helper = helper; 102 } 103 104 108 protected MetadataAccessor buildAccessor(Node node) { 109 MetadataAccessibleObject accessibleObject; 110 111 String attributeName = m_helper.getNodeValue(node, XMLConstants.ATT_NAME); 113 114 if (m_descriptor.usesPropertyAccess()) { 117 Method method = MetadataHelper.getMethodForPropertyName(attributeName, getJavaClass()); 118 119 if (method == null) { 120 m_validator.throwUnableToDetermineClassForProperty(attributeName, getJavaClass()); 121 } 122 123 accessibleObject = new MetadataMethod(method); 124 } else { 125 Field field = MetadataHelper.getFieldForName(attributeName, getJavaClass()); 126 127 if (field == null) { 128 m_validator.throwUnableToDetermineClassForField(attributeName, getJavaClass()); 129 } 130 131 accessibleObject = new MetadataField(field); 132 } 133 134 String nodeName = node.getLocalName(); 135 136 if (nodeName.equals(XMLConstants.ONE_TO_ONE)) { 137 return new XMLOneToOneAccessor(accessibleObject, node, this); 138 } else if (nodeName.equals(XMLConstants.MANY_TO_ONE)) { 139 return new XMLManyToOneAccessor(accessibleObject, node, this); 140 } else if (nodeName.equals(XMLConstants.ONE_TO_MANY)) { 141 if (MetadataHelper.isSupportedCollectionClass(accessibleObject.getRawClass())) { 142 return new XMLOneToManyAccessor(accessibleObject, node, this); 143 } else { 144 throw ValidationException.invalidCollectionTypeForRelationship(accessibleObject.getRawClass(), getAttributeName()); 145 } 147 } else if (nodeName.equals(XMLConstants.MANY_TO_MANY)) { 148 if (MetadataHelper.isSupportedCollectionClass(accessibleObject.getRawClass())) { 149 return new XMLManyToManyAccessor(accessibleObject, node, this); 150 } else { 151 throw ValidationException.invalidCollectionTypeForRelationship(accessibleObject.getRawClass(), getAttributeName()); 153 } 154 } else if (nodeName.equals(XMLConstants.EMBEDDED)) { 155 return new XMLEmbeddedAccessor(accessibleObject, node, this, false); 156 } else if (nodeName.equals(XMLConstants.EMBEDDED_ID)) { 157 return new XMLEmbeddedAccessor(accessibleObject, node, this, true); 158 } else if (nodeName.equals(XMLConstants.TRANSIENT)) { 159 return new XMLTransientAccessor(accessibleObject, node, this); 160 } else { 161 return new XMLBasicAccessor(accessibleObject, node, this); 162 } 163 } 164 165 168 public String getCatalog() { 169 return m_entityMappingsCatalog; 170 } 171 172 177 public String getEntityName() { 178 String entityName = m_helper.getNodeValue(m_node, XMLConstants.ATT_NAME); 179 180 if (entityName.equals("")) { 181 return super.getEntityName(); 182 } else { 183 return entityName; 184 } 185 } 186 187 190 public XMLHelper getHelper() { 191 return m_helper; 192 } 193 194 197 public String getSchema() { 198 return m_entityMappingsSchema; 199 } 200 201 206 public String getDiscriminatorValue() { 207 String discriminatorValue = m_helper.getNodeTextValue(m_node, XMLConstants.DISCRIMINATOR_VALUE); 208 209 if (discriminatorValue.equals("")) { 210 return super.getDiscriminatorValue(); 211 } else { 212 return discriminatorValue; 213 } 214 } 215 216 219 public String getDocumentName() { 220 return m_helper.getDocumentName(); 221 } 222 223 226 protected boolean hasEntityTag(Class cls) { 227 Node node = m_helper.locateEntityNode(cls); 228 229 if (node != null) { 230 return true; 231 } else { 232 return super.hasEntityTag(cls); 233 } 234 } 235 236 240 protected boolean hasInheritanceTag(Class entityClass) { 241 Node node = m_helper.locateEntityNode(entityClass); 242 243 if (node != null && m_helper.hasNode(node, XMLConstants.INHERITANCE)) { 244 return true; 245 } else { 246 return super.hasInheritanceTag(entityClass); 247 } 248 } 249 250 254 public boolean isXMLAccessor() { 255 return true; 256 } 257 258 262 public void process() { 263 m_descriptor.setIgnoreAnnotations(m_helper.getNodeValue(m_node, XMLConstants.ATT_METADATA_COMPLETE, m_descriptor.ignoreAnnotations())); 265 266 m_descriptor.setAccess(m_helper.getNodeValue(m_node, XMLConstants.ATT_ACCESS, m_entityMappingsAccess)); 268 269 m_descriptor.setSchema(m_entityMappingsSchema); 272 273 m_descriptor.setCatalog(m_entityMappingsCatalog); 276 277 super.process(); 278 } 279 280 285 protected ClassAccessor processAccessor(MetadataDescriptor descriptor) { 286 Node node = m_helper.locateEntityNode(descriptor.getJavaClass()); 287 288 if (node != null) { 289 XMLClassAccessor accessor = new XMLClassAccessor(new MetadataClass(descriptor.getJavaClass()), node, m_helper, m_processor, descriptor); 290 descriptor.setClassAccessor(accessor); 291 accessor.process(); 292 accessor.setIsProcessed(); 293 return accessor; 294 } else { 295 return super.processAccessor(descriptor); 296 } 297 } 298 299 303 protected void processAccessors() { 304 NodeList nodes = m_helper.getNodes(m_node, XMLConstants.ATTRIBUTES, XMLConstants.ALL_CHILDREN); 305 306 if (nodes != null) { 307 for (int i = 0; i < nodes.getLength(); i++) { 308 processAccessor(buildAccessor(nodes.item(i))); 309 } 310 } 311 312 super.processAccessors(); 313 } 314 315 321 protected void processAssociationOverrides() { 322 NodeList nodes = m_helper.getNodes(m_node, XMLConstants.ASSOCIATION_OVERRIDE); 324 325 if (nodes != null) { 326 for (int i = 0; i < nodes.getLength(); i++) { 327 Node node = nodes.item(i); 328 processAssociationOverride(m_helper.getNodeValue(node, XMLConstants.ATT_NAME), new XMLJoinColumns(node, m_helper)); 329 } 330 } 331 332 super.processAssociationOverrides(); 334 } 335 336 342 protected void processAttributeOverrides() { 343 NodeList nodes = m_helper.getNodes(m_node, XMLConstants.ATTRIBUTE_OVERRIDE); 344 345 if (nodes != null) { 346 for (int i = 0; i < nodes.getLength(); i++) { 347 processAttributeOverride(new XMLColumn(nodes.item(i), m_helper, getAnnotatedElement())); 348 } 349 } 350 351 352 super.processAttributeOverrides(); 354 } 355 356 362 protected void processDiscriminatorColumn() { 363 Node node = m_helper.getNode(m_node, XMLConstants.DISCRIMINATOR_COLUMN); 364 365 if (node == null) { 366 super.processDiscriminatorColumn(); 367 } else { 368 processDiscriminatorColumn(new XMLDiscriminatorColumn(node, m_helper)); 369 } 370 } 371 372 376 public MetadataEntityListener processEntityEventListener(ClassLoader loader) { 377 m_helper.setLoader(loader); 379 380 XMLEntityClassListener listener = new XMLEntityClassListener(getJavaClass()); 382 383 Method [] candidateMethods = MetadataHelper.getCandidateCallbackMethodsForEntityClass(getJavaClass()); 385 processLifecycleEvents(listener, m_node, m_helper, candidateMethods); 386 387 processCallbackMethods(candidateMethods, listener); 389 390 return listener; 393 } 394 395 400 public void processEntityListeners(Class entityClass, ClassLoader loader) { 401 m_helper.setLoader(loader); 403 404 NodeList nodes = m_helper.getNodes(m_node, new String [] {XMLConstants.ENTITY_LISTENERS, XMLConstants.ENTITY_LISTENER}); 405 406 if (nodes.getLength() > 0) { 407 for (int i = 0; i < nodes.getLength(); i++) { 408 Node node = nodes.item(i); 409 410 XMLEntityListener listener = new XMLEntityListener(m_helper.getClassForNode(node), entityClass); 412 413 Method [] candidateMethods = MetadataHelper.getCandidateCallbackMethodsForEntityListener(listener); 415 processLifecycleEvents(listener, node, m_helper, candidateMethods); 416 417 processCallbackMethods(candidateMethods, listener); 420 421 m_descriptor.addEntityListenerEventListener(listener); 423 } 424 } else { 425 super.processEntityListeners(entityClass, loader); 426 } 427 } 428 429 433 public void processEntityMappings() { 434 MetadataPersistenceUnit persistenceUnit = m_project.getPersistenceUnit(); 435 436 if (persistenceUnit != null) { 437 m_entityMappingsAccess = m_helper.getNodeTextValue(XMLConstants.ENTITY_MAPPINGS, XMLConstants.ACCESS, persistenceUnit.getAccess()); 439 m_entityMappingsSchema = m_helper.getNodeTextValue(XMLConstants.ENTITY_MAPPINGS, XMLConstants.SCHEMA, persistenceUnit.getSchema()); 440 m_entityMappingsCatalog = m_helper.getNodeTextValue(XMLConstants.ENTITY_MAPPINGS, XMLConstants.CATALOG, persistenceUnit.getCatalog()); 441 } else { 442 m_entityMappingsAccess = m_helper.getNodeTextValue(XMLConstants.ENTITY_MAPPINGS, XMLConstants.ACCESS); 443 m_entityMappingsSchema = m_helper.getNodeTextValue(XMLConstants.ENTITY_MAPPINGS, XMLConstants.SCHEMA); 444 m_entityMappingsCatalog = m_helper.getNodeTextValue(XMLConstants.ENTITY_MAPPINGS, XMLConstants.CATALOG); 445 } 446 447 NodeList tableGeneratorNodes = m_helper.getNodes(XMLConstants.ENTITY_MAPPINGS, XMLConstants.TABLE_GENERATOR); 449 450 if (tableGeneratorNodes != null) { 451 for (int i = 0; i < tableGeneratorNodes.getLength(); i++) { 452 processTableGenerator(tableGeneratorNodes.item(i)); 453 } 454 } 455 456 NodeList sequenceGeneratorNodes = m_helper.getNodes(XMLConstants.ENTITY_MAPPINGS, XMLConstants.SEQUENCE_GENERATOR); 458 459 if (sequenceGeneratorNodes != null) { 460 for (int i = 0; i < sequenceGeneratorNodes.getLength(); i++) { 461 processSequenceGenerator(sequenceGeneratorNodes.item(i)); 462 } 463 } 464 465 processNamedQueries(m_helper.getNodes(XMLConstants.ENTITY_MAPPINGS, XMLConstants.NAMED_QUERY)); 467 468 processNamedNativeQueries(m_helper.getNodes(XMLConstants.ENTITY_MAPPINGS, XMLConstants.NAMED_NATIVE_QUERY)); 470 471 processSqlResultSetMappings(m_helper.getNodes(XMLConstants.ENTITY_MAPPINGS, XMLConstants.SQL_RESULT_SET_MAPPING)); 473 } 474 475 480 protected void processExcludeDefaultListeners() { 481 if (m_helper.hasNode(m_node, XMLConstants.EXCLUDE_DEFAULT_LISTENERS)) { 482 m_descriptor.setExcludeDefaultListeners(true); 483 } else { 484 super.processExcludeDefaultListeners(); 485 } 486 } 487 488 493 protected void processExcludeSuperclassListeners() { 494 if (m_helper.hasNode(m_node, XMLConstants.EXCLUDE_SUPERCLASS_LISTENERS)) { 495 m_descriptor.setExcludeSuperclassListeners(true); 496 } else { 497 super.processExcludeSuperclassListeners(); 498 } 499 } 500 501 505 protected Class getIdClass() { 506 Node result = m_helper.getNode(m_node, XMLConstants.ID_CLASS); 507 508 if (result != null) { 509 return m_helper.getClassForNode(result); 510 } 511 512 return super.getIdClass(); 513 } 514 515 520 protected String getInheritanceStrategy() { 521 Node inheritanceNode = m_helper.getNode(m_node, XMLConstants.INHERITANCE); 522 523 if (inheritanceNode == null) { 524 return super.getInheritanceStrategy(); 525 } else { 526 return m_helper.getNodeValue(inheritanceNode, XMLConstants.ATT_STRATEGY); 527 } 528 } 529 530 534 protected MetadataPrimaryKeyJoinColumns getPrimaryKeyJoinColumns(String sourceTableName, String targetTableName) { 535 if (m_helper.nodeHasPrimaryKeyJoinColumns(m_node)) { 536 return new XMLPrimaryKeyJoinColumns(m_node, m_helper, sourceTableName, targetTableName); 537 } else { 538 return super.getPrimaryKeyJoinColumns(sourceTableName, targetTableName); 539 } 540 } 541 542 546 public void processMappedSuperclassEventListener(MetadataEntityListener listener, Class entityClass, ClassLoader loader) { 547 m_helper.setLoader(loader); 549 550 Method [] candidateMethods = MetadataHelper.getCandidateCallbackMethodsForMappedSuperclass(getJavaClass(), entityClass); 552 processLifecycleEvents(listener, m_node, m_helper, candidateMethods); 553 554 processCallbackMethods(candidateMethods, listener); 556 } 557 558 563 protected void processNamedNativeQueries() { 564 processNamedNativeQueries(m_helper.getNodes(m_node, XMLConstants.NAMED_NATIVE_QUERY)); 566 567 super.processNamedNativeQueries(); 569 } 570 571 578 protected void processNamedNativeQueries(NodeList queryNodes) { 579 if (queryNodes != null) { 580 for (int i = 0; i < queryNodes.getLength(); i++) { 581 processNamedNativeQuery(new XMLNamedNativeQuery(queryNodes.item(i), m_helper)); 583 } 584 } 585 } 586 587 592 protected void processNamedQueries() { 593 processNamedQueries(m_helper.getNodes(m_node, XMLConstants.NAMED_QUERY)); 595 596 super.processNamedQueries(); 598 } 599 600 607 protected void processNamedQueries(NodeList queryNodes) { 608 if (queryNodes != null) { 609 for (int i = 0; i < queryNodes.getLength(); i++) { 610 processNamedQuery(new XMLNamedQuery(queryNodes.item(i), m_helper)); 612 } 613 } 614 } 615 616 620 protected void processSecondaryTables() { 621 NodeList secondaryTables = m_helper.getNodes(m_node, XMLConstants.SECONDARY_TABLE); 622 623 if (secondaryTables != null) { 624 for (int i = 0; i < secondaryTables.getLength(); i++) { 625 processSecondaryTable(new XMLSecondaryTable(secondaryTables.item(i), m_helper)); 626 } 627 } 628 } 629 630 635 protected void processSequenceGenerator() { 636 processSequenceGenerator(m_helper.getNode(m_node, XMLConstants.SEQUENCE_GENERATOR)); 638 639 super.processSequenceGenerator(); 641 } 642 643 648 protected void processSequenceGenerator(Node node) { 649 if (node != null) { 650 processSequenceGenerator(new XMLSequenceGenerator(node, m_helper)); 652 } 653 } 654 655 660 protected void processSqlResultSetMappings() { 661 processSqlResultSetMappings(m_helper.getNodes(m_node, XMLConstants.SQL_RESULT_SET_MAPPING)); 663 664 super.processSqlResultSetMappings(); 666 } 667 668 672 protected void processSqlResultSetMappings(NodeList sqlResultSetNodes) { 673 if (sqlResultSetNodes != null) { 674 int nodeCount = sqlResultSetNodes.getLength(); 675 676 for (int i = 0; i < nodeCount; i++) { 677 processSqlResultSetMapping(new XMLSQLResultSetMapping(sqlResultSetNodes.item(i), m_helper)); 679 } 680 } 681 } 682 683 687 protected void processTable() { 688 if (m_descriptor.ignoreTables()) { 689 m_logger.logWarningMessage(m_logger.IGNORE_TABLE, getJavaClass()); 690 } else { 691 Node tableNode = m_helper.getNode(m_node, XMLConstants.TABLE); 692 693 if (tableNode != null) { 694 processTable(new XMLTable(tableNode, m_helper)); 695 } else { 696 super.processTable(); 699 } 700 } 701 } 702 703 708 protected void processTableGenerator() { 709 processTableGenerator(m_helper.getNode(m_node, XMLConstants.TABLE_GENERATOR)); 711 712 super.processTableGenerator(); 714 } 715 716 720 protected void processTableGenerator(Node node) { 721 if (node != null) { 722 processTableGenerator(new XMLTableGenerator(node, this)); 724 } 725 } 726 } 727 | Popular Tags |