blob: d9c2c680b8e291ce433d34b436d1269f84cb10f4 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2007, 2010 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.jpa1.context.java;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Vector;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jpt.core.context.BaseColumn;
import org.eclipse.jpt.core.context.BaseJoinColumn;
import org.eclipse.jpt.core.context.Entity;
import org.eclipse.jpt.core.context.JoinColumn;
import org.eclipse.jpt.core.context.NamedColumn;
import org.eclipse.jpt.core.context.PersistentAttribute;
import org.eclipse.jpt.core.context.RelationshipMapping;
import org.eclipse.jpt.core.context.TypeMapping;
import org.eclipse.jpt.core.context.java.JavaJoinColumn;
import org.eclipse.jpt.core.context.java.JavaJoinTable;
import org.eclipse.jpt.core.context.java.JavaJoinTableJoiningStrategy;
import org.eclipse.jpt.core.internal.context.MappingTools;
import org.eclipse.jpt.core.internal.resource.java.NullJoinColumnAnnotation;
import org.eclipse.jpt.core.internal.validation.DefaultJpaValidationMessages;
import org.eclipse.jpt.core.internal.validation.JpaValidationDescriptionMessages;
import org.eclipse.jpt.core.internal.validation.JpaValidationMessages;
import org.eclipse.jpt.core.resource.java.JoinColumnAnnotation;
import org.eclipse.jpt.core.resource.java.JoinTableAnnotation;
import org.eclipse.jpt.core.utility.TextRange;
import org.eclipse.jpt.utility.Filter;
import org.eclipse.jpt.utility.internal.CollectionTools;
import org.eclipse.jpt.utility.internal.StringTools;
import org.eclipse.jpt.utility.internal.iterators.CloneListIterator;
import org.eclipse.jpt.utility.internal.iterators.EmptyIterator;
import org.eclipse.jpt.utility.internal.iterators.EmptyListIterator;
import org.eclipse.jpt.utility.internal.iterators.SingleElementListIterator;
import org.eclipse.wst.validation.internal.provisional.core.IMessage;
import org.eclipse.wst.validation.internal.provisional.core.IReporter;
/**
* Java join table
*/
public class GenericJavaJoinTable
extends GenericJavaReferenceTable
implements JavaJoinTable
{
protected JavaJoinColumn defaultInverseJoinColumn;
protected final Vector<JavaJoinColumn> specifiedInverseJoinColumns = new Vector<JavaJoinColumn>();
protected final JavaJoinColumn.Owner inverseJoinColumnOwner;
public GenericJavaJoinTable(JavaJoinTableJoiningStrategy parent) {
super(parent);
this.inverseJoinColumnOwner = this.buildInverseJoinColumnOwner();
}
@Override
protected JavaJoinColumn.Owner buildJoinColumnOwner() {
return new JoinColumnOwner();
}
protected JavaJoinColumn.Owner buildInverseJoinColumnOwner() {
return new InverseJoinColumnOwner();
}
public RelationshipMapping getRelationshipMapping() {
return this.getParent().getRelationshipReference().getRelationshipMapping();
}
public PersistentAttribute getPersistentAttribute() {
return getRelationshipMapping().getPersistentAttribute();
}
public void initialize(JoinTableAnnotation joinTable) {
super.initialize(joinTable);
this.initializeSpecifiedInverseJoinColumns(joinTable);
this.initializeDefaultInverseJoinColumn(joinTable);
}
public void update(JoinTableAnnotation joinTable) {
super.update(joinTable);
this.updateSpecifiedInverseJoinColumns(joinTable);
this.updateDefaultInverseJoinColumn(joinTable);
}
// ********** AbstractJavaTable implementation **********
@Override
public JavaJoinTableJoiningStrategy getParent() {
return (JavaJoinTableJoiningStrategy) super.getParent();
}
@Override
protected String getAnnotationName() {
return JoinTableAnnotation.ANNOTATION_NAME;
}
@Override
protected String buildDefaultName() {
return this.getParent().getJoinTableDefaultName();
}
@Override
protected JoinTableAnnotation getAnnotation() {
return this.getParent().getAnnotation();
}
// ********** inverse join columns **********
public ListIterator<JavaJoinColumn> inverseJoinColumns() {
return this.hasSpecifiedInverseJoinColumns() ? this.specifiedInverseJoinColumns() : this.defaultInverseJoinColumns();
}
public int inverseJoinColumnsSize() {
return this.hasSpecifiedInverseJoinColumns() ? this.specifiedInverseJoinColumnsSize() : this.defaultInverseJoinColumnsSize();
}
public void convertDefaultToSpecifiedInverseJoinColumn() {
MappingTools.convertJoinTableDefaultToSpecifiedInverseJoinColumn(this);
}
protected JavaJoinColumn buildInverseJoinColumn(JoinColumnAnnotation joinColumnAnnotation) {
return this.buildJoinColumn(joinColumnAnnotation, this.inverseJoinColumnOwner);
}
// ********** default inverse join column **********
public JavaJoinColumn getDefaultInverseJoinColumn() {
return this.defaultInverseJoinColumn;
}
protected void setDefaultInverseJoinColumn(JavaJoinColumn defaultInverseJoinColumn) {
JavaJoinColumn old = this.defaultInverseJoinColumn;
this.defaultInverseJoinColumn = defaultInverseJoinColumn;
this.firePropertyChanged(DEFAULT_INVERSE_JOIN_COLUMN, old, defaultInverseJoinColumn);
}
protected ListIterator<JavaJoinColumn> defaultInverseJoinColumns() {
if (this.defaultInverseJoinColumn != null) {
return new SingleElementListIterator<JavaJoinColumn>(this.defaultInverseJoinColumn);
}
return EmptyListIterator.instance();
}
protected int defaultInverseJoinColumnsSize() {
return (this.defaultInverseJoinColumn == null) ? 0 : 1;
}
protected void initializeDefaultInverseJoinColumn(JoinTableAnnotation joinTableAnnotation) {
if (this.shouldBuildDefaultInverseJoinColumn()) {
this.defaultInverseJoinColumn = this.buildInverseJoinColumn(new NullJoinColumnAnnotation(joinTableAnnotation));
}
}
protected boolean shouldBuildDefaultInverseJoinColumn() {
return ! this.hasSpecifiedInverseJoinColumns();
}
protected void updateDefaultInverseJoinColumn(JoinTableAnnotation joinTableAnnotation) {
if (this.shouldBuildDefaultInverseJoinColumn()) {
if (this.defaultInverseJoinColumn == null) {
this.setDefaultInverseJoinColumn(this.buildInverseJoinColumn(new NullJoinColumnAnnotation(joinTableAnnotation)));
} else {
this.defaultInverseJoinColumn.update(new NullJoinColumnAnnotation(joinTableAnnotation));
}
} else {
this.setDefaultInverseJoinColumn(null);
}
}
// ********** specified inverse join columns **********
public ListIterator<JavaJoinColumn> specifiedInverseJoinColumns() {
return new CloneListIterator<JavaJoinColumn>(this.specifiedInverseJoinColumns);
}
public int specifiedInverseJoinColumnsSize() {
return this.specifiedInverseJoinColumns.size();
}
public boolean hasSpecifiedInverseJoinColumns() {
return this.specifiedInverseJoinColumns.size() != 0;
}
public JavaJoinColumn addSpecifiedInverseJoinColumn(int index) {
// Clear out the default now so it doesn't get removed during an update and
// cause change notifications to be sent to the UI in the wrong order.
// If the default is already null, nothing will happen.
JoinColumn oldDefault = this.defaultInverseJoinColumn;
this.defaultInverseJoinColumn = null;
JavaJoinColumn inverseJoinColumn = this.getJpaFactory().buildJavaJoinColumn(this, this.inverseJoinColumnOwner);
this.specifiedInverseJoinColumns.add(index, inverseJoinColumn);
JoinTableAnnotation joinTableAnnotation = this.getAnnotation();
JoinColumnAnnotation joinColumnAnnotation = joinTableAnnotation.addInverseJoinColumn(index);
inverseJoinColumn.initialize(joinColumnAnnotation);
this.fireItemAdded(SPECIFIED_INVERSE_JOIN_COLUMNS_LIST, index, inverseJoinColumn);
this.firePropertyChanged(DEFAULT_INVERSE_JOIN_COLUMN, oldDefault, null);
return inverseJoinColumn;
}
protected void addSpecifiedInverseJoinColumn(int index, JavaJoinColumn inverseJoinColumn) {
this.addItemToList(index, inverseJoinColumn, this.specifiedInverseJoinColumns, SPECIFIED_INVERSE_JOIN_COLUMNS_LIST);
}
protected void addSpecifiedInverseJoinColumn(JavaJoinColumn inverseJoinColumn) {
this.addSpecifiedInverseJoinColumn(this.specifiedInverseJoinColumns.size(), inverseJoinColumn);
}
public void removeSpecifiedInverseJoinColumn(JoinColumn inverseJoinColumn) {
this.removeSpecifiedInverseJoinColumn(this.specifiedInverseJoinColumns.indexOf(inverseJoinColumn));
}
public void removeSpecifiedInverseJoinColumn(int index) {
JavaJoinColumn removedJoinColumn = this.specifiedInverseJoinColumns.remove(index);
if ( ! this.hasSpecifiedInverseJoinColumns()) {
//create the defaultJoinColumn now or this will happen during project update
//after removing the join column from the resource model. That causes problems
//in the UI because the change notifications end up in the wrong order.
this.defaultInverseJoinColumn = this.buildInverseJoinColumn(new NullJoinColumnAnnotation(this.getAnnotation()));
}
this.getAnnotation().removeInverseJoinColumn(index);
this.fireItemRemoved(SPECIFIED_INVERSE_JOIN_COLUMNS_LIST, index, removedJoinColumn);
if (this.defaultInverseJoinColumn != null) {
//fire change notification if a defaultJoinColumn was created above
this.firePropertyChanged(DEFAULT_INVERSE_JOIN_COLUMN, null, this.defaultInverseJoinColumn);
}
}
protected void removeSpecifiedInverseJoinColumn_(JavaJoinColumn joinColumn) {
this.removeItemFromList(joinColumn, this.specifiedInverseJoinColumns, SPECIFIED_INVERSE_JOIN_COLUMNS_LIST);
}
public void moveSpecifiedInverseJoinColumn(int targetIndex, int sourceIndex) {
CollectionTools.move(this.specifiedInverseJoinColumns, targetIndex, sourceIndex);
this.getAnnotation().moveInverseJoinColumn(targetIndex, sourceIndex);
this.fireItemMoved(SPECIFIED_INVERSE_JOIN_COLUMNS_LIST, targetIndex, sourceIndex);
}
public void clearSpecifiedInverseJoinColumns() {
// for now, we have to remove annotations one at a time...
for (int i = this.specifiedInverseJoinColumns.size(); i-- > 0; ) {
this.removeSpecifiedInverseJoinColumn(i);
}
}
protected void initializeSpecifiedInverseJoinColumns(JoinTableAnnotation joinTableAnnotation) {
for (ListIterator<JoinColumnAnnotation> stream = joinTableAnnotation.inverseJoinColumns(); stream.hasNext(); ) {
this.specifiedInverseJoinColumns.add(this.buildInverseJoinColumn(stream.next()));
}
}
protected void updateSpecifiedInverseJoinColumns(JoinTableAnnotation joinTableAnnotation) {
ListIterator<JavaJoinColumn> joinColumns = this.specifiedInverseJoinColumns();
ListIterator<JoinColumnAnnotation> joinColumnAnnotations = joinTableAnnotation.inverseJoinColumns();
while (joinColumns.hasNext()) {
JavaJoinColumn joinColumn = joinColumns.next();
if (joinColumnAnnotations.hasNext()) {
joinColumn.update(joinColumnAnnotations.next());
} else {
this.removeSpecifiedInverseJoinColumn_(joinColumn);
}
}
while (joinColumnAnnotations.hasNext()) {
this.addSpecifiedInverseJoinColumn(this.buildInverseJoinColumn(joinColumnAnnotations.next()));
}
}
// ********** Java completion proposals **********
@Override
public Iterator<String> javaCompletionProposals(int pos, Filter<String> filter, CompilationUnit astRoot) {
Iterator<String> result = super.javaCompletionProposals(pos, filter, astRoot);
if (result != null) {
return result;
}
for (JavaJoinColumn column : CollectionTools.iterable(this.inverseJoinColumns())) {
result = column.javaCompletionProposals(pos, filter, astRoot);
if (result != null) {
return result;
}
}
return null;
}
// ********** validation **********
@Override
protected void validateJoinColumns(List<IMessage> messages, IReporter reporter, CompilationUnit astRoot) {
super.validateJoinColumns(messages, reporter, astRoot);
this.validateJoinColumns(this.inverseJoinColumns(), messages, reporter, astRoot);
}
@Override
protected boolean shouldValidateAgainstDatabase() {
return getParent().shouldValidateAgainstDatabase();
}
@Override
protected String getUnresolvedCatalogMessageId() {
return JpaValidationMessages.JOIN_TABLE_UNRESOLVED_CATALOG;
}
@Override
protected String getUnresolvedSchemaMessageId() {
return JpaValidationMessages.JOIN_TABLE_UNRESOLVED_SCHEMA;
}
@Override
protected String getUnresolvedNameMessageId() {
return JpaValidationMessages.JOIN_TABLE_UNRESOLVED_NAME;
}
// ********** join column owner adapters **********
/**
* just a little common behavior
*/
protected abstract class AbstractJoinColumnOwner
implements JavaJoinColumn.Owner
{
protected AbstractJoinColumnOwner() {
super();
}
public TypeMapping getTypeMapping() {
return GenericJavaJoinTable.this.getParent().getRelationshipReference().getTypeMapping();
}
public PersistentAttribute getPersistentAttribute() {
return GenericJavaJoinTable.this.getPersistentAttribute();
}
/**
* 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 !StringTools.stringsAreEqual(getDefaultTableName(), tableName);
}
/**
* the join column can only be on the join table itself
*/
public Iterator<String> candidateTableNames() {
return EmptyIterator.instance();
}
public org.eclipse.jpt.db.Table getDbTable(String tableName) {
if (GenericJavaJoinTable.this.getName() == null) {
return null;
}
return (GenericJavaJoinTable.this.getName().equals(tableName)) ? GenericJavaJoinTable.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 GenericJavaJoinTable.this.getName();
}
public TextRange getValidationTextRange(CompilationUnit astRoot) {
return GenericJavaJoinTable.this.getValidationTextRange(astRoot);
}
public IMessage buildTableNotValidMessage(BaseColumn column, TextRange textRange) {
return DefaultJpaValidationMessages.buildMessage(
IMessage.HIGH_SEVERITY,
this.getTableNotValidMessage(),
new String[] {
column.getTable(),
column.getName(),
JpaValidationDescriptionMessages.DOES_NOT_MATCH_JOIN_TABLE},
column,
textRange
);
}
protected abstract String getTableNotValidMessage();
public IMessage buildUnresolvedNameMessage(NamedColumn column, TextRange textRange) {
return DefaultJpaValidationMessages.buildMessage(
IMessage.HIGH_SEVERITY,
this.getUnresolvedNameMessage(),
new String[] {column.getName(), column.getDbTable().getName()},
column,
textRange
);
}
protected abstract String getUnresolvedNameMessage();
public IMessage buildUnresolvedReferencedColumnNameMessage(BaseJoinColumn column, TextRange textRange) {
return DefaultJpaValidationMessages.buildMessage(
IMessage.HIGH_SEVERITY,
this.getUnresolvedReferencedColumnNameMessage(),
new String[] {column.getReferencedColumnName(), column.getReferencedColumnDbTable().getName()},
column,
textRange
);
}
protected abstract String getUnresolvedReferencedColumnNameMessage();
public IMessage buildUnspecifiedNameMultipleJoinColumnsMessage(BaseJoinColumn column, TextRange textRange) {
return DefaultJpaValidationMessages.buildMessage(
IMessage.HIGH_SEVERITY,
this.getUnspecifiedNameMultipleJoinColumnsMessage(),
new String[0],
column,
textRange
);
}
protected abstract String getUnspecifiedNameMultipleJoinColumnsMessage();
public IMessage buildUnspecifiedReferencedColumnNameMultipleJoinColumnsMessage(BaseJoinColumn column, TextRange textRange) {
return DefaultJpaValidationMessages.buildMessage(
IMessage.HIGH_SEVERITY,
this.getUnspecifiedReferencedColumnNameMultipleJoinColumnsMessage(),
new String[0],
column,
textRange
);
}
protected abstract String getUnspecifiedReferencedColumnNameMultipleJoinColumnsMessage();
}
/**
* owner for "back-pointer" JoinColumns;
* these point at the source/owning entity
*/
protected class JoinColumnOwner
extends AbstractJoinColumnOwner
{
protected JoinColumnOwner() {
super();
}
public Entity getRelationshipTarget() {
return GenericJavaJoinTable.this.getParent().getRelationshipReference().getEntity();
}
public String getAttributeName() {
RelationshipMapping relationshipMapping = GenericJavaJoinTable.this.getRelationshipMapping();
if (relationshipMapping == null) {
return null;
}
Entity targetEntity = relationshipMapping.getResolvedTargetEntity();
if (targetEntity == null) {
return null;
}
for (PersistentAttribute each :
CollectionTools.iterable(
targetEntity.getPersistentType().allAttributes())) {
if (each.getMapping().isOwnedBy(relationshipMapping)) {
return each.getName();
}
}
return null;
}
public org.eclipse.jpt.db.Table getReferencedColumnDbTable() {
return getTypeMapping().getPrimaryDbTable();
}
public boolean isVirtual(BaseJoinColumn joinColumn) {
return GenericJavaJoinTable.this.defaultJoinColumn == joinColumn;
}
public String getDefaultColumnName() {
//built in MappingTools.buildJoinColumnDefaultName()
return null;
}
public int joinColumnsSize() {
return GenericJavaJoinTable.this.joinColumnsSize();
}
@Override
protected String getUnresolvedNameMessage() {
return JpaValidationMessages.JOIN_COLUMN_UNRESOLVED_NAME;
}
@Override
protected String getTableNotValidMessage() {
return JpaValidationMessages.JOIN_COLUMN_TABLE_NOT_VALID;
}
@Override
protected String getUnspecifiedNameMultipleJoinColumnsMessage() {
return JpaValidationMessages.JOIN_COLUMN_NAME_MUST_BE_SPECIFIED_MULTIPLE_JOIN_COLUMNS;
}
@Override
protected String getUnresolvedReferencedColumnNameMessage() {
return JpaValidationMessages.JOIN_COLUMN_UNRESOLVED_REFERENCED_COLUMN_NAME;
}
@Override
protected String getUnspecifiedReferencedColumnNameMultipleJoinColumnsMessage() {
return JpaValidationMessages.JOIN_COLUMN_REFERENCED_COLUMN_NAME_MUST_BE_SPECIFIED_MULTIPLE_JOIN_COLUMNS;
}
}
/**
* owner for "forward-pointer" JoinColumns;
* these point at the target/inverse entity
*/
protected class InverseJoinColumnOwner
extends AbstractJoinColumnOwner
{
protected InverseJoinColumnOwner() {
super();
}
public Entity getRelationshipTarget() {
RelationshipMapping relationshipMapping = GenericJavaJoinTable.this.getRelationshipMapping();
return relationshipMapping == null ? null : relationshipMapping.getResolvedTargetEntity();
}
public String getAttributeName() {
RelationshipMapping relationshipMapping = GenericJavaJoinTable.this.getRelationshipMapping();
return relationshipMapping == null ? null : relationshipMapping.getName();
}
public org.eclipse.jpt.db.Table getReferencedColumnDbTable() {
Entity relationshipTarget = getRelationshipTarget();
return (relationshipTarget == null) ? null : relationshipTarget.getPrimaryDbTable();
}
public boolean isVirtual(BaseJoinColumn joinColumn) {
return GenericJavaJoinTable.this.defaultInverseJoinColumn == joinColumn;
}
public String getDefaultColumnName() {
//built in MappingTools.buildJoinColumnDefaultName()
return null;
}
public int joinColumnsSize() {
return GenericJavaJoinTable.this.inverseJoinColumnsSize();
}
@Override
protected String getUnresolvedNameMessage() {
return JpaValidationMessages.INVERSE_JOIN_COLUMN_UNRESOLVED_NAME;
}
@Override
protected String getTableNotValidMessage() {
return JpaValidationMessages.INVERSE_JOIN_COLUMN_TABLE_NOT_VALID;
}
@Override
protected String getUnresolvedReferencedColumnNameMessage() {
return JpaValidationMessages.INVERSE_JOIN_COLUMN_UNRESOLVED_REFERENCED_COLUMN_NAME;
}
@Override
protected String getUnspecifiedNameMultipleJoinColumnsMessage() {
return JpaValidationMessages.INVERSE_JOIN_COLUMN_NAME_MUST_BE_SPECIFIED_MULTIPLE_JOIN_COLUMNS;
}
@Override
protected String getUnspecifiedReferencedColumnNameMultipleJoinColumnsMessage() {
return JpaValidationMessages.INVERSE_JOIN_COLUMN_REFERENCED_COLUMN_NAME_MUST_BE_SPECIFIED_MULTIPLE_JOIN_COLUMNS;
}
}
}