| /******************************************************************************* |
| * 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.java; |
| |
| import java.util.List; |
| import org.eclipse.jdt.core.dom.CompilationUnit; |
| import org.eclipse.jpt.common.utility.Filter; |
| 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.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.java.JavaJoinColumn; |
| import org.eclipse.jpt.jpa.core.context.java.JavaJpaContextNode; |
| import org.eclipse.jpt.jpa.core.context.java.JavaReadOnlyJoinColumn; |
| import org.eclipse.jpt.jpa.core.context.java.JavaReferenceTable; |
| import org.eclipse.jpt.jpa.core.internal.context.MappingTools; |
| import org.eclipse.jpt.jpa.core.internal.context.java.AbstractJavaTable; |
| import org.eclipse.jpt.jpa.core.internal.resource.java.NullJoinColumnAnnotation; |
| import org.eclipse.jpt.jpa.core.resource.java.JoinColumnAnnotation; |
| import org.eclipse.jpt.jpa.core.resource.java.ReferenceTableAnnotation; |
| import org.eclipse.wst.validation.internal.provisional.core.IMessage; |
| import org.eclipse.wst.validation.internal.provisional.core.IReporter; |
| |
| /** |
| * Java join table or collection table |
| */ |
| public abstract class GenericJavaReferenceTable<A extends ReferenceTableAnnotation> |
| extends AbstractJavaTable<A> |
| implements JavaReferenceTable |
| { |
| protected final ContextListContainer<JavaJoinColumn, JoinColumnAnnotation> specifiedJoinColumnContainer; |
| protected final JavaReadOnlyJoinColumn.Owner joinColumnOwner; |
| |
| protected JavaJoinColumn defaultJoinColumn; |
| |
| |
| protected GenericJavaReferenceTable(JavaJpaContextNode 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<JavaJoinColumn> 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<JavaJoinColumn> getSpecifiedJoinColumns() { |
| return this.specifiedJoinColumnContainer.getContextElements(); |
| } |
| |
| public int getSpecifiedJoinColumnsSize() { |
| return this.specifiedJoinColumnContainer.getContextElementsSize(); |
| } |
| |
| public boolean hasSpecifiedJoinColumns() { |
| return this.getSpecifiedJoinColumnsSize() != 0; |
| } |
| |
| public JavaJoinColumn getSpecifiedJoinColumn(int index) { |
| return this.specifiedJoinColumnContainer.getContextElement(index); |
| } |
| |
| public JavaJoinColumn addSpecifiedJoinColumn() { |
| return this.addSpecifiedJoinColumn(this.getSpecifiedJoinColumnsSize()); |
| } |
| |
| public JavaJoinColumn addSpecifiedJoinColumn(int index) { |
| JoinColumnAnnotation annotation = this.getTableAnnotation().addJoinColumn(index); |
| return this.specifiedJoinColumnContainer.addContextElement(index, annotation); |
| } |
| |
| public void removeSpecifiedJoinColumn(JoinColumn joinColumn) { |
| this.removeSpecifiedJoinColumn(this.specifiedJoinColumnContainer.indexOfContextElement((JavaJoinColumn) joinColumn)); |
| } |
| |
| public void removeSpecifiedJoinColumn(int index) { |
| this.getTableAnnotation().removeJoinColumn(index); |
| this.removeTableAnnotationIfUnset(); |
| this.specifiedJoinColumnContainer.removeContextElement(index); |
| } |
| |
| public void moveSpecifiedJoinColumn(int targetIndex, int sourceIndex) { |
| this.getTableAnnotation().moveJoinColumn(targetIndex, sourceIndex); |
| this.specifiedJoinColumnContainer.moveContextElement(targetIndex, sourceIndex); |
| } |
| |
| protected void syncSpecifiedJoinColumns() { |
| this.specifiedJoinColumnContainer.synchronizeWithResourceModel(); |
| } |
| |
| protected ListIterable<JoinColumnAnnotation> getJoinColumnAnnotations() { |
| return this.getTableAnnotation().getJoinColumns(); |
| } |
| |
| |
| /** |
| * join column container |
| */ |
| protected class SpecifiedJoinColumnContainer |
| extends ContextListContainer<JavaJoinColumn, JoinColumnAnnotation> |
| { |
| @Override |
| protected String getContextElementsPropertyName() { |
| return SPECIFIED_JOIN_COLUMNS_LIST; |
| } |
| @Override |
| protected JavaJoinColumn buildContextElement(JoinColumnAnnotation resourceElement) { |
| return GenericJavaReferenceTable.this.buildJoinColumn(resourceElement); |
| } |
| @Override |
| protected ListIterable<JoinColumnAnnotation> getResourceElements() { |
| return GenericJavaReferenceTable.this.getJoinColumnAnnotations(); |
| } |
| @Override |
| protected JoinColumnAnnotation getResourceElement(JavaJoinColumn contextElement) { |
| return (JoinColumnAnnotation) contextElement.getColumnAnnotation(); |
| } |
| } |
| |
| protected abstract JavaReadOnlyJoinColumn.Owner buildJoinColumnOwner(); |
| |
| protected ContextListContainer<JavaJoinColumn, JoinColumnAnnotation> buildSpecifiedJoinColumnContainer(){ |
| SpecifiedJoinColumnContainer container = new SpecifiedJoinColumnContainer(); |
| container.initialize(); |
| return container; |
| } |
| |
| // ********** default join column ********** |
| |
| public JavaJoinColumn getDefaultJoinColumn() { |
| return this.defaultJoinColumn; |
| } |
| |
| protected void setDefaultJoinColumn(JavaJoinColumn joinColumn) { |
| JavaJoinColumn old = this.defaultJoinColumn; |
| this.defaultJoinColumn = joinColumn; |
| this.firePropertyChanged(DEFAULT_JOIN_COLUMN_PROPERTY, old, joinColumn); |
| } |
| |
| protected ListIterable<JavaJoinColumn> getDefaultJoinColumns() { |
| return (this.defaultJoinColumn != null) ? |
| new SingleElementListIterable<JavaJoinColumn>(this.defaultJoinColumn) : |
| EmptyListIterable.<JavaJoinColumn>instance(); |
| } |
| |
| protected int getDefaultJoinColumnsSize() { |
| return (this.defaultJoinColumn == null) ? 0 : 1; |
| } |
| |
| protected void updateDefaultJoinColumn() { |
| if (this.buildsDefaultJoinColumn()) { |
| if (this.defaultJoinColumn == null) { |
| this.setDefaultJoinColumn(this.buildJoinColumn(new NullJoinColumnAnnotation(this.getTableAnnotation()))); |
| } 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 JavaJoinColumn buildJoinColumn(JoinColumnAnnotation joinColumnAnnotation) { |
| return this.buildJoinColumn(this.joinColumnOwner, joinColumnAnnotation); |
| } |
| |
| protected JavaJoinColumn buildJoinColumn(JavaReadOnlyJoinColumn.Owner jcOwner, JoinColumnAnnotation joinColumnAnnotation) { |
| return this.getJpaFactory().buildJavaJoinColumn(this, jcOwner, joinColumnAnnotation); |
| } |
| |
| @Override |
| protected String buildDefaultSchema() { |
| return this.getContextDefaultSchema(); |
| } |
| |
| @Override |
| protected String buildDefaultCatalog() { |
| return this.getContextDefaultCatalog(); |
| } |
| |
| |
| // ********** Java completion proposals ********** |
| |
| @Override |
| public Iterable<String> getJavaCompletionProposals(int pos, Filter<String> filter, CompilationUnit astRoot) { |
| Iterable<String> result = super.getJavaCompletionProposals(pos, filter, astRoot); |
| if (result != null) { |
| return result; |
| } |
| for (JavaJoinColumn column : this.getJoinColumns()) { |
| result = column.getJavaCompletionProposals(pos, filter, astRoot); |
| if (result != null) { |
| return result; |
| } |
| } |
| return null; |
| } |
| |
| |
| // ********** validation ********** |
| |
| @Override |
| public void validate(List<IMessage> messages, IReporter reporter, CompilationUnit astRoot) { |
| boolean continueValidating = this.buildTableValidator(astRoot).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, astRoot); |
| } |
| } |
| |
| protected void validateJoinColumns(List<IMessage> messages, IReporter reporter, CompilationUnit astRoot) { |
| this.validateNodes(this.getJoinColumns(), messages, reporter, astRoot); |
| } |
| } |
| |