blob: 2957c055a45c55e7453980be9d6a888a6ab57b80 [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.jpa.core.internal.resource.java.source;
import java.util.Map;
import org.eclipse.jdt.core.dom.CompilationUnit;
import org.eclipse.jpt.common.core.internal.utility.jdt.AnnotatedElementAnnotationElementAdapter;
import org.eclipse.jpt.common.core.internal.utility.jdt.EnumDeclarationAnnotationElementAdapter;
import org.eclipse.jpt.common.core.internal.utility.jdt.SimpleDeclarationAnnotationAdapter;
import org.eclipse.jpt.common.core.utility.jdt.AnnotationElementAdapter;
import org.eclipse.jpt.common.core.utility.jdt.DeclarationAnnotationAdapter;
import org.eclipse.jpt.common.core.utility.jdt.DeclarationAnnotationElementAdapter;
import org.eclipse.jpt.common.core.utility.jdt.Type;
import org.eclipse.jpt.jpa.core.resource.java.DiscriminatorColumnAnnotation;
import org.eclipse.jpt.jpa.core.resource.java.DiscriminatorType;
import org.eclipse.jpt.jpa.core.resource.java.JPA;
import org.eclipse.jpt.jpa.core.resource.java.JavaResourcePersistentType;
/**
* <code>javax.persistence.DiscriminatorColumn</code>
*/
public final class SourceDiscriminatorColumnAnnotation
extends SourceNamedColumnAnnotation
implements DiscriminatorColumnAnnotation
{
private static final DeclarationAnnotationAdapter DECLARATION_ANNOTATION_ADAPTER = new SimpleDeclarationAnnotationAdapter(ANNOTATION_NAME);
private static final DeclarationAnnotationElementAdapter<String> DISCRIMINATOR_TYPE_DECLARATION_ADAPTER = buildDiscriminatorTypeDeclarationAdapter();
private AnnotationElementAdapter<String> discriminatorTypeAdapter;
private DiscriminatorType discriminatorType;
private DeclarationAnnotationElementAdapter<Integer> lengthDeclarationAdapter;
private AnnotationElementAdapter<Integer> lengthAdapter;
private Integer length;
public SourceDiscriminatorColumnAnnotation(JavaResourcePersistentType parent, Type type) {
super(parent, type, DECLARATION_ANNOTATION_ADAPTER);
this.discriminatorTypeAdapter = this.buildDiscriminatorTypeAdapter();
this.lengthDeclarationAdapter = this.buildLengthDeclarationAdapter();
this.lengthAdapter = this.buildLengthAdapter();
}
public String getAnnotationName() {
return ANNOTATION_NAME;
}
@Override
public void initialize(CompilationUnit astRoot) {
super.initialize(astRoot);
this.discriminatorType = this.buildDiscriminatorType(astRoot);
this.length = this.buildLength(astRoot);
}
@Override
public void synchronizeWith(CompilationUnit astRoot) {
super.synchronizeWith(astRoot);
this.syncLength(this.buildLength(astRoot));
this.syncDiscriminatorType(this.buildDiscriminatorType(astRoot));
}
// ********** JavaSourceNamedColumnAnnotation implementation **********
@Override
protected String getNameElementName() {
return JPA.DISCRIMINATOR_COLUMN__NAME;
}
@Override
protected String getColumnDefinitionElementName() {
return JPA.DISCRIMINATOR_COLUMN__COLUMN_DEFINITION;
}
// ********** DiscriminatorColumn implementation **********
// ***** discriminator type
public DiscriminatorType getDiscriminatorType() {
return this.discriminatorType;
}
public void setDiscriminatorType(DiscriminatorType discriminatorType) {
if (this.attributeValueHasChanged(this.discriminatorType, discriminatorType)) {
this.discriminatorType = discriminatorType;
this.discriminatorTypeAdapter.setValue(DiscriminatorType.toJavaAnnotationValue(discriminatorType));
}
}
private void syncDiscriminatorType(DiscriminatorType astDiscriminatorType) {
DiscriminatorType old = this.discriminatorType;
this.discriminatorType = astDiscriminatorType;
this.firePropertyChanged(DISCRIMINATOR_TYPE_PROPERTY, old, astDiscriminatorType);
}
private DiscriminatorType buildDiscriminatorType(CompilationUnit astRoot) {
return DiscriminatorType.fromJavaAnnotationValue(this.discriminatorTypeAdapter.getValue(astRoot));
}
private AnnotationElementAdapter<String> buildDiscriminatorTypeAdapter() {
return new AnnotatedElementAnnotationElementAdapter<String>(this.annotatedElement, DISCRIMINATOR_TYPE_DECLARATION_ADAPTER);
}
// ***** length
public Integer getLength() {
return this.length;
}
public void setLength(Integer length) {
if (this.attributeValueHasChanged(this.length, length)) {
this.length = length;
this.lengthAdapter.setValue(length);
}
}
private void syncLength(Integer astLength) {
Integer old = this.length;
this.length = astLength;
this.firePropertyChanged(LENGTH_PROPERTY, old, astLength);
}
private Integer buildLength(CompilationUnit astRoot) {
return this.lengthAdapter.getValue(astRoot);
}
private DeclarationAnnotationElementAdapter<Integer> buildLengthDeclarationAdapter() {
return this.buildIntegerElementAdapter(JPA.DISCRIMINATOR_COLUMN__LENGTH);
}
private AnnotationElementAdapter<Integer> buildLengthAdapter() {
return this.buildIntegerElementAdapter(this.lengthDeclarationAdapter);
}
// ********** misc **********
@Override
public boolean isUnset() {
return super.isUnset() &&
(this.discriminatorType == null) &&
(this.length == null);
}
@Override
protected void rebuildAdapters() {
super.rebuildAdapters();
this.discriminatorTypeAdapter = this.buildDiscriminatorTypeAdapter();
this.lengthDeclarationAdapter = this.buildLengthDeclarationAdapter();
this.lengthAdapter = this.buildLengthAdapter();
}
@Override
public void storeOn(Map<String, Object> map) {
super.storeOn(map);
map.put(DISCRIMINATOR_TYPE_PROPERTY, this.discriminatorType);
this.discriminatorType = null;
map.put(LENGTH_PROPERTY, this.length);
this.length = null;
}
@Override
public void restoreFrom(Map<String, Object> map) {
super.restoreFrom(map);
this.setDiscriminatorType((DiscriminatorType) map.get(DISCRIMINATOR_TYPE_PROPERTY));
this.setLength((Integer) map.get(LENGTH_PROPERTY));
}
// ********** static methods **********
private static DeclarationAnnotationElementAdapter<String> buildDiscriminatorTypeDeclarationAdapter() {
return new EnumDeclarationAnnotationElementAdapter(DECLARATION_ANNOTATION_ADAPTER, JPA.DISCRIMINATOR_COLUMN__DISCRIMINATOR_TYPE);
}
}