| /******************************************************************************* |
| * 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.context.orm; |
| |
| import java.util.Iterator; |
| import java.util.List; |
| |
| import org.eclipse.jpt.core.MappingKeys; |
| import org.eclipse.jpt.core.context.BaseColumn; |
| import org.eclipse.jpt.core.context.Converter; |
| import org.eclipse.jpt.core.context.NamedColumn; |
| import org.eclipse.jpt.core.context.orm.OrmAttributeMapping; |
| import org.eclipse.jpt.core.context.orm.OrmColumn; |
| import org.eclipse.jpt.core.context.orm.OrmColumnMapping; |
| import org.eclipse.jpt.core.context.orm.OrmConverter; |
| import org.eclipse.jpt.core.context.orm.OrmPersistentAttribute; |
| import org.eclipse.jpt.core.context.orm.OrmVersionMapping; |
| 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.orm.Attributes; |
| import org.eclipse.jpt.core.resource.orm.OrmFactory; |
| import org.eclipse.jpt.core.resource.orm.XmlColumn; |
| import org.eclipse.jpt.core.resource.orm.XmlVersion; |
| import org.eclipse.jpt.core.utility.TextRange; |
| import org.eclipse.jpt.db.Table; |
| import org.eclipse.wst.validation.internal.provisional.core.IMessage; |
| import org.eclipse.wst.validation.internal.provisional.core.IReporter; |
| |
| |
| public abstract class AbstractOrmVersionMapping<T extends XmlVersion> |
| extends AbstractOrmAttributeMapping<T> |
| implements OrmVersionMapping |
| { |
| protected final OrmColumn column; |
| |
| protected OrmConverter converter; |
| |
| protected final OrmConverter nullConverter; |
| |
| protected AbstractOrmVersionMapping(OrmPersistentAttribute parent, T resourceMapping) { |
| super(parent, resourceMapping); |
| this.column = getXmlContextNodeFactory().buildOrmColumn(this, this); |
| this.column.initialize(this.getResourceColumn());//TODO pass in to constructor |
| this.nullConverter = this.getXmlContextNodeFactory().buildOrmNullConverter(this); |
| this.converter = this.buildConverter(this.getResourceConverterType()); |
| } |
| |
| public int getXmlSequence() { |
| return 30; |
| } |
| |
| public String getKey() { |
| return MappingKeys.VERSION_ATTRIBUTE_MAPPING_KEY; |
| } |
| |
| public void initializeOn(OrmAttributeMapping newMapping) { |
| newMapping.initializeFromOrmVersionMapping(this); |
| } |
| |
| @Override |
| public void initializeFromOrmColumnMapping(OrmColumnMapping oldMapping) { |
| super.initializeFromOrmColumnMapping(oldMapping); |
| getColumn().initializeFrom(oldMapping.getColumn()); |
| } |
| |
| public OrmColumn getColumn() { |
| return this.column; |
| } |
| |
| public OrmConverter getConverter() { |
| return this.converter; |
| } |
| |
| protected String getConverterType() { |
| return this.converter.getType(); |
| } |
| |
| public void setConverter(String converterType) { |
| if (this.valuesAreEqual(getConverterType(), converterType)) { |
| return; |
| } |
| OrmConverter oldConverter = this.converter; |
| OrmConverter newConverter = buildConverter(converterType); |
| this.converter = this.nullConverter; |
| if (oldConverter != null) { |
| oldConverter.removeFromResourceModel(); |
| } |
| this.converter = newConverter; |
| if (newConverter != null) { |
| newConverter.addToResourceModel(); |
| } |
| firePropertyChanged(CONVERTER_PROPERTY, oldConverter, newConverter); |
| } |
| |
| protected void setConverter(OrmConverter newConverter) { |
| OrmConverter oldConverter = this.converter; |
| this.converter = newConverter; |
| firePropertyChanged(CONVERTER_PROPERTY, oldConverter, newConverter); |
| } |
| |
| public void addToResourceModel(Attributes resourceAttributes) { |
| resourceAttributes.getVersions().add(this.resourceAttributeMapping); |
| } |
| |
| public void removeFromResourceModel(Attributes resourceAttributes) { |
| resourceAttributes.getVersions().remove(this.resourceAttributeMapping); |
| } |
| |
| //************** NamedColumn.Owner implementation *************** |
| |
| public String getDefaultColumnName() { |
| return getName(); |
| } |
| |
| public Table getDbTable(String tableName) { |
| return getTypeMapping().getDbTable(tableName); |
| } |
| |
| //************** BaseColumn.Owner implementation *************** |
| |
| public String getDefaultTableName() { |
| return getTypeMapping().getPrimaryTableName(); |
| } |
| |
| public boolean tableNameIsInvalid(String tableName) { |
| return getTypeMapping().tableNameIsInvalid(tableName); |
| } |
| |
| public Iterator<String> candidateTableNames() { |
| return getTypeMapping().associatedTableNamesIncludingInherited(); |
| } |
| |
| @Override |
| public void update() { |
| super.update(); |
| this.column.update(this.getResourceColumn()); |
| if (this.valuesAreEqual(getResourceConverterType(), getConverterType())) { |
| getConverter().update(); |
| } |
| else { |
| setConverter(buildConverter(getResourceConverterType())); |
| } |
| } |
| |
| protected OrmConverter buildConverter(String converterType) { |
| if (this.valuesAreEqual(converterType, Converter.NO_CONVERTER)) { |
| return this.nullConverter; |
| } |
| if (this.valuesAreEqual(converterType, Converter.TEMPORAL_CONVERTER)) { |
| return getXmlContextNodeFactory().buildOrmTemporalConverter(this, this.resourceAttributeMapping); |
| } |
| return null; |
| } |
| |
| protected String getResourceConverterType() { |
| if (this.resourceAttributeMapping.getTemporal() != null) { |
| return Converter.TEMPORAL_CONVERTER; |
| } |
| return Converter.NO_CONVERTER; |
| } |
| |
| //***************** XmlColumn.Owner implementation **************** |
| |
| public XmlColumn getResourceColumn() { |
| return this.resourceAttributeMapping.getColumn(); |
| } |
| |
| public void addResourceColumn() { |
| this.resourceAttributeMapping.setColumn(OrmFactory.eINSTANCE.createXmlColumn()); |
| } |
| |
| public void removeResourceColumn() { |
| this.resourceAttributeMapping.setColumn(null); |
| } |
| |
| // ****************** validation **************** |
| |
| @Override |
| public void validate(List<IMessage> messages, IReporter reporter) { |
| super.validate(messages, reporter); |
| |
| this.getColumn().validate(messages, reporter); |
| } |
| |
| public IMessage buildUnresolvedNameMessage(NamedColumn column, TextRange textRange) { |
| if (isVirtual()) { |
| return this.buildVirtualUnresolvedNameMessage(column, textRange); |
| } |
| return DefaultJpaValidationMessages.buildMessage( |
| IMessage.HIGH_SEVERITY, |
| JpaValidationMessages.COLUMN_UNRESOLVED_NAME, |
| new String[] {column.getName(), column.getDbTable().getName()}, |
| column, |
| textRange |
| ); |
| } |
| |
| protected IMessage buildVirtualUnresolvedNameMessage(NamedColumn column, TextRange textRange) { |
| return DefaultJpaValidationMessages.buildMessage( |
| IMessage.HIGH_SEVERITY, |
| JpaValidationMessages.VIRTUAL_ATTRIBUTE_COLUMN_UNRESOLVED_NAME, |
| new String[] {getName(), column.getName(), column.getDbTable().getName()}, |
| column, |
| textRange |
| ); |
| } |
| |
| public IMessage buildTableNotValidMessage(BaseColumn column, TextRange textRange) { |
| if (isVirtual()) { |
| return this.buildVirtualTableNotValidMessage(column, textRange); |
| } |
| return DefaultJpaValidationMessages.buildMessage( |
| IMessage.HIGH_SEVERITY, |
| JpaValidationMessages.COLUMN_TABLE_NOT_VALID, |
| new String[] { |
| column.getTable(), |
| column.getName(), |
| JpaValidationDescriptionMessages.NOT_VALID_FOR_THIS_ENTITY}, |
| column, |
| textRange |
| ); |
| } |
| |
| public IMessage buildVirtualTableNotValidMessage(BaseColumn column, TextRange textRange) { |
| return DefaultJpaValidationMessages.buildMessage( |
| IMessage.HIGH_SEVERITY, |
| JpaValidationMessages.VIRTUAL_ATTRIBUTE_COLUMN_TABLE_NOT_VALID, |
| new String[] { |
| getName(), |
| column.getTable(), |
| column.getName(), |
| JpaValidationDescriptionMessages.NOT_VALID_FOR_THIS_ENTITY}, |
| column, |
| textRange |
| ); |
| } |
| } |