| /******************************************************************************* |
| * Copyright (c) 2007, 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.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.LiveCloneListIterable; |
| import org.eclipse.jpt.common.utility.internal.iterables.SingleElementListIterable; |
| import org.eclipse.jpt.jpa.core.context.JoinColumn; |
| import org.eclipse.jpt.jpa.core.context.ReadOnlyJoinColumn; |
| import org.eclipse.jpt.jpa.core.context.ReadOnlyReferenceTable; |
| import org.eclipse.jpt.jpa.core.context.XmlContextNode; |
| import org.eclipse.jpt.jpa.core.context.orm.OrmJoinColumn; |
| import org.eclipse.jpt.jpa.core.context.orm.OrmReadOnlyJoinColumn; |
| import org.eclipse.jpt.jpa.core.context.orm.OrmReferenceTable; |
| import org.eclipse.jpt.jpa.core.internal.context.MappingTools; |
| import org.eclipse.jpt.jpa.core.internal.context.orm.AbstractOrmTable; |
| import org.eclipse.jpt.jpa.core.resource.orm.AbstractXmlReferenceTable; |
| import org.eclipse.jpt.jpa.core.resource.orm.OrmFactory; |
| import org.eclipse.jpt.jpa.core.resource.orm.XmlJoinColumn; |
| import org.eclipse.wst.validation.internal.provisional.core.IMessage; |
| import org.eclipse.wst.validation.internal.provisional.core.IReporter; |
| |
| /** |
| * <code>orm.xml</code> join table or collection table |
| */ |
| public abstract class GenericOrmReferenceTable<X extends AbstractXmlReferenceTable> |
| extends AbstractOrmTable<X> |
| implements OrmReferenceTable |
| { |
| protected final ContextListContainer<OrmJoinColumn, XmlJoinColumn> specifiedJoinColumnContainer; |
| protected final OrmReadOnlyJoinColumn.Owner joinColumnOwner; |
| |
| protected OrmJoinColumn defaultJoinColumn; |
| |
| |
| protected GenericOrmReferenceTable(XmlContextNode parent, Owner owner) { |
| super(parent, owner); |
| this.joinColumnOwner = this.buildJoinColumnOwner(); |
| this.specifiedJoinColumnContainer = this.buildSpecifiedJoinColumnContainer(); |
| } |
| |
| |
| // ********** synchronize/update ********** |
| |
| @Override |
| public void synchronizeWithResourceModel() { |
| super.synchronizeWithResourceModel(); |
| this.syncSpecifiedJoinColumns(); |
| } |
| |
| @Override |
| public void update() { |
| super.update(); |
| this.updateNodes(this.getSpecifiedJoinColumns()); |
| this.updateDefaultJoinColumn(); |
| } |
| |
| |
| // ********** join columns ********** |
| |
| public ListIterable<OrmJoinColumn> getJoinColumns() { |
| return this.hasSpecifiedJoinColumns() ? this.getSpecifiedJoinColumns() : this.getDefaultJoinColumns(); |
| } |
| |
| public int getJoinColumnsSize() { |
| return this.hasSpecifiedJoinColumns() ? this.getSpecifiedJoinColumnsSize() : this.getDefaultJoinColumnsSize(); |
| } |
| |
| public void convertDefaultJoinColumnToSpecified() { |
| MappingTools.convertReferenceTableDefaultToSpecifiedJoinColumn(this); |
| } |
| |
| |
| // ********** specified join columns ********** |
| |
| public ListIterable<OrmJoinColumn> getSpecifiedJoinColumns() { |
| return this.specifiedJoinColumnContainer.getContextElements(); |
| } |
| |
| public int getSpecifiedJoinColumnsSize() { |
| return this.specifiedJoinColumnContainer.getContextElementsSize(); |
| } |
| |
| public boolean hasSpecifiedJoinColumns() { |
| return this.getSpecifiedJoinColumnsSize() != 0; |
| } |
| |
| public OrmJoinColumn getSpecifiedJoinColumn(int index) { |
| return this.specifiedJoinColumnContainer.getContextElement(index); |
| } |
| |
| public OrmJoinColumn addSpecifiedJoinColumn() { |
| return this.addSpecifiedJoinColumn(this.getSpecifiedJoinColumnsSize()); |
| } |
| |
| public OrmJoinColumn addSpecifiedJoinColumn(int index) { |
| X xmlTable = this.getXmlTableForUpdate(); |
| XmlJoinColumn xmlJoinColumn = this.buildXmlJoinColumn(); |
| OrmJoinColumn joinColumn = this.specifiedJoinColumnContainer.addContextElement(index, xmlJoinColumn); |
| xmlTable.getJoinColumns().add(index, xmlJoinColumn); |
| return joinColumn; |
| } |
| |
| protected XmlJoinColumn buildXmlJoinColumn() { |
| return OrmFactory.eINSTANCE.createXmlJoinColumn(); |
| } |
| |
| public void removeSpecifiedJoinColumn(JoinColumn joinColumn) { |
| this.removeSpecifiedJoinColumn(this.specifiedJoinColumnContainer.indexOfContextElement((OrmJoinColumn) joinColumn)); |
| } |
| |
| public void removeSpecifiedJoinColumn(int index) { |
| this.specifiedJoinColumnContainer.removeContextElement(index); |
| this.getXmlTable().getJoinColumns().remove(index); |
| this.removeXmlTableIfUnset(); |
| } |
| |
| public void moveSpecifiedJoinColumn(int targetIndex, int sourceIndex) { |
| this.specifiedJoinColumnContainer.moveContextElement(targetIndex, sourceIndex); |
| this.getXmlTable().getJoinColumns().move(targetIndex, sourceIndex); |
| } |
| |
| protected void syncSpecifiedJoinColumns() { |
| this.specifiedJoinColumnContainer.synchronizeWithResourceModel(); |
| } |
| |
| protected ListIterable<XmlJoinColumn> getXmlJoinColumns() { |
| X xmlTable = this.getXmlTable(); |
| return (xmlTable == null) ? |
| EmptyListIterable.<XmlJoinColumn>instance() : |
| // clone to reduce chance of concurrency problems |
| new LiveCloneListIterable<XmlJoinColumn>(xmlTable.getJoinColumns()); |
| } |
| |
| protected ContextListContainer<OrmJoinColumn, XmlJoinColumn> buildSpecifiedJoinColumnContainer() { |
| SpecifiedJoinColumnContainer container = new SpecifiedJoinColumnContainer(); |
| container.initialize(); |
| return container; |
| } |
| |
| /** |
| * specified join column container |
| */ |
| protected class SpecifiedJoinColumnContainer |
| extends ContextListContainer<OrmJoinColumn, XmlJoinColumn> |
| { |
| @Override |
| protected String getContextElementsPropertyName() { |
| return SPECIFIED_JOIN_COLUMNS_LIST; |
| } |
| @Override |
| protected OrmJoinColumn buildContextElement(XmlJoinColumn resourceElement) { |
| return GenericOrmReferenceTable.this.buildJoinColumn(resourceElement); |
| } |
| @Override |
| protected ListIterable<XmlJoinColumn> getResourceElements() { |
| return GenericOrmReferenceTable.this.getXmlJoinColumns(); |
| } |
| @Override |
| protected XmlJoinColumn getResourceElement(OrmJoinColumn contextElement) { |
| return contextElement.getXmlColumn(); |
| } |
| } |
| |
| protected abstract OrmReadOnlyJoinColumn.Owner buildJoinColumnOwner(); |
| |
| |
| // ********** default join column ********** |
| |
| public OrmJoinColumn getDefaultJoinColumn() { |
| return this.defaultJoinColumn; |
| } |
| |
| protected void setDefaultJoinColumn(OrmJoinColumn joinColumn) { |
| OrmJoinColumn old = this.defaultJoinColumn; |
| this.defaultJoinColumn = joinColumn; |
| this.firePropertyChanged(DEFAULT_JOIN_COLUMN_PROPERTY, old, joinColumn); |
| } |
| |
| protected ListIterable<OrmJoinColumn> getDefaultJoinColumns() { |
| return (this.defaultJoinColumn != null) ? |
| new SingleElementListIterable<OrmJoinColumn>(this.defaultJoinColumn) : |
| EmptyListIterable.<OrmJoinColumn>instance(); |
| } |
| |
| protected int getDefaultJoinColumnsSize() { |
| return (this.defaultJoinColumn == null) ? 0 : 1; |
| } |
| |
| protected void updateDefaultJoinColumn() { |
| if (this.buildsDefaultJoinColumn()) { |
| if (this.defaultJoinColumn == null) { |
| this.setDefaultJoinColumn(this.buildJoinColumn(null)); |
| } else { |
| this.defaultJoinColumn.update(); |
| } |
| } else { |
| this.setDefaultJoinColumn(null); |
| } |
| } |
| |
| protected boolean buildsDefaultJoinColumn() { |
| return ! this.hasSpecifiedJoinColumns(); |
| } |
| |
| |
| // ********** misc ********** |
| |
| protected void initializeFrom(ReadOnlyReferenceTable oldTable) { |
| super.initializeFrom(oldTable); |
| for (ReadOnlyJoinColumn joinColumn : oldTable.getSpecifiedJoinColumns()) { |
| this.addSpecifiedJoinColumn().initializeFrom(joinColumn); |
| } |
| } |
| |
| protected void initializeFromVirtual(ReadOnlyReferenceTable virtualTable) { |
| super.initializeFromVirtual(virtualTable); |
| for (ReadOnlyJoinColumn joinColumn : virtualTable.getJoinColumns()) { |
| this.addSpecifiedJoinColumn().initializeFromVirtual(joinColumn); |
| } |
| } |
| |
| protected OrmJoinColumn buildJoinColumn(XmlJoinColumn xmlJoinColumn) { |
| return this.getContextNodeFactory().buildOrmJoinColumn(this, this.joinColumnOwner, xmlJoinColumn); |
| } |
| |
| @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); |
| } |
| } |