| /******************************************************************************* |
| * Copyright (c) 2007, 2011 Oracle. All rights reserved. |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License v1.0, which accompanies this distribution |
| * and is available at http://www.eclipse.org/legal/epl-v10.html. |
| * |
| * Contributors: |
| * Oracle - initial API and implementation |
| ******************************************************************************/ |
| package org.eclipse.jpt.jpa.core.internal.jpa1.context.java; |
| |
| import java.util.Iterator; |
| import java.util.List; |
| import org.eclipse.jdt.core.dom.CompilationUnit; |
| import org.eclipse.jpt.common.core.utility.TextRange; |
| import org.eclipse.jpt.common.utility.Filter; |
| import org.eclipse.jpt.common.utility.internal.CollectionTools; |
| import org.eclipse.jpt.common.utility.internal.iterators.CompositeIterator; |
| import org.eclipse.jpt.common.utility.internal.iterators.EmptyIterator; |
| import org.eclipse.jpt.jpa.core.MappingKeys; |
| import org.eclipse.jpt.jpa.core.context.AssociationOverride; |
| import org.eclipse.jpt.jpa.core.context.AssociationOverrideContainer; |
| import org.eclipse.jpt.jpa.core.context.AttributeMapping; |
| import org.eclipse.jpt.jpa.core.context.BaseColumn; |
| import org.eclipse.jpt.jpa.core.context.JoinColumn; |
| import org.eclipse.jpt.jpa.core.context.JoinTable; |
| import org.eclipse.jpt.jpa.core.context.OverrideContainer; |
| import org.eclipse.jpt.jpa.core.context.Override_; |
| import org.eclipse.jpt.jpa.core.context.Relationship; |
| import org.eclipse.jpt.jpa.core.context.Table; |
| import org.eclipse.jpt.jpa.core.context.TypeMapping; |
| import org.eclipse.jpt.jpa.core.context.java.JavaAssociationOverrideContainer; |
| import org.eclipse.jpt.jpa.core.context.java.JavaPersistentAttribute; |
| import org.eclipse.jpt.jpa.core.internal.context.AttributeMappingTools; |
| import org.eclipse.jpt.jpa.core.internal.context.BaseColumnTextRangeResolver; |
| import org.eclipse.jpt.jpa.core.internal.context.JoinColumnTextRangeResolver; |
| import org.eclipse.jpt.jpa.core.internal.context.JptValidator; |
| import org.eclipse.jpt.jpa.core.internal.context.MappingTools; |
| import org.eclipse.jpt.jpa.core.internal.context.OverrideTextRangeResolver; |
| import org.eclipse.jpt.jpa.core.internal.context.TableTextRangeResolver; |
| import org.eclipse.jpt.jpa.core.internal.context.java.AbstractJavaBaseEmbeddedMapping; |
| import org.eclipse.jpt.jpa.core.internal.jpa1.context.AssociationOverrideInverseJoinColumnValidator; |
| import org.eclipse.jpt.jpa.core.internal.jpa1.context.AssociationOverrideJoinColumnValidator; |
| import org.eclipse.jpt.jpa.core.internal.jpa1.context.AssociationOverrideJoinTableValidator; |
| import org.eclipse.jpt.jpa.core.internal.jpa1.context.AssociationOverrideValidator; |
| import org.eclipse.jpt.jpa.core.internal.jpa1.context.EmbeddableOverrideDescriptionProvider; |
| import org.eclipse.jpt.jpa.core.internal.jpa1.context.EntityTableDescriptionProvider; |
| import org.eclipse.jpt.jpa.core.internal.jpa1.context.JoinTableTableDescriptionProvider; |
| import org.eclipse.jpt.jpa.core.jpa2.context.java.JavaEmbeddedMapping2_0; |
| import org.eclipse.jpt.jpa.core.resource.java.EmbeddedAnnotation; |
| import org.eclipse.jpt.jpa.core.resource.java.JavaResourcePersistentMember; |
| import org.eclipse.wst.validation.internal.provisional.core.IMessage; |
| import org.eclipse.wst.validation.internal.provisional.core.IReporter; |
| |
| /** |
| * Java embedded mapping |
| */ |
| public class GenericJavaEmbeddedMapping |
| extends AbstractJavaBaseEmbeddedMapping<EmbeddedAnnotation> |
| implements JavaEmbeddedMapping2_0 |
| { |
| protected final JavaAssociationOverrideContainer associationOverrideContainer; |
| |
| |
| public GenericJavaEmbeddedMapping(JavaPersistentAttribute parent) { |
| super(parent); |
| this.associationOverrideContainer = this.buildAssociationOverrideContainer(); |
| } |
| |
| |
| // ********** synchronize/update ********** |
| |
| @Override |
| public void synchronizeWithResourceModel() { |
| super.synchronizeWithResourceModel(); |
| this.associationOverrideContainer.synchronizeWithResourceModel(); |
| } |
| |
| @Override |
| public void update() { |
| super.update(); |
| this.associationOverrideContainer.update(); |
| } |
| |
| |
| // ********** association override container ********** |
| |
| protected JavaAssociationOverrideContainer buildAssociationOverrideContainer() { |
| return this.isJpa2_0Compatible() ? |
| this.getJpaFactory2_0().buildJavaAssociationOverrideContainer(this, this.buildAssociationOverrideContainerOwner()) : |
| new GenericJavaAssociationOverrideContainer(this, null); |
| } |
| |
| public JavaAssociationOverrideContainer getAssociationOverrideContainer() { |
| return this.associationOverrideContainer; |
| } |
| |
| protected JavaAssociationOverrideContainer.Owner buildAssociationOverrideContainerOwner() { |
| return new AssociationOverrideContainerOwner(); |
| } |
| |
| |
| // ********** embedded mappings ********** |
| |
| /** |
| * This is only to build the choices for a "mapped by" setting in a |
| * relationship mapping. JPA 2.0 does not support relationship mappings |
| * in an embedded ID class; so we only put this logic here. |
| */ |
| @Override |
| @SuppressWarnings("unchecked") |
| public Iterator<String> allMappingNames() { |
| return this.isJpa2_0Compatible() ? |
| new CompositeIterator<String>(super.allMappingNames(), this.allEmbeddableAttributeMappingNames()) : |
| super.allMappingNames(); |
| } |
| |
| protected Iterator<String> allEmbeddableAttributeMappingNames() { |
| return this.qualifiedEmbeddableOverridableMappingNames(AttributeMappingTools.ALL_MAPPING_NAMES_TRANSFORMER); |
| } |
| |
| @Override |
| public AttributeMapping resolveAttributeMapping(String attributeName) { |
| AttributeMapping resolvedMapping = super.resolveAttributeMapping(attributeName); |
| if (resolvedMapping != null) { |
| return resolvedMapping; |
| } |
| return this.isJpa2_0Compatible() ? this.resolveAttributeMapping_(attributeName) : null; |
| } |
| |
| protected AttributeMapping resolveAttributeMapping_(String attributeName) { |
| attributeName = this.unqualify(attributeName); |
| if (attributeName == null) { |
| return null; |
| } |
| // recurse into the embeddable mappings |
| for (AttributeMapping mapping : CollectionTools.iterable(this.embeddableAttributeMappings())) { |
| AttributeMapping resolvedMapping = mapping.resolveAttributeMapping(attributeName); |
| if (resolvedMapping != null) { |
| return resolvedMapping; |
| } |
| } |
| return null; |
| } |
| |
| |
| // ********** misc ********** |
| |
| public String getKey() { |
| return MappingKeys.EMBEDDED_ATTRIBUTE_MAPPING_KEY; |
| } |
| |
| @Override |
| protected String getAnnotationName() { |
| return EmbeddedAnnotation.ANNOTATION_NAME; |
| } |
| |
| @Override |
| public Relationship resolveOverriddenRelationship(String attributeName) { |
| return this.isJpa2_0Compatible() ? this.resolveOverriddenRelationship_(attributeName) : null; |
| } |
| |
| protected Relationship resolveOverriddenRelationship_(String attributeName) { |
| attributeName = this.unqualify(attributeName); |
| if (attributeName == null) { |
| return null; |
| } |
| AssociationOverride override = this.associationOverrideContainer.getSpecifiedOverrideNamed(attributeName); |
| // recurse into the target embeddable if necessary |
| return (override != null) ? override.getRelationship() : this.resolveOverriddenRelationshipInTargetEmbeddable(attributeName); |
| } |
| |
| protected Relationship resolveOverriddenRelationshipInTargetEmbeddable(String attributeName) { |
| return (this.targetEmbeddable == null) ? null : this.targetEmbeddable.resolveOverriddenRelationship(attributeName); |
| } |
| |
| |
| // ********** Java completion proposals ********** |
| |
| @Override |
| public Iterator<String> javaCompletionProposals(int pos, Filter<String> filter, CompilationUnit astRoot) { |
| Iterator<String> result = super.javaCompletionProposals(pos, filter, astRoot); |
| if (result != null) { |
| return result; |
| } |
| |
| result = this.associationOverrideContainer.javaCompletionProposals(pos, filter, astRoot); |
| if (result != null) { |
| return result; |
| } |
| |
| return null; |
| } |
| |
| |
| // ********** validation ********** |
| |
| @Override |
| protected void validateOverrides(List<IMessage> messages, IReporter reporter, CompilationUnit astRoot) { |
| super.validateOverrides(messages, reporter, astRoot); |
| this.associationOverrideContainer.validate(messages, reporter, astRoot); |
| } |
| |
| |
| // ********** association override container owner ********** |
| |
| protected class AssociationOverrideContainerOwner |
| implements JavaAssociationOverrideContainer.Owner |
| { |
| public JavaResourcePersistentMember getResourcePersistentMember() { |
| return GenericJavaEmbeddedMapping.this.getResourcePersistentAttribute(); |
| } |
| |
| public TypeMapping getTypeMapping() { |
| return GenericJavaEmbeddedMapping.this.getTypeMapping(); |
| } |
| |
| public TypeMapping getOverridableTypeMapping() { |
| return GenericJavaEmbeddedMapping.this.getTargetEmbeddable(); |
| } |
| |
| public Iterator<String> allOverridableNames() { |
| TypeMapping typeMapping = this.getOverridableTypeMapping(); |
| return (typeMapping != null) ? typeMapping.allOverridableAssociationNames() : EmptyIterator.<String>instance(); |
| } |
| |
| public Relationship resolveOverriddenRelationship(String attributeName) { |
| return MappingTools.resolveOverriddenRelationship(this.getOverridableTypeMapping(), attributeName); |
| } |
| |
| public boolean tableNameIsInvalid(String tableName) { |
| return this.getTypeMapping().tableNameIsInvalid(tableName); |
| } |
| |
| public Iterator<String> candidateTableNames() { |
| return this.getTypeMapping().allAssociatedTableNames(); |
| } |
| |
| public org.eclipse.jpt.jpa.db.Table resolveDbTable(String tableName) { |
| return this.getTypeMapping().resolveDbTable(tableName); |
| } |
| |
| public String getDefaultTableName() { |
| return this.getTypeMapping().getPrimaryTableName(); |
| } |
| |
| public String getPossiblePrefix() { |
| return null; |
| } |
| |
| public String getWritePrefix() { |
| return null; |
| } |
| |
| // no maps, so all overrides are relevant |
| public boolean isRelevant(String overrideName) { |
| return true; |
| } |
| |
| public TextRange getValidationTextRange(CompilationUnit astRoot) { |
| return GenericJavaEmbeddedMapping.this.getValidationTextRange(astRoot); |
| } |
| |
| public JptValidator buildValidator(Override_ override, OverrideContainer container, OverrideTextRangeResolver textRangeResolver) { |
| return new AssociationOverrideValidator(this.getPersistentAttribute(), (AssociationOverride) override, (AssociationOverrideContainer) container, textRangeResolver, new EmbeddableOverrideDescriptionProvider()); |
| } |
| |
| public JptValidator buildColumnValidator(Override_ override, BaseColumn column, BaseColumn.Owner owner, BaseColumnTextRangeResolver textRangeResolver) { |
| return new AssociationOverrideJoinColumnValidator(this.getPersistentAttribute(), (AssociationOverride) override, (JoinColumn) column, (JoinColumn.Owner) owner, (JoinColumnTextRangeResolver) textRangeResolver, new EntityTableDescriptionProvider()); |
| } |
| |
| public JptValidator buildJoinTableJoinColumnValidator(AssociationOverride override, JoinColumn column, JoinColumn.Owner owner, JoinColumnTextRangeResolver textRangeResolver) { |
| return new AssociationOverrideJoinColumnValidator(this.getPersistentAttribute(), override, column, owner, textRangeResolver, new JoinTableTableDescriptionProvider()); |
| } |
| |
| public JptValidator buildJoinTableInverseJoinColumnValidator(AssociationOverride override, JoinColumn column, JoinColumn.Owner owner, JoinColumnTextRangeResolver textRangeResolver) { |
| return new AssociationOverrideInverseJoinColumnValidator(this.getPersistentAttribute(), override, column, owner, textRangeResolver, new JoinTableTableDescriptionProvider()); |
| } |
| |
| public JptValidator buildTableValidator(AssociationOverride override, Table table, TableTextRangeResolver textRangeResolver) { |
| return new AssociationOverrideJoinTableValidator(this.getPersistentAttribute(), override, (JoinTable) table, textRangeResolver); |
| } |
| |
| protected JavaPersistentAttribute getPersistentAttribute() { |
| return GenericJavaEmbeddedMapping.this.getPersistentAttribute(); |
| } |
| } |
| } |