blob: 08f215b296344a593a247cc9b1fa7fbecf1179c2 [file] [log] [blame]
/*******************************************************************************
* Copyright (c) 2008, 2012 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.eclipselink.core.internal.resource.java.source;
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.CombinationIndexedDeclarationAnnotationAdapter;
import org.eclipse.jpt.common.core.internal.utility.jdt.ConversionDeclarationAnnotationElementAdapter;
import org.eclipse.jpt.common.core.internal.utility.jdt.ElementIndexedAnnotationAdapter;
import org.eclipse.jpt.common.core.internal.utility.jdt.NestedIndexedDeclarationAnnotationAdapter;
import org.eclipse.jpt.common.core.internal.utility.jdt.SimpleDeclarationAnnotationAdapter;
import org.eclipse.jpt.common.core.internal.utility.jdt.StringExpressionConverter;
import org.eclipse.jpt.common.core.resource.java.JavaResourceAnnotatedElement;
import org.eclipse.jpt.common.core.utility.TextRange;
import org.eclipse.jpt.common.core.utility.jdt.AnnotatedElement;
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.IndexedAnnotationAdapter;
import org.eclipse.jpt.common.core.utility.jdt.IndexedDeclarationAnnotationAdapter;
import org.eclipse.jpt.common.utility.internal.iterables.ListIterable;
import org.eclipse.jpt.jpa.eclipselink.core.resource.java.EclipseLink;
import org.eclipse.jpt.jpa.eclipselink.core.resource.java.EclipseLinkConversionValueAnnotation;
import org.eclipse.jpt.jpa.eclipselink.core.resource.java.EclipseLinkObjectTypeConverterAnnotation;
/**
* <code>org.eclipse.persistence.annotations.ObjectTypeConverter</code>
*/
public final class SourceEclipseLinkObjectTypeConverterAnnotation
extends SourceEclipseLinkBaseTypeConverterAnnotation
implements EclipseLinkObjectTypeConverterAnnotation
{
private static final DeclarationAnnotationAdapter DECLARATION_ANNOTATION_ADAPTER = new SimpleDeclarationAnnotationAdapter(ANNOTATION_NAME);
private static final DeclarationAnnotationAdapter CONTAINER_DECLARATION_ANNOTATION_ADAPTER = new SimpleDeclarationAnnotationAdapter(EclipseLink.OBJECT_TYPE_CONVERTERS);
private final DeclarationAnnotationElementAdapter<String> defaultObjectValueDeclarationAdapter;
private final AnnotationElementAdapter<String> defaultObjectValueAdapter;
private String defaultObjectValue;
private TextRange defaultObjectValueTextRange;
private final ConversionValuesAnnotationContainer conversionValuesContainer = new ConversionValuesAnnotationContainer();
public static SourceEclipseLinkObjectTypeConverterAnnotation buildSourceObjectTypeConverterAnnotation(JavaResourceAnnotatedElement parent, AnnotatedElement element, int index) {
IndexedDeclarationAnnotationAdapter idaa = buildObjectTypeConverterDeclarationAnnotationAdapter(index);
IndexedAnnotationAdapter iaa = buildObjectTypeConverterAnnotationAdapter(element, idaa);
return new SourceEclipseLinkObjectTypeConverterAnnotation(
parent,
element,
idaa,
iaa);
}
private SourceEclipseLinkObjectTypeConverterAnnotation(
JavaResourceAnnotatedElement parent,
AnnotatedElement element,
IndexedDeclarationAnnotationAdapter daa,
IndexedAnnotationAdapter annotationAdapter) {
super(parent, element, daa, annotationAdapter);
this.defaultObjectValueDeclarationAdapter = this.buildDefaultObjectValueAdapter();
this.defaultObjectValueAdapter = new AnnotatedElementAnnotationElementAdapter<String>(element, this.defaultObjectValueDeclarationAdapter);
}
public String getAnnotationName() {
return ANNOTATION_NAME;
}
@Override
public void initialize(CompilationUnit astRoot) {
super.initialize(astRoot);
this.defaultObjectValue = this.buildDefaultObjectValue(astRoot);
this.defaultObjectValueTextRange = this.buildDefaultObjectValueTextRange(astRoot);
this.conversionValuesContainer.initializeFromContainerAnnotation(this.getAstAnnotation(astRoot));
}
@Override
public void synchronizeWith(CompilationUnit astRoot) {
super.synchronizeWith(astRoot);
this.syncDefaultObjectValue(this.buildDefaultObjectValue(astRoot));
this.defaultObjectValueTextRange = this.buildDefaultObjectValueTextRange(astRoot);
this.conversionValuesContainer.synchronize(this.getAstAnnotation(astRoot));
}
@Override
public boolean isUnset() {
return super.isUnset() &&
(this.defaultObjectValue == null) &&
this.conversionValuesContainer.isEmpty();
}
// ********** SourceNamedConverterAnnotation implementation **********
@Override
String getNameElementName() {
return EclipseLink.OBJECT_TYPE_CONVERTER__NAME;
}
// ********** SourceBaseTypeConverterAnnotation implementation **********
@Override
String getDataTypeElementName() {
return EclipseLink.OBJECT_TYPE_CONVERTER__DATA_TYPE;
}
@Override
String getObjectTypeElementName() {
return EclipseLink.OBJECT_TYPE_CONVERTER__OBJECT_TYPE;
}
// ********** ObjectTypeConverterAnnotation implementation **********
// ***** default object value
public String getDefaultObjectValue() {
return this.defaultObjectValue;
}
public void setDefaultObjectValue(String defaultObjectValue) {
if (this.attributeValueHasChanged(this.defaultObjectValue, defaultObjectValue)) {
this.defaultObjectValue = defaultObjectValue;
this.defaultObjectValueAdapter.setValue(defaultObjectValue);
}
}
private void syncDefaultObjectValue(String astDefaultObjectValue) {
String old = this.defaultObjectValue;
this.defaultObjectValue = astDefaultObjectValue;
this.firePropertyChanged(DEFAULT_OBJECT_VALUE_PROPERTY, old, astDefaultObjectValue);
}
private String buildDefaultObjectValue(CompilationUnit astRoot) {
return this.defaultObjectValueAdapter.getValue(astRoot);
}
public TextRange getDefaultObjectValueTextRange() {
return this.defaultObjectValueTextRange;
}
private TextRange buildDefaultObjectValueTextRange(CompilationUnit astRoot) {
return this.getElementTextRange(this.defaultObjectValueDeclarationAdapter, astRoot);
}
private DeclarationAnnotationElementAdapter<String> buildDefaultObjectValueAdapter() {
return new ConversionDeclarationAnnotationElementAdapter<String>(this.daa, EclipseLink.OBJECT_TYPE_CONVERTER__DEFAULT_OBJECT_VALUE, StringExpressionConverter.instance());
}
// ***** conversion values
public ListIterable<EclipseLinkConversionValueAnnotation> getConversionValues() {
return this.conversionValuesContainer.getNestedAnnotations();
}
public int getConversionValuesSize() {
return this.conversionValuesContainer.getNestedAnnotationsSize();
}
public EclipseLinkConversionValueAnnotation conversionValueAt(int index) {
return this.conversionValuesContainer.getNestedAnnotation(index);
}
public EclipseLinkConversionValueAnnotation addConversionValue(int index) {
return this.conversionValuesContainer.addNestedAnnotation(index);
}
public void moveConversionValue(int targetIndex, int sourceIndex) {
this.conversionValuesContainer.moveNestedAnnotation(targetIndex, sourceIndex);
}
public void removeConversionValue(int index) {
this.conversionValuesContainer.removeNestedAnnotation(index);
}
protected EclipseLinkConversionValueAnnotation buildConversionValue(int index) {
return SourceEclipseLinkConversionValueAnnotation.buildNestedSourceConversionValueAnnotation(
this, this.annotatedElement, this.buildConversionValueIndexedDeclarationAnnotationAdapter(index));
}
private IndexedDeclarationAnnotationAdapter buildConversionValueIndexedDeclarationAnnotationAdapter(int index) {
return new NestedIndexedDeclarationAnnotationAdapter(
this.daa, EclipseLink.OBJECT_TYPE_CONVERTER__CONVERSION_VALUES, index, EclipseLink.CONVERSION_VALUE);
}
// ********** static methods **********
private static IndexedAnnotationAdapter buildObjectTypeConverterAnnotationAdapter(AnnotatedElement annotatedElement, IndexedDeclarationAnnotationAdapter idaa) {
return new ElementIndexedAnnotationAdapter(annotatedElement, idaa);
}
private static IndexedDeclarationAnnotationAdapter buildObjectTypeConverterDeclarationAnnotationAdapter(int index) {
IndexedDeclarationAnnotationAdapter idaa =
new CombinationIndexedDeclarationAnnotationAdapter(
DECLARATION_ANNOTATION_ADAPTER,
CONTAINER_DECLARATION_ANNOTATION_ADAPTER,
index,
ANNOTATION_NAME);
return idaa;
}
// ********** hint container **********
/**
* adapt the AnnotationContainer interface to the xml schema's xmlns
*/
class ConversionValuesAnnotationContainer
extends AnnotationContainer<EclipseLinkConversionValueAnnotation>
{
@Override
protected String getNestedAnnotationsListName() {
return CONVERSION_VALUES_LIST;
}
@Override
protected String getElementName() {
return EclipseLink.OBJECT_TYPE_CONVERTER__CONVERSION_VALUES;
}
@Override
protected String getNestedAnnotationName() {
return EclipseLinkConversionValueAnnotation.ANNOTATION_NAME;
}
@Override
protected EclipseLinkConversionValueAnnotation buildNestedAnnotation(int index) {
return SourceEclipseLinkObjectTypeConverterAnnotation.this.buildConversionValue(index);
}
}
}