| /******************************************************************************* |
| * 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.core.internal.context.orm; |
| |
| import java.util.ListIterator; |
| import java.util.Vector; |
| import org.eclipse.jpt.core.context.Entity; |
| import org.eclipse.jpt.core.context.JoinColumn; |
| import org.eclipse.jpt.core.context.JoinTable; |
| import org.eclipse.jpt.core.context.PersistentAttribute; |
| import org.eclipse.jpt.core.context.ReadOnlyBaseJoinColumn; |
| import org.eclipse.jpt.core.context.ReadOnlyJoinColumn; |
| import org.eclipse.jpt.core.context.RelationshipMapping; |
| import org.eclipse.jpt.core.context.TypeMapping; |
| import org.eclipse.jpt.core.context.orm.OrmVirtualJoinColumn; |
| import org.eclipse.jpt.core.context.orm.OrmVirtualJoinTable; |
| import org.eclipse.jpt.core.context.orm.OrmVirtualJoinTableRelationshipStrategy; |
| import org.eclipse.jpt.core.context.orm.OrmVirtualRelationship; |
| import org.eclipse.jpt.core.internal.context.ContextContainerTools; |
| import org.eclipse.jpt.core.internal.context.MappingTools; |
| import org.eclipse.jpt.utility.internal.CollectionTools; |
| import org.eclipse.jpt.utility.internal.iterables.EmptyListIterable; |
| import org.eclipse.jpt.utility.internal.iterables.ListIterable; |
| import org.eclipse.jpt.utility.internal.iterables.LiveCloneListIterable; |
| import org.eclipse.jpt.utility.internal.iterables.SingleElementListIterable; |
| |
| /** |
| * <code>orm.xml</code> virtual join table |
| */ |
| public class GenericOrmVirtualJoinTable |
| extends AbstractOrmVirtualReferenceTable<JoinTable> |
| implements OrmVirtualJoinTable |
| { |
| protected final JoinTable overriddenTable; |
| |
| protected final Vector<OrmVirtualJoinColumn> specifiedInverseJoinColumns = new Vector<OrmVirtualJoinColumn>(); |
| protected final SpecifiedInverseJoinColumnContainerAdapter specifiedInverseJoinColumnContainerAdapter = new SpecifiedInverseJoinColumnContainerAdapter(); |
| protected final ReadOnlyJoinColumn.Owner inverseJoinColumnOwner; |
| |
| protected OrmVirtualJoinColumn defaultInverseJoinColumn; |
| |
| |
| public GenericOrmVirtualJoinTable(OrmVirtualJoinTableRelationshipStrategy parent, JoinTable overriddenTable) { |
| super(parent); |
| this.overriddenTable = overriddenTable; |
| this.inverseJoinColumnOwner = this.buildInverseJoinColumnOwner(); |
| } |
| |
| |
| // ********** synchronize/update ********** |
| |
| @Override |
| public void update() { |
| super.update(); |
| this.updateSpecifiedInverseJoinColumns(); |
| this.updateDefaultInverseJoinColumn(); |
| } |
| |
| |
| // ********** table ********** |
| |
| @Override |
| public JoinTable getOverriddenTable() { |
| return this.overriddenTable; |
| } |
| |
| |
| // ********** inverse join columns ********** |
| |
| public ListIterator<OrmVirtualJoinColumn> inverseJoinColumns() { |
| return this.getInverseJoinColumns().iterator(); |
| } |
| |
| protected ListIterable<OrmVirtualJoinColumn> getInverseJoinColumns() { |
| return this.hasSpecifiedInverseJoinColumns() ? this.getSpecifiedInverseJoinColumns() : this.getDefaultInverseJoinColumns(); |
| } |
| |
| public int inverseJoinColumnsSize() { |
| return this.hasSpecifiedInverseJoinColumns() ? this.specifiedInverseJoinColumnsSize() : this.getDefaultInverseJoinColumnsSize(); |
| } |
| |
| |
| // ********** specified inverse join columns ********** |
| |
| public ListIterator<OrmVirtualJoinColumn> specifiedInverseJoinColumns() { |
| return this.getSpecifiedInverseJoinColumns().iterator(); |
| } |
| |
| protected ListIterable<OrmVirtualJoinColumn> getSpecifiedInverseJoinColumns() { |
| return new LiveCloneListIterable<OrmVirtualJoinColumn>(this.specifiedInverseJoinColumns); |
| } |
| |
| public int specifiedInverseJoinColumnsSize() { |
| return this.specifiedInverseJoinColumns.size(); |
| } |
| |
| public boolean hasSpecifiedInverseJoinColumns() { |
| return this.specifiedInverseJoinColumns.size() != 0; |
| } |
| |
| public OrmVirtualJoinColumn getSpecifiedInverseJoinColumn(int index) { |
| return this.specifiedInverseJoinColumns.get(index); |
| } |
| |
| protected void updateSpecifiedInverseJoinColumns() { |
| ContextContainerTools.update(this.specifiedInverseJoinColumnContainerAdapter); |
| } |
| |
| protected Iterable<JoinColumn> getOverriddenInverseJoinColumns() { |
| return CollectionTools.iterable(this.getOverriddenTable().specifiedInverseJoinColumns()); |
| } |
| |
| protected void moveSpecifiedInverseJoinColumn(int index, OrmVirtualJoinColumn joinColumn) { |
| this.moveItemInList(index, joinColumn, this.specifiedInverseJoinColumns, SPECIFIED_INVERSE_JOIN_COLUMNS_LIST); |
| } |
| |
| protected OrmVirtualJoinColumn addSpecifiedInverseJoinColumn(int index, JoinColumn joinColumn) { |
| OrmVirtualJoinColumn virtualJoinColumn = this.buildInverseJoinColumn(joinColumn); |
| this.addItemToList(index, virtualJoinColumn, this.specifiedInverseJoinColumns, SPECIFIED_INVERSE_JOIN_COLUMNS_LIST); |
| return virtualJoinColumn; |
| } |
| |
| protected void removeSpecifiedInverseJoinColumn(OrmVirtualJoinColumn joinColumn) { |
| this.removeItemFromList(joinColumn, this.specifiedInverseJoinColumns, SPECIFIED_INVERSE_JOIN_COLUMNS_LIST); |
| } |
| |
| /** |
| * specified inverse join column container adapter |
| */ |
| protected class SpecifiedInverseJoinColumnContainerAdapter |
| implements ContextContainerTools.Adapter<OrmVirtualJoinColumn, JoinColumn> |
| { |
| public Iterable<OrmVirtualJoinColumn> getContextElements() { |
| return GenericOrmVirtualJoinTable.this.getSpecifiedInverseJoinColumns(); |
| } |
| public Iterable<JoinColumn> getResourceElements() { |
| return GenericOrmVirtualJoinTable.this.getOverriddenInverseJoinColumns(); |
| } |
| public JoinColumn getResourceElement(OrmVirtualJoinColumn contextElement) { |
| return contextElement.getOverriddenColumn(); |
| } |
| public void moveContextElement(int index, OrmVirtualJoinColumn element) { |
| GenericOrmVirtualJoinTable.this.moveSpecifiedInverseJoinColumn(index, element); |
| } |
| public void addContextElement(int index, JoinColumn element) { |
| GenericOrmVirtualJoinTable.this.addSpecifiedInverseJoinColumn(index, element); |
| } |
| public void removeContextElement(OrmVirtualJoinColumn element) { |
| GenericOrmVirtualJoinTable.this.removeSpecifiedInverseJoinColumn(element); |
| } |
| } |
| |
| |
| // ********** default inverse join column ********** |
| |
| public OrmVirtualJoinColumn getDefaultInverseJoinColumn() { |
| return this.defaultInverseJoinColumn; |
| } |
| |
| protected void setDefaultInverseJoinColumn(OrmVirtualJoinColumn joinColumn) { |
| OrmVirtualJoinColumn old = this.defaultInverseJoinColumn; |
| this.defaultInverseJoinColumn = joinColumn; |
| this.firePropertyChanged(DEFAULT_INVERSE_JOIN_COLUMN, old, joinColumn); |
| } |
| |
| protected ListIterable<OrmVirtualJoinColumn> getDefaultInverseJoinColumns() { |
| return (this.defaultInverseJoinColumn != null) ? |
| new SingleElementListIterable<OrmVirtualJoinColumn>(this.defaultInverseJoinColumn) : |
| EmptyListIterable.<OrmVirtualJoinColumn>instance(); |
| } |
| |
| protected int getDefaultInverseJoinColumnsSize() { |
| return (this.defaultInverseJoinColumn == null) ? 0 : 1; |
| } |
| |
| protected void updateDefaultInverseJoinColumn() { |
| if (this.buildsDefaultInverseJoinColumn()) { |
| if (this.defaultInverseJoinColumn == null) { |
| this.setDefaultInverseJoinColumn(this.buildInverseJoinColumn(this.getOverriddenTable().getDefaultInverseJoinColumn())); |
| } else { |
| this.defaultInverseJoinColumn.update(); |
| } |
| } else { |
| this.setDefaultInverseJoinColumn(null); |
| } |
| } |
| |
| protected boolean buildsDefaultInverseJoinColumn() { |
| return ! this.hasSpecifiedInverseJoinColumns(); |
| } |
| |
| |
| // ********** misc ********** |
| |
| @Override |
| public OrmVirtualJoinTableRelationshipStrategy getParent() { |
| return (OrmVirtualJoinTableRelationshipStrategy) super.getParent(); |
| } |
| |
| protected OrmVirtualJoinTableRelationshipStrategy getJoinStrategy() { |
| return this.getParent(); |
| } |
| |
| @Override |
| protected ReadOnlyJoinColumn.Owner buildJoinColumnOwner() { |
| return new JoinColumnOwner(); |
| } |
| |
| protected ReadOnlyJoinColumn.Owner buildInverseJoinColumnOwner() { |
| return new InverseJoinColumnOwner(); |
| } |
| |
| protected OrmVirtualJoinColumn buildInverseJoinColumn(JoinColumn joinColumn) { |
| return this.buildJoinColumn(this.inverseJoinColumnOwner, joinColumn); |
| } |
| |
| @Override |
| protected String buildDefaultName() { |
| return this.getJoinStrategy().getJoinTableDefaultName(); |
| } |
| |
| public RelationshipMapping getRelationshipMapping() { |
| return this.getJoinStrategy().getRelationship().getMapping(); |
| } |
| |
| public PersistentAttribute getPersistentAttribute() { |
| return this.getRelationshipMapping().getPersistentAttribute(); |
| } |
| |
| |
| // ********** join column owners ********** |
| |
| /** |
| * just a little common behavior |
| */ |
| protected abstract class AbstractJoinColumnOwner |
| implements ReadOnlyJoinColumn.Owner |
| { |
| protected AbstractJoinColumnOwner() { |
| super(); |
| } |
| |
| public TypeMapping getTypeMapping() { |
| return this.getRelationship().getTypeMapping(); |
| } |
| |
| /** |
| * by default, the join column is, obviously, in the join table; |
| * not sure whether it can be anywhere else... |
| */ |
| public String getDefaultTableName() { |
| return GenericOrmVirtualJoinTable.this.getName(); |
| } |
| |
| /** |
| * @see MappingTools#buildJoinColumnDefaultName(org.eclipse.jpt.core.context.ReadOnlyJoinColumn, org.eclipse.jpt.core.context.ReadOnlyJoinColumn.Owner) |
| */ |
| public String getDefaultColumnName() { |
| throw new UnsupportedOperationException(); |
| } |
| |
| protected OrmVirtualRelationship getRelationship() { |
| return GenericOrmVirtualJoinTable.this.getJoinStrategy().getRelationship(); |
| } |
| } |
| |
| |
| /** |
| * 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(GenericOrmVirtualJoinTable.this.getRelationshipMapping()); |
| } |
| |
| public boolean joinColumnIsDefault(ReadOnlyBaseJoinColumn joinColumn) { |
| return GenericOrmVirtualJoinTable.this.defaultJoinColumn == joinColumn; |
| } |
| |
| public int joinColumnsSize() { |
| return GenericOrmVirtualJoinTable.this.joinColumnsSize(); |
| } |
| } |
| |
| |
| /** |
| * 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 = GenericOrmVirtualJoinTable.this.getRelationshipMapping(); |
| return (relationshipMapping == null) ? null : relationshipMapping.getResolvedTargetEntity(); |
| } |
| |
| public String getAttributeName() { |
| RelationshipMapping relationshipMapping = GenericOrmVirtualJoinTable.this.getRelationshipMapping(); |
| return (relationshipMapping == null) ? null : relationshipMapping.getName(); |
| } |
| |
| public boolean joinColumnIsDefault(ReadOnlyBaseJoinColumn joinColumn) { |
| return GenericOrmVirtualJoinTable.this.defaultInverseJoinColumn == joinColumn; |
| } |
| |
| public int joinColumnsSize() { |
| return GenericOrmVirtualJoinTable.this.inverseJoinColumnsSize(); |
| } |
| } |
| } |