| /******************************************************************************* |
| * Copyright (c) 2009, 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.context.java; |
| |
| import java.util.Iterator; |
| import org.eclipse.jdt.core.dom.CompilationUnit; |
| import org.eclipse.jpt.common.core.utility.TextRange; |
| import org.eclipse.jpt.common.utility.internal.iterators.SubIteratorWrapper; |
| import org.eclipse.jpt.common.utility.internal.iterators.SuperIteratorWrapper; |
| import org.eclipse.jpt.jpa.core.context.Entity; |
| import org.eclipse.jpt.jpa.core.context.JoinColumn; |
| import org.eclipse.jpt.jpa.core.context.NamedColumn; |
| import org.eclipse.jpt.jpa.core.context.PersistentAttribute; |
| import org.eclipse.jpt.jpa.core.context.ReadOnlyBaseJoinColumn; |
| import org.eclipse.jpt.jpa.core.context.RelationshipMapping; |
| import org.eclipse.jpt.jpa.core.context.TypeMapping; |
| import org.eclipse.jpt.jpa.core.context.java.JavaJoinColumn; |
| import org.eclipse.jpt.jpa.core.context.java.JavaMappingJoinColumnRelationship; |
| 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.NamedColumnTextRangeResolver; |
| import org.eclipse.jpt.jpa.core.internal.jpa1.context.EntityTableDescriptionProvider; |
| import org.eclipse.jpt.jpa.core.internal.jpa1.context.JoinColumnValidator; |
| import org.eclipse.jpt.jpa.core.internal.resource.java.NullJoinColumnAnnotation; |
| import org.eclipse.jpt.jpa.core.internal.validation.JpaValidationDescriptionMessages; |
| import org.eclipse.jpt.jpa.core.resource.java.JavaResourcePersistentAttribute; |
| import org.eclipse.jpt.jpa.core.resource.java.JoinColumnAnnotation; |
| import org.eclipse.jpt.jpa.core.resource.java.JoinColumnsAnnotation; |
| import org.eclipse.jpt.jpa.core.resource.java.NestableAnnotation; |
| import org.eclipse.jpt.jpa.core.resource.java.NestableJoinColumnAnnotation; |
| import org.eclipse.jpt.jpa.db.Table; |
| |
| public class GenericJavaMappingJoinColumnRelationshipStrategy |
| extends AbstractJavaJoinColumnRelationshipStrategy |
| { |
| protected final boolean targetForeignKey; |
| |
| |
| /** |
| * The default strategy is for a "source" foreign key. |
| */ |
| public GenericJavaMappingJoinColumnRelationshipStrategy(JavaMappingJoinColumnRelationship parent) { |
| this(parent, false); |
| } |
| |
| public GenericJavaMappingJoinColumnRelationshipStrategy(JavaMappingJoinColumnRelationship parent, boolean targetForeignKey) { |
| super(parent); |
| this.targetForeignKey = targetForeignKey; |
| } |
| |
| |
| // ********** join column annotations ********** |
| |
| @Override |
| protected Iterator<JoinColumnAnnotation> joinColumnAnnotations() { |
| return new SuperIteratorWrapper<JoinColumnAnnotation>(this.nestableJoinColumnAnnotations()); |
| } |
| |
| protected Iterator<NestableJoinColumnAnnotation> nestableJoinColumnAnnotations() { |
| return new SubIteratorWrapper<NestableAnnotation, NestableJoinColumnAnnotation>(this.nestableJoinColumnAnnotations_()); |
| } |
| |
| protected Iterator<NestableAnnotation> nestableJoinColumnAnnotations_() { |
| return this.getResourcePersistentAttribute().annotations(JoinColumnAnnotation.ANNOTATION_NAME, JoinColumnsAnnotation.ANNOTATION_NAME); |
| } |
| |
| @Override |
| protected JoinColumnAnnotation addJoinColumnAnnotation(int index) { |
| return (JoinColumnAnnotation) this.getResourcePersistentAttribute().addAnnotation(index, JoinColumnAnnotation.ANNOTATION_NAME, JoinColumnsAnnotation.ANNOTATION_NAME); |
| } |
| |
| @Override |
| protected void removeJoinColumnAnnotation(int index) { |
| this.getResourcePersistentAttribute().removeAnnotation(index, JoinColumnAnnotation.ANNOTATION_NAME, JoinColumnsAnnotation.ANNOTATION_NAME); |
| } |
| |
| @Override |
| protected void moveJoinColumnAnnotation(int targetIndex, int sourceIndex) { |
| this.getResourcePersistentAttribute().moveAnnotation(targetIndex, sourceIndex, JoinColumnsAnnotation.ANNOTATION_NAME); |
| } |
| |
| @Override |
| protected JoinColumnAnnotation buildNullJoinColumnAnnotation() { |
| return new NullJoinColumnAnnotation(this.getResourcePersistentAttribute()); |
| } |
| |
| |
| // ********** misc ********** |
| |
| protected JavaResourcePersistentAttribute getResourcePersistentAttribute() { |
| return this.getRelationship().getMapping().getResourcePersistentAttribute(); |
| } |
| |
| @Override |
| public JavaMappingJoinColumnRelationship getRelationship() { |
| return (JavaMappingJoinColumnRelationship) super.getRelationship(); |
| } |
| |
| @Override |
| protected JavaJoinColumn.Owner buildJoinColumnOwner() { |
| return new JoinColumnOwner(); |
| } |
| |
| public boolean isOverridable() { |
| return true; |
| } |
| |
| public TypeMapping getRelationshipSource() { |
| RelationshipMapping mapping = this.getRelationshipMapping(); |
| return this.targetForeignKey ? |
| mapping.getResolvedTargetEntity() : |
| mapping.getTypeMapping(); |
| } |
| |
| public TypeMapping getRelationshipTarget() { |
| RelationshipMapping mapping = this.getRelationshipMapping(); |
| return this.targetForeignKey ? |
| mapping.getTypeMapping() : |
| mapping.getResolvedTargetEntity(); |
| } |
| |
| protected Entity getRelationshipTargetEntity() { |
| TypeMapping target = this.getRelationshipTarget(); |
| return (target instanceof Entity) ? (Entity) target : null; |
| } |
| |
| public boolean isTargetForeignKey() { |
| return this.targetForeignKey; |
| } |
| |
| |
| // ********** validation ********** |
| |
| public String getColumnTableNotValidDescription() { |
| return JpaValidationDescriptionMessages.NOT_VALID_FOR_THIS_ENTITY; |
| } |
| |
| public TextRange getValidationTextRange(CompilationUnit astRoot) { |
| return this.getRelationship().getValidationTextRange(astRoot); |
| } |
| |
| |
| // ********** join column owner ********** |
| |
| protected class JoinColumnOwner |
| implements JavaJoinColumn.Owner |
| { |
| protected JoinColumnOwner() { |
| super(); |
| } |
| |
| /** |
| * by default, the join column is in the type mapping's primary table |
| */ |
| public String getDefaultTableName() { |
| return GenericJavaMappingJoinColumnRelationshipStrategy.this.getTableName(); |
| } |
| |
| public String getDefaultColumnName() { |
| //built in MappingTools.buildJoinColumnDefaultName() |
| return null; |
| } |
| |
| public String getAttributeName() { |
| return GenericJavaMappingJoinColumnRelationshipStrategy.this.getRelationshipMapping().getName(); |
| } |
| |
| protected PersistentAttribute getPersistentAttribute() { |
| return GenericJavaMappingJoinColumnRelationshipStrategy.this.getRelationshipMapping().getPersistentAttribute(); |
| } |
| |
| public TypeMapping getTypeMapping() { |
| return GenericJavaMappingJoinColumnRelationshipStrategy.this.getRelationshipSource(); |
| } |
| |
| public Entity getRelationshipTarget() { |
| return GenericJavaMappingJoinColumnRelationshipStrategy.this.getRelationshipTargetEntity(); |
| } |
| |
| public boolean tableNameIsInvalid(String tableName) { |
| return GenericJavaMappingJoinColumnRelationshipStrategy.this.tableNameIsInvalid(tableName); |
| } |
| |
| /** |
| * the join column can be on a secondary table |
| */ |
| public Iterator<String> candidateTableNames() { |
| return GenericJavaMappingJoinColumnRelationshipStrategy.this.candidateTableNames(); |
| } |
| |
| public Table resolveDbTable(String tableName) { |
| return GenericJavaMappingJoinColumnRelationshipStrategy.this.resolveDbTable(tableName); |
| } |
| |
| public Table getReferencedColumnDbTable() { |
| return GenericJavaMappingJoinColumnRelationshipStrategy.this.getReferencedColumnDbTable(); |
| } |
| |
| public boolean joinColumnIsDefault(ReadOnlyBaseJoinColumn joinColumn) { |
| return GenericJavaMappingJoinColumnRelationshipStrategy.this.defaultJoinColumn == joinColumn; |
| } |
| |
| public int joinColumnsSize() { |
| return GenericJavaMappingJoinColumnRelationshipStrategy.this.joinColumnsSize(); |
| } |
| |
| public TextRange getValidationTextRange(CompilationUnit astRoot) { |
| return GenericJavaMappingJoinColumnRelationshipStrategy.this.getValidationTextRange(astRoot); |
| } |
| |
| public JptValidator buildColumnValidator(NamedColumn column, NamedColumnTextRangeResolver textRangeResolver) { |
| return new JoinColumnValidator(this.getPersistentAttribute(), (JoinColumn) column, this, (JoinColumnTextRangeResolver) textRangeResolver, new EntityTableDescriptionProvider()); |
| } |
| } |
| } |