| /******************************************************************************* |
| * Copyright (c) 2010, 2015 Oracle. All rights reserved. |
| * This program and the accompanying materials are made available under the |
| * terms of the Eclipse Public License 2.0, which accompanies this distribution |
| * and is available at https://www.eclipse.org/legal/epl-2.0/. |
| * |
| * Contributors: |
| * Oracle - initial API and implementation |
| ******************************************************************************/ |
| package org.eclipse.jpt.jpa.core.internal.context.orm; |
| |
| import java.util.List; |
| import org.eclipse.core.runtime.IProgressMonitor; |
| import org.eclipse.jpt.common.core.utility.TextRange; |
| import org.eclipse.jpt.common.utility.internal.iterable.EmptyIterable; |
| import org.eclipse.jpt.common.utility.internal.iterable.EmptyListIterable; |
| import org.eclipse.jpt.common.utility.internal.iterable.SingleElementListIterable; |
| import org.eclipse.jpt.common.utility.internal.iterable.SuperListIterableWrapper; |
| import org.eclipse.jpt.common.utility.iterable.ListIterable; |
| import org.eclipse.jpt.jpa.core.context.BaseColumn; |
| import org.eclipse.jpt.jpa.core.context.Entity; |
| import org.eclipse.jpt.jpa.core.context.JoinColumn; |
| import org.eclipse.jpt.jpa.core.context.JoinColumnRelationship; |
| import org.eclipse.jpt.jpa.core.context.JoinColumnRelationshipStrategy; |
| import org.eclipse.jpt.jpa.core.context.JpaContextModel; |
| import org.eclipse.jpt.jpa.core.context.NamedColumn; |
| 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.VirtualJoinColumn; |
| import org.eclipse.jpt.jpa.core.context.VirtualJoinColumnRelationshipStrategy; |
| import org.eclipse.jpt.jpa.core.context.VirtualOverrideRelationship; |
| import org.eclipse.jpt.jpa.core.internal.context.JpaValidator; |
| import org.eclipse.jpt.jpa.core.internal.context.MappingTools; |
| 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 GenericOrmVirtualOverrideJoinColumnRelationshipStrategy |
| extends AbstractOrmXmlContextModel<VirtualOverrideRelationship> |
| implements VirtualJoinColumnRelationshipStrategy |
| { |
| protected final ContextListContainer<VirtualJoinColumn, JoinColumn> specifiedJoinColumnContainer; |
| protected final JoinColumn.ParentAdapter joinColumnParentAdapter; |
| |
| protected VirtualJoinColumn defaultJoinColumn; |
| |
| |
| public GenericOrmVirtualOverrideJoinColumnRelationshipStrategy(VirtualOverrideRelationship parent) { |
| super(parent); |
| this.joinColumnParentAdapter = this.buildJoinColumnParentAdapter(); |
| this.specifiedJoinColumnContainer = this.buildSpecifiedJoinColumnContainer(); |
| } |
| |
| |
| // ********** synchronize/update ********** |
| |
| @Override |
| public void update(IProgressMonitor monitor) { |
| super.update(monitor); |
| this.updateSpecifiedJoinColumns(monitor); |
| this.updateDefaultJoinColumn(monitor); |
| } |
| |
| |
| // ********** join columns ********** |
| |
| public ListIterable<VirtualJoinColumn> getJoinColumns() { |
| return this.hasSpecifiedJoinColumns() ? this.getSpecifiedJoinColumns() : this.getDefaultJoinColumns(); |
| } |
| |
| public int getJoinColumnsSize() { |
| return this.hasSpecifiedJoinColumns() ? this.getSpecifiedJoinColumnsSize() : this.getDefaultJoinColumnsSize(); |
| } |
| |
| |
| // ********** specified join columns ********** |
| |
| public ListIterable<VirtualJoinColumn> getSpecifiedJoinColumns() { |
| return this.specifiedJoinColumnContainer; |
| } |
| |
| public int getSpecifiedJoinColumnsSize() { |
| return this.specifiedJoinColumnContainer.size(); |
| } |
| |
| public boolean hasSpecifiedJoinColumns() { |
| return this.getSpecifiedJoinColumnsSize() != 0; |
| } |
| |
| public VirtualJoinColumn getSpecifiedJoinColumn(int index) { |
| return this.specifiedJoinColumnContainer.get(index); |
| } |
| |
| protected void updateSpecifiedJoinColumns(IProgressMonitor monitor) { |
| this.specifiedJoinColumnContainer.update(monitor); |
| } |
| |
| protected ListIterable<JoinColumn> getOverriddenSpecifiedJoinColumns() { |
| JoinColumnRelationshipStrategy overriddenStrategy = this.getOverriddenStrategy(); |
| return (overriddenStrategy == null) ? |
| EmptyListIterable.<JoinColumn>instance() : |
| new SuperListIterableWrapper<JoinColumn>(overriddenStrategy.getSpecifiedJoinColumns()); |
| } |
| |
| protected ContextListContainer<VirtualJoinColumn, JoinColumn> buildSpecifiedJoinColumnContainer() { |
| return this.buildVirtualContextListContainer(SPECIFIED_JOIN_COLUMNS_LIST, new SpecifiedJoinColumnContainerAdapter()); |
| } |
| |
| /** |
| * specified join column container adapter |
| */ |
| public class SpecifiedJoinColumnContainerAdapter |
| extends AbstractContainerAdapter<VirtualJoinColumn, JoinColumn> |
| { |
| public VirtualJoinColumn buildContextElement(JoinColumn resourceElement) { |
| return GenericOrmVirtualOverrideJoinColumnRelationshipStrategy.this.buildJoinColumn(resourceElement); |
| } |
| public ListIterable<JoinColumn> getResourceElements() { |
| return GenericOrmVirtualOverrideJoinColumnRelationshipStrategy.this.getOverriddenSpecifiedJoinColumns(); |
| } |
| public JoinColumn extractResourceElement(VirtualJoinColumn contextElement) { |
| return contextElement.getOverriddenColumn(); |
| } |
| } |
| |
| protected JoinColumn.ParentAdapter buildJoinColumnParentAdapter() { |
| return new JoinColumnParentAdapter(); |
| } |
| |
| |
| // ********** default join column ********** |
| |
| public VirtualJoinColumn getDefaultJoinColumn() { |
| return this.defaultJoinColumn; |
| } |
| |
| protected void setDefaultJoinColumn(VirtualJoinColumn joinColumn) { |
| VirtualJoinColumn old = this.defaultJoinColumn; |
| this.defaultJoinColumn = joinColumn; |
| this.firePropertyChanged(DEFAULT_JOIN_COLUMN_PROPERTY, old, joinColumn); |
| } |
| |
| protected ListIterable<VirtualJoinColumn> getDefaultJoinColumns() { |
| return (this.defaultJoinColumn != null) ? |
| new SingleElementListIterable<VirtualJoinColumn>(this.defaultJoinColumn) : |
| EmptyListIterable.<VirtualJoinColumn>instance(); |
| } |
| |
| protected int getDefaultJoinColumnsSize() { |
| return (this.defaultJoinColumn == null) ? 0 : 1; |
| } |
| |
| protected void updateDefaultJoinColumn(IProgressMonitor monitor) { |
| JoinColumn overriddenDefaultJoinColumn = this.getOverriddenDefaultJoinColumn(); |
| if (overriddenDefaultJoinColumn == null) { |
| if (this.defaultJoinColumn != null) { |
| this.setDefaultJoinColumn(null); |
| } |
| } else { |
| if ((this.defaultJoinColumn != null) && (this.defaultJoinColumn.getOverriddenColumn() == overriddenDefaultJoinColumn)) { |
| this.defaultJoinColumn.update(monitor); |
| } else { |
| this.setDefaultJoinColumn(this.buildJoinColumn(overriddenDefaultJoinColumn)); |
| } |
| } |
| } |
| |
| protected JoinColumn getOverriddenDefaultJoinColumn() { |
| JoinColumnRelationshipStrategy overriddenStrategy = this.getOverriddenStrategy(); |
| return (overriddenStrategy == null) ? null : overriddenStrategy.getDefaultJoinColumn(); |
| } |
| |
| |
| // ********** misc ********** |
| |
| public VirtualOverrideRelationship getRelationship() { |
| return this.parent; |
| } |
| |
| protected JoinColumnRelationshipStrategy getOverriddenStrategy() { |
| JoinColumnRelationship relationship = this.getOverriddenJoinColumnRelationship(); |
| return (relationship == null) ? null : relationship.getJoinColumnStrategy(); |
| } |
| |
| protected JoinColumnRelationship getOverriddenJoinColumnRelationship() { |
| Relationship relationship = this.resolveOverriddenRelationship(); |
| return (relationship instanceof JoinColumnRelationship) ? (JoinColumnRelationship) relationship : null; |
| } |
| |
| protected Relationship resolveOverriddenRelationship() { |
| return this.getRelationship().resolveOverriddenRelationship(); |
| } |
| |
| public boolean isTargetForeignKey() { |
| RelationshipMapping relationshipMapping = this.getRelationshipMapping(); |
| return (relationshipMapping != null) && |
| relationshipMapping.getRelationship().isTargetForeignKey(); |
| } |
| |
| public TypeMapping getRelationshipSource() { |
| return this.isTargetForeignKey() ? |
| // relationship mapping is not null |
| this.getRelationshipMapping().getResolvedTargetEntity() : |
| this.getRelationship().getTypeMapping(); |
| } |
| |
| public TypeMapping getRelationshipTarget() { |
| return this.isTargetForeignKey() ? |
| this.getRelationship().getTypeMapping() : |
| // relationship mapping may still be null |
| this.getRelationshipMappingTargetEntity(); |
| } |
| |
| protected TypeMapping getRelationshipMappingTargetEntity() { |
| RelationshipMapping mapping = this.getRelationshipMapping(); |
| return (mapping == null) ? null : mapping.getResolvedTargetEntity(); |
| } |
| |
| protected Entity getRelationshipTargetEntity() { |
| TypeMapping target = this.getRelationshipTarget(); |
| return (target instanceof Entity) ? (Entity) target : null; |
| } |
| |
| protected RelationshipMapping getRelationshipMapping() { |
| return this.getRelationship().getMapping(); |
| } |
| |
| public String getTableName() { |
| return this.isTargetForeignKey() ? |
| this.getTargetDefaultTableName() : |
| this.getRelationship().getDefaultTableName(); |
| } |
| |
| protected String getTargetDefaultTableName() { |
| TypeMapping typeMapping = this.getRelationshipMapping().getResolvedTargetEntity(); |
| return (typeMapping == null) ? null : typeMapping.getPrimaryTableName(); |
| } |
| |
| protected Table resolveDbTable(String tableName) { |
| return this.isTargetForeignKey() ? |
| this.resolveTargetDbTable(tableName) : |
| this.getRelationship().resolveDbTable(tableName); |
| } |
| |
| protected Table resolveTargetDbTable(String tableName) { |
| TypeMapping typeMapping = this.getRelationshipMapping().getResolvedTargetEntity(); |
| return (typeMapping == null) ? null : typeMapping.resolveDbTable(tableName); |
| } |
| |
| protected boolean tableNameIsInvalid(String tableName) { |
| return this.isTargetForeignKey() ? |
| this.targetTableNameIsInvalid(tableName) : |
| this.getRelationship().tableNameIsInvalid(tableName); |
| } |
| |
| protected boolean targetTableNameIsInvalid(String tableName) { |
| TypeMapping typeMapping = this.getRelationshipMapping().getResolvedTargetEntity(); |
| return (typeMapping != null) && typeMapping.tableNameIsInvalid(tableName); |
| } |
| |
| protected Table getReferencedColumnDbTable() { |
| TypeMapping relationshipTarget = this.getRelationshipTarget(); |
| return (relationshipTarget == null) ? null : relationshipTarget.getPrimaryDbTable(); |
| } |
| |
| protected Iterable<String> getCandidateTableNames() { |
| return this.isTargetForeignKey() ? |
| this.getTargetCandidateTableNames() : |
| this.getRelationship().getCandidateTableNames(); |
| } |
| |
| protected Iterable<String> getTargetCandidateTableNames() { |
| TypeMapping typeMapping = this.getRelationshipMapping().getResolvedTargetEntity(); |
| return (typeMapping != null) ? typeMapping.getAllAssociatedTableNames() : EmptyIterable.<String>instance(); |
| } |
| |
| public TextRange getValidationTextRange() { |
| return this.getRelationship().getValidationTextRange(); |
| } |
| |
| protected String getAttributeName() { |
| return this.getRelationship().getAttributeName(); |
| } |
| |
| protected VirtualJoinColumn buildJoinColumn(JoinColumn overriddenJoinColumn) { |
| return this.getContextModelFactory().buildOrmVirtualJoinColumn(this.joinColumnParentAdapter, overriddenJoinColumn); |
| } |
| |
| |
| // ********** validation ********** |
| |
| @Override |
| public void validate(List<IMessage> messages, IReporter reporter) { |
| super.validate(messages, reporter); |
| for (VirtualJoinColumn joinColumn : this.getJoinColumns()) { |
| joinColumn.validate(messages, reporter); |
| } |
| } |
| |
| |
| // ********** join column parent adapter ********** |
| |
| public class JoinColumnParentAdapter |
| implements JoinColumn.ParentAdapter |
| { |
| public JpaContextModel getColumnParent() { |
| return GenericOrmVirtualOverrideJoinColumnRelationshipStrategy.this; |
| } |
| |
| public String getDefaultTableName() { |
| return GenericOrmVirtualOverrideJoinColumnRelationshipStrategy.this.getTableName(); |
| } |
| |
| public String getDefaultColumnName(NamedColumn column) { |
| return MappingTools.buildJoinColumnDefaultName((JoinColumn) column, this); |
| } |
| |
| public String getAttributeName() { |
| return GenericOrmVirtualOverrideJoinColumnRelationshipStrategy.this.getAttributeName(); |
| } |
| |
| public Entity getRelationshipTarget() { |
| return GenericOrmVirtualOverrideJoinColumnRelationshipStrategy.this.getRelationshipTargetEntity(); |
| } |
| |
| public boolean tableNameIsInvalid(String tableName) { |
| return GenericOrmVirtualOverrideJoinColumnRelationshipStrategy.this.tableNameIsInvalid(tableName); |
| } |
| |
| public Iterable<String> getCandidateTableNames() { |
| return GenericOrmVirtualOverrideJoinColumnRelationshipStrategy.this.getCandidateTableNames(); |
| } |
| |
| public Table resolveDbTable(String tableName) { |
| return GenericOrmVirtualOverrideJoinColumnRelationshipStrategy.this.resolveDbTable(tableName); |
| } |
| |
| public Table getReferencedColumnDbTable() { |
| return GenericOrmVirtualOverrideJoinColumnRelationshipStrategy.this.getReferencedColumnDbTable(); |
| } |
| |
| public int getJoinColumnsSize() { |
| return GenericOrmVirtualOverrideJoinColumnRelationshipStrategy.this.getJoinColumnsSize(); |
| } |
| |
| public TextRange getValidationTextRange() { |
| return GenericOrmVirtualOverrideJoinColumnRelationshipStrategy.this.getValidationTextRange(); |
| } |
| |
| public JpaValidator buildColumnValidator(NamedColumn column) { |
| return GenericOrmVirtualOverrideJoinColumnRelationshipStrategy.this.getRelationship().buildColumnValidator((BaseColumn) column, this); |
| } |
| } |
| } |