| /******************************************************************************* |
| * Copyright (c) 2010, 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.List; |
| import org.eclipse.jdt.core.dom.CompilationUnit; |
| import org.eclipse.jpt.common.core.utility.TextRange; |
| import org.eclipse.jpt.jpa.core.context.Entity; |
| import org.eclipse.jpt.jpa.core.context.OverrideRelationship; |
| import org.eclipse.jpt.jpa.core.context.ReadOnlyBaseColumn; |
| import org.eclipse.jpt.jpa.core.context.ReadOnlyTableColumn.Owner; |
| import org.eclipse.jpt.jpa.core.context.ReadOnlyJoinColumn; |
| import org.eclipse.jpt.jpa.core.context.ReadOnlyJoinTable; |
| import org.eclipse.jpt.jpa.core.context.ReadOnlyRelationship; |
| import org.eclipse.jpt.jpa.core.context.Relationship; |
| import org.eclipse.jpt.jpa.core.context.RelationshipMapping; |
| import org.eclipse.jpt.jpa.core.context.TypeMapping; |
| import org.eclipse.jpt.jpa.core.context.java.JavaVirtualAssociationOverride; |
| import org.eclipse.jpt.jpa.core.context.java.JavaVirtualJoinColumnRelationshipStrategy; |
| import org.eclipse.jpt.jpa.core.context.java.JavaVirtualJoinTableRelationshipStrategy; |
| import org.eclipse.jpt.jpa.core.context.java.JavaVirtualRelationshipStrategy; |
| import org.eclipse.jpt.jpa.core.internal.context.TableColumnTextRangeResolver; |
| 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.TableTextRangeResolver; |
| import org.eclipse.jpt.jpa.core.internal.context.java.AbstractJavaJpaContextNode; |
| import org.eclipse.jpt.jpa.core.internal.jpa2.context.java.GenericJavaVirtualOverrideJoinTableRelationshipStrategy2_0; |
| import org.eclipse.jpt.jpa.core.jpa2.context.MappingRelationshipStrategy2_0; |
| import org.eclipse.jpt.jpa.core.jpa2.context.ReadOnlyAssociationOverride2_0; |
| import org.eclipse.jpt.jpa.core.jpa2.context.java.JavaVirtualOverrideRelationship2_0; |
| import org.eclipse.jpt.jpa.db.Table; |
| import org.eclipse.wst.validation.internal.provisional.core.IMessage; |
| import org.eclipse.wst.validation.internal.provisional.core.IReporter; |
| |
| public class GenericJavaVirtualOverrideRelationship |
| extends AbstractJavaJpaContextNode |
| implements JavaVirtualOverrideRelationship2_0 |
| { |
| protected JavaVirtualRelationshipStrategy strategy; |
| |
| protected final JavaVirtualJoinColumnRelationshipStrategy joinColumnStrategy; |
| |
| // JPA 2.0 |
| protected final JavaVirtualJoinTableRelationshipStrategy joinTableStrategy; |
| |
| |
| public GenericJavaVirtualOverrideRelationship(JavaVirtualAssociationOverride parent) { |
| super(parent); |
| this.joinColumnStrategy = this.buildJoinColumnStrategy(); |
| this.joinTableStrategy = this.buildJoinTableStrategy(); |
| } |
| |
| |
| // ********** synchronize/update ********** |
| |
| @Override |
| public void update() { |
| super.update(); |
| this.setStrategy(this.buildStrategy()); |
| this.joinColumnStrategy.update(); |
| this.joinTableStrategy.update(); |
| } |
| |
| |
| // ********** strategy ********** |
| |
| public JavaVirtualRelationshipStrategy getStrategy() { |
| return this.strategy; |
| } |
| |
| protected void setStrategy(JavaVirtualRelationshipStrategy strategy) { |
| JavaVirtualRelationshipStrategy old = this.strategy; |
| this.strategy = strategy; |
| this.firePropertyChanged(STRATEGY_PROPERTY, old, strategy); |
| } |
| |
| protected JavaVirtualRelationshipStrategy buildStrategy() { |
| return this.isJpa2_0Compatible() ? |
| this.buildStrategy2_0() : |
| this.joinColumnStrategy; |
| } |
| |
| /** |
| * The overridden mapping determines the override's strategy. |
| */ |
| protected JavaVirtualRelationshipStrategy buildStrategy2_0() { |
| MappingRelationshipStrategy2_0 mappingStrategy = this.getMappingStrategy(); |
| return (mappingStrategy != null) ? |
| (JavaVirtualRelationshipStrategy) mappingStrategy.selectOverrideStrategy(this) : |
| this.buildMissingMappingStrategy(); |
| } |
| |
| /** |
| * Get the strategy from the overridden mapping. |
| */ |
| protected MappingRelationshipStrategy2_0 getMappingStrategy() { |
| RelationshipMapping mapping = this.getMapping(); |
| return (mapping == null) ? null : (MappingRelationshipStrategy2_0) mapping.getRelationship().getStrategy(); |
| } |
| |
| /** |
| * Return the strategy to use when the override's name does not match the |
| * name of an appropriate relationship mapping. |
| */ |
| protected JavaVirtualRelationshipStrategy buildMissingMappingStrategy() { |
| return this.joinColumnStrategy.hasSpecifiedJoinColumns() ? |
| this.joinColumnStrategy : |
| this.joinTableStrategy; |
| } |
| |
| |
| // ********** join column strategy ********** |
| |
| public JavaVirtualJoinColumnRelationshipStrategy getJoinColumnStrategy() { |
| return this.joinColumnStrategy; |
| } |
| |
| public boolean strategyIsJoinColumn() { |
| return this.strategy == this.joinColumnStrategy; |
| } |
| |
| public boolean mayHaveDefaultJoinColumn() { |
| // association overrides do not have defaults |
| return false; |
| } |
| |
| protected JavaVirtualJoinColumnRelationshipStrategy buildJoinColumnStrategy() { |
| return new GenericJavaVirtualOverrideJoinColumnRelationshipStrategy(this); |
| } |
| |
| |
| // ********** join table strategy ********** |
| |
| public JavaVirtualJoinTableRelationshipStrategy getJoinTableStrategy() { |
| return this.joinTableStrategy; |
| } |
| |
| public boolean strategyIsJoinTable() { |
| return this.strategy == this.joinTableStrategy; |
| } |
| |
| public boolean mayHaveDefaultJoinTable() { |
| // association overrides do not have defaults |
| return false; |
| } |
| |
| protected JavaVirtualJoinTableRelationshipStrategy buildJoinTableStrategy() { |
| return new GenericJavaVirtualOverrideJoinTableRelationshipStrategy2_0(this); |
| } |
| |
| |
| // ********** conversions ********** |
| |
| public void initializeOn(Relationship newRelationship) { |
| newRelationship.initializeFromJoinTableRelationship(this); |
| newRelationship.initializeFromJoinColumnRelationship(this); |
| } |
| |
| public void initializeOnSpecified(OverrideRelationship specifiedRelationship) { |
| specifiedRelationship.initializeFromVirtualJoinColumnRelationship(this); |
| specifiedRelationship.initializeFromVirtualJoinTableRelationship(this); |
| } |
| |
| |
| // ********** misc ********** |
| |
| @Override |
| public JavaVirtualAssociationOverride getParent() { |
| return (JavaVirtualAssociationOverride) super.getParent(); |
| } |
| |
| protected JavaVirtualAssociationOverride getAssociationOverride() { |
| return this.getParent(); |
| } |
| |
| protected ReadOnlyAssociationOverride2_0 getAssociationOverride2_0() { |
| return (ReadOnlyAssociationOverride2_0) this.getAssociationOverride(); |
| } |
| |
| public TypeMapping getTypeMapping() { |
| return this.getAssociationOverride().getTypeMapping(); |
| } |
| |
| public String getAttributeName() { |
| return this.getAssociationOverride().getName(); |
| } |
| |
| public boolean tableNameIsInvalid(String tableName) { |
| return this.getAssociationOverride().tableNameIsInvalid(tableName); |
| } |
| |
| public Iterable<String> getCandidateTableNames() { |
| return this.getAssociationOverride().getCandidateTableNames(); |
| } |
| |
| public Table resolveDbTable(String tableName) { |
| return this.getAssociationOverride().resolveDbTable(tableName); |
| } |
| |
| public String getDefaultTableName() { |
| return this.getAssociationOverride().getDefaultTableName(); |
| } |
| |
| public JptValidator buildColumnValidator(ReadOnlyBaseColumn column, Owner owner, TableColumnTextRangeResolver textRangeResolver) { |
| return this.getAssociationOverride().buildColumnValidator(column, owner, textRangeResolver); |
| } |
| |
| public Entity getEntity() { |
| TypeMapping typeMapping = this.getTypeMapping(); |
| return (typeMapping instanceof Entity) ? (Entity) typeMapping : null; |
| } |
| |
| public boolean isVirtual() { |
| return true; |
| } |
| |
| public RelationshipMapping getMapping() { |
| return this.getAssociationOverride().getMapping(); |
| } |
| |
| public ReadOnlyRelationship resolveOverriddenRelationship() { |
| return this.getAssociationOverride().resolveOverriddenRelationship(); |
| } |
| |
| |
| // ********** validation ********** |
| |
| public TextRange getValidationTextRange(CompilationUnit astRoot) { |
| return this.getAssociationOverride().getValidationTextRange(astRoot); |
| } |
| |
| @Override |
| public void validate(List<IMessage> messages, IReporter reporter, CompilationUnit astRoot) { |
| super.validate(messages, reporter, astRoot); |
| this.strategy.validate(messages, reporter, astRoot); |
| } |
| |
| public JptValidator buildJoinTableValidator(ReadOnlyJoinTable table, TableTextRangeResolver textRangeResolver) { |
| return this.getAssociationOverride2_0().buildJoinTableValidator(table, textRangeResolver); |
| } |
| |
| public JptValidator buildJoinTableJoinColumnValidator(ReadOnlyJoinColumn column, ReadOnlyJoinColumn.Owner owner, JoinColumnTextRangeResolver textRangeResolver) { |
| return this.getAssociationOverride2_0().buildJoinTableJoinColumnValidator(column, owner, textRangeResolver); |
| } |
| |
| public JptValidator buildJoinTableInverseJoinColumnValidator(ReadOnlyJoinColumn column, ReadOnlyJoinColumn.Owner owner, JoinColumnTextRangeResolver textRangeResolver) { |
| return this.getAssociationOverride2_0().buildJoinTableInverseJoinColumnValidator(column, owner, textRangeResolver); |
| } |
| } |