| /******************************************************************************* |
| * 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.context.orm; |
| |
| import java.util.List; |
| 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.SingleElementListIterable; |
| import org.eclipse.jpt.common.utility.internal.iterables.SuperListIterableWrapper; |
| import org.eclipse.jpt.jpa.core.context.ReadOnlyJoinColumn; |
| import org.eclipse.jpt.jpa.core.context.ReadOnlyReferenceTable; |
| import org.eclipse.jpt.jpa.core.context.VirtualReferenceTable; |
| import org.eclipse.jpt.jpa.core.context.XmlContextNode; |
| import org.eclipse.jpt.jpa.core.context.orm.OrmReadOnlyJoinColumn; |
| import org.eclipse.jpt.jpa.core.context.orm.OrmVirtualJoinColumn; |
| import org.eclipse.wst.validation.internal.provisional.core.IMessage; |
| import org.eclipse.wst.validation.internal.provisional.core.IReporter; |
| |
| public abstract class AbstractOrmVirtualReferenceTable<T extends ReadOnlyReferenceTable> |
| extends AbstractOrmVirtualTable<T> |
| implements VirtualReferenceTable |
| { |
| protected final ContextListContainer<OrmVirtualJoinColumn, ReadOnlyJoinColumn> specifiedJoinColumnContainer; |
| protected final OrmReadOnlyJoinColumn.Owner joinColumnOwner; |
| |
| protected OrmVirtualJoinColumn defaultJoinColumn; |
| |
| |
| protected AbstractOrmVirtualReferenceTable(XmlContextNode parent, Owner owner, T overridenTable) { |
| super(parent, owner, overridenTable); |
| this.joinColumnOwner = this.buildJoinColumnOwner(); |
| this.specifiedJoinColumnContainer = this.buildSpecifiedJoinColumnContainer(); |
| } |
| |
| |
| // ********** synchronize/update ********** |
| |
| @Override |
| public void update() { |
| super.update(); |
| this.updateSpecifiedJoinColumns(); |
| this.updateDefaultJoinColumn(); |
| } |
| |
| |
| // ********** join columns ********** |
| |
| public ListIterable<OrmVirtualJoinColumn> getJoinColumns() { |
| return this.hasSpecifiedJoinColumns() ? this.getSpecifiedJoinColumns() : this.getDefaultJoinColumns(); |
| } |
| |
| public int getJoinColumnsSize() { |
| return this.hasSpecifiedJoinColumns() ? this.getSpecifiedJoinColumnsSize() : this.getDefaultJoinColumnsSize(); |
| } |
| |
| |
| // ********** specified join columns ********** |
| |
| public ListIterable<OrmVirtualJoinColumn> getSpecifiedJoinColumns() { |
| return this.specifiedJoinColumnContainer.getContextElements(); |
| } |
| |
| public int getSpecifiedJoinColumnsSize() { |
| return this.specifiedJoinColumnContainer.getContextElementsSize(); |
| } |
| |
| public boolean hasSpecifiedJoinColumns() { |
| return this.getSpecifiedJoinColumnsSize() != 0; |
| } |
| |
| public OrmVirtualJoinColumn getSpecifiedJoinColumn(int index) { |
| return this.specifiedJoinColumnContainer.getContextElement(index); |
| } |
| |
| protected void updateSpecifiedJoinColumns() { |
| this.specifiedJoinColumnContainer.update(); |
| } |
| |
| protected ListIterable<ReadOnlyJoinColumn> getOverriddenJoinColumns() { |
| return new SuperListIterableWrapper<ReadOnlyJoinColumn>(this.getOverriddenTable().getSpecifiedJoinColumns()); |
| } |
| |
| protected void moveSpecifiedJoinColumn(int index, OrmVirtualJoinColumn joinColumn) { |
| this.specifiedJoinColumnContainer.moveContextElement(index, joinColumn); |
| } |
| |
| protected OrmVirtualJoinColumn addSpecifiedJoinColumn(int index, ReadOnlyJoinColumn joinColumn) { |
| return this.specifiedJoinColumnContainer.addContextElement(index, joinColumn); |
| } |
| |
| protected void removeSpecifiedJoinColumn(OrmVirtualJoinColumn joinColumn) { |
| this.specifiedJoinColumnContainer.removeContextElement(joinColumn); |
| } |
| |
| protected ContextListContainer<OrmVirtualJoinColumn, ReadOnlyJoinColumn> buildSpecifiedJoinColumnContainer() { |
| return new SpecifiedJoinColumnContainer(); |
| } |
| |
| /** |
| * specified join column container |
| */ |
| protected class SpecifiedJoinColumnContainer |
| extends ContextListContainer<OrmVirtualJoinColumn, ReadOnlyJoinColumn> |
| { |
| @Override |
| protected String getContextElementsPropertyName() { |
| return SPECIFIED_JOIN_COLUMNS_LIST; |
| } |
| @Override |
| protected OrmVirtualJoinColumn buildContextElement(ReadOnlyJoinColumn resourceElement) { |
| return AbstractOrmVirtualReferenceTable.this.buildJoinColumn(resourceElement); |
| } |
| @Override |
| protected ListIterable<ReadOnlyJoinColumn> getResourceElements() { |
| return AbstractOrmVirtualReferenceTable.this.getOverriddenJoinColumns(); |
| } |
| @Override |
| protected ReadOnlyJoinColumn getResourceElement(OrmVirtualJoinColumn contextElement) { |
| return contextElement.getOverriddenColumn(); |
| } |
| } |
| |
| |
| // ********** default join column ********** |
| |
| public OrmVirtualJoinColumn getDefaultJoinColumn() { |
| return this.defaultJoinColumn; |
| } |
| |
| protected void setDefaultJoinColumn(OrmVirtualJoinColumn joinColumn) { |
| OrmVirtualJoinColumn old = this.defaultJoinColumn; |
| this.defaultJoinColumn = joinColumn; |
| this.firePropertyChanged(DEFAULT_JOIN_COLUMN_PROPERTY, old, joinColumn); |
| } |
| |
| protected ListIterable<OrmVirtualJoinColumn> getDefaultJoinColumns() { |
| return (this.defaultJoinColumn != null) ? |
| new SingleElementListIterable<OrmVirtualJoinColumn>(this.defaultJoinColumn) : |
| EmptyListIterable.<OrmVirtualJoinColumn>instance(); |
| } |
| |
| protected int getDefaultJoinColumnsSize() { |
| return (this.defaultJoinColumn == null) ? 0 : 1; |
| } |
| |
| protected void updateDefaultJoinColumn() { |
| if (this.buildsDefaultJoinColumn()) { |
| if (this.defaultJoinColumn == null) { |
| this.setDefaultJoinColumn(this.buildJoinColumn(this.getOverriddenTable().getDefaultJoinColumn())); |
| } else { |
| this.defaultJoinColumn.update(); |
| } |
| } else { |
| this.setDefaultJoinColumn(null); |
| } |
| } |
| |
| protected boolean buildsDefaultJoinColumn() { |
| return ! this.hasSpecifiedJoinColumns(); |
| } |
| |
| |
| // ********** misc ********** |
| |
| protected OrmVirtualJoinColumn buildJoinColumn(ReadOnlyJoinColumn joinColumn) { |
| return this.buildJoinColumn(this.joinColumnOwner, joinColumn); |
| } |
| |
| protected OrmVirtualJoinColumn buildJoinColumn(OrmReadOnlyJoinColumn.Owner columnOwner, ReadOnlyJoinColumn joinColumn) { |
| return this.getContextNodeFactory().buildOrmVirtualJoinColumn(this, columnOwner, joinColumn); |
| } |
| |
| protected abstract OrmReadOnlyJoinColumn.Owner buildJoinColumnOwner(); |
| |
| @Override |
| protected String buildDefaultSchema() { |
| return this.getContextDefaultSchema(); |
| } |
| |
| @Override |
| protected String buildDefaultCatalog() { |
| return this.getContextDefaultCatalog(); |
| } |
| |
| |
| // ********** validation ********** |
| |
| @Override |
| public void validate(List<IMessage> messages, IReporter reporter) { |
| boolean continueValidating = this.buildTableValidator().validate(messages, reporter); |
| |
| //join column validation will handle the check for whether to validate against the database |
| //some validation messages are not database specific. If the database validation for the |
| //table fails we will stop there and not validate the join columns at all |
| if (continueValidating) { |
| this.validateJoinColumns(messages, reporter); |
| } |
| } |
| |
| protected void validateJoinColumns(List<IMessage> messages, IReporter reporter) { |
| this.validateNodes(this.getJoinColumns(), messages, reporter); |
| } |
| } |