| /******************************************************************************* |
| * Copyright (c) 2007, 2012 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.orm; |
| |
| import java.util.List; |
| import org.eclipse.jpt.common.core.utility.TextRange; |
| import org.eclipse.jpt.common.utility.internal.Tools; |
| import org.eclipse.jpt.common.utility.internal.iterables.EmptyIterable; |
| import org.eclipse.jpt.common.utility.internal.iterables.EmptyListIterable; |
| import org.eclipse.jpt.common.utility.internal.iterables.ListIterable; |
| import org.eclipse.jpt.common.utility.internal.iterables.LiveCloneListIterable; |
| import org.eclipse.jpt.common.utility.internal.iterables.SingleElementListIterable; |
| import org.eclipse.jpt.jpa.core.context.Entity; |
| import org.eclipse.jpt.jpa.core.context.JoinColumn; |
| import org.eclipse.jpt.jpa.core.context.PersistentAttribute; |
| import org.eclipse.jpt.jpa.core.context.ReadOnlyJoinColumn; |
| import org.eclipse.jpt.jpa.core.context.ReadOnlyJoinTable; |
| import org.eclipse.jpt.jpa.core.context.ReadOnlyNamedColumn; |
| import org.eclipse.jpt.jpa.core.context.RelationshipMapping; |
| import org.eclipse.jpt.jpa.core.context.TypeMapping; |
| import org.eclipse.jpt.jpa.core.context.orm.OrmJoinColumn; |
| import org.eclipse.jpt.jpa.core.context.orm.OrmJoinTable; |
| import org.eclipse.jpt.jpa.core.context.orm.OrmJoinTableRelationshipStrategy; |
| import org.eclipse.jpt.jpa.core.context.orm.OrmReadOnlyJoinColumn; |
| import org.eclipse.jpt.jpa.core.context.orm.OrmRelationship; |
| 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.NamedColumnTextRangeResolver; |
| import org.eclipse.jpt.jpa.core.resource.orm.XmlJoinColumn; |
| import org.eclipse.jpt.jpa.core.resource.orm.XmlJoinTable; |
| import org.eclipse.wst.validation.internal.provisional.core.IMessage; |
| import org.eclipse.wst.validation.internal.provisional.core.IReporter; |
| |
| /** |
| * <code>orm.xml</code> join table |
| */ |
| public class GenericOrmJoinTable |
| extends GenericOrmReferenceTable<XmlJoinTable> |
| implements OrmJoinTable |
| { |
| protected final ContextListContainer<OrmJoinColumn, XmlJoinColumn> specifiedInverseJoinColumnContainer; |
| protected final OrmReadOnlyJoinColumn.Owner inverseJoinColumnOwner; |
| |
| protected OrmJoinColumn defaultInverseJoinColumn; |
| |
| |
| public GenericOrmJoinTable(OrmJoinTableRelationshipStrategy parent, Owner owner) { |
| super(parent, owner); |
| this.inverseJoinColumnOwner = this.buildInverseJoinColumnOwner(); |
| this.specifiedInverseJoinColumnContainer = this.buildSpecifiedInverseJoinColumnContainer(); |
| } |
| |
| @Override |
| protected OrmReadOnlyJoinColumn.Owner buildJoinColumnOwner() { |
| return new JoinColumnOwner(); |
| } |
| |
| |
| // ********** synchronize/update ********** |
| |
| @Override |
| public void synchronizeWithResourceModel() { |
| super.synchronizeWithResourceModel(); |
| this.syncSpecifiedInverseJoinColumns(); |
| if (this.defaultInverseJoinColumn != null) { |
| this.defaultInverseJoinColumn.synchronizeWithResourceModel(); |
| } |
| } |
| |
| @Override |
| public void update() { |
| super.update(); |
| this.updateNodes(this.getSpecifiedInverseJoinColumns()); |
| this.updateDefaultInverseJoinColumn(); |
| } |
| |
| |
| // ********** XML table ********** |
| |
| @Override |
| protected XmlJoinTable getXmlTable() { |
| return this.getRelationshipStrategy().getXmlJoinTable(); |
| } |
| |
| @Override |
| protected XmlJoinTable buildXmlTable() { |
| return this.getRelationshipStrategy().buildXmlJoinTable(); |
| } |
| |
| @Override |
| protected void removeXmlTable() { |
| this.getRelationshipStrategy().removeXmlJoinTable(); |
| } |
| |
| |
| // ********** inverse join columns ********** |
| |
| public ListIterable<OrmJoinColumn> getInverseJoinColumns() { |
| return this.hasSpecifiedInverseJoinColumns() ? this.getSpecifiedInverseJoinColumns() : this.getDefaultInverseJoinColumns(); |
| } |
| |
| public int getInverseJoinColumnsSize() { |
| return this.hasSpecifiedInverseJoinColumns() ? this.getSpecifiedInverseJoinColumnsSize() : this.getDefaultInverseJoinColumnsSize(); |
| } |
| |
| public void convertDefaultInverseJoinColumnToSpecified() { |
| MappingTools.convertJoinTableDefaultToSpecifiedInverseJoinColumn(this); |
| } |
| |
| |
| // ********** specified inverse join columns ********** |
| |
| public ListIterable<OrmJoinColumn> getSpecifiedInverseJoinColumns() { |
| return this.specifiedInverseJoinColumnContainer.getContextElements(); |
| } |
| |
| public int getSpecifiedInverseJoinColumnsSize() { |
| return this.specifiedInverseJoinColumnContainer.getContextElementsSize(); |
| } |
| |
| public boolean hasSpecifiedInverseJoinColumns() { |
| return this.getSpecifiedInverseJoinColumnsSize() != 0; |
| } |
| |
| public OrmJoinColumn getSpecifiedInverseJoinColumn(int index) { |
| return this.specifiedInverseJoinColumnContainer.getContextElement(index); |
| } |
| |
| public OrmJoinColumn addSpecifiedInverseJoinColumn() { |
| return this.addSpecifiedInverseJoinColumn(this.getSpecifiedInverseJoinColumnsSize()); |
| } |
| |
| public OrmJoinColumn addSpecifiedInverseJoinColumn(int index) { |
| XmlJoinTable xmlTable = this.getXmlTableForUpdate(); |
| XmlJoinColumn xmlJoinColumn = this.buildXmlJoinColumn(); |
| OrmJoinColumn joinColumn = this.specifiedInverseJoinColumnContainer.addContextElement(index, xmlJoinColumn); |
| xmlTable.getInverseJoinColumns().add(index, xmlJoinColumn); |
| return joinColumn; |
| } |
| |
| public void removeSpecifiedInverseJoinColumn(JoinColumn joinColumn) { |
| this.removeSpecifiedInverseJoinColumn(this.specifiedInverseJoinColumnContainer.indexOfContextElement((OrmJoinColumn) joinColumn)); |
| } |
| |
| public void removeSpecifiedInverseJoinColumn(int index) { |
| this.specifiedInverseJoinColumnContainer.removeContextElement(index); |
| this.getXmlTable().getInverseJoinColumns().remove(index); |
| this.removeXmlTableIfUnset(); |
| } |
| |
| public void moveSpecifiedInverseJoinColumn(int targetIndex, int sourceIndex) { |
| this.specifiedInverseJoinColumnContainer.moveContextElement(targetIndex, sourceIndex); |
| this.getXmlTable().getInverseJoinColumns().move(targetIndex, sourceIndex); |
| } |
| |
| public void clearSpecifiedInverseJoinColumns() { |
| this.specifiedInverseJoinColumnContainer.clearContextList(); |
| this.getXmlTable().getInverseJoinColumns().clear(); |
| } |
| |
| protected void syncSpecifiedInverseJoinColumns() { |
| this.specifiedInverseJoinColumnContainer.synchronizeWithResourceModel(); |
| } |
| |
| protected ListIterable<XmlJoinColumn> getXmlInverseJoinColumns() { |
| XmlJoinTable xmlTable = this.getXmlTable(); |
| return (xmlTable == null) ? |
| EmptyListIterable.<XmlJoinColumn>instance() : |
| // clone to reduce chance of concurrency problems |
| new LiveCloneListIterable<XmlJoinColumn>(xmlTable.getInverseJoinColumns()); |
| } |
| |
| protected ContextListContainer<OrmJoinColumn, XmlJoinColumn> buildSpecifiedInverseJoinColumnContainer() { |
| SpecifiedInverseJoinColumnContainer container = new SpecifiedInverseJoinColumnContainer(); |
| container.initialize(); |
| return container; |
| } |
| |
| /** |
| * specified inverse join column container |
| */ |
| protected class SpecifiedInverseJoinColumnContainer |
| extends ContextListContainer<OrmJoinColumn, XmlJoinColumn> |
| { |
| @Override |
| protected String getContextElementsPropertyName() { |
| return SPECIFIED_INVERSE_JOIN_COLUMNS_LIST; |
| } |
| @Override |
| protected OrmJoinColumn buildContextElement(XmlJoinColumn resourceElement) { |
| return GenericOrmJoinTable.this.buildInverseJoinColumn(resourceElement); |
| } |
| @Override |
| protected ListIterable<XmlJoinColumn> getResourceElements() { |
| return GenericOrmJoinTable.this.getXmlInverseJoinColumns(); |
| } |
| @Override |
| protected XmlJoinColumn getResourceElement(OrmJoinColumn contextElement) { |
| return contextElement.getXmlColumn(); |
| } |
| } |
| |
| protected OrmReadOnlyJoinColumn.Owner buildInverseJoinColumnOwner() { |
| return new InverseJoinColumnOwner(); |
| } |
| |
| |
| // ********** default inverse join column ********** |
| |
| public OrmJoinColumn getDefaultInverseJoinColumn() { |
| return this.defaultInverseJoinColumn; |
| } |
| |
| protected void setDefaultInverseJoinColumn(OrmJoinColumn joinColumn) { |
| OrmJoinColumn old = this.defaultInverseJoinColumn; |
| this.defaultInverseJoinColumn = joinColumn; |
| this.firePropertyChanged(DEFAULT_INVERSE_JOIN_COLUMN, old, joinColumn); |
| } |
| |
| protected ListIterable<OrmJoinColumn> getDefaultInverseJoinColumns() { |
| return (this.defaultInverseJoinColumn != null) ? |
| new SingleElementListIterable<OrmJoinColumn>(this.defaultInverseJoinColumn) : |
| EmptyListIterable.<OrmJoinColumn>instance(); |
| } |
| |
| protected int getDefaultInverseJoinColumnsSize() { |
| return (this.defaultInverseJoinColumn == null) ? 0 : 1; |
| } |
| |
| protected void updateDefaultInverseJoinColumn() { |
| if (this.buildsDefaultInverseJoinColumn()) { |
| if (this.defaultInverseJoinColumn == null) { |
| this.setDefaultInverseJoinColumn(this.buildInverseJoinColumn(null)); |
| } else { |
| this.defaultInverseJoinColumn.update(); |
| } |
| } else { |
| this.setDefaultInverseJoinColumn(null); |
| } |
| } |
| |
| protected boolean buildsDefaultInverseJoinColumn() { |
| return ! this.hasSpecifiedInverseJoinColumns(); |
| } |
| |
| |
| // ********** misc ********** |
| |
| @Override |
| public OrmJoinTableRelationshipStrategy getParent() { |
| return (OrmJoinTableRelationshipStrategy) super.getParent(); |
| } |
| |
| protected OrmJoinTableRelationshipStrategy getRelationshipStrategy() { |
| return this.getParent(); |
| } |
| |
| @Override |
| protected String buildDefaultName() { |
| return this.getRelationshipStrategy().getJoinTableDefaultName(); |
| } |
| |
| public void initializeFrom(ReadOnlyJoinTable oldTable) { |
| super.initializeFrom(oldTable); |
| for (ReadOnlyJoinColumn joinColumn : oldTable.getSpecifiedInverseJoinColumns()) { |
| this.addSpecifiedInverseJoinColumn().initializeFrom(joinColumn); |
| } |
| } |
| |
| public void initializeFromVirtual(ReadOnlyJoinTable virtualTable) { |
| super.initializeFromVirtual(virtualTable); |
| for (ReadOnlyJoinColumn joinColumn : virtualTable.getInverseJoinColumns()) { |
| this.addSpecifiedInverseJoinColumn().initializeFromVirtual(joinColumn); |
| } |
| } |
| |
| protected OrmJoinColumn buildInverseJoinColumn(XmlJoinColumn xmlJoinColumn) { |
| return this.getContextNodeFactory().buildOrmJoinColumn(this, this.inverseJoinColumnOwner, xmlJoinColumn); |
| } |
| |
| public RelationshipMapping getRelationshipMapping() { |
| return this.getRelationshipStrategy().getRelationship().getMapping(); |
| } |
| |
| public PersistentAttribute getPersistentAttribute() { |
| return this.getRelationshipMapping().getPersistentAttribute(); |
| } |
| |
| |
| // ********** validation ********** |
| |
| @Override |
| protected void validateJoinColumns(List<IMessage> messages, IReporter reporter) { |
| super.validateJoinColumns(messages, reporter); |
| this.validateNodes(this.getInverseJoinColumns(), messages, reporter); |
| } |
| |
| public boolean validatesAgainstDatabase() { |
| return this.getRelationshipStrategy().validatesAgainstDatabase(); |
| } |
| |
| // ********** completion proposals ********** |
| |
| @Override |
| public Iterable<String> getXmlCompletionProposals(int pos) { |
| Iterable<String> result = super.getXmlCompletionProposals(pos); |
| if (result != null) { |
| return result; |
| } |
| for (OrmJoinColumn column : this.getInverseJoinColumns()) { |
| result = column.getXmlCompletionProposals(pos); |
| if (result != null) { |
| return result; |
| } |
| } |
| return null; |
| } |
| |
| // ********** join column owners ********** |
| |
| /** |
| * just a little common behavior |
| */ |
| protected abstract class AbstractJoinColumnOwner |
| implements OrmReadOnlyJoinColumn.Owner |
| { |
| protected AbstractJoinColumnOwner() { |
| super(); |
| } |
| |
| public String getDefaultColumnName(ReadOnlyNamedColumn column) { |
| return MappingTools.buildJoinColumnDefaultName((ReadOnlyJoinColumn) column, this); |
| } |
| |
| /** |
| * If there is a specified table name it needs to be the same |
| * the default table name. The table is always the join table. |
| */ |
| public boolean tableNameIsInvalid(String tableName) { |
| return Tools.valuesAreDifferent(this.getDefaultTableName(), tableName); |
| } |
| |
| /** |
| * the join column can only be on the join table itself |
| */ |
| public Iterable<String> getCandidateTableNames() { |
| return EmptyIterable.instance(); |
| } |
| |
| public org.eclipse.jpt.jpa.db.Table resolveDbTable(String tableName) { |
| return Tools.valuesAreEqual(GenericOrmJoinTable.this.getName(), tableName) ? |
| GenericOrmJoinTable.this.getDbTable() : |
| null; |
| } |
| |
| /** |
| * by default, the join column is, obviously, in the join table; |
| * not sure whether it can be anywhere else... |
| */ |
| public String getDefaultTableName() { |
| return GenericOrmJoinTable.this.getName(); |
| } |
| |
| public TextRange getValidationTextRange() { |
| return GenericOrmJoinTable.this.getValidationTextRange(); |
| } |
| |
| protected OrmRelationship getRelationship() { |
| return this.getRelationshipStrategy().getRelationship(); |
| } |
| |
| protected OrmJoinTableRelationshipStrategy getRelationshipStrategy() { |
| return GenericOrmJoinTable.this.getRelationshipStrategy(); |
| } |
| } |
| |
| |
| /** |
| * owner for "back-pointer" join columns; |
| * these point at the source/owning entity |
| */ |
| protected class JoinColumnOwner |
| extends AbstractJoinColumnOwner |
| { |
| protected JoinColumnOwner() { |
| super(); |
| } |
| |
| public Entity getRelationshipTarget() { |
| return this.getRelationship().getEntity(); |
| } |
| |
| public String getAttributeName() { |
| return MappingTools.getTargetAttributeName(GenericOrmJoinTable.this.getRelationshipMapping()); |
| } |
| |
| @Override |
| public org.eclipse.jpt.jpa.db.Table resolveDbTable(String tableName) { |
| org.eclipse.jpt.jpa.db.Table dbTable = super.resolveDbTable(tableName); |
| return (dbTable != null) ? dbTable : this.getTypeMapping().resolveDbTable(tableName); |
| } |
| |
| public org.eclipse.jpt.jpa.db.Table getReferencedColumnDbTable() { |
| return this.getTypeMapping().getPrimaryDbTable(); |
| } |
| |
| protected TypeMapping getTypeMapping() { |
| return this.getRelationship().getTypeMapping(); |
| } |
| |
| public int getJoinColumnsSize() { |
| return GenericOrmJoinTable.this.getJoinColumnsSize(); |
| } |
| |
| public JptValidator buildColumnValidator(ReadOnlyNamedColumn column, NamedColumnTextRangeResolver textRangeResolver) { |
| return this.getRelationshipStrategy().buildJoinTableJoinColumnValidator((ReadOnlyJoinColumn) column, this, (JoinColumnTextRangeResolver) textRangeResolver); |
| } |
| } |
| |
| |
| /** |
| * owner for "forward-pointer" join columns; |
| * these point at the target/inverse entity |
| */ |
| protected class InverseJoinColumnOwner |
| extends AbstractJoinColumnOwner |
| { |
| protected InverseJoinColumnOwner() { |
| super(); |
| } |
| |
| public Entity getRelationshipTarget() { |
| RelationshipMapping relationshipMapping = GenericOrmJoinTable.this.getRelationshipMapping(); |
| return (relationshipMapping == null) ? null : relationshipMapping.getResolvedTargetEntity(); |
| } |
| |
| public String getAttributeName() { |
| RelationshipMapping relationshipMapping = GenericOrmJoinTable.this.getRelationshipMapping(); |
| return (relationshipMapping == null) ? null : relationshipMapping.getName(); |
| } |
| |
| @Override |
| public org.eclipse.jpt.jpa.db.Table resolveDbTable(String tableName) { |
| org.eclipse.jpt.jpa.db.Table dbTable = super.resolveDbTable(tableName); |
| if (dbTable != null) { |
| return dbTable; |
| } |
| Entity relationshipTarget = this.getRelationshipTarget(); |
| return (relationshipTarget == null) ? null : relationshipTarget.resolveDbTable(tableName); |
| } |
| |
| public org.eclipse.jpt.jpa.db.Table getReferencedColumnDbTable() { |
| Entity relationshipTarget = this.getRelationshipTarget(); |
| return (relationshipTarget == null) ? null : relationshipTarget.getPrimaryDbTable(); |
| } |
| |
| public int getJoinColumnsSize() { |
| return GenericOrmJoinTable.this.getInverseJoinColumnsSize(); |
| } |
| |
| public JptValidator buildColumnValidator(ReadOnlyNamedColumn column, NamedColumnTextRangeResolver textRangeResolver) { |
| return this.getRelationshipStrategy().buildJoinTableInverseJoinColumnValidator((ReadOnlyJoinColumn) column, this, (JoinColumnTextRangeResolver) textRangeResolver); |
| } |
| } |
| } |