blob: 201603d2e8b383f531f3d0a6b0915ce2160798a3 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2009, 2013 Oracle. All rights reserved.
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0, which accompanies this distribution
* and is available at https://www.eclipse.org/legal/epl-2.0/.
*
* Contributors:
* Oracle - initial API and implementation
******************************************************************************/
package org.eclipse.jpt.jpa.core.internal.jpa2.context.orm;
import org.eclipse.jpt.common.core.utility.TextRange;
import org.eclipse.jpt.common.utility.internal.ObjectTools;
import org.eclipse.jpt.common.utility.internal.iterable.EmptyIterable;
import org.eclipse.jpt.jpa.core.context.Entity;
import org.eclipse.jpt.jpa.core.context.JoinColumn;
import org.eclipse.jpt.jpa.core.context.JpaContextModel;
import org.eclipse.jpt.jpa.core.context.NamedColumn;
import org.eclipse.jpt.jpa.core.context.SpecifiedPersistentAttribute;
import org.eclipse.jpt.jpa.core.context.TypeMapping;
import org.eclipse.jpt.jpa.core.internal.context.JpaValidator;
import org.eclipse.jpt.jpa.core.internal.context.MappingTools;
import org.eclipse.jpt.jpa.core.internal.jpa1.context.CollectionTableTableDescriptionProvider;
import org.eclipse.jpt.jpa.core.internal.jpa1.context.JoinColumnValidator;
import org.eclipse.jpt.jpa.core.internal.jpa1.context.orm.GenericOrmReferenceTable;
import org.eclipse.jpt.jpa.core.jpa2.context.orm.OrmCollectionTable2_0;
import org.eclipse.jpt.jpa.core.jpa2.context.orm.OrmElementCollectionMapping2_0;
import org.eclipse.jpt.jpa.core.resource.orm.OrmFactory;
import org.eclipse.jpt.jpa.core.resource.orm.XmlCollectionTable;
import org.eclipse.jpt.jpa.core.resource.orm.XmlElementCollection;
/**
* <code>orm.xml</code> collection table
*/
public class GenericOrmCollectionTable2_0
extends GenericOrmReferenceTable<OrmElementCollectionMapping2_0, OrmCollectionTable2_0.ParentAdapter, XmlCollectionTable>
implements OrmCollectionTable2_0
{
public GenericOrmCollectionTable2_0(OrmCollectionTable2_0.ParentAdapter parentAdapter) {
super(parentAdapter);
}
@Override
protected JoinColumn.ParentAdapter buildJoinColumnParentAdapter() {
return new JoinColumnParentAdapter();
}
// ********** XML table **********
@Override
protected XmlCollectionTable getXmlTable() {
return this.getXmlAttributeMapping().getCollectionTable();
}
@Override
protected XmlCollectionTable buildXmlTable() {
XmlCollectionTable xmlCollectionTable = OrmFactory.eINSTANCE.createXmlCollectionTable();
this.getXmlAttributeMapping().setCollectionTable(xmlCollectionTable);
return xmlCollectionTable;
}
@Override
protected void removeXmlTable() {
this.getXmlAttributeMapping().setCollectionTable(null);
}
protected XmlElementCollection getXmlAttributeMapping() {
return this.getElementCollectionMapping().getXmlAttributeMapping();
}
// ********** misc **********
public SpecifiedPersistentAttribute getPersistentAttribute() {
return this.getElementCollectionMapping().getPersistentAttribute();
}
protected OrmElementCollectionMapping2_0 getElementCollectionMapping() {
return this.parent;
}
@Override
protected String buildDefaultName() {
return MappingTools.buildCollectionTableDefaultName(this.getElementCollectionMapping());
}
public void initializeFrom(OrmCollectionTable2_0 oldTable) {
super.initializeFrom(oldTable);
}
// ********** validation **********
public boolean validatesAgainstDatabase() {
return this.getElementCollectionMapping().validatesAgainstDatabase();
}
// ********** join column parent adapter **********
/**
* parent adapter for "back-pointer" join columns;
* these point at the source/owning entity
*/
public class JoinColumnParentAdapter
implements JoinColumn.ParentAdapter
{
public JpaContextModel getColumnParent() {
return GenericOrmCollectionTable2_0.this;
}
protected TypeMapping getTypeMapping() {
return this.getElementCollectionMapping().getTypeMapping();
}
public org.eclipse.jpt.jpa.db.Table resolveDbTable(String tableName) {
return ObjectTools.equals(GenericOrmCollectionTable2_0.this.getName(), tableName) ?
GenericOrmCollectionTable2_0.this.getDbTable() :
null;
}
public String getDefaultColumnName(NamedColumn column) {
return MappingTools.buildJoinColumnDefaultName((JoinColumn) column, this);
}
/**
* by default, the join column is, obviously, in the collection table;
* not sure whether it can be anywhere else...
*/
public String getDefaultTableName() {
return GenericOrmCollectionTable2_0.this.getName();
}
public TextRange getValidationTextRange() {
return GenericOrmCollectionTable2_0.this.getValidationTextRange();
}
public org.eclipse.jpt.jpa.db.Table getReferencedColumnDbTable() {
return this.getTypeMapping().getPrimaryDbTable();
}
/**
* the default table name is always valid and a specified table name
* is prohibited (which will be handled elsewhere)
*/
public boolean tableNameIsInvalid(String tableName) {
return false;
}
public Iterable<String> getCandidateTableNames() {
return EmptyIterable.instance();
}
public Entity getRelationshipTarget() {
return this.getElementCollectionMapping().getEntity();
}
public String getAttributeName() {
//TODO
return null; //I *think* this is correct
// //return GenericJavaCollectionTable2_0.this.getParent().getName();
// Entity targetEntity = GenericOrmCollectionTable2_0.this.getRelationshipMapping().getResolvedTargetEntity();
// if (targetEntity == null) {
// return null;
// }
// for (PersistentAttribute each : CollectionTools.iterable(targetEntity.getPersistentType().allAttributes())) {
// if (each.getMapping().isOwnedBy(getRelationshipMapping())) {
// return each.getName();
// }
// }
// return null;
}
protected SpecifiedPersistentAttribute getPersistentAttribute() {
return GenericOrmCollectionTable2_0.this.getPersistentAttribute();
}
public int getJoinColumnsSize() {
return GenericOrmCollectionTable2_0.this.getJoinColumnsSize();
}
protected OrmElementCollectionMapping2_0 getElementCollectionMapping() {
return GenericOrmCollectionTable2_0.this.getElementCollectionMapping();
}
public JpaValidator buildColumnValidator(NamedColumn column) {
return new JoinColumnValidator(this.getPersistentAttribute(), (JoinColumn) column, this, new CollectionTableTableDescriptionProvider());
}
}
}