1 21 package oracle.toplink.essentials.internal.ejb.cmp3.metadata; 22 23 import java.util.Collection ; 24 import java.util.List ; 25 import java.util.HashMap ; 26 import java.util.HashSet ; 27 import java.util.Hashtable ; 28 29 import oracle.toplink.essentials.sequencing.Sequence; 30 import oracle.toplink.essentials.sequencing.TableSequence; 31 import oracle.toplink.essentials.sequencing.NativeSequence; 32 33 import oracle.toplink.essentials.sessions.Project; 34 import oracle.toplink.essentials.sessions.DatasourceLogin; 35 import oracle.toplink.essentials.descriptors.ClassDescriptor; 36 import oracle.toplink.essentials.exceptions.ValidationException; 37 import oracle.toplink.essentials.queryframework.EJBQLPlaceHolderQuery; 38 39 import oracle.toplink.essentials.internal.helper.DatabaseTable; 40 import oracle.toplink.essentials.internal.sessions.AbstractSession; 41 42 import oracle.toplink.essentials.internal.ejb.cmp3.EJBQueryImpl; 43 import oracle.toplink.essentials.internal.ejb.cmp3.base.QueryHintsHandler; 44 45 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.accessors.RelationshipAccessor; 46 47 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.queries.MetadataQueryHint; 48 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.queries.MetadataNamedQuery; 49 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.queries.MetadataNamedNativeQuery; 50 51 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.sequencing.MetadataGeneratedValue; 52 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.sequencing.MetadataTableGenerator; 53 import oracle.toplink.essentials.internal.ejb.cmp3.metadata.sequencing.MetadataSequenceGenerator; 54 55 import oracle.toplink.essentials.internal.ejb.cmp3.xml.XMLHelper; 56 57 import org.w3c.dom.Node ; 58 import org.w3c.dom.NodeList ; 59 60 64 public class MetadataProject { 65 protected AbstractSession m_session; 67 68 protected boolean m_enableLazyForOneToOne; 70 71 protected MetadataPersistenceUnit m_persistenceUnit; 73 74 protected HashMap <String , Node > m_mappedSuperclassNodes; 76 protected HashMap <String , XMLHelper> m_mappedSuperclasses; 77 78 protected HashMap <String , Node > m_embeddableNodes; 80 protected HashMap <String , XMLHelper> m_embeddables; 81 82 protected HashMap <String , MetadataDescriptor> m_allDescriptors; 84 85 protected HashSet <MetadataDescriptor> m_relationshipDescriptors; 87 88 protected HashMap <String , MetadataNamedQuery> m_namedQueries; 90 91 protected HashMap <String , MetadataNamedNativeQuery> m_namedNativeQueries; 93 94 protected HashMap <Class , MetadataGeneratedValue> m_generatedValues; 96 protected HashMap <String , MetadataTableGenerator> m_tableGenerators; 97 protected HashMap <String , MetadataSequenceGenerator> m_sequenceGenerators; 98 99 protected HashMap <XMLHelper, NodeList > m_defaultListeners; 102 103 106 public MetadataProject(AbstractSession session, boolean enableLazyForOneToOne) { 107 m_session = session; 108 m_enableLazyForOneToOne = enableLazyForOneToOne; 109 110 m_defaultListeners = new HashMap <XMLHelper, NodeList >(); 111 112 m_namedQueries = new HashMap <String , MetadataNamedQuery>(); 113 m_namedNativeQueries = new HashMap <String , MetadataNamedNativeQuery>(); 114 115 m_mappedSuperclassNodes = new HashMap <String , Node >(); 116 m_mappedSuperclasses = new HashMap <String , XMLHelper>(); 117 118 m_embeddableNodes = new HashMap <String , Node >(); 119 m_embeddables = new HashMap <String , XMLHelper>(); 120 121 m_allDescriptors = new HashMap <String , MetadataDescriptor>(); 122 m_relationshipDescriptors = new HashSet <MetadataDescriptor>(); 123 124 m_generatedValues = new HashMap <Class , MetadataGeneratedValue>(); 125 m_tableGenerators = new HashMap <String , MetadataTableGenerator>(); 126 m_sequenceGenerators = new HashMap <String , MetadataSequenceGenerator>(); 127 } 128 129 132 public void addDefaultListeners(NodeList nodes, XMLHelper helper) { 133 m_defaultListeners.put(helper, nodes); 134 } 135 136 141 public void addDescriptor(MetadataDescriptor descriptor) { 142 if (m_persistenceUnit != null) { 144 descriptor.setAccess(m_persistenceUnit.getAccess()); 145 descriptor.setSchema(m_persistenceUnit.getSchema()); 146 descriptor.setCatalog(m_persistenceUnit.getCatalog()); 147 descriptor.setIsCascadePersist(m_persistenceUnit.isCascadePersist()); 148 descriptor.setIgnoreAnnotations(m_persistenceUnit.isMetadataComplete()); 149 } 150 151 Project project = getSession().getProject(); 152 ClassDescriptor descriptorOnProject = MetadataHelper.findDescriptor(project, descriptor.getJavaClass()); 153 154 if (descriptorOnProject == null) { 155 project.addDescriptor(descriptor.getDescriptor()); 156 } else { 157 descriptor.setDescriptor(descriptorOnProject); 158 } 159 160 m_allDescriptors.put(descriptor.getJavaClassName(), descriptor); 161 } 162 163 166 public void addGeneratedValue(MetadataGeneratedValue metadatageneratedvalue, Class entityClass) { 167 m_generatedValues.put(entityClass, metadatageneratedvalue); 168 } 169 170 174 public void addMappedSuperclass(Class mappedSuperclass, Node node, XMLHelper helper) { 175 m_mappedSuperclasses.put(mappedSuperclass.getName(), helper); 176 m_mappedSuperclassNodes.put(mappedSuperclass.getName(), node); 177 } 178 179 183 public void addEmbeddable(Class embeddable, Node node, XMLHelper helper) { 184 m_embeddables.put(embeddable.getName(), helper); 185 m_embeddableNodes.put(embeddable.getName(), node); 186 } 187 188 191 public void addNamedNativeQuery(MetadataNamedNativeQuery namedNativeQuery) { 192 m_namedNativeQueries.put(namedNativeQuery.getName(), namedNativeQuery); 193 } 194 195 198 public void addNamedQuery(MetadataNamedQuery namedQuery) { 199 m_namedQueries.put(namedQuery.getName(), namedQuery); 200 } 201 202 205 public void addRelationshipDescriptor(MetadataDescriptor descriptor) { 206 m_relationshipDescriptors.add(descriptor); 207 } 208 209 212 public void addSequenceGenerator(MetadataSequenceGenerator sequenceGenerator) { 213 m_sequenceGenerators.put(sequenceGenerator.getName(), sequenceGenerator); 214 } 215 216 219 public void addTableGenerator(MetadataTableGenerator tableGenerator) { 220 m_tableGenerators.put(tableGenerator.getName(), tableGenerator); 221 } 222 223 226 public boolean containsDescriptor(Class cls) { 227 return m_allDescriptors.containsKey(cls.getName()); 228 } 229 230 233 public boolean enableLazyForOneToOne() { 234 return m_enableLazyForOneToOne; 235 } 236 237 240 public HashMap <XMLHelper, NodeList > getDefaultListeners() { 241 return m_defaultListeners; 242 } 243 244 247 public MetadataDescriptor getDescriptor(Class cls) { 248 if (cls == null) { 249 return null; 250 } else { 251 MetadataDescriptor descriptor = m_allDescriptors.get(cls.getName()); 252 if (descriptor == null) { 253 throw ValidationException.classNotListedInPersistenceUnit(cls.getName()); 254 } else { 255 return descriptor; 256 } 257 } 258 } 259 260 263 public Collection <MetadataDescriptor> getDescriptors() { 264 return m_allDescriptors.values(); 265 } 266 267 270 public XMLHelper getMappedSuperclassHelper(Class cls) { 271 return m_mappedSuperclasses.get(cls.getName()); 272 } 273 274 277 public Node getMappedSuperclassNode(Class cls) { 278 return m_mappedSuperclassNodes.get(cls.getName()); 279 } 280 281 284 public XMLHelper getEmbeddableHelper(Class cls) { 285 return m_embeddables.get(cls.getName()); 286 } 287 288 291 public Node getEmbeddableNode(Class cls) { 292 return m_embeddableNodes.get(cls.getName()); 293 } 294 295 298 public MetadataNamedNativeQuery getNamedNativeQuery(String name) { 299 return m_namedNativeQueries.get(name); 300 } 301 302 305 public MetadataNamedQuery getNamedQuery(String name) { 306 return m_namedQueries.get(name); 307 } 308 309 313 public MetadataPersistenceUnit getPersistenceUnit() { 314 return m_persistenceUnit; 315 } 316 317 320 public HashSet <MetadataDescriptor> getRelationshipDescriptors() { 321 return m_relationshipDescriptors; 322 } 323 324 327 public MetadataSequenceGenerator getSequenceGenerator(String name) { 328 return m_sequenceGenerators.get(name); 329 } 330 331 334 public Collection <MetadataSequenceGenerator> getSequenceGenerators() { 335 return m_sequenceGenerators.values(); 336 } 337 338 341 public AbstractSession getSession() { 342 return m_session; 343 } 344 345 348 public MetadataTableGenerator getTableGenerator(String name) { 349 return m_tableGenerators.get(name); 350 } 351 352 355 public Collection <MetadataTableGenerator> getTableGenerators() { 356 return m_tableGenerators.values(); 357 } 358 359 362 public boolean hasConflictingSequenceGenerator(MetadataSequenceGenerator sequenceGenerator) { 363 if (hasSequenceGenerator(sequenceGenerator.getName())) { 364 return ! getSequenceGenerator(sequenceGenerator.getName()).equals(sequenceGenerator); 365 } else { 366 return false; 367 } 368 } 369 370 373 public boolean hasConflictingTableGenerator(MetadataTableGenerator tableGenerator) { 374 if (hasTableGenerator(tableGenerator.getName())) { 375 return ! getTableGenerator(tableGenerator.getName()).equals(tableGenerator); 376 } else { 377 return false; 378 } 379 } 380 381 384 public boolean hasDescriptors() { 385 return ! m_allDescriptors.isEmpty(); 386 } 387 388 391 public boolean hasMappedSuperclass(Class cls) { 392 return m_mappedSuperclasses.containsKey(cls.getName()); 393 } 394 395 398 public boolean hasEmbeddable(Class cls) { 399 return m_embeddables.containsKey(cls.getName()); 400 } 401 402 405 public boolean hasNamedNativeQuery(String name) { 406 return m_namedNativeQueries.containsKey(name); 407 } 408 409 412 public boolean hasNamedQuery(String name) { 413 return m_namedQueries.containsKey(name); 414 } 415 416 420 public boolean hasPersistenceUnit() { 421 return m_persistenceUnit != null; 422 } 423 424 427 public boolean hasSequenceGenerator(String name) { 428 return getSequenceGenerator(name) != null; 429 } 430 431 434 public boolean hasTableGenerator(String name) { 435 return getTableGenerator(name) != null; 436 } 437 438 442 public AbstractSession process() { 443 processSequencing(); 444 445 processRelationshipDescriptors(); 446 447 return m_session; 448 } 449 450 455 public void processNamedNativeQueries(ClassLoader loader) { 456 for (MetadataNamedNativeQuery query : m_namedNativeQueries.values()) { 457 HashMap <String , String > hints = processQueryHints(query.getHints(), query.getName()); 458 459 Class resultClass = query.getResultClass(); 460 461 if (resultClass != void.class) { 462 resultClass = MetadataHelper.getClassForName(resultClass.getName(), loader); 463 m_session.addQuery(query.getName(), EJBQueryImpl.buildSQLDatabaseQuery(resultClass, query.getEJBQLString(), hints)); 464 } else { 465 String resultSetMapping = query.getResultSetMapping(); 466 if (! resultSetMapping.equals("")) { 467 m_session.addQuery(query.getName(), EJBQueryImpl.buildSQLDatabaseQuery(resultSetMapping, query.getEJBQLString(), hints)); 468 } else { 469 m_session.addQuery(query.getName(), EJBQueryImpl.buildSQLDatabaseQuery(query.getEJBQLString(), hints)); 471 } 472 } 473 } 474 } 475 476 480 public void processNamedQueries(MetadataValidator validator) { 481 for (MetadataNamedQuery query : m_namedQueries.values()) { 482 try { 483 HashMap <String , String > hints = processQueryHints(query.getHints(), query.getName()); 484 m_session.addEjbqlPlaceHolderQuery(new EJBQLPlaceHolderQuery(query.getName(), query.getEJBQLString(), hints)); 485 } catch (Exception exception) { 486 validator.throwErrorProcessingNamedQueryAnnotation(query.getClass(), query.getName(), exception); 487 } 488 } 489 } 490 491 495 protected HashMap <String , String > processQueryHints(List <MetadataQueryHint> hints, String queryName) { 496 HashMap <String , String > hm = new HashMap <String , String >(); 497 498 for (MetadataQueryHint hint : hints) { 499 QueryHintsHandler.verify(hint.getName(), hint.getValue(), queryName, m_session); 500 hm.put(hint.getName(), hint.getValue()); 501 } 502 503 return hm; 504 } 505 506 510 protected void processRelationshipDescriptors() { 511 for (MetadataDescriptor descriptor : (HashSet <MetadataDescriptor>) getRelationshipDescriptors()) { 512 for (RelationshipAccessor accessor : (Collection <RelationshipAccessor>) descriptor.getRelationshipAccessors()) { 513 accessor.processRelationship(); 514 } 515 } 516 } 517 518 522 protected void processSequencing() { 523 if (! m_generatedValues.isEmpty()) { 524 DatasourceLogin login = m_session.getProject().getLogin(); 525 526 for (MetadataGeneratedValue generatedValue : m_generatedValues.values()) { 528 String type = generatedValue.getStrategy(); 529 String generatorName = generatedValue.getGenerator(); 530 531 if (type.equals(MetadataConstants.TABLE)) { 532 MetadataSequenceGenerator sequenceGenerator = m_sequenceGenerators.get(generatorName); 533 534 if (sequenceGenerator != null) { 535 } 537 } else if (type.equals(MetadataConstants.SEQUENCE) || type.equals(MetadataConstants.IDENTITY)) { 538 MetadataTableGenerator tableGenerator = m_tableGenerators.get(generatorName); 539 540 if (tableGenerator != null) { 541 } 543 } 544 } 545 546 Sequence defaultAutoSequence = null; 547 TableSequence defaultTableSequence = new TableSequence(MetadataConstants.DEFAULT_TABLE_GENERATOR); 548 NativeSequence defaultNativeSequence = new NativeSequence(MetadataConstants.DEFAULT_SEQUENCE_GENERATOR); 549 550 Hashtable <String , Sequence> sequences = new Hashtable <String , Sequence>(); 552 553 for (MetadataSequenceGenerator sequenceGenerator : m_sequenceGenerators.values()) { 554 String sequenceGeneratorName = sequenceGenerator.getName(); 555 String seqName = (sequenceGenerator.getSequenceName().equals("")) ? sequenceGeneratorName : sequenceGenerator.getSequenceName(); 556 NativeSequence sequence = new NativeSequence(seqName, sequenceGenerator.getAllocationSize()); 557 sequences.put(sequenceGeneratorName, sequence); 558 559 if (sequenceGeneratorName.equals(MetadataConstants.DEFAULT_AUTO_GENERATOR)) { 560 defaultAutoSequence = sequence; 563 } else if (sequenceGeneratorName.equals(MetadataConstants.DEFAULT_SEQUENCE_GENERATOR)) { 564 defaultNativeSequence = sequence; 569 } 570 } 571 572 for (MetadataTableGenerator tableGenerator : m_tableGenerators.values()) { 573 String tableGeneratorName = tableGenerator.getName(); 574 String seqName = (tableGenerator.getPkColumnValue().equals("")) ? tableGeneratorName : tableGenerator.getPkColumnValue(); 575 TableSequence sequence = new TableSequence(seqName, tableGenerator.getAllocationSize()); 576 sequences.put(tableGeneratorName, sequence); 577 578 sequence.setTable(new DatabaseTable(MetadataHelper.getFullyQualifiedTableName(tableGenerator.getTable(), sequence.getTableName(), tableGenerator.getCatalog(), tableGenerator.getSchema()))); 583 584 for (String uniqueConstraint : tableGenerator.getUniqueConstraints()) { 586 sequence.getTable().addUniqueConstraint(uniqueConstraint); 587 } 588 589 if (! tableGenerator.getPkColumnName().equals("")) { 590 sequence.setNameFieldName(tableGenerator.getPkColumnName()); 591 } 592 593 if (! tableGenerator.getValueColumnName().equals("")) { 594 sequence.setCounterFieldName(tableGenerator.getValueColumnName()); 595 } 596 597 if (tableGeneratorName.equals(MetadataConstants.DEFAULT_AUTO_GENERATOR)) { 598 defaultAutoSequence = sequence; 601 } else if (tableGeneratorName.equals(MetadataConstants.DEFAULT_TABLE_GENERATOR)) { 602 defaultTableSequence = sequence; 607 } 608 } 609 610 for (Class entityClass : m_generatedValues.keySet()) { 613 MetadataDescriptor descriptor = m_allDescriptors.get(entityClass.getName()); 614 MetadataGeneratedValue generatedValue = m_generatedValues.get(entityClass); 615 String generatorName = generatedValue.getGenerator(); 616 Sequence sequence = null; 617 618 if (! generatorName.equals("")) { 619 sequence = sequences.get(generatorName); 620 } 621 622 if (sequence == null) { 623 if (generatedValue.getStrategy().equals(MetadataConstants.TABLE)) { 624 if (generatorName.equals("")) { 625 sequence = defaultTableSequence; 626 } else { 627 sequence = (Sequence)defaultTableSequence.clone(); 628 sequence.setName(generatorName); 629 } 630 } else if (generatedValue.getStrategy().equals(MetadataConstants.SEQUENCE) || generatedValue.getStrategy().equals(MetadataConstants.IDENTITY)) { 631 if (generatorName.equals("")) { 632 sequence = defaultNativeSequence; 633 } else { 634 sequence = (Sequence)defaultNativeSequence.clone(); 635 sequence.setName(generatorName); 636 } 637 } 638 } 639 640 if (sequence != null) { 641 descriptor.setSequenceNumberName(sequence.getName()); 642 login.addSequence(sequence); 643 } else if (generatedValue.getStrategy().equals(MetadataConstants.AUTO)) { 644 if (defaultAutoSequence != null) { 645 descriptor.setSequenceNumberName(defaultAutoSequence.getName()); 646 login.setDefaultSequence(defaultAutoSequence); 647 } else { 648 descriptor.setSequenceNumberName(MetadataConstants.DEFAULT_AUTO_GENERATOR); 649 } 650 } 651 } 652 } 653 } 654 655 659 public void setPersistenceUnit(MetadataPersistenceUnit persistenceUnit) { 660 m_persistenceUnit = persistenceUnit; 661 } 662 } 663 | Popular Tags |