| /******************************************************************************* |
| * 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.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.jpa.core.context.Entity; |
| import org.eclipse.jpt.jpa.core.context.MappedByRelationship; |
| import org.eclipse.jpt.jpa.core.context.OverrideRelationship; |
| import org.eclipse.jpt.jpa.core.context.ReadOnlyJoinColumnRelationship; |
| import org.eclipse.jpt.jpa.core.context.ReadOnlyJoinTableRelationship; |
| import org.eclipse.jpt.jpa.core.context.ReadOnlyOverrideRelationship; |
| 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.JavaAssociationOverride; |
| import org.eclipse.jpt.jpa.core.context.java.JavaJoinColumnRelationshipStrategy; |
| import org.eclipse.jpt.jpa.core.context.java.JavaJoinTableRelationshipStrategy; |
| import org.eclipse.jpt.jpa.core.context.java.JavaRelationshipStrategy; |
| import org.eclipse.jpt.jpa.core.internal.context.java.AbstractJavaJpaContextNode; |
| import org.eclipse.jpt.jpa.core.internal.context.java.GenericJavaOverrideJoinColumnRelationshipStrategy; |
| import org.eclipse.jpt.jpa.core.internal.jpa2.context.java.GenericJavaOverrideJoinTableRelationshipStrategy2_0; |
| import org.eclipse.jpt.jpa.core.jpa2.context.java.JavaOverrideRelationship2_0; |
| import org.eclipse.wst.validation.internal.provisional.core.IMessage; |
| import org.eclipse.wst.validation.internal.provisional.core.IReporter; |
| |
| public class GenericJavaOverrideRelationship |
| extends AbstractJavaJpaContextNode |
| implements JavaOverrideRelationship2_0 |
| { |
| protected JavaRelationshipStrategy strategy; |
| |
| protected final JavaJoinColumnRelationshipStrategy joinColumnStrategy; |
| |
| // JPA 2.0 |
| protected final JavaJoinTableRelationshipStrategy joinTableStrategy; |
| |
| |
| public GenericJavaOverrideRelationship(JavaAssociationOverride parent) { |
| super(parent); |
| this.joinColumnStrategy = this.buildJoinColumnStrategy(); |
| this.joinTableStrategy = this.buildJoinTableStrategy(); |
| } |
| |
| |
| // ********** synchronize/update ********** |
| |
| @Override |
| public void synchronizeWithResourceModel() { |
| super.synchronizeWithResourceModel(); |
| this.joinColumnStrategy.synchronizeWithResourceModel(); |
| this.joinTableStrategy.synchronizeWithResourceModel(); |
| } |
| |
| @Override |
| public void update() { |
| super.update(); |
| this.setStrategy(this.buildStrategy()); |
| this.joinColumnStrategy.update(); |
| this.joinTableStrategy.update(); |
| } |
| |
| |
| // ********** strategy ********** |
| |
| public JavaRelationshipStrategy getStrategy() { |
| return this.strategy; |
| } |
| |
| protected void setStrategy(JavaRelationshipStrategy strategy) { |
| JavaRelationshipStrategy old = this.strategy; |
| this.strategy = strategy; |
| this.firePropertyChanged(STRATEGY_PROPERTY, old, strategy); |
| } |
| |
| protected JavaRelationshipStrategy buildStrategy() { |
| if (this.isJpa2_0Compatible()) { |
| if (this.joinColumnStrategy.hasSpecifiedJoinColumns()) { |
| return this.joinColumnStrategy; |
| } |
| return this.joinTableStrategy; |
| } |
| return this.joinColumnStrategy; |
| } |
| |
| |
| // ********** join column strategy ********** |
| |
| public JavaJoinColumnRelationshipStrategy getJoinColumnStrategy() { |
| return this.joinColumnStrategy; |
| } |
| |
| public boolean strategyIsJoinColumn() { |
| return this.strategy == this.joinColumnStrategy; |
| } |
| |
| public void setStrategyToJoinColumn() { |
| this.joinColumnStrategy.addStrategy(); |
| this.joinTableStrategy.removeStrategy(); |
| } |
| |
| public boolean mayHaveDefaultJoinColumn() { |
| return false; |
| } |
| |
| protected JavaJoinColumnRelationshipStrategy buildJoinColumnStrategy() { |
| return new GenericJavaOverrideJoinColumnRelationshipStrategy(this); |
| } |
| |
| |
| // ********** join table strategy ********** |
| |
| public JavaJoinTableRelationshipStrategy getJoinTableStrategy() { |
| return this.joinTableStrategy; |
| } |
| |
| public boolean strategyIsJoinTable() { |
| return this.strategy == this.joinTableStrategy; |
| } |
| |
| public void setStrategyToJoinTable() { |
| this.joinTableStrategy.addStrategy(); |
| this.joinColumnStrategy.removeStrategy(); |
| } |
| |
| public boolean mayHaveDefaultJoinTable() { |
| return this.isVirtual() && this.strategyIsJoinTable(); |
| } |
| |
| protected JavaJoinTableRelationshipStrategy buildJoinTableStrategy() { |
| return this.isJpa2_0Compatible() ? |
| new GenericJavaOverrideJoinTableRelationshipStrategy2_0(this) : |
| new NullJavaJoinTableRelationshipStrategy(this); |
| } |
| |
| |
| // ********** conversions ********** |
| |
| public void initializeFrom(ReadOnlyRelationship oldRelationship) { |
| oldRelationship.initializeOn(this); |
| } |
| |
| public void initializeOn(Relationship newRelationship) { |
| newRelationship.initializeFromJoinTableRelationship(this); |
| newRelationship.initializeFromJoinColumnRelationship(this); |
| } |
| |
| public void initializeFromMappedByRelationship(MappedByRelationship oldRelationship) { |
| // NOP |
| } |
| |
| public void initializeFromJoinTableRelationship(ReadOnlyJoinTableRelationship oldRelationship) { |
| this.joinTableStrategy.initializeFrom(oldRelationship.getJoinTableStrategy()); |
| } |
| |
| public void initializeFromJoinColumnRelationship(ReadOnlyJoinColumnRelationship oldRelationship) { |
| this.joinColumnStrategy.initializeFrom(oldRelationship.getJoinColumnStrategy()); |
| } |
| |
| public void initializeFromVirtual(ReadOnlyOverrideRelationship virtualRelationship) { |
| virtualRelationship.initializeOnSpecified(this); |
| } |
| |
| public void initializeOnSpecified(OverrideRelationship specifiedRelationship) { |
| throw new UnsupportedOperationException(); |
| } |
| |
| public void initializeFromVirtualJoinTableRelationship(ReadOnlyJoinTableRelationship virtualRelationship) { |
| this.joinTableStrategy.initializeFromVirtual(virtualRelationship.getJoinTableStrategy()); |
| } |
| |
| public void initializeFromVirtualJoinColumnRelationship(ReadOnlyJoinColumnRelationship virtualRelationship) { |
| this.joinColumnStrategy.initializeFromVirtual(virtualRelationship.getJoinColumnStrategy()); |
| } |
| |
| |
| // ********** misc ********** |
| |
| @Override |
| public JavaAssociationOverride getParent() { |
| return (JavaAssociationOverride) super.getParent(); |
| } |
| |
| public JavaAssociationOverride getAssociationOverride() { |
| return this.getParent(); |
| } |
| |
| public TypeMapping getTypeMapping() { |
| return this.getAssociationOverride().getContainer().getTypeMapping(); |
| } |
| |
| public Entity getEntity() { |
| TypeMapping typeMapping = this.getTypeMapping(); |
| return (typeMapping instanceof Entity) ? (Entity) typeMapping : null; |
| } |
| |
| public boolean isVirtual() { |
| return false; |
| } |
| |
| public RelationshipMapping getMapping() { |
| return this.getAssociationOverride().getMapping(); |
| } |
| |
| |
| // ********** 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.joinColumnStrategy.javaCompletionProposals(pos, filter, astRoot); |
| if (result != null) { |
| return result; |
| } |
| |
| return this.joinTableStrategy.javaCompletionProposals(pos, filter, astRoot); |
| } |
| |
| |
| // ********** 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.joinColumnStrategy.validate(messages, reporter, astRoot); |
| this.joinTableStrategy.validate(messages, reporter, astRoot); |
| } |
| } |