### Eclipse Workspace Patch 1.0 #P org.eclipse.jpt.jpa.core diff --git src/org/eclipse/jpt/jpa/core/context/NamedNativeQuery.java src/org/eclipse/jpt/jpa/core/context/NamedNativeQuery.java index 942c151..bb21fea 100644 --- src/org/eclipse/jpt/jpa/core/context/NamedNativeQuery.java +++ src/org/eclipse/jpt/jpa/core/context/NamedNativeQuery.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2012 Oracle. All rights reserved. + * Copyright (c) 2007, 2013 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. @@ -24,6 +24,16 @@ public interface NamedNativeQuery extends Query { + + // ********** query ********** + + String QUERY_PROPERTY = "query"; //$NON-NLS-1$ + + String getQuery(); + + void setQuery(String query); + + // ********** result class ********** String getResultClass(); diff --git src/org/eclipse/jpt/jpa/core/context/NamedQuery.java src/org/eclipse/jpt/jpa/core/context/NamedQuery.java index 92ac55e..98e52cf 100644 --- src/org/eclipse/jpt/jpa/core/context/NamedQuery.java +++ src/org/eclipse/jpt/jpa/core/context/NamedQuery.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2010 Oracle. All rights reserved. + * Copyright (c) 2007, 2013 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. @@ -8,6 +8,9 @@ * Oracle - initial API and implementation ******************************************************************************/ package org.eclipse.jpt.jpa.core.context; + +import java.util.List; +import org.eclipse.jpt.common.core.utility.TextRange; /** * named query @@ -24,5 +27,20 @@ public interface NamedQuery extends Query { - // nothing yet + + // ********** query ********** + + String QUERY_PROPERTY = "query"; //$NON-NLS-1$ + + String getQuery(); + + void setQuery(String query); + + + /** + * Returns the list of {@link TextRange} of the query property, which is either a single object + * if the string is not split or many objects if the JPQL query is split into many strings. + */ + List getQueryTextRanges(); + } diff --git src/org/eclipse/jpt/jpa/core/context/Query.java src/org/eclipse/jpt/jpa/core/context/Query.java index 9308187..ac0b7e0 100644 --- src/org/eclipse/jpt/jpa/core/context/Query.java +++ src/org/eclipse/jpt/jpa/core/context/Query.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2012 Oracle. All rights reserved. + * Copyright (c) 2007, 2013 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. @@ -47,15 +47,6 @@ NamedQuery.class, NamedNativeQuery.class ); - - // ********** query ********** - - String QUERY_PROPERTY = "query"; //$NON-NLS-1$ - - String getQuery(); - - void setQuery(String query); - // ********** hints ********** @@ -112,10 +103,4 @@ * Returns the {@link TextRange} of the name property. */ TextRange getNameTextRange(); - - /** - * Returns the list of {@link TextRange} of the query property, which is either a single object - * if the string is not split or many objects if the JPQL query is split into many strings. - */ - List getQueryTextRanges(); } diff --git src/org/eclipse/jpt/jpa/core/internal/context/java/AbstractJavaQuery.java src/org/eclipse/jpt/jpa/core/internal/context/java/AbstractJavaQuery.java index f4cba84..f7507ab 100644 --- src/org/eclipse/jpt/jpa/core/internal/context/java/AbstractJavaQuery.java +++ src/org/eclipse/jpt/jpa/core/internal/context/java/AbstractJavaQuery.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2012 Oracle. All rights reserved. + * Copyright (c) 2007, 2013 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. @@ -43,8 +43,6 @@ protected String name; - protected String query; - protected final ContextListContainer hintContainer; @@ -52,7 +50,6 @@ super(parent); this.queryAnnotation = queryAnnotation; this.name = queryAnnotation.getName(); - this.query = queryAnnotation.getQuery(); this.hintContainer = this.buildHintContainer(); } @@ -63,7 +60,6 @@ public void synchronizeWithResourceModel() { super.synchronizeWithResourceModel(); this.setName_(this.queryAnnotation.getName()); - this.setQuery_(this.queryAnnotation.getQuery()); this.syncHints(); } @@ -89,24 +85,6 @@ String old = this.name; this.name = name; this.firePropertyChanged(NAME_PROPERTY, old, name); - } - - - // ********** query ********** - - public String getQuery() { - return this.query; - } - - public void setQuery(String query) { - this.queryAnnotation.setQuery(query); - this.setQuery_(query); - } - - protected void setQuery_(String query) { - String old = this.query; - this.query = query; - this.firePropertyChanged(QUERY_PROPERTY, old, query); } @@ -198,7 +176,6 @@ public void validate(JpaJpqlQueryHelper queryHelper, List messages, IReporter reporter) { super.validate(messages, reporter); this.validateName(messages); - this.validateQuery(queryHelper, messages, reporter); } protected void validateName(List messages) { @@ -215,24 +192,6 @@ } } - public void validateQuery(JpaJpqlQueryHelper queryHelper, List messages, IReporter reporter) { - if (StringTools.isBlank(this.query)){ - messages.add( - DefaultJpaValidationMessages.buildMessage( - IMessage.HIGH_SEVERITY, - JpaValidationMessages.QUERY_STATEMENT_UNDEFINED, - new String[] {this.name}, - this, - this.getNameTextRange() - ) - ); - } else { - this.validateQuery_(queryHelper, messages, reporter); - } - } - - protected abstract void validateQuery_(JpaJpqlQueryHelper queryHelper, List messages, IReporter reporter); - public TextRange getValidationTextRange() { TextRange textRange = this.queryAnnotation.getTextRange(); return (textRange != null) ? textRange : this.getParent().getValidationTextRange(); @@ -240,10 +199,6 @@ public TextRange getNameTextRange() { return this.getValidationTextRange(this.queryAnnotation.getNameTextRange()); - } - - public List getQueryTextRanges() { - return this.queryAnnotation.getQueryTextRanges(); } public boolean isEquivalentTo(JpaNamedContextNode node) { @@ -254,7 +209,6 @@ protected boolean isEquivalentTo(Query other) { return ObjectTools.equals(this.name, other.getName()) && - ObjectTools.equals(this.query, other.getQuery()) && this.hintsAreEquivalentTo(other); } diff --git src/org/eclipse/jpt/jpa/core/internal/jpa1/context/java/GenericJavaNamedNativeQuery.java src/org/eclipse/jpt/jpa/core/internal/jpa1/context/java/GenericJavaNamedNativeQuery.java index 5629f3e..7464518 100644 --- src/org/eclipse/jpt/jpa/core/internal/jpa1/context/java/GenericJavaNamedNativeQuery.java +++ src/org/eclipse/jpt/jpa/core/internal/jpa1/context/java/GenericJavaNamedNativeQuery.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2012 Oracle. All rights reserved. + * Copyright (c) 2007, 2013 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. @@ -11,12 +11,15 @@ import java.util.List; import org.eclipse.jpt.common.utility.internal.ObjectTools; +import org.eclipse.jpt.common.utility.internal.StringTools; import org.eclipse.jpt.jpa.core.context.NamedNativeQuery; import org.eclipse.jpt.jpa.core.context.Query; import org.eclipse.jpt.jpa.core.context.java.JavaNamedNativeQuery; import org.eclipse.jpt.jpa.core.context.java.JavaQueryContainer; import org.eclipse.jpt.jpa.core.context.orm.OrmQueryContainer; import org.eclipse.jpt.jpa.core.internal.context.java.AbstractJavaQuery; +import org.eclipse.jpt.jpa.core.internal.validation.DefaultJpaValidationMessages; +import org.eclipse.jpt.jpa.core.internal.validation.JpaValidationMessages; import org.eclipse.jpt.jpa.core.jpql.JpaJpqlQueryHelper; import org.eclipse.jpt.jpa.core.resource.java.NamedNativeQueryAnnotation; import org.eclipse.wst.validation.internal.provisional.core.IMessage; @@ -29,6 +32,8 @@ extends AbstractJavaQuery implements JavaNamedNativeQuery { + protected String query; + protected String resultClass; protected String fullyQualifiedResultClass; @@ -37,6 +42,7 @@ public GenericJavaNamedNativeQuery(JavaQueryContainer parent, NamedNativeQueryAnnotation queryAnnotation) { super(parent, queryAnnotation); + this.query = queryAnnotation.getQuery(); this.resultClass = queryAnnotation.getResultClass(); this.resultSetMapping = queryAnnotation.getResultSetMapping(); } @@ -47,6 +53,7 @@ @Override public void synchronizeWithResourceModel() { super.synchronizeWithResourceModel(); + this.setQuery_(this.queryAnnotation.getQuery()); this.setResultClass_(this.queryAnnotation.getResultClass()); this.setResultSetMapping_(this.queryAnnotation.getResultSetMapping()); } @@ -56,6 +63,24 @@ super.update(); this.setFullyQualifiedResultClass(this.buildFullyQualifiedResultClass()); } + + // ********** query ********** + + public String getQuery() { + return this.query; + } + + public void setQuery(String query) { + this.queryAnnotation.setQuery(query); + this.setQuery_(query); + } + + protected void setQuery_(String query) { + String old = this.query; + this.query = query; + this.firePropertyChanged(QUERY_PROPERTY, old, query); + } + // ********** result class ********** @@ -122,7 +147,27 @@ // ********** validation ********** - @Override + public void validate(JpaJpqlQueryHelper queryHelper, List messages, IReporter reporter) { + super.validate(messages, reporter); + this.validateQuery(queryHelper, messages, reporter); + } + + public void validateQuery(JpaJpqlQueryHelper queryHelper, List messages, IReporter reporter) { + if (StringTools.isBlank(this.query)){ + messages.add( + DefaultJpaValidationMessages.buildMessage( + IMessage.HIGH_SEVERITY, + JpaValidationMessages.QUERY_STATEMENT_UNDEFINED, + new String[] {this.name}, + this, + this.getNameTextRange() + ) + ); + } else { + this.validateQuery_(queryHelper, messages, reporter); + } + } + protected void validateQuery_(JpaJpqlQueryHelper queryHelper, List messages, IReporter reporter) { // nothing yet } @@ -134,7 +179,8 @@ } protected boolean isEquivalentTo(NamedNativeQuery other) { - return ObjectTools.equals(this.resultClass, other.getResultClass()) && + return ObjectTools.equals(this.query, other.getQuery()) && + ObjectTools.equals(this.resultClass, other.getResultClass()) && ObjectTools.equals(this.resultSetMapping, other.getResultSetMapping()); } diff --git src/org/eclipse/jpt/jpa/core/internal/jpa1/context/java/GenericJavaNamedQuery.java src/org/eclipse/jpt/jpa/core/internal/jpa1/context/java/GenericJavaNamedQuery.java index 2067ebf..d25d3a0 100644 --- src/org/eclipse/jpt/jpa/core/internal/jpa1/context/java/GenericJavaNamedQuery.java +++ src/org/eclipse/jpt/jpa/core/internal/jpa1/context/java/GenericJavaNamedQuery.java @@ -10,11 +10,17 @@ package org.eclipse.jpt.jpa.core.internal.jpa1.context.java; import java.util.List; +import org.eclipse.jpt.common.core.utility.TextRange; +import org.eclipse.jpt.common.utility.internal.ObjectTools; +import org.eclipse.jpt.common.utility.internal.StringTools; +import org.eclipse.jpt.jpa.core.context.NamedNativeQuery; import org.eclipse.jpt.jpa.core.context.NamedQuery; import org.eclipse.jpt.jpa.core.context.Query; import org.eclipse.jpt.jpa.core.context.java.JavaQueryContainer; import org.eclipse.jpt.jpa.core.context.orm.OrmQueryContainer; import org.eclipse.jpt.jpa.core.internal.context.java.AbstractJavaQuery; +import org.eclipse.jpt.jpa.core.internal.validation.DefaultJpaValidationMessages; +import org.eclipse.jpt.jpa.core.internal.validation.JpaValidationMessages; import org.eclipse.jpt.jpa.core.jpa2.context.LockModeType2_0; import org.eclipse.jpt.jpa.core.jpa2.context.NamedQuery2_0; import org.eclipse.jpt.jpa.core.jpa2.context.java.JavaNamedQuery2_0; @@ -31,12 +37,15 @@ extends AbstractJavaQuery implements JavaNamedQuery2_0 { + protected String query; + protected LockModeType2_0 specifiedLockMode; protected LockModeType2_0 defaultLockMode; public GenericJavaNamedQuery(JavaQueryContainer parent, NamedQueryAnnotation queryAnnotation) { super(parent, queryAnnotation); this.specifiedLockMode = this.buildSpecifiedLockMode(); + this.query = queryAnnotation.getQuery(); } // ********** synchronize/update ********** @@ -44,6 +53,7 @@ @Override public void synchronizeWithResourceModel() { super.synchronizeWithResourceModel(); + this.setQuery_(this.queryAnnotation.getQuery()); this.setSpecifiedLockMode_(this.buildSpecifiedLockMode()); } @@ -52,6 +62,27 @@ super.update(); this.setDefaultLockMode(this.buildDefaultLockMode()); } + + + // ********** query ********** + + public String getQuery() { + return this.query; + } + + public void setQuery(String query) { + this.queryAnnotation.setQuery(query); + this.setQuery_(query); + this.query = queryAnnotation.getQuery(); +} + + protected void setQuery_(String query) { + String old = this.query; + this.query = query; + this.firePropertyChanged(QUERY_PROPERTY, old, query); + } + + // ********** lock mode ********** public LockModeType2_0 getLockMode() { @@ -95,7 +126,8 @@ return LockModeType2_0.NONE; } return null; - } +} + // ********** metadata conversion ********* @@ -109,17 +141,45 @@ // ********** validation ********** - @Override + public void validate(JpaJpqlQueryHelper queryHelper, List messages, IReporter reporter) { + super.validate(messages, reporter); + this.validateQuery(queryHelper, messages, reporter); + } + + public void validateQuery(JpaJpqlQueryHelper queryHelper, List messages, IReporter reporter) { + if (StringTools.isBlank(this.query)){ + messages.add( + DefaultJpaValidationMessages.buildMessage( + IMessage.HIGH_SEVERITY, + JpaValidationMessages.QUERY_STATEMENT_UNDEFINED, + new String[] {this.name}, + this, + this.getNameTextRange() + ) + ); + } else { + this.validateQuery_(queryHelper, messages, reporter); + } + } + protected void validateQuery_(JpaJpqlQueryHelper queryHelper, List messages, IReporter reporter) { queryHelper.validate( - this, - this.query, - this.query, - this.queryAnnotation.getQueryTextRanges(), - 1, - JpaJpqlQueryHelper.EscapeType.JAVA, - messages - ); + this, + this.query, + this.query, + this.queryAnnotation.getQueryTextRanges(), + 1, + JpaJpqlQueryHelper.EscapeType.JAVA, + messages + ); + } + + public List getQueryTextRanges() { + return this.queryAnnotation.getQueryTextRanges(); + } + + protected boolean isEquivalentTo(NamedNativeQuery other) { + return ObjectTools.equals(this.query, other.getQuery()); } @Override @@ -129,7 +189,8 @@ } protected boolean isEquivalentTo(NamedQuery other) { - boolean queriesEquivalent = super.isEquivalentTo(other); + boolean queriesEquivalent = super.isEquivalentTo(other) && + ObjectTools.equals(this.query, other.getQuery()); if (this.isJpa2_0Compatible()) { return queriesEquivalent && this.isEquivalentTo((NamedQuery2_0) other); } @@ -145,4 +206,4 @@ public Class getType() { return NamedQuery.class; } -} \ No newline at end of file +} diff --git src/org/eclipse/jpt/jpa/core/internal/jpa1/context/orm/GenericOrmNamedNativeQuery.java src/org/eclipse/jpt/jpa/core/internal/jpa1/context/orm/GenericOrmNamedNativeQuery.java index b6294c4..bef9880 100644 --- src/org/eclipse/jpt/jpa/core/internal/jpa1/context/orm/GenericOrmNamedNativeQuery.java +++ src/org/eclipse/jpt/jpa/core/internal/jpa1/context/orm/GenericOrmNamedNativeQuery.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2012 Oracle. All rights reserved. + * Copyright (c) 2007, 2013 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. @@ -9,9 +9,7 @@ ******************************************************************************/ package org.eclipse.jpt.jpa.core.internal.jpa1.context.orm; -import java.util.Collections; import java.util.List; -import org.eclipse.jpt.common.core.utility.TextRange; import org.eclipse.jpt.common.utility.internal.ObjectTools; import org.eclipse.jpt.common.utility.internal.StringTools; import org.eclipse.jpt.jpa.core.context.JpaContextNode; @@ -170,10 +168,6 @@ ) ); } - } - - public List getQueryTextRanges() { - return Collections.singletonList(this.xmlQuery.getQueryTextRange()); } @Override diff --git src/org/eclipse/jpt/jpa/core/internal/jpa2_1/Generic2_1JpaAnnotationDefinitionProvider.java src/org/eclipse/jpt/jpa/core/internal/jpa2_1/Generic2_1JpaAnnotationDefinitionProvider.java index 0fea8ec..9a2a9f5 100644 --- src/org/eclipse/jpt/jpa/core/internal/jpa2_1/Generic2_1JpaAnnotationDefinitionProvider.java +++ src/org/eclipse/jpt/jpa/core/internal/jpa2_1/Generic2_1JpaAnnotationDefinitionProvider.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2012 Oracle. All rights reserved. + * Copyright (c) 2012, 2013 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. @@ -33,6 +33,8 @@ import org.eclipse.jpt.jpa.core.internal.jpa2.resource.java.SequenceGenerator2_0AnnotationDefinition; import org.eclipse.jpt.jpa.core.internal.jpa2.resource.java.StaticMetamodelAnnotationDefinition; import org.eclipse.jpt.jpa.core.internal.jpa2_1.resource.java.Converter2_1AnnotationDefinition; +import org.eclipse.jpt.jpa.core.internal.jpa2_1.resource.java.NamedStoredProcedureQueries2_1AnnotationDefinition; +import org.eclipse.jpt.jpa.core.internal.jpa2_1.resource.java.NamedStoredProcedureQuery2_1AnnotationDefinition; import org.eclipse.jpt.jpa.core.internal.resource.java.AssociationOverridesAnnotationDefinition; import org.eclipse.jpt.jpa.core.internal.resource.java.AttributeOverrideAnnotationDefinition; import org.eclipse.jpt.jpa.core.internal.resource.java.AttributeOverridesAnnotationDefinition; @@ -148,7 +150,8 @@ TableGeneratorAnnotationDefinition.instance(), TemporalAnnotationDefinition.instance(), TransientAnnotationDefinition.instance(), - VersionAnnotationDefinition.instance() + VersionAnnotationDefinition.instance(), + NamedStoredProcedureQueries2_1AnnotationDefinition.instance(), }; @Override @@ -165,5 +168,6 @@ NamedNativeQueryAnnotationDefinition.instance(), PrimaryKeyJoinColumnAnnotationDefinition.instance(), SecondaryTableAnnotationDefinition.instance(), + NamedStoredProcedureQuery2_1AnnotationDefinition.instance() }; } diff --git src/org/eclipse/jpt/jpa/core/internal/jpa2_1/resource/java/NamedStoredProcedureQueries2_1AnnotationDefinition.java src/org/eclipse/jpt/jpa/core/internal/jpa2_1/resource/java/NamedStoredProcedureQueries2_1AnnotationDefinition.java new file mode 100644 index 0000000..f119e0a --- /dev/null +++ src/org/eclipse/jpt/jpa/core/internal/jpa2_1/resource/java/NamedStoredProcedureQueries2_1AnnotationDefinition.java @@ -0,0 +1,60 @@ +/******************************************************************************* + * Copyright (c) 2013 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.jpa2_1.resource.java; + +import org.eclipse.jdt.core.IAnnotation; +import org.eclipse.jpt.common.core.internal.resource.java.binary.BinaryNamedAnnotation; +import org.eclipse.jpt.common.core.internal.resource.java.source.SourceNamedAnnotation; +import org.eclipse.jpt.common.core.resource.java.Annotation; +import org.eclipse.jpt.common.core.resource.java.AnnotationDefinition; +import org.eclipse.jpt.common.core.resource.java.JavaResourceAnnotatedElement; +import org.eclipse.jpt.common.core.utility.jdt.AnnotatedElement; +import org.eclipse.jpt.jpa.core.jpa2_1.resource.java.JPA2_1; + +/** + * javax.persistence.NamedStoredProcedureQueries + */ +public final class NamedStoredProcedureQueries2_1AnnotationDefinition + implements AnnotationDefinition +{ + // singleton + private static final AnnotationDefinition INSTANCE = new NamedStoredProcedureQueries2_1AnnotationDefinition(); + + /** + * Return the singleton. + */ + public static AnnotationDefinition instance() { + return INSTANCE; + } + + /** + * Ensure single instance. + */ + private NamedStoredProcedureQueries2_1AnnotationDefinition() { + super(); + } + + public Annotation buildAnnotation(JavaResourceAnnotatedElement parent, AnnotatedElement annotatedElement) { + return new SourceNamedAnnotation(parent, annotatedElement, getAnnotationName()); + } + + public Annotation buildNullAnnotation(JavaResourceAnnotatedElement parent) { + throw new UnsupportedOperationException(); + } + + public Annotation buildAnnotation(JavaResourceAnnotatedElement parent, IAnnotation jdtAnnotation) { + return new BinaryNamedAnnotation(parent, jdtAnnotation, getAnnotationName()); + } + + public String getAnnotationName() { + return JPA2_1.NAMED_STORED_PROCEDURE_QUERIES; + } + +} diff --git src/org/eclipse/jpt/jpa/core/internal/jpa2_1/resource/java/NamedStoredProcedureQuery2_1AnnotationDefinition.java src/org/eclipse/jpt/jpa/core/internal/jpa2_1/resource/java/NamedStoredProcedureQuery2_1AnnotationDefinition.java new file mode 100644 index 0000000..3ca9e0a --- /dev/null +++ src/org/eclipse/jpt/jpa/core/internal/jpa2_1/resource/java/NamedStoredProcedureQuery2_1AnnotationDefinition.java @@ -0,0 +1,63 @@ +/******************************************************************************* + * Copyright (c) 2013 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.jpa2_1.resource.java; + +import org.eclipse.jdt.core.IAnnotation; +import org.eclipse.jpt.common.core.resource.java.JavaResourceAnnotatedElement; +import org.eclipse.jpt.common.core.resource.java.NestableAnnotation; +import org.eclipse.jpt.common.core.resource.java.NestableAnnotationDefinition; +import org.eclipse.jpt.common.core.utility.jdt.AnnotatedElement; +import org.eclipse.jpt.jpa.core.internal.jpa2_1.resource.java.binary.BinaryNamedStoredProcedureQuery2_1Annotation; +import org.eclipse.jpt.jpa.core.internal.jpa2_1.resource.java.source.SourceNamedStoredProcedureQuery2_1Annotation; +import org.eclipse.jpt.jpa.core.jpa2_1.resource.java.JPA2_1; + +/** + * javax.persistence.NamedStoredProcedureQuery + */ +public final class NamedStoredProcedureQuery2_1AnnotationDefinition + implements NestableAnnotationDefinition +{ + // singleton + private static final NestableAnnotationDefinition INSTANCE = new NamedStoredProcedureQuery2_1AnnotationDefinition(); + + /** + * Return the singleton. + */ + public static NestableAnnotationDefinition instance() { + return INSTANCE; + } + + /** + * Ensure single instance. + */ + private NamedStoredProcedureQuery2_1AnnotationDefinition() { + super(); + } + + public NestableAnnotation buildAnnotation(JavaResourceAnnotatedElement parent, AnnotatedElement annotatedElement, int index) { + return SourceNamedStoredProcedureQuery2_1Annotation.buildSourceNamedStoredProcedureQuery2_1Annotation(parent, annotatedElement, index); + } + + public NestableAnnotation buildAnnotation(JavaResourceAnnotatedElement parent, IAnnotation jdtAnnotation, int index) { + return new BinaryNamedStoredProcedureQuery2_1Annotation(parent, jdtAnnotation); + } + + public String getNestableAnnotationName() { + return JPA2_1.NAMED_STORED_PROCEDURE_QUERY; + } + + public String getContainerAnnotationName() { + return JPA2_1.NAMED_STORED_PROCEDURE_QUERIES; + } + + public String getElementName() { + return JPA2_1.NAMED_STORED_PROCEDURE_QUERIES__VALUE; + } +} diff --git src/org/eclipse/jpt/jpa/core/internal/jpa2_1/resource/java/binary/BinaryNamedStoredProcedureQuery2_1Annotation.java src/org/eclipse/jpt/jpa/core/internal/jpa2_1/resource/java/binary/BinaryNamedStoredProcedureQuery2_1Annotation.java new file mode 100644 index 0000000..9e8d8f4 --- /dev/null +++ src/org/eclipse/jpt/jpa/core/internal/jpa2_1/resource/java/binary/BinaryNamedStoredProcedureQuery2_1Annotation.java @@ -0,0 +1,251 @@ +/******************************************************************************* + * Copyright (c) 2013 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.jpa2_1.resource.java.binary; + +import java.util.Vector; +import org.eclipse.jdt.core.IAnnotation; +import org.eclipse.jpt.common.core.resource.java.JavaResourceNode; +import org.eclipse.jpt.common.core.utility.TextRange; +import org.eclipse.jpt.common.utility.internal.iterable.LiveCloneListIterable; +import org.eclipse.jpt.common.utility.iterable.ListIterable; +import org.eclipse.jpt.jpa.core.internal.resource.java.binary.BinaryQueryAnnotation; +import org.eclipse.jpt.jpa.core.jpa2_1.resource.java.JPA2_1; +import org.eclipse.jpt.jpa.core.jpa2_1.resource.java.NamedStoredProcedureQuery2_1Annoation; +import org.eclipse.jpt.jpa.core.jpa2_1.resource.java.StoredProcedureParameter2_1Annotation; + +/** + * javax.persistence.NamedStoredProcedureQuery + */ +public final class BinaryNamedStoredProcedureQuery2_1Annotation + extends BinaryQueryAnnotation + implements NamedStoredProcedureQuery2_1Annoation +{ + private String procedureName; + private final Vector parameters; + private final Vector resultClasses; + private final Vector resultSetMappings; + + + public BinaryNamedStoredProcedureQuery2_1Annotation(JavaResourceNode parent, IAnnotation jdtAnnotation) { + super(parent, jdtAnnotation); + this.procedureName = this.buildProcedureName(); + this.parameters = this.buildParameters(); + this.resultClasses = this.buildResultClasses(); + this.resultSetMappings = this.buildResultSetMappings(); + } + + @Override + public void update() { + super.update(); + this.setProcedureName_(this.buildProcedureName()); + this.updateParameters(); + this.updateResultClasses(); + this.updateResultSetMappings(); + } + + public String getAnnotationName() { + return ANNOTATION_NAME; + } + + // ********** BinaryNamedStoredProcedureQueryAnnotation implementation ********** + + @Override + public String getNameElementName() { + return JPA2_1.NAMED_STORED_PROCEDURE_QUERY__NAME; + } + + @Override + public String getHintsElementName() { + return JPA2_1.NAMED_STORED_PROCEDURE_QUERY__HINTS; + } + + // ********** NamedStoredProcedureQueryAnnotation implementation ********** + + //****** procedure name ****** + public String getProcedureName() { + return this.procedureName; + } + + public void setProcedureName(String procedureName) { + throw new UnsupportedOperationException(); + } + + private void setProcedureName_(String procedureName) { + String old = this.procedureName; + this.procedureName = procedureName; + this.firePropertyChanged(PROCEDURE_NAME_PROPERTY, old, procedureName); + } + + private String buildProcedureName() { + return (String) this.getJdtMemberValue(this.getProcedureNameElementName()); + } + + public String getProcedureNameElementName() { + return JPA2_1.NAMED_STORED_PROCEDURE_QUERY__PROCEDURE_NAME; + } + + public TextRange getProcedureNameTextRange() { + throw new UnsupportedOperationException(); + } + + // ********* parameters *********** + public ListIterable getParameters() { + return new LiveCloneListIterable(this.parameters); + } + + public int getParametersSize() { + return this.parameters.size(); + } + + public StoredProcedureParameter2_1Annotation parameterAt(int index) { + return this.parameters.get(index); + } + + public StoredProcedureParameter2_1Annotation addParameter(int index) { + throw new UnsupportedOperationException(); + } + + public void moveParameter(int targetIndex, int sourceIndex) { + throw new UnsupportedOperationException(); + } + + public void removeParameter(int index) { + throw new UnsupportedOperationException(); + } + + private Vector buildParameters() { + Object[] jdtParameters = this.getJdtMemberValues(this.getParametersElementName()); + Vector result = new Vector(jdtParameters.length); + for (Object jdtParameter : jdtParameters) { + result.add(new BinaryStoredProcedureParameter2_1Annotation(this, (IAnnotation) jdtParameter)); + } + return result; + } + + private String getParametersElementName() { + return JPA2_1.NAMED_STORED_PROCEDURE_QUERY__PARAMETERS; + } + + // TODO + private void updateParameters() { + throw new UnsupportedOperationException(); + } + + // *********** result classes ********** + public ListIterable getResultClasses() { + return new LiveCloneListIterable(this.resultClasses); + } + + public int getResultClassesSize() { + return this.resultClasses.size(); + } + + public String resultClassAt(int index) { + return this.resultClasses.elementAt(index); + } + + public void addResultClass(String resultClass) { + throw new UnsupportedOperationException(); + } + + public void addResultClass(int index, String resultClass) { + throw new UnsupportedOperationException(); + } + + public void moveResultClass(int targetIndex, int sourceIndex) { + throw new UnsupportedOperationException(); + } + + public boolean resultClassTouches(int pos) { + throw new UnsupportedOperationException(); + } + + public void removeResultClass(String resultClass) { + throw new UnsupportedOperationException(); + } + + public void removeResultClass(int index) { + throw new UnsupportedOperationException(); + } + + private Vector buildResultClasses() { + Object[] jdtResultClasses = this.getJdtMemberValues(this.getResultClassesElementName()); + Vector result = new Vector(jdtResultClasses.length); + for (Object resultClass : jdtResultClasses) { + result.add((String) resultClass); + } + return result; + } + + private String getResultClassesElementName() { + return JPA2_1.NAMED_STORED_PROCEDURE_QUERY__RESULT_CLASSES; + } + + //TODO + private void updateResultClasses() { + throw new UnsupportedOperationException(); + } + + // *********** result set mappings ********** + public ListIterable getResultSetMappings() { + return new LiveCloneListIterable(this.resultSetMappings); + } + + public int getResultSetMappingsSize() { + return this.resultSetMappings.size(); + } + + public String resultSetMappingAt(int index) { + return this.resultSetMappings.elementAt(index); + } + + public void addResultSetMapping(String resultSetMapping) { + throw new UnsupportedOperationException(); + } + + public void addResultSetMapping(int index, String resultSetMapping) { + throw new UnsupportedOperationException(); + } + + public void moveResultSetMapping(int targetIndex, int sourceIndex) { + throw new UnsupportedOperationException(); + } + + public boolean resultSetMappingTouches(int pos) { + throw new UnsupportedOperationException(); + } + + public void removeResultSetMapping(String resultSetMapping) { + throw new UnsupportedOperationException(); + } + + public void removeResultSetMapping(int index) { + throw new UnsupportedOperationException(); + } + + private Vector buildResultSetMappings() { + Object[] jdtResultSetMappings = this.getJdtMemberValues(this.getResultSetMappingsElementName()); + Vector result = new Vector(jdtResultSetMappings.length); + for (Object resultSetMapping : jdtResultSetMappings) { + result.add((String) resultSetMapping); + } + return result; + } + + private String getResultSetMappingsElementName() { + return JPA2_1.NAMED_STORED_PROCEDURE_QUERY__RESULT_SET_MAPPINGS; + } + + //TODO + private void updateResultSetMappings() { + throw new UnsupportedOperationException(); + } + +} diff --git src/org/eclipse/jpt/jpa/core/internal/jpa2_1/resource/java/binary/BinaryStoredProcedureParameter2_1Annotation.java src/org/eclipse/jpt/jpa/core/internal/jpa2_1/resource/java/binary/BinaryStoredProcedureParameter2_1Annotation.java new file mode 100644 index 0000000..244ff1c --- /dev/null +++ src/org/eclipse/jpt/jpa/core/internal/jpa2_1/resource/java/binary/BinaryStoredProcedureParameter2_1Annotation.java @@ -0,0 +1,142 @@ +/******************************************************************************* + * Copyright (c) 2013 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.jpa2_1.resource.java.binary; + +import org.eclipse.jdt.core.IAnnotation; +import org.eclipse.jpt.common.core.internal.resource.java.binary.BinaryAnnotation; +import org.eclipse.jpt.common.core.resource.java.JavaResourceNode; +import org.eclipse.jpt.common.core.utility.TextRange; +import org.eclipse.jpt.jpa.core.jpa2_1.resource.java.JPA2_1; +import org.eclipse.jpt.jpa.core.jpa2_1.resource.java.ParameterMode2_1; +import org.eclipse.jpt.jpa.core.jpa2_1.resource.java.StoredProcedureParameter2_1Annotation; + +/** + * javax.persistence.StoredProcedureParameter + */ +public final class BinaryStoredProcedureParameter2_1Annotation + extends BinaryAnnotation + implements StoredProcedureParameter2_1Annotation +{ + private String name; + private ParameterMode2_1 mode; + private String type; + + + public BinaryStoredProcedureParameter2_1Annotation(JavaResourceNode parent, IAnnotation jdtAnnotation) { + super(parent, jdtAnnotation); + this.name = this.buildName(); + this.mode = this.buildMode(); + this.type = this.buildType(); + } + + public String getAnnotationName() { + return ANNOTATION_NAME; + } + + @Override + public void update() { + super.update(); + this.setName_(this.buildName()); + this.setMode_(this.buildMode()); + this.setType_(this.buildType()); + } + + + // ********** BinaryStoredProcedureParameterAnnotation implementation ********** + + String getNameElementName() { + return JPA2_1.NAMED_STORED_PROCEDURE_PARAMETER__NAME; + } + + String getModeElementName() { + return JPA2_1.NAMED_STORED_PROCEDURE_PARAMETER__MODE; + } + + String getTypeElementName() { + return JPA2_1.NAMED_STORED_PROCEDURE_PARAMETER__TYPE; + } + + // ********** StoredProcedureParameterAnnotation implementation ********** + + // ***** name + public String getName() { + return this.name; + } + + public void setName(String name) { + throw new UnsupportedOperationException(); + } + + private void setName_(String name) { + String old = this.name; + this.name = name; + this.firePropertyChanged(NAME_PROPERTY, old, name); + } + + private String buildName() { + return (String) this.getJdtMemberValue(this.getNameElementName()); + } + + public TextRange getNameTextRange() { + throw new UnsupportedOperationException(); + } + + // ***** mode + public ParameterMode2_1 getMode() { + return this.mode; + } + + public void setMode(ParameterMode2_1 mode) { + throw new UnsupportedOperationException(); + } + + private void setMode_(ParameterMode2_1 mode) { + ParameterMode2_1 old = this.mode; + this.mode = mode; + this.firePropertyChanged(MODE_PROPERTY, old, mode); + } + + private ParameterMode2_1 buildMode() { + return ParameterMode2_1.fromJavaAnnotationValue(this.getJdtMemberValue(this.getModeElementName())); + } + + public TextRange getModeTextRange() { + throw new UnsupportedOperationException(); + } + + // ***** type + public String getType() { + return this.type; + } + + public void setType(String type) { + throw new UnsupportedOperationException(); + } + + private void setType_(String type) { + String old = this.type; + this.type = type; + this.firePropertyChanged(TYPE_PROPERTY, old, type); + } + + private String buildType() { + return (String) this.getJdtMemberValue(this.getTypeElementName()); + } + + public TextRange getTypeTextRange() { + throw new UnsupportedOperationException(); + } + + // ***** fully-qualified type name + public String getFullyQualifiedTypeName() { + return this.type; + } + +} diff --git src/org/eclipse/jpt/jpa/core/internal/jpa2_1/resource/java/source/SourceNamedStoredProcedureQuery2_1Annotation.java src/org/eclipse/jpt/jpa/core/internal/jpa2_1/resource/java/source/SourceNamedStoredProcedureQuery2_1Annotation.java new file mode 100644 index 0000000..43f0ba1 --- /dev/null +++ src/org/eclipse/jpt/jpa/core/internal/jpa2_1/resource/java/source/SourceNamedStoredProcedureQuery2_1Annotation.java @@ -0,0 +1,475 @@ +/******************************************************************************* + * Copyright (c) 2013 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.jpa2_1.resource.java.source; + +import java.util.Arrays; +import java.util.Vector; +import org.eclipse.jdt.core.dom.Annotation; +import org.eclipse.jpt.common.core.internal.utility.jdt.AnnotatedElementAnnotationElementAdapter; +import org.eclipse.jpt.common.core.internal.utility.jdt.AnnotationStringArrayExpressionConverter; +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.SimpleDeclarationAnnotationAdapter; +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.ExpressionConverter; +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.collection.ListTools; +import org.eclipse.jpt.common.utility.internal.iterable.LiveCloneListIterable; +import org.eclipse.jpt.common.utility.iterable.ListIterable; +import org.eclipse.jpt.jpa.core.internal.resource.java.source.SourceQueryAnnotation; +import org.eclipse.jpt.jpa.core.internal.resource.java.source.SourceQueryHintAnnotation; +import org.eclipse.jpt.jpa.core.jpa2_1.resource.java.JPA2_1; +import org.eclipse.jpt.jpa.core.jpa2_1.resource.java.NamedStoredProcedureQuery2_1Annoation; +import org.eclipse.jpt.jpa.core.jpa2_1.resource.java.StoredProcedureParameter2_1Annotation; +import org.eclipse.jpt.jpa.core.resource.java.QueryHintAnnotation; + +/** + * javax.persistence.NamedStoredProcedureQuery + */ +public final class SourceNamedStoredProcedureQuery2_1Annotation + extends SourceQueryAnnotation + implements NamedStoredProcedureQuery2_1Annoation +{ + private static final DeclarationAnnotationAdapter DECLARATION_ANNOTATION_ADAPTER = new SimpleDeclarationAnnotationAdapter(ANNOTATION_NAME); + private static final DeclarationAnnotationAdapter CONTAINER_DECLARATION_ANNOTATION_ADAPTER = new SimpleDeclarationAnnotationAdapter(JPA2_1.NAMED_STORED_PROCEDURE_QUERIES); + + DeclarationAnnotationElementAdapter procedureNameDeclarationAdapter; + AnnotationElementAdapter procedureNameAdapter; + String procedureName; + TextRange procedureNameTextRange; + + final StoredProcedureParameterAnnotationContainer storedProcedureParametersContainer = new StoredProcedureParameterAnnotationContainer(); + + private DeclarationAnnotationElementAdapter resultClassesDeclarationAdapter; + private AnnotationElementAdapter resultClassesAdapter; + private final Vector resultClasses = new Vector(); + private TextRange resultClassesTextRange; + + /** + * TODO: Need to handle fullyQualifiedResultClassName/fqResultClassNameStale for each of the result classes + private final Vector fullyQualifiedResultClassNames = new Vector(); + // we need a flag since the f-q name can be null + private boolean fqResultClassNameStale = true; + */ + + private DeclarationAnnotationElementAdapter resultSetMappingsDeclarationAdapter; + private AnnotationElementAdapter resultSetMappingsAdapter; + private final Vector resultSetMappings = new Vector(); + private TextRange resultSetMappingsTextRange; + + public static SourceNamedStoredProcedureQuery2_1Annotation buildSourceNamedStoredProcedureQuery2_1Annotation( + JavaResourceAnnotatedElement parent, + AnnotatedElement element, + int index) + { + IndexedDeclarationAnnotationAdapter idaa = buildNamedStoredProcedureQuery2_1DeclarationAnnotationAdapter(index); + IndexedAnnotationAdapter iaa = buildNamedStoredProcedureQuery2_1AnnotationAdapter(element, idaa); + return new SourceNamedStoredProcedureQuery2_1Annotation( + parent, + element, + idaa, + iaa); + } + + private SourceNamedStoredProcedureQuery2_1Annotation( + JavaResourceAnnotatedElement parent, + AnnotatedElement element, + IndexedDeclarationAnnotationAdapter daa, + IndexedAnnotationAdapter annotationAdapter) + { + super(parent, element, daa, annotationAdapter); + this.procedureNameDeclarationAdapter = this.buildProcedureNameDeclarationAdapter(); + this.procedureNameAdapter = this.buildProcedureNameAdapter(); + this.resultClassesDeclarationAdapter = this.buildResultClassesDeclarationAdapter(); + this.resultClassesAdapter = this.buildResultClassesAdapter(); + this.resultSetMappingsDeclarationAdapter = this.buildResultSetMappingsDeclarationAdapter(); + this.resultSetMappingsAdapter = this.buildResultSetMappingsAdapter(); + } + + public String getAnnotationName() { + return ANNOTATION_NAME; + } + + @Override + public void initialize(Annotation astAnnotation) { + super.initialize(astAnnotation); + + this.procedureName = this.buildProcedureName(astAnnotation); + this.procedureNameTextRange = this.buildProcedureNameTextRange(astAnnotation); + + this.storedProcedureParametersContainer.initializeFromContainerAnnotation(astAnnotation); + + this.initializeResultClasses(astAnnotation); + this.resultClassesTextRange = this.buildResultClassesTextRange(astAnnotation); + + this.initializeResultSetMappings(astAnnotation); + this.resultSetMappingsTextRange = this.buildResultSetMappingsTextRange(astAnnotation); + } + + @Override + public void synchronizeWith(Annotation astAnnotation) { + super.synchronizeWith(astAnnotation); + + this.syncProcedureName(this.buildProcedureName(astAnnotation)); + this.procedureNameTextRange = this.buildProcedureNameTextRange(astAnnotation); + + this.storedProcedureParametersContainer.synchronize(astAnnotation); + + this.syncResultClasses(astAnnotation); + this.resultClassesTextRange = this.buildResultClassesTextRange(astAnnotation); + + this.syncResultSetMappings(astAnnotation); + this.resultSetMappingsTextRange = this.buildResultSetMappingsTextRange(astAnnotation); + + } + + // ********** AbstractNamedStoredProcedureQuery2_1Annotation implementation ********** + + @Override + public String getNameElementName() { + return JPA2_1.NAMED_STORED_PROCEDURE_QUERY__NAME; + } + + @Override + public String getHintsElementName() { + return JPA2_1.NAMED_STORED_PROCEDURE_QUERY__HINTS; + } + + @Override + public QueryHintAnnotation buildHint(int index) { + return SourceQueryHintAnnotation.buildNamedStoredProcedureQuery2_1QueryHint(this, this.annotatedElement, this.daa, index); + } + + + // ********** NamedStoredProcedureQuery2_1Annotation implementation ********** + + String getProcedureNameElementName() { + return JPA2_1.NAMED_STORED_PROCEDURE_QUERY__PROCEDURE_NAME; + } + + String getParametersElementName() { + return JPA2_1.NAMED_STORED_PROCEDURE_QUERY__PARAMETERS; + } + + public StoredProcedureParameter2_1Annotation buildParameter(int index) { + return SourceStoredProcedureParameter2_1Annotation.buildNamedStoredProcedureQuery2_1Parameter(this, this.annotatedElement, this.daa, index); + } + + String getResultClassesElementName() { + return JPA2_1.NAMED_STORED_PROCEDURE_QUERY__RESULT_CLASSES; + } + + String getResultSetMappingsElementName() { + return JPA2_1.NAMED_STORED_PROCEDURE_QUERY__RESULT_SET_MAPPINGS; + } + + // ***** procedure name + public String getProcedureName() { + return this.procedureName; + } + + public void setProcedureName(String procedureName) { + if (this.attributeValueHasChanged(this.procedureName, procedureName)) { + this.procedureName = procedureName; + this.procedureNameAdapter.setValue(procedureName); + } + } + + private void syncProcedureName(String procedureName) { + String old = this.procedureName; + this.procedureName = procedureName; + this.firePropertyChanged(PROCEDURE_NAME_PROPERTY, old, procedureName); + } + + private String buildProcedureName(Annotation astAnnotation) { + return this.procedureNameAdapter.getValue(astAnnotation); + } + + public TextRange getProcedureNameTextRange() { + return this.procedureNameTextRange; + } + + private TextRange buildProcedureNameTextRange(Annotation astAnnotation) { + return this.getElementTextRange(this.procedureNameDeclarationAdapter, astAnnotation); + } + + private DeclarationAnnotationElementAdapter buildProcedureNameDeclarationAdapter() { + return ConversionDeclarationAnnotationElementAdapter.forStrings(this.daa, this.getProcedureNameElementName()); + } + + private AnnotationElementAdapter buildProcedureNameAdapter() { + return this.buildStringElementAdapter(this.procedureNameDeclarationAdapter); + } + + + // ***** parameters + public ListIterable getParameters() { + return this.storedProcedureParametersContainer.getNestedAnnotations(); + } + + public int getParametersSize() { + return this.storedProcedureParametersContainer.getNestedAnnotationsSize(); + } + + public StoredProcedureParameter2_1Annotation parameterAt(int index) { + return this.storedProcedureParametersContainer.getNestedAnnotation(index); + } + + public StoredProcedureParameter2_1Annotation addParameter(int index) { + return this.storedProcedureParametersContainer.addNestedAnnotation(index); + } + + public void moveParameter(int targetIndex, int sourceIndex) { + this.storedProcedureParametersContainer.moveNestedAnnotation(targetIndex, sourceIndex); + } + + public void removeParameter(int index) { + this.storedProcedureParametersContainer.removeNestedAnnotation(index); + } + + + // ***** result classes + public ListIterable getResultClasses() { + return new LiveCloneListIterable(this.resultClasses); + } + + public int getResultClassesSize() { + return this.resultClasses.size(); + } + + public String resultClassAt(int index) { + return this.resultClasses.elementAt(index); + } + + public void addResultClass(String resultClass) { + this.addResultClass(this.resultClasses.size(), resultClass); + } + + public void addResultClass(int index, String resultClass) { + this.resultClasses.add(index, resultClass); + this.writeResultClasses(); + } + + public void moveResultClass(int targetIndex, int sourceIndex) { + ListTools.move(this.resultClasses, targetIndex, sourceIndex); + this.writeResultClasses(); + } + + public void removeResultClass(String resultClass) { + this.resultClasses.remove(resultClass); + this.writeResultClasses(); + } + + public void removeResultClass(int index) { + this.resultClasses.remove(index); + this.writeResultClasses(); + } + + private void writeResultClasses() { + this.resultClassesAdapter.setValue(this.resultClasses.toArray(new String[this.resultClasses.size()])); + } + + private void initializeResultClasses(Annotation astAnnotation) { + String[] astResultClasses = this.resultClassesAdapter.getValue(astAnnotation); + for (int i = 0; i < astResultClasses.length; i++) { + this.resultClasses.add(astResultClasses[i]); + } + } + + private void syncResultClasses(Annotation astAnnotation) { + String[] javaResultClasses = this.resultClassesAdapter.getValue(astAnnotation); + this.synchronizeList(Arrays.asList(javaResultClasses), this.resultClasses, RESULT_CLASSES_LIST); + } + + private TextRange buildResultClassesTextRange(Annotation astAnnotation) { + return this.getElementTextRange(this.resultClassesDeclarationAdapter, astAnnotation); + } + + public boolean resultClassesTouches(int pos) { + return this.textRangeTouches(this.resultClassesTextRange, pos); + } + + private DeclarationAnnotationElementAdapter buildResultClassesDeclarationAdapter() { + return buildResultClassesArrayAnnotationElementAdapter(this.daa, JPA2_1.NAMED_STORED_PROCEDURE_QUERY__RESULT_CLASSES); + } + + private AnnotationElementAdapter buildResultClassesAdapter() { + return this.buildAnnotationElementAdapter(this.resultClassesDeclarationAdapter); + } + + + // ***** result set mappings + public ListIterable getResultSetMappings() { + return new LiveCloneListIterable(this.resultSetMappings); + } + + public int getResultSetMappingsSize() { + return this.resultSetMappings.size(); + } + + public String resultSetMappingAt(int index) { + return this.resultSetMappings.elementAt(index); + } + + public void addResultSetMapping(String resultClass) { + this.addResultSetMapping(this.resultSetMappings.size(), resultClass); + } + + public void addResultSetMapping(int index, String resultClass) { + this.resultSetMappings.add(index, resultClass); + this.writeResultSetMappings(); + } + + public void moveResultSetMapping(int targetIndex, int sourceIndex) { + ListTools.move(this.resultSetMappings, targetIndex, sourceIndex); + this.writeResultSetMappings(); + } + + public void removeResultSetMapping(String resultSetMapping) { + this.resultSetMappings.remove(resultSetMapping); + this.writeResultSetMappings(); + } + + public void removeResultSetMapping(int index) { + this.resultSetMappings.remove(index); + this.writeResultSetMappings(); + } + + private void writeResultSetMappings() { + this.resultSetMappingsAdapter.setValue(this.resultSetMappings.toArray(new String[this.resultSetMappings.size()])); + } + + private void initializeResultSetMappings(Annotation astAnnotation) { + String[] astResultClasses = this.resultSetMappingsAdapter.getValue(astAnnotation); + for (int i = 0; i < astResultClasses.length; i++) { + this.resultSetMappings.add(astResultClasses[i]); + } + } + + private void syncResultSetMappings(Annotation astAnnotation) { + String[] javaResultClasses = this.resultSetMappingsAdapter.getValue(astAnnotation); + this.synchronizeList(Arrays.asList(javaResultClasses), this.resultSetMappings, RESULT_SET_MAPPINGS_LIST); + } + + private TextRange buildResultSetMappingsTextRange(Annotation astAnnotation) { + return this.getElementTextRange(this.resultSetMappingsDeclarationAdapter, astAnnotation); + } + + public boolean resultSetMappingsTouches(int pos) { + return this.textRangeTouches(this.resultSetMappingsTextRange, pos); + } + + private DeclarationAnnotationElementAdapter buildResultSetMappingsDeclarationAdapter() { + return buildResultSetMappingsArrayAnnotationElementAdapter(this.daa, JPA2_1.NAMED_STORED_PROCEDURE_QUERY__RESULT_SET_MAPPINGS); + } + + private AnnotationElementAdapter buildResultSetMappingsAdapter() { + return this.buildAnnotationElementAdapter(this.resultSetMappingsDeclarationAdapter); + } + + // ********** + private AnnotationElementAdapter buildAnnotationElementAdapter(DeclarationAnnotationElementAdapter daea) { + return new AnnotatedElementAnnotationElementAdapter(this.annotatedElement, daea); + } + + private static DeclarationAnnotationElementAdapter buildResultClassesArrayAnnotationElementAdapter( + DeclarationAnnotationAdapter annotationAdapter, + String elementName) + { + return buildArrayAnnotationElementAdapter( + annotationAdapter, + elementName, + AnnotationStringArrayExpressionConverter.forTypes()); + } + + private static DeclarationAnnotationElementAdapter buildResultSetMappingsArrayAnnotationElementAdapter( + DeclarationAnnotationAdapter annotationAdapter, + String elementName) + { + return buildArrayAnnotationElementAdapter( + annotationAdapter, + elementName, + AnnotationStringArrayExpressionConverter.forStrings()); + } + + private static DeclarationAnnotationElementAdapter buildArrayAnnotationElementAdapter( + DeclarationAnnotationAdapter annotationAdapter, + String elementName, + ExpressionConverter converter) + { + return new ConversionDeclarationAnnotationElementAdapter( + annotationAdapter, + elementName, + converter); + } + + + // ********** stored procedure parameter container ********** + /** + * adapt the AnnotationContainer interface to the xml schema's xmlns + */ + class StoredProcedureParameterAnnotationContainer + extends AnnotationContainer + { + @Override + protected String getNestedAnnotationsListName() { + return PARAMETERS_LIST; + } + @Override + protected String getElementName() { + return SourceNamedStoredProcedureQuery2_1Annotation.this.getParametersElementName(); + } + @Override + protected String getNestedAnnotationName() { + return StoredProcedureParameter2_1Annotation.ANNOTATION_NAME; + } + @Override + protected StoredProcedureParameter2_1Annotation buildNestedAnnotation(int index) { + return SourceNamedStoredProcedureQuery2_1Annotation.this.buildParameter(index); + } + } + + // ********** misc ********** + + @Override + public boolean isUnset() { + return super.isUnset() && + (this.procedureName == null) && + (this.storedProcedureParametersContainer.isEmpty()) && + (this.resultClasses.isEmpty()) && + (this.resultSetMappings.isEmpty()); + } + + // ********** static methods ********** + + private static IndexedAnnotationAdapter buildNamedStoredProcedureQuery2_1AnnotationAdapter( + AnnotatedElement annotatedElement, + IndexedDeclarationAnnotationAdapter idaa) + { + return new ElementIndexedAnnotationAdapter(annotatedElement, idaa); + } + + private static IndexedDeclarationAnnotationAdapter buildNamedStoredProcedureQuery2_1DeclarationAnnotationAdapter(int index) + { + return new CombinationIndexedDeclarationAnnotationAdapter( + DECLARATION_ANNOTATION_ADAPTER, + CONTAINER_DECLARATION_ANNOTATION_ADAPTER, + index, + ANNOTATION_NAME); + } + +} diff --git src/org/eclipse/jpt/jpa/core/internal/jpa2_1/resource/java/source/SourceStoredProcedureParameter2_1Annotation.java src/org/eclipse/jpt/jpa/core/internal/jpa2_1/resource/java/source/SourceStoredProcedureParameter2_1Annotation.java new file mode 100644 index 0000000..c610e32 --- /dev/null +++ src/org/eclipse/jpt/jpa/core/internal/jpa2_1/resource/java/source/SourceStoredProcedureParameter2_1Annotation.java @@ -0,0 +1,319 @@ +/******************************************************************************* + * Copyright (c) 2013 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.jpa2_1.resource.java.source; + +import org.eclipse.jdt.core.dom.Annotation; +import org.eclipse.jpt.common.core.internal.resource.java.source.SourceAnnotation; +import org.eclipse.jpt.common.core.internal.utility.jdt.ASTTools; +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.EnumDeclarationAnnotationElementAdapter; +import org.eclipse.jpt.common.core.internal.utility.jdt.NestedIndexedDeclarationAnnotationAdapter; +import org.eclipse.jpt.common.core.internal.utility.jdt.TypeStringExpressionConverter; +import org.eclipse.jpt.common.core.resource.java.JavaResourceNode; +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.ExpressionConverter; +import org.eclipse.jpt.common.core.utility.jdt.IndexedDeclarationAnnotationAdapter; +import org.eclipse.jpt.jpa.core.jpa2_1.resource.java.JPA2_1; +import org.eclipse.jpt.jpa.core.jpa2_1.resource.java.ParameterMode2_1; +import org.eclipse.jpt.jpa.core.jpa2_1.resource.java.StoredProcedureParameter2_1Annotation; + +/** + * javax.persistence.StoredProcedureParameter + */ +public final class SourceStoredProcedureParameter2_1Annotation + extends SourceAnnotation + implements StoredProcedureParameter2_1Annotation +{ + private DeclarationAnnotationElementAdapter nameDeclarationAdapter; + private AnnotationElementAdapter nameAdapter; + private String name; + private TextRange nameTextRange; + + private DeclarationAnnotationElementAdapter modeDeclarationAdapter; + private AnnotationElementAdapter modeAdapter; + private ParameterMode2_1 mode; + private TextRange modeTextRange; + + private DeclarationAnnotationElementAdapter typeDeclarationAdapter; + private AnnotationElementAdapter typeAdapter; + private String type; + private TextRange typeTextRange; + + private String fullyQualifiedTypeName; + // we need a flag since the f-q name can be null + private boolean fqTypeNameStale = true; + + public static SourceStoredProcedureParameter2_1Annotation buildNamedStoredProcedureQuery2_1Parameter( + JavaResourceNode parent, + AnnotatedElement element, + DeclarationAnnotationAdapter namedStoredProcedureQueryAdapter, + int index) + { + return buildNestedSourceStoredProcedureParameter2_1Annotation( + parent, + element, + buildStoredProcedureParameter2_1AnnotationAdapter(namedStoredProcedureQueryAdapter, + index)); + } + + + public static SourceStoredProcedureParameter2_1Annotation buildNestedSourceStoredProcedureParameter2_1Annotation( + JavaResourceNode parent, + AnnotatedElement element, + IndexedDeclarationAnnotationAdapter idaa) + { + return new SourceStoredProcedureParameter2_1Annotation( + parent, + element, + idaa); + } + + private SourceStoredProcedureParameter2_1Annotation( + JavaResourceNode parent, + AnnotatedElement element, + IndexedDeclarationAnnotationAdapter idaa) + { + super(parent, element, idaa, new ElementIndexedAnnotationAdapter(element, idaa)); + this.nameDeclarationAdapter = this.buildNameDeclarationAdapter(); + this.nameAdapter = this.buildNameAdapter(); + this.modeDeclarationAdapter = this.buildModeDeclarationAdapter(); + this.modeAdapter = this.buildModeAdapter(); + this.typeDeclarationAdapter = this.buildTypeDeclarationAdapter(); + this.typeAdapter = this.buildTypeAdapter(); + } + + public String getAnnotationName() { + return ANNOTATION_NAME; + } + + @Override + public void initialize(Annotation astAnnotation) { + super.initialize(astAnnotation); + this.name = this.buildName(astAnnotation); + this.nameTextRange = this.buildNameTextRange(astAnnotation); + + this.mode = this.buildMode(astAnnotation); + this.modeTextRange = this.buildModeTextRange(astAnnotation); + + this.type = this.buildType(astAnnotation); + this.typeTextRange = this.buildTypeTextRange(astAnnotation); + } + + @Override + public void synchronizeWith(Annotation astAnnotation) { + super.synchronizeWith(astAnnotation); + this.syncName(this.buildName(astAnnotation)); + this.nameTextRange = this.buildNameTextRange(astAnnotation); + + this.syncMode(this.buildMode(astAnnotation)); + this.modeTextRange = this.buildModeTextRange(astAnnotation); + + this.syncType(this.buildType(astAnnotation)); + this.typeTextRange = this.buildTypeTextRange(astAnnotation); + } + + + // ********** StoredProcedureParameterAnnotation implementation ********** + + // ***** name + public String getName() { + return this.name; + } + + public void setName(String name) { + if (this.attributeValueHasChanged(this.name, name)) { + this.name = name; + this.nameAdapter.setValue(name); + } + } + + private void syncName(String astName) { + String old = this.name; + this.name = astName; + this.firePropertyChanged(NAME_PROPERTY, old, astName); + } + + private String buildName(Annotation astAnnotation) { + return this.nameAdapter.getValue(astAnnotation); + } + + public TextRange getNameTextRange() { + return this.nameTextRange; + } + + private TextRange buildNameTextRange(Annotation astAnnotation) { + return this.getElementTextRange(this.nameDeclarationAdapter, astAnnotation); + } + + private DeclarationAnnotationElementAdapter buildNameDeclarationAdapter() { + return ConversionDeclarationAnnotationElementAdapter.forStrings(this.daa, JPA2_1.NAMED_STORED_PROCEDURE_PARAMETER__NAME); + } + + private AnnotationElementAdapter buildNameAdapter() { + return this.buildStringElementAdapter(this.nameDeclarationAdapter); + } + + // ***** mode + public ParameterMode2_1 getMode() { + return this.mode; + } + + public void setMode(ParameterMode2_1 mode) { + if (this.attributeValueHasChanged(this.mode, mode)) { + this.mode = mode; + this.modeAdapter.setValue(ParameterMode2_1.toJavaAnnotationValue(mode)); + } + } + + private void syncMode(ParameterMode2_1 astValue) { + ParameterMode2_1 old = this.mode; + this.mode = astValue; + this.firePropertyChanged(MODE_PROPERTY, old, astValue); + } + + private ParameterMode2_1 buildMode(Annotation astAnnotation) { + return ParameterMode2_1.fromJavaAnnotationValue(this.modeAdapter.getValue(astAnnotation)); + } + + public TextRange getModeTextRange() { + return this.modeTextRange; + } + + private TextRange buildModeTextRange(Annotation astAnnotation) { + return this.getElementTextRange(this.modeDeclarationAdapter, astAnnotation); + } + + private DeclarationAnnotationElementAdapter buildModeDeclarationAdapter() { + return new EnumDeclarationAnnotationElementAdapter(this.daa, JPA2_1.NAMED_STORED_PROCEDURE_PARAMETER__MODE); + } + + private AnnotationElementAdapter buildModeAdapter() { + return this.buildStringElementAdapter(this.modeDeclarationAdapter); + } + + // ***** type + public String getType() { + return this.type; + } + + public void setType(String type) { + if (this.attributeValueHasChanged(this.type, type)) { + this.type = type; + this.fqTypeNameStale = true; + this.typeAdapter.setValue(type); + } + } + + private void syncType(String astType) { + if (this.attributeValueHasChanged(this.type, astType)) { + this.syncType_(astType); + } + } + + private void syncType_(String astType) { + String old = this.type; + this.type = astType; + this.fqTypeNameStale = true; + this.firePropertyChanged(TYPE_PROPERTY, old, astType); + } + + private String buildType(Annotation astAnnotation) { + return this.typeAdapter.getValue(astAnnotation); + } + + public TextRange getTypeTextRange() { + return this.typeTextRange; + } + + private TextRange buildTypeTextRange(Annotation astAnnotation) { + return this.getElementTextRange(this.typeDeclarationAdapter, astAnnotation); + } + + private DeclarationAnnotationElementAdapter buildTypeDeclarationAdapter() { + return buildTypeDeclarationAdapter(this.daa, JPA2_1.NAMED_STORED_PROCEDURE_PARAMETER__TYPE); + } + + private static DeclarationAnnotationElementAdapter buildTypeDeclarationAdapter( + DeclarationAnnotationAdapter annotationAdapter, + String elementName) + { + return buildAnnotationElementAdapter( + annotationAdapter, + elementName, + TypeStringExpressionConverter.instance()); + } + + private static DeclarationAnnotationElementAdapter buildAnnotationElementAdapter( + DeclarationAnnotationAdapter annotationAdapter, + String elementName, + ExpressionConverter converter) + { + return new ConversionDeclarationAnnotationElementAdapter( + annotationAdapter, + elementName, + converter); + } + + private AnnotationElementAdapter buildTypeAdapter() { + return this.buildStringElementAdapter(this.typeDeclarationAdapter); + } + + // ***** fully-qualified type name + public String getFullyQualifiedTypeName() { + if (this.fqTypeNameStale) { + this.fullyQualifiedTypeName = this.buildFullyQualifiedTypeName(); + this.fqTypeNameStale = false; + } + return this.fullyQualifiedTypeName; + } + + private String buildFullyQualifiedTypeName() { + return (this.type == null) ? null : this.buildFullyQualifiedTypeName_(); + } + + private String buildFullyQualifiedTypeName_() { + return ASTTools.resolveFullyQualifiedName(this.typeAdapter.getExpression(this.buildASTRoot())); + } + + + // ********** misc ********** + + @Override + public boolean isUnset() { + return super.isUnset() && + (this.name == null) && + (this.mode == null) && + (this.type == null); + } + + @Override + public void toString(StringBuilder sb) { + sb.append(this.name); + } + + + // ********** static methods ********** + private static IndexedDeclarationAnnotationAdapter buildStoredProcedureParameter2_1AnnotationAdapter( + DeclarationAnnotationAdapter namedStoredProcedureQuery2_1Adapter, + int index) + { + return new NestedIndexedDeclarationAnnotationAdapter( + namedStoredProcedureQuery2_1Adapter, + JPA2_1.NAMED_STORED_PROCEDURE_QUERY__PARAMETERS, + index, + ANNOTATION_NAME); + } + +} diff --git src/org/eclipse/jpt/jpa/core/internal/resource/java/binary/BinaryNamedNativeQueryAnnotation.java src/org/eclipse/jpt/jpa/core/internal/resource/java/binary/BinaryNamedNativeQueryAnnotation.java index bcb3e04..a90992a 100644 --- src/org/eclipse/jpt/jpa/core/internal/resource/java/binary/BinaryNamedNativeQueryAnnotation.java +++ src/org/eclipse/jpt/jpa/core/internal/resource/java/binary/BinaryNamedNativeQueryAnnotation.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2012 Oracle. All rights reserved. + * 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 v1.0, which accompanies this distribution * and is available at http://www.eclipse.org/legal/epl-v10.html. @@ -9,6 +9,7 @@ ******************************************************************************/ package org.eclipse.jpt.jpa.core.internal.resource.java.binary; +import java.util.List; import org.eclipse.jdt.core.IAnnotation; import org.eclipse.jpt.common.core.resource.java.JavaResourceNode; import org.eclipse.jpt.common.core.utility.TextRange; @@ -22,12 +23,14 @@ extends BinaryQueryAnnotation implements NamedNativeQueryAnnotation { + private String query; private String resultClass; private String resultSetMapping; public BinaryNamedNativeQueryAnnotation(JavaResourceNode parent, IAnnotation jdtAnnotation) { super(parent, jdtAnnotation); + this.query = this.buildQuery(); this.resultClass = this.buildResultClass(); this.resultSetMapping = this.buildResultSetMapping(); } @@ -39,31 +42,54 @@ @Override public void update() { super.update(); + this.setQuery_(this.buildQuery()); this.setResultClass_(this.buildResultClass()); this.setResultSetMapping_(this.buildResultSetMapping()); } - // ********** BinaryBaseNamedQueryAnnotation implementation ********** + // ********** BinaryNamedNativeQueryAnnotation implementation ********** @Override - String getNameElementName() { + public String getNameElementName() { return JPA.NAMED_NATIVE_QUERY__NAME; } @Override - String getQueryElementName() { - return JPA.NAMED_NATIVE_QUERY__QUERY; - } - - @Override - String getHintsElementName() { + public String getHintsElementName() { return JPA.NAMED_NATIVE_QUERY__HINTS; } // ********** NamedNativeQueryAnnotation implementation ********** + String getQueryElementName() { + return JPA.NAMED_NATIVE_QUERY__QUERY; + } + + // ***** query + public String getQuery() { + return this.query; + } + + public void setQuery(String query) { + throw new UnsupportedOperationException(); + } + + private void setQuery_(String query) { + String old = this.query; + this.query = query; + this.firePropertyChanged(QUERY_PROPERTY, old, query); + } + + private String buildQuery() { + return (String) this.getJdtMemberValue(this.getQueryElementName()); + } + + public List getQueryTextRanges() { + throw new UnsupportedOperationException(); + } + // ***** result class public String getResultClass() { return this.resultClass; diff --git src/org/eclipse/jpt/jpa/core/internal/resource/java/binary/BinaryNamedQueryAnnotation.java src/org/eclipse/jpt/jpa/core/internal/resource/java/binary/BinaryNamedQueryAnnotation.java index 878abcb..f8a3e40 100644 --- src/org/eclipse/jpt/jpa/core/internal/resource/java/binary/BinaryNamedQueryAnnotation.java +++ src/org/eclipse/jpt/jpa/core/internal/resource/java/binary/BinaryNamedQueryAnnotation.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009, 2011 Oracle. All rights reserved. + * 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 v1.0, which accompanies this distribution * and is available at http://www.eclipse.org/legal/epl-v10.html. @@ -9,8 +9,10 @@ ******************************************************************************/ package org.eclipse.jpt.jpa.core.internal.resource.java.binary; +import java.util.List; import org.eclipse.jdt.core.IAnnotation; import org.eclipse.jpt.common.core.resource.java.JavaResourceNode; +import org.eclipse.jpt.common.core.utility.TextRange; import org.eclipse.jpt.jpa.core.resource.java.JPA; import org.eclipse.jpt.jpa.core.resource.java.NamedQueryAnnotation; @@ -21,30 +23,64 @@ extends BinaryQueryAnnotation implements NamedQueryAnnotation { + private String query; + public BinaryNamedQueryAnnotation(JavaResourceNode parent, IAnnotation jdtAnnotation) { super(parent, jdtAnnotation); + this.query = this.buildQuery(); } public String getAnnotationName() { return ANNOTATION_NAME; } + @Override + public void update() { + super.update(); + this.setQuery_(this.buildQuery()); + } - // ********** BinaryBaseNamedQueryAnnotation implementation ********** + + // ********** BinaryNamedQueryAnnotation implementation ********** @Override - String getNameElementName() { + public String getNameElementName() { return JPA.NAMED_QUERY__NAME; } - @Override String getQueryElementName() { return JPA.NAMED_QUERY__QUERY; } @Override - String getHintsElementName() { + public String getHintsElementName() { return JPA.NAMED_QUERY__HINTS; } + + // ********** NamedNativeQueryAnnotation implementation ********** + + // ***** query + public String getQuery() { + return this.query; + } + + public void setQuery(String query) { + throw new UnsupportedOperationException(); + } + + private void setQuery_(String query) { + String old = this.query; + this.query = query; + this.firePropertyChanged(QUERY_PROPERTY, old, query); + } + + private String buildQuery() { + return (String) this.getJdtMemberValue(this.getQueryElementName()); + } + + public List getQueryTextRanges() { + throw new UnsupportedOperationException(); + } + } diff --git src/org/eclipse/jpt/jpa/core/internal/resource/java/binary/BinaryQueryAnnotation.java src/org/eclipse/jpt/jpa/core/internal/resource/java/binary/BinaryQueryAnnotation.java index 7c08b89..2c1ae1b 100644 --- src/org/eclipse/jpt/jpa/core/internal/resource/java/binary/BinaryQueryAnnotation.java +++ src/org/eclipse/jpt/jpa/core/internal/resource/java/binary/BinaryQueryAnnotation.java @@ -9,7 +9,6 @@ ******************************************************************************/ package org.eclipse.jpt.jpa.core.internal.resource.java.binary; -import java.util.List; import java.util.Vector; import org.eclipse.jdt.core.IAnnotation; import org.eclipse.jpt.common.core.internal.resource.java.binary.BinaryAnnotation; @@ -23,20 +22,19 @@ /** * javax.persistence.NamedQuery * javax.persistence.NamedNativeQuery + * javax.persistence.NamedStoredProcedureQuery */ -abstract class BinaryQueryAnnotation +public abstract class BinaryQueryAnnotation extends BinaryAnnotation implements QueryAnnotation { String name; - String query; final Vector hints; - BinaryQueryAnnotation(JavaResourceNode parent, IAnnotation jdtAnnotation) { + protected BinaryQueryAnnotation(JavaResourceNode parent, IAnnotation jdtAnnotation) { super(parent, jdtAnnotation); this.name = this.buildName(); - this.query = this.buildQuery(); this.hints = this.buildHints(); } @@ -44,7 +42,6 @@ public void update() { super.update(); this.setName_(this.buildName()); - this.setQuery_(this.buildQuery()); this.updateHints(); } @@ -75,34 +72,9 @@ return (String) this.getJdtMemberValue(this.getNameElementName()); } - abstract String getNameElementName(); + public abstract String getNameElementName(); public TextRange getNameTextRange() { - throw new UnsupportedOperationException(); - } - - // ***** query - public String getQuery() { - return this.query; - } - - public void setQuery(String query) { - throw new UnsupportedOperationException(); - } - - private void setQuery_(String query) { - String old = this.query; - this.query = query; - this.firePropertyChanged(QUERY_PROPERTY, old, query); - } - - private String buildQuery() { - return (String) this.getJdtMemberValue(this.getQueryElementName()); - } - - abstract String getQueryElementName(); - - public List getQueryTextRanges() { throw new UnsupportedOperationException(); } @@ -141,7 +113,7 @@ return result; } - abstract String getHintsElementName(); + public abstract String getHintsElementName(); // TODO private void updateHints() { diff --git src/org/eclipse/jpt/jpa/core/internal/resource/java/source/SourceNamedNativeQueryAnnotation.java src/org/eclipse/jpt/jpa/core/internal/resource/java/source/SourceNamedNativeQueryAnnotation.java index 38cf034..f17fd50 100644 --- src/org/eclipse/jpt/jpa/core/internal/resource/java/source/SourceNamedNativeQueryAnnotation.java +++ src/org/eclipse/jpt/jpa/core/internal/resource/java/source/SourceNamedNativeQueryAnnotation.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2012 Oracle. All rights reserved. + * Copyright (c) 2007, 2013 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. @@ -9,6 +9,7 @@ ******************************************************************************/ package org.eclipse.jpt.jpa.core.internal.resource.java.source; +import java.util.List; import org.eclipse.jdt.core.dom.Annotation; import org.eclipse.jpt.common.core.internal.utility.jdt.ASTTools; import org.eclipse.jpt.common.core.internal.utility.jdt.CombinationIndexedDeclarationAnnotationAdapter; @@ -37,6 +38,11 @@ { private static final DeclarationAnnotationAdapter DECLARATION_ANNOTATION_ADAPTER = new SimpleDeclarationAnnotationAdapter(ANNOTATION_NAME); private static final DeclarationAnnotationAdapter CONTAINER_DECLARATION_ANNOTATION_ADAPTER = new SimpleDeclarationAnnotationAdapter(JPA.NAMED_NATIVE_QUERIES); + + DeclarationAnnotationElementAdapter queryDeclarationAdapter; + AnnotationElementAdapter queryAdapter; + String query; + List queryTextRanges; private DeclarationAnnotationElementAdapter resultClassDeclarationAdapter; private AnnotationElementAdapter resultClassAdapter; @@ -71,6 +77,8 @@ IndexedDeclarationAnnotationAdapter daa, IndexedAnnotationAdapter annotationAdapter) { super(parent, element, daa, annotationAdapter); + this.queryDeclarationAdapter = this.buildQueryDeclarationAdapter(); + this.queryAdapter = this.buildQueryAdapter(); this.resultClassDeclarationAdapter = this.buildResultClassDeclarationAdapter(); this.resultClassAdapter = this.buildResultClassAdapter(); this.resultSetMappingDeclarationAdapter = this.buildResultSetMappingAdapter(daa); @@ -84,6 +92,9 @@ @Override public void initialize(Annotation astAnnotation) { super.initialize(astAnnotation); + + this.query = this.buildQuery(astAnnotation); + this.queryTextRanges = this.buildQueryTextRanges(astAnnotation); this.resultClass = this.buildResultClass(astAnnotation); this.resultClassTextRange = this.buildResultClassTextRange(astAnnotation); @@ -95,6 +106,9 @@ @Override public void synchronizeWith(Annotation astAnnotation) { super.synchronizeWith(astAnnotation); + + this.syncQuery(this.buildQuery(astAnnotation)); + this.queryTextRanges = this.buildQueryTextRanges(astAnnotation); this.syncResultClass(this.buildResultClass(astAnnotation)); this.resultClassTextRange = this.buildResultClassTextRange(astAnnotation); @@ -104,30 +118,67 @@ } - // ********** AbstractBaseNamedQueryAnnotation implementation ********** + // ********** AbstractNamedNativeQueryAnnotation implementation ********** @Override - String getNameElementName() { + public String getNameElementName() { return JPA.NAMED_NATIVE_QUERY__NAME; } @Override - String getQueryElementName() { - return JPA.NAMED_NATIVE_QUERY__QUERY; - } - - @Override - String getHintsElementName() { + public String getHintsElementName() { return JPA.NAMED_NATIVE_QUERY__HINTS; } @Override - QueryHintAnnotation buildHint(int index) { + public QueryHintAnnotation buildHint(int index) { return SourceQueryHintAnnotation.buildNamedNativeQueryQueryHint(this, this.annotatedElement, this.daa, index); } // ********** NamedNativeQueryAnnotation implementation ********** + + String getQueryElementName() { + return JPA.NAMED_NATIVE_QUERY__QUERY; + } + + // ***** query + public String getQuery() { + return this.query; + } + + public void setQuery(String query) { + if (this.attributeValueHasChanged(this.query, query)) { + this.query = query; + this.queryAdapter.setValue(query); + } + } + + private void syncQuery(String annotationQuery) { + String old = this.query; + this.query = annotationQuery; + this.firePropertyChanged(QUERY_PROPERTY, old, annotationQuery); + } + + private String buildQuery(Annotation astAnnotation) { + return this.queryAdapter.getValue(astAnnotation); + } + + public List getQueryTextRanges() { + return this.queryTextRanges; + } + + private List buildQueryTextRanges(Annotation astAnnotation) { + return this.getElementTextRanges(this.queryDeclarationAdapter, astAnnotation); + } + + private DeclarationAnnotationElementAdapter buildQueryDeclarationAdapter() { + return ConversionDeclarationAnnotationElementAdapter.forStrings(this.daa, this.getQueryElementName()); + } + + private AnnotationElementAdapter buildQueryAdapter() { + return this.buildStringElementAdapter(this.queryDeclarationAdapter); + } // ***** result class public String getResultClass() { @@ -236,6 +287,7 @@ @Override public boolean isUnset() { return super.isUnset() && + (this.query == null) && (this.resultClass == null) && (this.resultSetMapping == null); } diff --git src/org/eclipse/jpt/jpa/core/internal/resource/java/source/SourceNamedQueryAnnotation.java src/org/eclipse/jpt/jpa/core/internal/resource/java/source/SourceNamedQueryAnnotation.java index 6dc5549..bb818e8 100644 --- src/org/eclipse/jpt/jpa/core/internal/resource/java/source/SourceNamedQueryAnnotation.java +++ src/org/eclipse/jpt/jpa/core/internal/resource/java/source/SourceNamedQueryAnnotation.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2011 Oracle. All rights reserved. + * Copyright (c) 2007, 2013 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. @@ -9,13 +9,19 @@ ******************************************************************************/ package org.eclipse.jpt.jpa.core.internal.resource.java.source; +import java.util.List; +import org.eclipse.jdt.core.dom.Annotation; 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.SimpleDeclarationAnnotationAdapter; import org.eclipse.jpt.common.core.resource.java.JavaResourceNode; +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.AnnotationAdapter; +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.jpa.core.resource.java.JPA; @@ -32,39 +38,107 @@ private static final DeclarationAnnotationAdapter DECLARATION_ANNOTATION_ADAPTER = new SimpleDeclarationAnnotationAdapter(ANNOTATION_NAME); private static final DeclarationAnnotationAdapter CONTAINER_DECLARATION_ANNOTATION_ADAPTER = new SimpleDeclarationAnnotationAdapter(JPA.NAMED_QUERIES); + DeclarationAnnotationElementAdapter queryDeclarationAdapter; + AnnotationElementAdapter queryAdapter; + String query; + List queryTextRanges; protected SourceNamedQueryAnnotation(JavaResourceNode parent, AnnotatedElement element, DeclarationAnnotationAdapter daa, AnnotationAdapter annotationAdapter) { super(parent, element, daa, annotationAdapter); + this.queryDeclarationAdapter = this.buildQueryDeclarationAdapter(); + this.queryAdapter = this.buildQueryAdapter(); } - public String getAnnotationName() { return ANNOTATION_NAME; } + @Override + public void initialize(Annotation astAnnotation) { + super.initialize(astAnnotation); + + this.query = this.buildQuery(astAnnotation); + this.queryTextRanges = this.buildQueryTextRanges(astAnnotation); + } + + @Override + public void synchronizeWith(Annotation astAnnotation) { + super.synchronizeWith(astAnnotation); + + this.syncQuery(this.buildQuery(astAnnotation)); + this.queryTextRanges = this.buildQueryTextRanges(astAnnotation); + } // ********** AbstractBaseNamedQueryAnnotation implementation ********** @Override - String getNameElementName() { + public String getNameElementName() { return JPA.NAMED_QUERY__NAME; } - @Override String getQueryElementName() { return JPA.NAMED_QUERY__QUERY; } @Override - String getHintsElementName() { + public String getHintsElementName() { return JPA.NAMED_QUERY__HINTS; } @Override - QueryHintAnnotation buildHint(int index) { + public QueryHintAnnotation buildHint(int index) { return SourceQueryHintAnnotation.buildNamedQueryQueryHint(this, this.annotatedElement, this.daa, index); } + // ***** query + public String getQuery() { + return this.query; + } + + public void setQuery(String query) { + if (this.attributeValueHasChanged(this.query, query)) { + this.query = query; + this.queryAdapter.setValue(query); + } + } + + private void syncQuery(String annotationQuery) { + String old = this.query; + this.query = annotationQuery; + this.firePropertyChanged(QUERY_PROPERTY, old, annotationQuery); + } + + private String buildQuery(Annotation astAnnotation) { + return this.queryAdapter.getValue(astAnnotation); + } + + public List getQueryTextRanges() { + return this.queryTextRanges; + } + + private List buildQueryTextRanges(Annotation astAnnotation) { + return this.getElementTextRanges(this.queryDeclarationAdapter, astAnnotation); + } + + private DeclarationAnnotationElementAdapter buildQueryDeclarationAdapter() { + return ConversionDeclarationAnnotationElementAdapter.forStrings(this.daa, this.getQueryElementName()); + } + + private AnnotationElementAdapter buildQueryAdapter() { + return this.buildStringElementAdapter(this.queryDeclarationAdapter); + } + + // ********** misc ********** + + @Override + public boolean isUnset() { + return super.isUnset() && + (this.query == null); + } + + + // ********** static methods ********** + protected static IndexedAnnotationAdapter buildNamedQueryAnnotationAdapter(AnnotatedElement annotatedElement, IndexedDeclarationAnnotationAdapter idaa) { return new ElementIndexedAnnotationAdapter(annotatedElement, idaa); } diff --git src/org/eclipse/jpt/jpa/core/internal/resource/java/source/SourceQueryAnnotation.java src/org/eclipse/jpt/jpa/core/internal/resource/java/source/SourceQueryAnnotation.java index e1d0bb6..c80ee56 100644 --- src/org/eclipse/jpt/jpa/core/internal/resource/java/source/SourceQueryAnnotation.java +++ src/org/eclipse/jpt/jpa/core/internal/resource/java/source/SourceQueryAnnotation.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2012 Oracle. All rights reserved. + * Copyright (c) 2007, 2013 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. @@ -9,7 +9,6 @@ ******************************************************************************/ package org.eclipse.jpt.jpa.core.internal.resource.java.source; -import java.util.List; import org.eclipse.jdt.core.dom.Annotation; import org.eclipse.jpt.common.core.internal.resource.java.source.SourceAnnotation; import org.eclipse.jpt.common.core.internal.utility.jdt.ConversionDeclarationAnnotationElementAdapter; @@ -28,9 +27,10 @@ *
    *
  • javax.persistence.NamedQuery *
  • javax.persistence.NamedNativeQuery + *
  • javax.persistence.NamedStoredProcedureQuery *
*/ -abstract class SourceQueryAnnotation +public abstract class SourceQueryAnnotation extends SourceAnnotation implements QueryAnnotation { @@ -39,20 +39,14 @@ String name; TextRange nameTextRange; - DeclarationAnnotationElementAdapter queryDeclarationAdapter; - AnnotationElementAdapter queryAdapter; - String query; - List queryTextRanges; - final QueryHintsAnnotationContainer hintsContainer = new QueryHintsAnnotationContainer(); - SourceQueryAnnotation(JavaResourceNode parent, AnnotatedElement element, DeclarationAnnotationAdapter daa, AnnotationAdapter annotationAdapter) { + protected SourceQueryAnnotation(JavaResourceNode parent, AnnotatedElement element, DeclarationAnnotationAdapter daa, AnnotationAdapter annotationAdapter) { super(parent, element, daa, annotationAdapter); this.nameDeclarationAdapter = this.buildNameDeclarationAdapter(); this.nameAdapter = this.buildNameAdapter(); - this.queryDeclarationAdapter = this.buildQueryDeclarationAdapter(); - this.queryAdapter = this.buildQueryAdapter(); + } @Override @@ -60,8 +54,6 @@ super.initialize(astAnnotation); this.name = this.buildName(astAnnotation); this.nameTextRange = this.buildNameTextRange(astAnnotation); - this.query = this.buildQuery(astAnnotation); - this.queryTextRanges = this.buildQueryTextRanges(astAnnotation); this.hintsContainer.initializeFromContainerAnnotation(astAnnotation); } @@ -70,8 +62,6 @@ super.synchronizeWith(astAnnotation); this.syncName(this.buildName(astAnnotation)); this.nameTextRange = this.buildNameTextRange(astAnnotation); - this.syncQuery(this.buildQuery(astAnnotation)); - this.queryTextRanges = this.buildQueryTextRanges(astAnnotation); this.hintsContainer.synchronize(astAnnotation); } @@ -116,47 +106,8 @@ return this.buildStringElementAdapter(this.nameDeclarationAdapter); } - abstract String getNameElementName(); + public abstract String getNameElementName(); - // ***** query - public String getQuery() { - return this.query; - } - - public void setQuery(String query) { - if (this.attributeValueHasChanged(this.query, query)) { - this.query = query; - this.queryAdapter.setValue(query); - } - } - - private void syncQuery(String annotationQuery) { - String old = this.query; - this.query = annotationQuery; - this.firePropertyChanged(QUERY_PROPERTY, old, annotationQuery); - } - - private String buildQuery(Annotation astAnnotation) { - return this.queryAdapter.getValue(astAnnotation); - } - - public List getQueryTextRanges() { - return this.queryTextRanges; - } - - private List buildQueryTextRanges(Annotation astAnnotation) { - return this.getElementTextRanges(this.queryDeclarationAdapter, astAnnotation); - } - - private DeclarationAnnotationElementAdapter buildQueryDeclarationAdapter() { - return ConversionDeclarationAnnotationElementAdapter.forStrings(this.daa, this.getQueryElementName()); - } - - private AnnotationElementAdapter buildQueryAdapter() { - return this.buildStringElementAdapter(this.queryDeclarationAdapter); - } - - abstract String getQueryElementName(); // ***** hints @@ -184,9 +135,9 @@ this.hintsContainer.removeNestedAnnotation(index); } - abstract QueryHintAnnotation buildHint(int index); + public abstract QueryHintAnnotation buildHint(int index); - abstract String getHintsElementName(); + public abstract String getHintsElementName(); // ********** misc ********** @@ -195,7 +146,6 @@ public boolean isUnset() { return super.isUnset() && (this.name == null) && - (this.query == null) && this.hintsContainer.isEmpty(); } diff --git src/org/eclipse/jpt/jpa/core/internal/resource/java/source/SourceQueryHintAnnotation.java src/org/eclipse/jpt/jpa/core/internal/resource/java/source/SourceQueryHintAnnotation.java index 0b1ee9c..7ac7e31 100644 --- src/org/eclipse/jpt/jpa/core/internal/resource/java/source/SourceQueryHintAnnotation.java +++ src/org/eclipse/jpt/jpa/core/internal/resource/java/source/SourceQueryHintAnnotation.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2012 Oracle. All rights reserved. + * Copyright (c) 2007, 2013 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. @@ -21,6 +21,7 @@ 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.IndexedDeclarationAnnotationAdapter; +import org.eclipse.jpt.jpa.core.jpa2_1.resource.java.JPA2_1; import org.eclipse.jpt.jpa.core.resource.java.JPA; import org.eclipse.jpt.jpa.core.resource.java.QueryHintAnnotation; @@ -48,6 +49,10 @@ static SourceQueryHintAnnotation buildNamedNativeQueryQueryHint(JavaResourceNode parent, AnnotatedElement element, DeclarationAnnotationAdapter namedNativeQueryAdapter, int index) { return buildNestedSourceQueryHintAnnotation(parent, element, buildNamedNativeQueryQueryHintAnnotationAdapter(namedNativeQueryAdapter, index)); + } + + public static SourceQueryHintAnnotation buildNamedStoredProcedureQuery2_1QueryHint(JavaResourceNode parent, AnnotatedElement element, DeclarationAnnotationAdapter namedStoredProcedureQuery2_1Adapter, int index) { + return buildNestedSourceQueryHintAnnotation(parent, element, buildNamedStoredProcedureQuery2_1QueryHintAnnotationAdapter(namedStoredProcedureQuery2_1Adapter, index)); } public static SourceQueryHintAnnotation buildNestedSourceQueryHintAnnotation( @@ -193,4 +198,7 @@ private static IndexedDeclarationAnnotationAdapter buildNamedNativeQueryQueryHintAnnotationAdapter(DeclarationAnnotationAdapter namedNativeQueryAdapter, int index) { return new NestedIndexedDeclarationAnnotationAdapter(namedNativeQueryAdapter, JPA.NAMED_NATIVE_QUERY__HINTS, index, ANNOTATION_NAME); } + private static IndexedDeclarationAnnotationAdapter buildNamedStoredProcedureQuery2_1QueryHintAnnotationAdapter(DeclarationAnnotationAdapter namedStoredProcedureQuery2_1Adapter, int index) { + return new NestedIndexedDeclarationAnnotationAdapter(namedStoredProcedureQuery2_1Adapter, JPA2_1.NAMED_STORED_PROCEDURE_QUERY__HINTS, index, ANNOTATION_NAME); + } } diff --git src/org/eclipse/jpt/jpa/core/jpa2_1/resource/java/JPA2_1.java src/org/eclipse/jpt/jpa/core/jpa2_1/resource/java/JPA2_1.java index 37a569f..cef9a92 100644 --- src/org/eclipse/jpt/jpa/core/jpa2_1/resource/java/JPA2_1.java +++ src/org/eclipse/jpt/jpa/core/jpa2_1/resource/java/JPA2_1.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2012 Oracle. All rights reserved. + * Copyright (c) 2012, 2013 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. @@ -36,5 +36,27 @@ // JPA 2.1 annotations String CONVERTER = PACKAGE_ + "Converter"; String CONVERTER__AUTO_APPLY = "autoApply"; + + String NAMED_STORED_PROCEDURE_QUERIES = PACKAGE_ + "NamedStoredProcedureQueries"; + String NAMED_STORED_PROCEDURE_QUERIES__VALUE = "value"; + String NAMED_STORED_PROCEDURE_QUERY =PACKAGE_ + "NamedStoredProcedureQuery"; + String NAMED_STORED_PROCEDURE_QUERY__NAME = "name"; + String NAMED_STORED_PROCEDURE_QUERY__PROCEDURE_NAME= "procedureName"; + String NAMED_STORED_PROCEDURE_QUERY__PARAMETERS = "parameters"; + String NAMED_STORED_PROCEDURE_QUERY__RESULT_CLASSES = "resultClasses"; + String NAMED_STORED_PROCEDURE_QUERY__RESULT_SET_MAPPINGS= "resultSetMappings"; + String NAMED_STORED_PROCEDURE_QUERY__HINTS = "hints"; + String NAMED_STORED_PROCEDURE_PARAMETER = PACKAGE_ + "StoredProcedureParameter"; + String NAMED_STORED_PROCEDURE_PARAMETER__NAME = "name"; + String NAMED_STORED_PROCEDURE_PARAMETER__MODE = "mode"; + String NAMED_STORED_PROCEDURE_PARAMETER__TYPE = "type"; + + // JPA 2.1 enums + String PARAMETER_MODE = PACKAGE_ + "ParameterMode"; + String PARAMETER_MODE_ = PARAMETER_MODE + '.'; + String PARAMETER_MODE__IN = PARAMETER_MODE_ + "IN"; + String PARAMETER_MODE__INOUT = PARAMETER_MODE_ + "INOUT"; + String PARAMETER_MODE__OUT = PARAMETER_MODE_ + "OUT"; + String PARAMETER_MODE__REF_CURSOR= PARAMETER_MODE_ + "REF_CURSOR"; } diff --git src/org/eclipse/jpt/jpa/core/jpa2_1/resource/java/NamedStoredProcedureQuery2_1Annoation.java src/org/eclipse/jpt/jpa/core/jpa2_1/resource/java/NamedStoredProcedureQuery2_1Annoation.java new file mode 100644 index 0000000..39d4aff --- /dev/null +++ src/org/eclipse/jpt/jpa/core/jpa2_1/resource/java/NamedStoredProcedureQuery2_1Annoation.java @@ -0,0 +1,171 @@ +/******************************************************************************* + * Copyright (c) 2013 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.jpa2_1.resource.java; + +import org.eclipse.jpt.common.core.utility.TextRange; +import org.eclipse.jpt.common.utility.iterable.ListIterable; +import org.eclipse.jpt.jpa.core.resource.java.QueryAnnotation; + +/** + * Corresponds to the JPA annotation + * javax.persistence.NamedStoredProcedureQuery + *

+ * Provisional API: This interface is part of an interim API that is still + * under development and expected to change significantly before reaching + * stability. It is available at this early stage to solicit feedback from + * pioneering adopters on the understanding that any code that uses this API + * will almost certainly be broken (repeatedly) as the API evolves. + * + * @version 3.3 + * @since 3.3 + */ +public interface NamedStoredProcedureQuery2_1Annoation + extends QueryAnnotation +{ + String ANNOTATION_NAME = JPA2_1.NAMED_STORED_PROCEDURE_QUERY; + + + // ********** procedure name ********** + + /** + * Corresponds to the 'procedureName' element of the *NamedStoredProcedureQuery annotation. + * Return null if the element does not exist in Java. + */ + String getProcedureName(); + String PROCEDURE_NAME_PROPERTY = "procedureName"; //$NON-NLS-1$ + + /** + * Corresponds to the 'procedureName' element of the *NamedStoredProcedureQuery annotation. + * Set to null to remove the element. + */ + void setProcedureName(String procedureName); + + /** + * Return the {@link TextRange} for the 'procedureName' element. If element + * does not exist return the {@link TextRange} for the *NamedStoredProcedureQuery annotation. + */ + TextRange getProcedureNameTextRange(); + + + // ********** parameters ********** + + /** + * Corresponds to the 'parameters' element of the *NamedStoredProcedureQuery annotation. + * Return an empty iterator if the element does not exist in Java. + */ + ListIterable getParameters(); + String PARAMETERS_LIST = "parameters"; //$NON-NLS-1$ + + /** + * Corresponds to the 'parameters' element of the *NamedStoredProcedureQuery annotation. + */ + int getParametersSize(); + + /** + * Corresponds to the 'parameters' element of the *NamedStoredProcedureQuery annotation. + */ + StoredProcedureParameter2_1Annotation parameterAt(int index); + + /** + * Corresponds to the 'parameters' element of the *NamedStoredProcedureQuery annotation. + */ + StoredProcedureParameter2_1Annotation addParameter(int index); + + /** + * Corresponds to the 'parameters' element of the *NamedStoredProcedureQuery annotation. + */ + void moveParameter(int targetIndex, int sourceIndex); + + /** + * Corresponds to the 'parameters' element of the *NamedStoredProcedureQuery annotation. + */ + void removeParameter(int index); + + + // ********** result classes ********** + + /** + * Corresponds to the 'resultClasses' element of the *NamedStoredProcedureQuery annotation. + * Return an empty iterator if the element does not exist in Java. + */ + ListIterable getResultClasses(); + String RESULT_CLASSES_LIST = "resultClasses"; //$NON-NLS-1$ + + /** + * Corresponds to the 'resultClasses' element of the *NamedStoredProcedureQuery annotation. + */ + int getResultClassesSize(); + + /** + * Corresponds to the 'resultClasses' element of the *NamedStoredProcedureQuery annotation. + */ + String resultClassAt(int index); + + /** + * Corresponds to the 'resultClasses' element of the *NamedStoredProcedureQuery annotation. + */ + void addResultClass(String resultClass); + + /** + * Corresponds to the 'resultClasses' element of the *NamedStoredProcedureQuery annotation. + */ + void moveResultClass(int targetIndex, int sourceIndex); + + /** + * Corresponds to the 'resultClasses' element of the *NamedStoredProcedureQuery annotation. + */ + void removeResultClass(String resultClass); + + /** + * Corresponds to the 'resultClasses' element of the *NamedStoredProcedureQuery annotation. + */ + void removeResultClass(int index); + + + // ********** result set mappings ********** + + /** + * Corresponds to the 'resultSetMappings' element of the *NamedStoredProcedureQuery annotation. + * Return an empty iterator if the element does not exist in Java. + */ + ListIterable getResultSetMappings(); + String RESULT_SET_MAPPINGS_LIST = "resultSetMappings"; //$NON-NLS-1$ + + /** + * Corresponds to the 'resultSetMappings' element of the *NamedStoredProcedureQuery annotation. + */ + int getResultSetMappingsSize(); + + /** + * Corresponds to the 'resultSetMappings' element of the *NamedStoredProcedureQuery annotation. + */ + String resultSetMappingAt(int index); + + /** + * Corresponds to the 'resultSetMappings' element of the *NamedStoredProcedureQuery annotation. + */ + void addResultSetMapping(String resultSetMapping); + + /** + * Corresponds to the 'resultSetMappings' element of the *NamedStoredProcedureQuery annotation. + */ + void moveResultSetMapping(int targetIndex, int sourceIndex); + + /** + * Corresponds to the 'resultSetMappings' element of the *NamedStoredProcedureQuery annotation. + */ + void removeResultSetMapping(String resultSetMapping); + + /** + * Corresponds to the 'resultSetMappings' element of the *NamedStoredProcedureQuery annotation. + */ + void removeResultSetMapping(int index); + +} diff --git src/org/eclipse/jpt/jpa/core/jpa2_1/resource/java/ParameterMode2_1.java src/org/eclipse/jpt/jpa/core/jpa2_1/resource/java/ParameterMode2_1.java new file mode 100644 index 0000000..1fb4fbd --- /dev/null +++ src/org/eclipse/jpt/jpa/core/jpa2_1/resource/java/ParameterMode2_1.java @@ -0,0 +1,67 @@ +/******************************************************************************* + * Copyright (c) 2013 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.jpa2_1.resource.java; + + +/** + * Corresponds to the JPA 2.1 enum + * javax.persistence.ParameterMode + *

+ * Provisional API: This interface is part of an interim API that is still + * under development and expected to change significantly before reaching + * stability. It is available at this early stage to solicit feedback from + * pioneering adopters on the understanding that any code that uses this API + * will almost certainly be broken (repeatedly) as the API evolves. + * + * @version 3.3 + * @since 3.3 + */ +public enum ParameterMode2_1 +{ + + IN(JPA2_1.PARAMETER_MODE__IN), + INOUT(JPA2_1.PARAMETER_MODE__INOUT), + OUT(JPA2_1.PARAMETER_MODE__OUT), + REF_CURSOR(JPA2_1.PARAMETER_MODE__REF_CURSOR); + + + private String javaAnnotationValue; + + ParameterMode2_1(String javaAnnotationValue) { + if (javaAnnotationValue == null) { + throw new NullPointerException(); + } + this.javaAnnotationValue = javaAnnotationValue; + } + + public String getJavaAnnotationValue() { + return this.javaAnnotationValue; + } + + + // ********** static methods ********** + + public static ParameterMode2_1 fromJavaAnnotationValue(Object javaAnnotationValue) { + return (javaAnnotationValue == null) ? null : fromJavaAnnotationValue_(javaAnnotationValue); + } + + private static ParameterMode2_1 fromJavaAnnotationValue_(Object javaAnnotationValue) { + for (ParameterMode2_1 parameterMode : ParameterMode2_1.values()) { + if (parameterMode.getJavaAnnotationValue().equals(javaAnnotationValue)) { + return parameterMode; + } + } + return null; + } + + public static String toJavaAnnotationValue(ParameterMode2_1 parameterMode) { + return (parameterMode == null) ? null : parameterMode.getJavaAnnotationValue(); + } +} diff --git src/org/eclipse/jpt/jpa/core/jpa2_1/resource/java/StoredProcedureParameter2_1Annotation.java src/org/eclipse/jpt/jpa/core/jpa2_1/resource/java/StoredProcedureParameter2_1Annotation.java new file mode 100644 index 0000000..21b15e3 --- /dev/null +++ src/org/eclipse/jpt/jpa/core/jpa2_1/resource/java/StoredProcedureParameter2_1Annotation.java @@ -0,0 +1,105 @@ +/******************************************************************************* + * Copyright (c) 2013 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.jpa2_1.resource.java; + +import org.eclipse.jpt.common.core.resource.java.NestableAnnotation; +import org.eclipse.jpt.common.core.utility.TextRange; + +/** + * Corresponds to the JPA annotation + * javax.persistence.StoredProcedureParameter + *

+ * Provisional API: This interface is part of an interim API that is still + * under development and expected to change significantly before reaching + * stability. It is available at this early stage to solicit feedback from + * pioneering adopters on the understanding that any code that uses this API + * will almost certainly be broken (repeatedly) as the API evolves. + * + * @version 3.3 + * @since 3.3 + */ +public interface StoredProcedureParameter2_1Annotation + extends NestableAnnotation +{ + String ANNOTATION_NAME = JPA2_1.NAMED_STORED_PROCEDURE_PARAMETER; + + // ********* name ********** + /** + * Corresponds to the 'name' element of the StoredProcedureParameter annotation. + * Return null if the element does not exist in the annotation + */ + String getName(); + String NAME_PROPERTY = "name"; //$NON-NLS-1$ + + /** + * Corresponds to the 'name' element of the StoredProcedureParameter annotation. + * Setting to null will remove the element. + */ + void setName(String name); + + /** + * Return the {@link TextRange} for the 'name' element. If the element + * does not exist return the {@link TextRange} for the StoredProcedureParameter annotation. + */ + TextRange getNameTextRange(); + + // *********** mode ************* + + /** + * Corresponds to the 'mode' element of the StoredProcedureParameter annotation. + * Return null if the element does not exist in the annotation + */ + ParameterMode2_1 getMode(); + String MODE_PROPERTY = "mode"; //$NON-NLS-1$ + + /** + * Corresponds to the 'mode' element of the StoredProcedureParameter annotation. + * Setting to null will remove the element. + */ + void setMode(ParameterMode2_1 mode); + + /** + * Return the {@link TextRange} for the 'mode' element. If the element + * does not exist return the {@link TextRange} for the StoredProcedureParameter annotation. + */ + TextRange getModeTextRange(); + + // ********** type *********** + + /** + * Corresponds to the 'type' element of the StoredProcedureParameter annotation. + * Return null if the element does not exist in the annotation + */ + String getType(); + String TYPE_PROPERTY = "type"; //$NON-NLS-1$ + + /** + * Corresponds to the 'type' element of the StoredProcedureParameter annotation. + * Setting to null will remove the element. + */ + void setType(String type); + + /** + * Return the {@link TextRange} for the 'type' element. If the element + * does not exist return the {@link TextRange} for the StoredProcedureParameter annotation. + */ + TextRange getTypeTextRange(); + + /** + * Return the named native query's fully-qualified type name as + * resolved by the AST's bindings. + *

+	 *     @NamedStoredProcedureQuery(type=Employee.class)
+	 * 
+ * will return "model.Employee" if there is an import for + * model.Employee. + */ + String getFullyQualifiedTypeName(); +} diff --git src/org/eclipse/jpt/jpa/core/resource/java/NamedNativeQueryAnnotation.java src/org/eclipse/jpt/jpa/core/resource/java/NamedNativeQueryAnnotation.java index 67b3c2b..b87c089 100644 --- src/org/eclipse/jpt/jpa/core/resource/java/NamedNativeQueryAnnotation.java +++ src/org/eclipse/jpt/jpa/core/resource/java/NamedNativeQueryAnnotation.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2012 Oracle. All rights reserved. + * Copyright (c) 2007, 2013 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. @@ -9,6 +9,7 @@ ******************************************************************************/ package org.eclipse.jpt.jpa.core.resource.java; +import java.util.List; import org.eclipse.jpt.common.core.utility.TextRange; @@ -31,6 +32,28 @@ String ANNOTATION_NAME = JPA.NAMED_NATIVE_QUERY; + // ********** query ********** + + /** + * Corresponds to the 'query' element of the *Query annotation. + * Return null if the element does not exist in Java. + */ + String getQuery(); + String QUERY_PROPERTY = "query"; //$NON-NLS-1$ + + /** + * Corresponds to the 'query' element of the *Query annotation. + * Set to null to remove the element. + */ + void setQuery(String query); + + /** + * Return the {@link TextRange} for the 'query' element. If element + * does not exist return the {@link TextRange} for the *Query annotation. + */ + List getQueryTextRanges(); + + // ********** result class ********** /** diff --git src/org/eclipse/jpt/jpa/core/resource/java/NamedQueryAnnotation.java src/org/eclipse/jpt/jpa/core/resource/java/NamedQueryAnnotation.java index e0c098e..b1ae05d 100644 --- src/org/eclipse/jpt/jpa/core/resource/java/NamedQueryAnnotation.java +++ src/org/eclipse/jpt/jpa/core/resource/java/NamedQueryAnnotation.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2010 Oracle. All rights reserved. + * Copyright (c) 2007, 2013 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. @@ -8,6 +8,9 @@ * Oracle - initial API and implementation ******************************************************************************/ package org.eclipse.jpt.jpa.core.resource.java; + +import java.util.List; +import org.eclipse.jpt.common.core.utility.TextRange; /** * Corresponds to the JPA annotation @@ -26,4 +29,26 @@ extends QueryAnnotation { String ANNOTATION_NAME = JPA.NAMED_QUERY; + + // ********** query ********** + + /** + * Corresponds to the 'query' element of the *Query annotation. + * Return null if the element does not exist in Java. + */ + String getQuery(); + String QUERY_PROPERTY = "query"; //$NON-NLS-1$ + + /** + * Corresponds to the 'query' element of the *Query annotation. + * Set to null to remove the element. + */ + void setQuery(String query); + + /** + * Return the {@link TextRange} for the 'query' element. If element + * does not exist return the {@link TextRange} for the *Query annotation. + */ + List getQueryTextRanges(); + } diff --git src/org/eclipse/jpt/jpa/core/resource/java/QueryAnnotation.java src/org/eclipse/jpt/jpa/core/resource/java/QueryAnnotation.java index 5a5fb99..231ab4a 100644 --- src/org/eclipse/jpt/jpa/core/resource/java/QueryAnnotation.java +++ src/org/eclipse/jpt/jpa/core/resource/java/QueryAnnotation.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2012 Oracle. All rights reserved. + * Copyright (c) 2007, 2013 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. @@ -9,7 +9,6 @@ ******************************************************************************/ package org.eclipse.jpt.jpa.core.resource.java; -import java.util.List; import org.eclipse.jpt.common.core.resource.java.NestableAnnotation; import org.eclipse.jpt.common.core.utility.TextRange; import org.eclipse.jpt.common.utility.iterable.ListIterable; @@ -51,28 +50,6 @@ * does not exist return the {@link TextRange} for the *Query annotation. */ TextRange getNameTextRange(); - - - // ********** query ********** - - /** - * Corresponds to the 'query' element of the *Query annotation. - * Return null if the element does not exist in Java. - */ - String getQuery(); - String QUERY_PROPERTY = "query"; //$NON-NLS-1$ - - /** - * Corresponds to the 'query' element of the *Query annotation. - * Set to null to remove the element. - */ - void setQuery(String query); - - /** - * Return the {@link TextRange} for the 'query' element. If element - * does not exist return the {@link TextRange} for the *Query annotation. - */ - List getQueryTextRanges(); // ********** hints ********** #P org.eclipse.jpt.jpa.ui diff --git src/org/eclipse/jpt/jpa/ui/internal/details/NamedNativeQueryPropertyComposite.java src/org/eclipse/jpt/jpa/ui/internal/details/NamedNativeQueryPropertyComposite.java index d025568..2980f08 100644 --- src/org/eclipse/jpt/jpa/ui/internal/details/NamedNativeQueryPropertyComposite.java +++ src/org/eclipse/jpt/jpa/ui/internal/details/NamedNativeQueryPropertyComposite.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008, 2012 Oracle. All rights reserved. + * Copyright (c) 2008, 2013 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. @@ -13,8 +13,8 @@ import org.eclipse.jpt.common.ui.internal.widgets.ClassChooserPane; import org.eclipse.jpt.common.ui.internal.widgets.Pane; import org.eclipse.jpt.common.utility.internal.model.value.PropertyAspectAdapter; -import org.eclipse.jpt.common.utility.model.value.PropertyValueModel; import org.eclipse.jpt.common.utility.model.value.ModifiablePropertyValueModel; +import org.eclipse.jpt.common.utility.model.value.PropertyValueModel; import org.eclipse.jpt.jpa.core.context.NamedNativeQuery; import org.eclipse.jpt.jpa.core.context.Query; import org.eclipse.swt.SWT; @@ -121,7 +121,7 @@ } private ModifiablePropertyValueModel buildQueryHolder() { - return new PropertyAspectAdapter(getSubjectHolder(), Query.QUERY_PROPERTY) { + return new PropertyAspectAdapter(getSubjectHolder(), NamedNativeQuery.QUERY_PROPERTY) { @Override protected String buildValue_() { return this.subject.getQuery(); diff --git src/org/eclipse/jpt/jpa/ui/internal/details/NamedQueryPropertyComposite.java src/org/eclipse/jpt/jpa/ui/internal/details/NamedQueryPropertyComposite.java index 8bd6e2c..c9ea3d5 100644 --- src/org/eclipse/jpt/jpa/ui/internal/details/NamedQueryPropertyComposite.java +++ src/org/eclipse/jpt/jpa/ui/internal/details/NamedQueryPropertyComposite.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2008, 2012 Oracle. All rights reserved. + * Copyright (c) 2008, 2013 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. @@ -83,7 +83,7 @@ } protected ModifiablePropertyValueModel buildQueryHolder() { - return new PropertyAspectAdapter(getSubjectHolder(), Query.QUERY_PROPERTY) { + return new PropertyAspectAdapter(getSubjectHolder(), NamedQuery.QUERY_PROPERTY) { @Override protected String buildValue_() { return this.subject.getQuery(); diff --git src/org/eclipse/jpt/jpa/ui/internal/jpa2/details/NamedQueryProperty2_0Composite.java src/org/eclipse/jpt/jpa/ui/internal/jpa2/details/NamedQueryProperty2_0Composite.java index 7b63d6b..b438609 100644 --- src/org/eclipse/jpt/jpa/ui/internal/jpa2/details/NamedQueryProperty2_0Composite.java +++ src/org/eclipse/jpt/jpa/ui/internal/jpa2/details/NamedQueryProperty2_0Composite.java @@ -1,5 +1,5 @@ /******************************************************************************* -* Copyright (c) 2009, 2012 Oracle. All rights reserved. +* 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 v1.0, which accompanies this distribution * and is available at http://www.eclipse.org/legal/epl-v10.html. @@ -16,6 +16,7 @@ import org.eclipse.jpt.common.utility.model.value.ModifiablePropertyValueModel; import org.eclipse.jpt.common.utility.model.value.PropertyValueModel; import org.eclipse.jpt.jpa.core.JpaPreferences; +import org.eclipse.jpt.jpa.core.context.NamedQuery; import org.eclipse.jpt.jpa.core.context.Query; import org.eclipse.jpt.jpa.core.jpa2.context.LockModeType2_0; import org.eclipse.jpt.jpa.core.jpa2.context.NamedQuery2_0; @@ -91,7 +92,7 @@ } protected ModifiablePropertyValueModel buildQueryHolder() { - return new PropertyAspectAdapter(getSubjectHolder(), Query.QUERY_PROPERTY) { + return new PropertyAspectAdapter(getSubjectHolder(), NamedQuery.QUERY_PROPERTY) { @Override protected String buildValue_() { return this.subject.getQuery(); #P org.eclipse.jpt.jpa.core.tests diff --git src/org/eclipse/jpt/jpa/core/tests/internal/jpa2_1/resource/java/JavaResource2_1Tests.java src/org/eclipse/jpt/jpa/core/tests/internal/jpa2_1/resource/java/JavaResource2_1Tests.java new file mode 100644 index 0000000..7b17174 --- /dev/null +++ src/org/eclipse/jpt/jpa/core/tests/internal/jpa2_1/resource/java/JavaResource2_1Tests.java @@ -0,0 +1,33 @@ +/******************************************************************************* + * Copyright (c) 2013 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.tests.internal.jpa2_1.resource.java; + +import junit.framework.Test; +import junit.framework.TestSuite; + +public class JavaResource2_1Tests +{ + + public static Test suite() { + TestSuite suite = new TestSuite(JavaResource2_1Tests.class.getPackage().getName()); + + suite.addTestSuite(NamedStoredProcedureQuery2_1AnnoationTests.class); + suite.addTestSuite(NamedStoredProcedureQueries2_1AnnotationTests.class); + suite.addTestSuite(StoredProcedureParameter2_1AnnotationTests.class); + + return suite; + } + + private JavaResource2_1Tests() { + super(); + throw new UnsupportedOperationException(); + } + +} diff --git src/org/eclipse/jpt/jpa/core/tests/internal/jpa2_1/resource/java/JavaResourceModel2_1TestCase.java src/org/eclipse/jpt/jpa/core/tests/internal/jpa2_1/resource/java/JavaResourceModel2_1TestCase.java new file mode 100644 index 0000000..440bb69 --- /dev/null +++ src/org/eclipse/jpt/jpa/core/tests/internal/jpa2_1/resource/java/JavaResourceModel2_1TestCase.java @@ -0,0 +1,32 @@ +/******************************************************************************* + * Copyright (c) 2013 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.tests.internal.jpa2_1.resource.java; + +import org.eclipse.jpt.common.core.AnnotationProvider; +import org.eclipse.jpt.jpa.core.internal.JpaAnnotationProvider; +import org.eclipse.jpt.jpa.core.internal.jpa2_1.Generic2_1JpaAnnotationDefinitionProvider; +import org.eclipse.jpt.jpa.core.tests.internal.resource.java.JpaJavaResourceModelTestCase; + +public class JavaResourceModel2_1TestCase + extends JpaJavaResourceModelTestCase +{ + + + public JavaResourceModel2_1TestCase(String name) { + super(name); + } + + @Override + protected AnnotationProvider buildAnnotationProvider() { + return new JpaAnnotationProvider( + Generic2_1JpaAnnotationDefinitionProvider.instance()); + } + +} diff --git src/org/eclipse/jpt/jpa/core/tests/internal/jpa2_1/resource/java/NamedStoredProcedureQueries2_1AnnotationTests.java src/org/eclipse/jpt/jpa/core/tests/internal/jpa2_1/resource/java/NamedStoredProcedureQueries2_1AnnotationTests.java new file mode 100644 index 0000000..618773a --- /dev/null +++ src/org/eclipse/jpt/jpa/core/tests/internal/jpa2_1/resource/java/NamedStoredProcedureQueries2_1AnnotationTests.java @@ -0,0 +1,369 @@ +/******************************************************************************* + * Copyright (c) 2013 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.tests.internal.jpa2_1.resource.java; + +import java.util.Iterator; +import java.util.ListIterator; +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jpt.common.core.resource.java.JavaResourceType; +import org.eclipse.jpt.common.core.resource.java.NestableAnnotation; +import org.eclipse.jpt.common.utility.internal.iterator.ArrayIterator; +import org.eclipse.jpt.jpa.core.jpa2_1.resource.java.JPA2_1; +import org.eclipse.jpt.jpa.core.jpa2_1.resource.java.NamedStoredProcedureQuery2_1Annoation; +import org.eclipse.jpt.jpa.core.jpa2_1.resource.java.StoredProcedureParameter2_1Annotation; +import org.eclipse.jpt.jpa.core.resource.java.JPA; +import org.eclipse.jpt.jpa.core.resource.java.QueryHintAnnotation; + +public class NamedStoredProcedureQueries2_1AnnotationTests + extends JavaResourceModel2_1TestCase +{ + + private static final String QUERY_NAME = "myQuery"; + private static final String QUERY_PROCEDURE_NAME = "myProcedure"; + + public NamedStoredProcedureQueries2_1AnnotationTests(String name) { + super(name); + } + + private ICompilationUnit createTestNamedStoredProcedureQueries() throws Exception { + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator imports() { + return new ArrayIterator(JPA2_1.NAMED_STORED_PROCEDURE_QUERIES, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@NamedStoredProcedureQueries(@NamedStoredProcedureQuery)"); + } + }); + } + + private ICompilationUnit createTestNamedStoredProcedureQueryWithName() throws Exception { + return createTestQueryWithStringElement("name", QUERY_NAME); + } + + private ICompilationUnit createTestNamedStoredProcedureQueryWithProcedureName() throws Exception { + return createTestQueryWithStringElement("procedureName", QUERY_PROCEDURE_NAME); + } + + private ICompilationUnit createTestQueryWithStringElement(final String elementName, final String value) throws Exception { + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator imports() { + return new ArrayIterator(JPA2_1.NAMED_STORED_PROCEDURE_QUERIES, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@NamedStoredProcedureQueries(@NamedStoredProcedureQuery(" + elementName + " = \"" + value + "\"))"); + } + }); + } + + private ICompilationUnit createTestNamedStoredProcedureQueryWithParameters() throws Exception { + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator imports() { + return new ArrayIterator(JPA2_1.NAMED_STORED_PROCEDURE_QUERIES, JPA2_1.NAMED_STORED_PROCEDURE_QUERY, JPA2_1.NAMED_STORED_PROCEDURE_PARAMETER, JPA2_1.PARAMETER_MODE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@NamedStoredProcedureQueries(@NamedStoredProcedureQuery(parameters = {@StoredProcedureParameter(name = \"BAR\", " + + "mode=ParameterMode.IN, type=Integer.class), @StoredProcedureParameter}))"); + } + }); + } + + private ICompilationUnit createTestNamedStoredProcedureQueryWithResultClasses() throws Exception { + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator imports() { + return new ArrayIterator(JPA2_1.NAMED_STORED_PROCEDURE_QUERIES, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@NamedStoredProcedureQueries(@NamedStoredProcedureQuery(resultClasses={Employee.class, Address.class})"); + } + }); + } + + private ICompilationUnit createTestNamedStoredProcedureQueryWithResultSetMappings() throws Exception { + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator imports() { + return new ArrayIterator(JPA2_1.NAMED_STORED_PROCEDURE_QUERIES, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@NamedStoredProcedureQueries(@NamedStoredProcedureQuery(resultSetMappings={\"EmployeeResultSetMapping\", \"AddressResultSetMapping\"})"); + } + }); + } + + private ICompilationUnit createTestNamedStoredProcedureQueryWithHints() throws Exception { + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator imports() { + return new ArrayIterator(JPA2_1.NAMED_STORED_PROCEDURE_QUERIES, JPA2_1.NAMED_STORED_PROCEDURE_QUERY, JPA.QUERY_HINT); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@NamedStoredProcedureQueries(@NamedStoredProcedureQuery(hints = {@QueryHint(name = \"BAR\", value = \"FOO\"), @QueryHint}))"); + } + }); + } + + private ICompilationUnit createTestNamedStoredProcedureQuery() throws Exception { + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator imports() { + return new ArrayIterator(JPA2_1.NAMED_STORED_PROCEDURE_QUERY, JPA.QUERY_HINT); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@NamedStoredProcedureQuery(name = \"foo\", hints = @QueryHint(name = \"BAR\", value = \"FOO\"))"); + } + }); + } + + public void testNamedStoredProcedureQuery() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueries(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + assertNotNull(namedQuery); + } + + // ******** name ********* + + public void testGetName() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueryWithName(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + assertEquals(QUERY_NAME, namedQuery.getName()); + } + + public void testSetName() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueryWithName(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + assertEquals(QUERY_NAME, namedQuery.getName()); + + namedQuery.setName("foo"); + assertEquals("foo", namedQuery.getName()); + + assertSourceContains("@NamedStoredProcedureQuery(name = \"foo\")", cu); + + namedQuery.setName(null); + assertNull(namedQuery.getName()); + + assertSourceDoesNotContain("@NamedStoredProcedureQuery(", cu); + } + + // ********** procedure name ************ + + public void testGetProcedureName() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueryWithProcedureName(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + assertEquals(QUERY_PROCEDURE_NAME, namedQuery.getProcedureName()); + } + + public void testSetProcedureName() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueryWithProcedureName(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + assertEquals(QUERY_PROCEDURE_NAME, namedQuery.getProcedureName()); + + namedQuery.setProcedureName("foo"); + assertEquals("foo", namedQuery.getProcedureName()); + + assertSourceContains("@NamedStoredProcedureQuery(procedureName = \"foo\")", cu); + + namedQuery.setProcedureName(null); + assertNull(namedQuery.getProcedureName()); + + assertSourceDoesNotContain("@NamedStoredProcedureQuery(", cu); + } + + // *********** parameters ********* + + public void testParameters1() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueries(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + + assertEquals(0, namedQuery.getParametersSize()); + } + + public void testParameters2() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueryWithParameters(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + + assertEquals(2, namedQuery.getParametersSize()); + + ListIterator iterator = namedQuery.getParameters().iterator(); + assertEquals("BAR", iterator.next().getName()); + assertNull(iterator.next().getName()); + } + + // *********** result classes ******* + + public void testResultClasses1() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueries(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + + assertEquals(0, namedQuery.getResultClassesSize()); + } + + public void testResultClasses2() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueryWithResultClasses(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + + assertEquals(2, namedQuery.getResultClassesSize()); + + ListIterator iterator = namedQuery.getResultClasses().iterator(); + assertEquals("Employee", iterator.next()); + assertEquals("Address", iterator.next()); + + } + + // ********** result set mappings ******** + + public void testResultSetMappings1() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueries(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + + assertEquals(0, namedQuery.getResultSetMappingsSize()); + } + + public void testResultSetMappings2() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueryWithResultSetMappings(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + + assertEquals(2, namedQuery.getResultSetMappingsSize()); + + ListIterator iterator = namedQuery.getResultSetMappings().iterator(); + assertEquals("EmployeeResultSetMapping", iterator.next()); + assertEquals("AddressResultSetMapping", iterator.next()); + + } + + // *********** hints ******** + + public void testHints1() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueries(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + + assertEquals(0, namedQuery.getHintsSize()); + } + + public void testHints2() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueryWithHints(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + + assertEquals(2, namedQuery.getHintsSize()); + + ListIterator iterator = namedQuery.getHints().iterator(); + assertEquals("BAR", iterator.next().getName()); + assertNull(iterator.next().getName()); + } + + // ********** other tests ******** + + public void testAddNamedQueryCopyExisting() throws Exception { + ICompilationUnit cu = createTestNamedStoredProcedureQuery(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + String expected1 = "@NamedStoredProcedureQueries({"; + String expected2 = "@NamedStoredProcedureQuery(name = \"foo\", hints = @QueryHint(name = \"BAR\", value = \"FOO\")),"; + String expected3 = "@NamedStoredProcedureQuery(name = \"BAR\") })"; + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.addAnnotation(1, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + namedQuery.setName("BAR"); + assertSourceContains(expected1, cu); + assertSourceContains(expected2, cu); + assertSourceContains(expected3, cu); + + assertNull(resourceType.getAnnotation(JPA2_1.NAMED_STORED_PROCEDURE_QUERY)); + assertNotNull(resourceType.getContainerAnnotation(JPA2_1.NAMED_STORED_PROCEDURE_QUERIES)); + assertNotNull(resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY)); + assertEquals(2, resourceType.getAnnotationsSize(JPA2_1.NAMED_STORED_PROCEDURE_QUERY)); + } + + public void testAddNamedQueryToBeginningOfList() throws Exception { + ICompilationUnit cu = createTestNamedStoredProcedureQuery(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + String expected1 = "@NamedStoredProcedureQueries({"; + String expected2 = "@NamedStoredProcedureQuery(name = \"foo\", hints = @QueryHint(name = \"BAR\", value = \"FOO\")),"; + String expected3 = "@NamedStoredProcedureQuery(name = \"BAR\") })"; + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.addAnnotation(1, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + namedQuery.setName("BAR"); + assertSourceContains(expected1, cu); + assertSourceContains(expected2, cu); + assertSourceContains(expected3, cu); + + expected2 = "@NamedStoredProcedureQuery(name = \"BAZ\"),"; + expected3 = "@NamedStoredProcedureQuery(name = \"foo\", hints = @QueryHint(name = \"BAR\", value = \"FOO\")), @NamedStoredProcedureQuery(name = \"BAR\") })"; + namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.addAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + namedQuery.setName("BAZ"); + assertSourceContains(expected1, cu); + assertSourceContains(expected2, cu); + assertSourceContains(expected3, cu); + + Iterator namedQueries = resourceType.getAnnotations(JPA2_1.NAMED_STORED_PROCEDURE_QUERY).iterator(); + assertEquals("BAZ", ((NamedStoredProcedureQuery2_1Annoation) namedQueries.next()).getName()); + assertEquals("foo", ((NamedStoredProcedureQuery2_1Annoation) namedQueries.next()).getName()); + assertEquals("BAR", ((NamedStoredProcedureQuery2_1Annoation) namedQueries.next()).getName()); + + assertNull(resourceType.getAnnotation(JPA2_1.NAMED_STORED_PROCEDURE_QUERY)); + assertNotNull(resourceType.getContainerAnnotation(JPA2_1.NAMED_STORED_PROCEDURE_QUERIES)); + assertNotNull(resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY)); + assertEquals(3, resourceType.getAnnotationsSize(JPA2_1.NAMED_STORED_PROCEDURE_QUERY)); + } + + public void testRemoveNamedQueryCopyExisting() throws Exception { + ICompilationUnit cu = createTestNamedStoredProcedureQuery(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + String expected1 = "@NamedStoredProcedureQueries({"; + String expected2 = "@NamedStoredProcedureQuery(name = \"foo\", hints = @QueryHint(name = \"BAR\", value = \"FOO\")),"; + String expected3 = "@NamedStoredProcedureQuery(name = \"BAR\") })"; + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.addAnnotation(1, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + namedQuery.setName("BAR"); + assertSourceContains(expected1, cu); + assertSourceContains(expected2, cu); + assertSourceContains(expected3, cu); + + expected1 = "@NamedStoredProcedureQuery(name = \"foo\", hints = @QueryHint(name = \"BAR\", value = \"FOO\"))"; + resourceType.removeAnnotation(1, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + assertSourceContains(expected1, cu); + assertSourceDoesNotContain("@NamedStoredProcedureQueries", cu); + } + +} diff --git src/org/eclipse/jpt/jpa/core/tests/internal/jpa2_1/resource/java/NamedStoredProcedureQuery2_1AnnoationTests.java src/org/eclipse/jpt/jpa/core/tests/internal/jpa2_1/resource/java/NamedStoredProcedureQuery2_1AnnoationTests.java new file mode 100644 index 0000000..3c80cf1 --- /dev/null +++ src/org/eclipse/jpt/jpa/core/tests/internal/jpa2_1/resource/java/NamedStoredProcedureQuery2_1AnnoationTests.java @@ -0,0 +1,722 @@ +/******************************************************************************* + * Copyright (c) 2013 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.tests.internal.jpa2_1.resource.java; + +import java.util.Iterator; +import java.util.ListIterator; +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jpt.common.core.resource.java.JavaResourceType; +import org.eclipse.jpt.common.utility.internal.iterator.ArrayIterator; +import org.eclipse.jpt.jpa.core.jpa2_1.resource.java.JPA2_1; +import org.eclipse.jpt.jpa.core.jpa2_1.resource.java.NamedStoredProcedureQuery2_1Annoation; +import org.eclipse.jpt.jpa.core.jpa2_1.resource.java.StoredProcedureParameter2_1Annotation; +import org.eclipse.jpt.jpa.core.resource.java.JPA; +import org.eclipse.jpt.jpa.core.resource.java.QueryHintAnnotation; + +public class NamedStoredProcedureQuery2_1AnnoationTests + extends JavaResourceModel2_1TestCase +{ + + private static final String QUERY_NAME = "myQuery"; + private static final String PROCEDURE_NAME = "myProcedure"; + + public NamedStoredProcedureQuery2_1AnnoationTests(String name) { + super(name); + } + + private ICompilationUnit createTestNamedStoredProcedureQuery() throws Exception { + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator imports() { + return new ArrayIterator(JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@NamedStoredProcedureQuery"); + } + }); + } + + private ICompilationUnit createTestNamedStoredProcedureQueryWithName() throws Exception { + return createTestNamedStoredProcedureQueryWithStringElement("name", QUERY_NAME); + } + + private ICompilationUnit createTestNamedNativeQueryWithProcedureName() throws Exception { + return createTestNamedStoredProcedureQueryWithStringElement("procedureName", PROCEDURE_NAME); + } + + + private ICompilationUnit createTestNamedStoredProcedureQueryWithStringElement(final String elementName, final String value) throws Exception { + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator imports() { + return new ArrayIterator(JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@NamedStoredProcedureQuery(" + elementName + " = \"" + value + "\")"); + } + }); + } + + private ICompilationUnit createTestNamedStoredProcedureQueryWithParameters() throws Exception { + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator imports() { + return new ArrayIterator(JPA2_1.NAMED_STORED_PROCEDURE_QUERY, JPA2_1.NAMED_STORED_PROCEDURE_PARAMETER, JPA2_1.PARAMETER_MODE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@NamedStoredProcedureQuery(parameters = {@StoredProcedureParameter(name = \"BAR\", " + + "mode=ParameterMode.IN, type=Integer.class), @StoredProcedureParameter})"); + } + }); + } + + private ICompilationUnit createTestNamedStoredProcedureQueryWithResultClasses() throws Exception { + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator imports() { + return new ArrayIterator(JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@NamedStoredProcedureQuery(resultClasses = { Employee.class, Address.class })"); + } + }); + } + + private ICompilationUnit createTestNamedStoredProcedureQueryWithResultSetMappings() throws Exception { + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator imports() { + return new ArrayIterator(JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@NamedStoredProcedureQuery(resultSetMappings = { \"EmpRSMapping\", \"AddrRSMapping\" })"); + } + }); + } + + private ICompilationUnit createTestNamedStoredProcedureQueryWithHints() throws Exception { + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator imports() { + return new ArrayIterator(JPA2_1.NAMED_STORED_PROCEDURE_QUERY, JPA.QUERY_HINT); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@NamedStoredProcedureQuery(hints = {@QueryHint(name = \"BAR\", value = \"FOO\"), @QueryHint})"); + } + }); + } + + public void testNamedStoredProcedureQuery() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQuery(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + assertNotNull(namedQuery); + } + + // ************ name *********** + + public void testGetName() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueryWithName(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + assertEquals(QUERY_NAME, namedQuery.getName()); + } + + public void testSetName() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueryWithName(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + assertEquals(QUERY_NAME, namedQuery.getName()); + + namedQuery.setName("foo"); + assertEquals("foo", namedQuery.getName()); + + assertSourceContains("@NamedStoredProcedureQuery(name = \"foo\")", cu); + + namedQuery.setName(null); + assertNull(namedQuery.getName()); + + assertSourceDoesNotContain("@NamedStoredProcedureQuery(", cu); + } + + // ************ procedure name *************** + + public void testGetProcedureName() throws Exception { + ICompilationUnit cu = this.createTestNamedNativeQueryWithProcedureName(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + assertEquals(PROCEDURE_NAME, namedQuery.getProcedureName()); + } + + public void testSetProcedureName() throws Exception { + ICompilationUnit cu = this.createTestNamedNativeQueryWithProcedureName(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + assertEquals(PROCEDURE_NAME, namedQuery.getProcedureName()); + + namedQuery.setProcedureName("foo"); + assertEquals("foo", namedQuery.getProcedureName()); + + assertSourceContains("@NamedStoredProcedureQuery(procedureName = \"foo\")", cu); + + namedQuery.setProcedureName(null); + assertNull(namedQuery.getProcedureName()); + + assertSourceDoesNotContain("@NamedStoredProcedureQuery(", cu); + } + + // ************ parameters ********* + + public void testParameters1() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQuery(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + + assertEquals(0, namedQuery.getParametersSize()); + } + + public void testParameters2() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQuery(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + + namedQuery.addParameter(0); + namedQuery.addParameter(1); + + assertEquals(2, namedQuery.getParametersSize()); + } + + public void testParameters3() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueryWithParameters(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + + assertEquals(2, namedQuery.getParametersSize()); + + ListIterator iterator = namedQuery.getParameters().iterator(); + assertEquals("BAR", iterator.next().getName()); + assertNull(iterator.next().getName()); + } + + public void testAddParameter() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQuery(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + + namedQuery.addParameter(0).setName("FOO"); + namedQuery.addParameter(1); + namedQuery.addParameter(0).setName("BAR"); + + assertEquals("BAR", namedQuery.parameterAt(0).getName()); + assertEquals("FOO", namedQuery.parameterAt(1).getName()); + assertNull(namedQuery.parameterAt(2).getName()); + assertSourceContains("@NamedStoredProcedureQuery(parameters = {@StoredProcedureParameter(name = \"BAR\")," + + "@StoredProcedureParameter(name = \"FOO\"), @StoredProcedureParameter})", cu); + } + + public void testRemoveParameter() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueryWithParameters(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + + namedQuery.addParameter(0).setName("BAZ"); + + assertEquals("BAZ", namedQuery.parameterAt(0).getName()); + assertEquals("BAR", namedQuery.parameterAt(1).getName()); + assertNull(namedQuery.parameterAt(2).getName()); + assertEquals(3, namedQuery.getParametersSize()); + + namedQuery.removeParameter(2); + assertEquals("BAZ", namedQuery.parameterAt(0).getName()); + assertEquals("BAR", namedQuery.parameterAt(1).getName()); + assertEquals(2, namedQuery.getParametersSize()); + assertSourceContains("@NamedStoredProcedureQuery(parameters = {@StoredProcedureParameter(name = \"BAZ\"), " + + "@StoredProcedureParameter(name = \"BAR\", mode=ParameterMode.IN, type=Integer.class)})", cu); + + namedQuery.removeParameter(0); + assertEquals("BAR", namedQuery.parameterAt(0).getName()); + assertEquals(1, namedQuery.getParametersSize()); + assertSourceContains("@NamedStoredProcedureQuery(parameters = " + + "@StoredProcedureParameter(name = \"BAR\", mode=ParameterMode.IN, type=Integer.class))", cu); + + namedQuery.removeParameter(0); + assertEquals(0, namedQuery.getParametersSize()); + assertSourceDoesNotContain("@NamedStoredProcedureQuery(", cu); + } + + public void testMoveParameter1() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueryWithParameters(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + + namedQuery.addParameter(0).setName("BAZ"); + + assertEquals("BAZ", namedQuery.parameterAt(0).getName()); + assertEquals("BAR", namedQuery.parameterAt(1).getName()); + assertNull(namedQuery.parameterAt(2).getName()); + assertEquals(3, namedQuery.getParametersSize()); + + namedQuery.moveParameter(2, 0); + + assertEquals("BAR", namedQuery.parameterAt(0).getName()); + assertNull(namedQuery.parameterAt(1).getName()); + assertEquals("BAZ", namedQuery.parameterAt(2).getName()); + assertEquals(3, namedQuery.getParametersSize()); + assertSourceContains("@NamedStoredProcedureQuery(parameters = {@StoredProcedureParameter(name = \"BAR\", mode=ParameterMode.IN, type=Integer.class), " + + "@StoredProcedureParameter, @StoredProcedureParameter(name = \"BAZ\")})", cu); + } + + public void testMoveParameter2() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueryWithParameters(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + namedQuery.addParameter(0).setName("BAZ"); + + assertEquals("BAZ", namedQuery.parameterAt(0).getName()); + assertEquals("BAR", namedQuery.parameterAt(1).getName()); + assertNull(namedQuery.parameterAt(2).getName()); + assertEquals(3, namedQuery.getParametersSize()); + + namedQuery.moveParameter(0, 2); + + assertNull(namedQuery.parameterAt(0).getName()); + assertEquals("BAZ", namedQuery.parameterAt(1).getName()); + assertEquals("BAR", namedQuery.parameterAt(2).getName()); + assertEquals(3, namedQuery.getParametersSize()); + assertSourceContains("@NamedStoredProcedureQuery(parameters = {@StoredProcedureParameter, @StoredProcedureParameter(name = \"BAZ\"), " + + "@StoredProcedureParameter(name = \"BAR\", mode=ParameterMode.IN, type=Integer.class)})", cu); + } + + // ************ result classes ********* + + public void testResultClasses1() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQuery(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + + assertEquals(0, namedQuery.getResultClassesSize()); + } + + public void testResultClasses2() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueryWithResultClasses(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + + assertEquals(2, namedQuery.getResultClassesSize()); + + ListIterator iterator = namedQuery.getResultClasses().iterator(); + assertEquals("Employee", iterator.next()); + assertEquals("Address", iterator.next()); + } + + public void testAddResultClass() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQuery(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + + namedQuery.addResultClass("Project"); + namedQuery.addResultClass("Phone"); + + assertEquals(2, namedQuery.getResultClassesSize()); + + assertEquals("Project", namedQuery.resultClassAt(0)); + assertEquals("Phone", namedQuery.resultClassAt(1)); + assertSourceContains("@NamedStoredProcedureQuery(resultClasses = { Project.class, Phone.class })", cu); + } + + public void testRemoveResultClass1() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueryWithResultClasses(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + + namedQuery.addResultClass("Project"); + + assertEquals("Employee", namedQuery.resultClassAt(0)); + assertEquals("Address", namedQuery.resultClassAt(1)); + assertEquals("Project", namedQuery.resultClassAt(2)); + assertEquals(3, namedQuery.getResultClassesSize()); + + namedQuery.removeResultClass(1); + assertEquals("Employee", namedQuery.resultClassAt(0)); + assertEquals("Project", namedQuery.resultClassAt(1)); + assertEquals(2, namedQuery.getResultClassesSize()); + assertSourceContains("@NamedStoredProcedureQuery(resultClasses = { Employee.class, Project.class })", cu); + + namedQuery.removeResultClass(0); + assertEquals("Project", namedQuery.resultClassAt(0)); + assertEquals(1, namedQuery.getResultClassesSize()); + assertSourceContains("@NamedStoredProcedureQuery(resultClasses = Project.class)", cu); + + namedQuery.removeResultClass(0); + assertEquals(0, namedQuery.getResultClassesSize()); + assertSourceDoesNotContain("@NamedStoredProcedureQuery(", cu); + } + + public void testRemoveResultClass2() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueryWithResultClasses(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + + namedQuery.addResultClass("Project"); + + assertEquals("Employee", namedQuery.resultClassAt(0)); + assertEquals("Address", namedQuery.resultClassAt(1)); + assertEquals("Project", namedQuery.resultClassAt(2)); + assertEquals(3, namedQuery.getResultClassesSize()); + + namedQuery.removeResultClass("Address"); + assertEquals("Employee", namedQuery.resultClassAt(0)); + assertEquals("Project", namedQuery.resultClassAt(1)); + assertEquals(2, namedQuery.getResultClassesSize()); + assertSourceContains("@NamedStoredProcedureQuery(resultClasses = { Employee.class, Project.class })", cu); + + namedQuery.removeResultClass("Employee"); + assertEquals("Project", namedQuery.resultClassAt(0)); + assertEquals(1, namedQuery.getResultClassesSize()); + assertSourceContains("@NamedStoredProcedureQuery(resultClasses = Project.class)", cu); + + namedQuery.removeResultClass("Project"); + assertEquals(0, namedQuery.getResultClassesSize()); + assertSourceDoesNotContain("@NamedStoredProcedureQuery(", cu); + } + + public void testMoveResultClass1() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueryWithResultClasses(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + + namedQuery.addResultClass("Project"); + + assertEquals("Employee", namedQuery.resultClassAt(0)); + assertEquals("Address", namedQuery.resultClassAt(1)); + assertEquals("Project", namedQuery.resultClassAt(2)); + assertEquals(3, namedQuery.getResultClassesSize()); + + namedQuery.moveResultClass(2, 0); + + assertEquals("Address", namedQuery.resultClassAt(0)); + assertEquals("Project", namedQuery.resultClassAt(1)); + assertEquals("Employee", namedQuery.resultClassAt(2)); + assertEquals(3, namedQuery.getResultClassesSize()); + assertSourceContains("@NamedStoredProcedureQuery(resultClasses = { Address.class, Project.class, Employee.class })", cu); + } + + public void testMoveResultClass2() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueryWithResultClasses(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + namedQuery.addResultClass("Project"); + + assertEquals("Employee", namedQuery.resultClassAt(0)); + assertEquals("Address", namedQuery.resultClassAt(1)); + assertEquals("Project", namedQuery.resultClassAt(2)); + assertEquals(3, namedQuery.getResultClassesSize()); + + namedQuery.moveResultClass(0, 2); + + assertEquals("Project", namedQuery.resultClassAt(0)); + assertEquals("Employee", namedQuery.resultClassAt(1)); + assertEquals("Address", namedQuery.resultClassAt(2)); + assertEquals(3, namedQuery.getResultClassesSize()); + assertSourceContains("@NamedStoredProcedureQuery(resultClasses = { Project.class, Employee.class, Address.class })", cu); + } + + + // ************ result set mappings ********* + + public void testResultSetMappings1() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQuery(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + + assertEquals(0, namedQuery.getResultSetMappingsSize()); + } + + public void testResultSetMappings2() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueryWithResultSetMappings(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + + assertEquals(2, namedQuery.getResultSetMappingsSize()); + + ListIterator iterator = namedQuery.getResultSetMappings().iterator(); + assertEquals("EmpRSMapping", iterator.next()); + assertEquals("AddrRSMapping", iterator.next()); + assertSourceContains("@NamedStoredProcedureQuery(resultSetMappings = { \"EmpRSMapping\", \"AddrRSMapping\" })", cu); + } + + public void testAddResultSetMapping() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQuery(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + + namedQuery.addResultSetMapping("ProjRSMapping"); + namedQuery.addResultSetMapping("PhRSMapping"); + + assertEquals(2, namedQuery.getResultSetMappingsSize()); + + assertEquals("ProjRSMapping", namedQuery.resultSetMappingAt(0)); + assertEquals("PhRSMapping", namedQuery.resultSetMappingAt(1)); + assertSourceContains("@NamedStoredProcedureQuery(resultSetMappings = { \"ProjRSMapping\", \"PhRSMapping\" })", cu); + } + + public void testRemoveResultSetMapping1() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueryWithResultSetMappings(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + + namedQuery.addResultSetMapping("ProjRSMapping"); + + assertEquals("EmpRSMapping", namedQuery.resultSetMappingAt(0)); + assertEquals("AddrRSMapping", namedQuery.resultSetMappingAt(1)); + assertEquals("ProjRSMapping", namedQuery.resultSetMappingAt(2)); + assertEquals(3, namedQuery.getResultSetMappingsSize()); + + namedQuery.removeResultSetMapping(1); + assertEquals("EmpRSMapping", namedQuery.resultSetMappingAt(0)); + assertEquals("ProjRSMapping", namedQuery.resultSetMappingAt(1)); + assertEquals(2, namedQuery.getResultSetMappingsSize()); + assertSourceContains("@NamedStoredProcedureQuery(resultSetMappings = { \"EmpRSMapping\", \"ProjRSMapping\" })", cu); + + namedQuery.removeResultSetMapping(0); + assertEquals("ProjRSMapping", namedQuery.resultSetMappingAt(0)); + assertEquals(1, namedQuery.getResultSetMappingsSize()); + assertSourceContains("@NamedStoredProcedureQuery(resultSetMappings = \"ProjRSMapping\")", cu); + + namedQuery.removeResultSetMapping(0); + assertEquals(0, namedQuery.getResultSetMappingsSize()); + assertSourceDoesNotContain("@NamedStoredProcedureQuery(", cu); + } + + public void testRemoveResultSetMapping2() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueryWithResultSetMappings(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + + namedQuery.addResultSetMapping("ProjRSMapping"); + + assertEquals("EmpRSMapping", namedQuery.resultSetMappingAt(0)); + assertEquals("AddrRSMapping", namedQuery.resultSetMappingAt(1)); + assertEquals("ProjRSMapping", namedQuery.resultSetMappingAt(2)); + assertEquals(3, namedQuery.getResultSetMappingsSize()); + + namedQuery.removeResultSetMapping("AddrRSMapping"); + assertEquals("EmpRSMapping", namedQuery.resultSetMappingAt(0)); + assertEquals("ProjRSMapping", namedQuery.resultSetMappingAt(1)); + assertEquals(2, namedQuery.getResultSetMappingsSize()); + assertSourceContains("@NamedStoredProcedureQuery(resultSetMappings = { \"EmpRSMapping\", \"ProjRSMapping\" })", cu); + + namedQuery.removeResultSetMapping("EmpRSMapping"); + assertEquals("ProjRSMapping", namedQuery.resultSetMappingAt(0)); + assertEquals(1, namedQuery.getResultSetMappingsSize()); + assertSourceContains("@NamedStoredProcedureQuery(resultSetMappings = \"ProjRSMapping\")", cu); + + namedQuery.removeResultSetMapping("ProjRSMapping"); + assertEquals(0, namedQuery.getResultSetMappingsSize()); + assertSourceDoesNotContain("@NamedStoredProcedureQuery(", cu); + } + public void testMoveResultSetMapping1() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueryWithResultSetMappings(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + + namedQuery.addResultSetMapping("ProjRSMapping"); + + assertEquals("EmpRSMapping", namedQuery.resultSetMappingAt(0)); + assertEquals("AddrRSMapping", namedQuery.resultSetMappingAt(1)); + assertEquals("ProjRSMapping", namedQuery.resultSetMappingAt(2)); + assertEquals(3, namedQuery.getResultSetMappingsSize()); + + namedQuery.moveResultSetMapping(2, 0); + + assertEquals("AddrRSMapping", namedQuery.resultSetMappingAt(0)); + assertEquals("ProjRSMapping", namedQuery.resultSetMappingAt(1)); + assertEquals("EmpRSMapping", namedQuery.resultSetMappingAt(2)); + assertEquals(3, namedQuery.getResultSetMappingsSize()); + assertSourceContains("@NamedStoredProcedureQuery(resultSetMappings = { \"AddrRSMapping\", \"ProjRSMapping\", \"EmpRSMapping\" })", cu); + } + + public void testMoveResultSetMapping2() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueryWithResultSetMappings(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + namedQuery.addResultSetMapping("ProjRSMapping"); + + assertEquals("EmpRSMapping", namedQuery.resultSetMappingAt(0)); + assertEquals("AddrRSMapping", namedQuery.resultSetMappingAt(1)); + assertEquals("ProjRSMapping", namedQuery.resultSetMappingAt(2)); + assertEquals(3, namedQuery.getResultSetMappingsSize()); + + namedQuery.moveResultSetMapping(0, 2); + + assertEquals("ProjRSMapping", namedQuery.resultSetMappingAt(0)); + assertEquals("EmpRSMapping", namedQuery.resultSetMappingAt(1)); + assertEquals("AddrRSMapping", namedQuery.resultSetMappingAt(2)); + assertEquals(3, namedQuery.getResultSetMappingsSize()); + assertSourceContains("@NamedStoredProcedureQuery(resultSetMappings = { \"ProjRSMapping\", \"EmpRSMapping\", \"AddrRSMapping\" })", cu); + } + + // *********** hints ************ + + public void testHints1() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQuery(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + + assertEquals(0, namedQuery.getHintsSize()); + } + + public void testHints2() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQuery(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + + namedQuery.addHint(0); + namedQuery.addHint(1); + + assertEquals(2, namedQuery.getHintsSize()); + } + + public void testHints3() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueryWithHints(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + + assertEquals(2, namedQuery.getHintsSize()); + + ListIterator iterator = namedQuery.getHints().iterator(); + assertEquals("BAR", iterator.next().getName()); + assertNull(iterator.next().getName()); + } + + public void testAddHint() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQuery(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + + namedQuery.addHint(0).setName("FOO"); + namedQuery.addHint(1); + namedQuery.addHint(0).setName("BAR"); + + assertEquals("BAR", namedQuery.hintAt(0).getName()); + assertEquals("FOO", namedQuery.hintAt(1).getName()); + assertNull(namedQuery.hintAt(2).getName()); + assertSourceContains("@NamedStoredProcedureQuery(hints = {@QueryHint(name = \"BAR\"),@QueryHint(name = \"FOO\"), @QueryHint})", cu); + } + + public void testRemoveHint() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueryWithHints(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + namedQuery.addHint(0).setName("BAZ"); + + assertEquals("BAZ", namedQuery.hintAt(0).getName()); + assertEquals("BAR", namedQuery.hintAt(1).getName()); + assertNull(namedQuery.hintAt(2).getName()); + assertEquals(3, namedQuery.getHintsSize()); + + namedQuery.removeHint(2); + assertEquals("BAZ", namedQuery.hintAt(0).getName()); + assertEquals("BAR", namedQuery.hintAt(1).getName()); + assertEquals(2, namedQuery.getHintsSize()); + assertSourceContains("@NamedStoredProcedureQuery(hints = {@QueryHint(name = \"BAZ\"), @QueryHint(name = \"BAR\", value = \"FOO\")})", cu); + + namedQuery.removeHint(0); + assertEquals("BAR", namedQuery.hintAt(0).getName()); + assertEquals(1, namedQuery.getHintsSize()); + assertSourceContains("@NamedStoredProcedureQuery(hints = @QueryHint(name = \"BAR\", value = \"FOO\"))", cu); + + + namedQuery.removeHint(0); + assertEquals(0, namedQuery.getHintsSize()); + assertSourceDoesNotContain("@NamedStoredProcedureQuery(", cu); + } + + public void testMoveHint1() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueryWithHints(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + + namedQuery.addHint(0).setName("BAZ"); + + assertEquals("BAZ", namedQuery.hintAt(0).getName()); + assertEquals("BAR", namedQuery.hintAt(1).getName()); + assertNull(namedQuery.hintAt(2).getName()); + assertEquals(3, namedQuery.getHintsSize()); + + namedQuery.moveHint(2, 0); + + assertEquals("BAR", namedQuery.hintAt(0).getName()); + assertNull(namedQuery.hintAt(1).getName()); + assertEquals("BAZ", namedQuery.hintAt(2).getName()); + assertEquals(3, namedQuery.getHintsSize()); + assertSourceContains("@NamedStoredProcedureQuery(hints = {@QueryHint(name = \"BAR\", value = \"FOO\"), @QueryHint, @QueryHint(name = \"BAZ\")})", cu); + } + + public void testMoveHint2() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueryWithHints(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + namedQuery.addHint(0).setName("BAZ"); + + assertEquals("BAZ", namedQuery.hintAt(0).getName()); + assertEquals("BAR", namedQuery.hintAt(1).getName()); + assertNull(namedQuery.hintAt(2).getName()); + assertEquals(3, namedQuery.getHintsSize()); + + namedQuery.moveHint(0, 2); + + assertNull(namedQuery.hintAt(0).getName()); + assertEquals("BAZ", namedQuery.hintAt(1).getName()); + assertEquals("BAR", namedQuery.hintAt(2).getName()); + assertEquals(3, namedQuery.getHintsSize()); + assertSourceContains("@NamedStoredProcedureQuery(hints = {@QueryHint, @QueryHint(name = \"BAZ\"), @QueryHint(name = \"BAR\", value = \"FOO\")})", cu); + } + +} diff --git src/org/eclipse/jpt/jpa/core/tests/internal/jpa2_1/resource/java/StoredProcedureParameter2_1AnnotationTests.java src/org/eclipse/jpt/jpa/core/tests/internal/jpa2_1/resource/java/StoredProcedureParameter2_1AnnotationTests.java new file mode 100644 index 0000000..8f6e8a7 --- /dev/null +++ src/org/eclipse/jpt/jpa/core/tests/internal/jpa2_1/resource/java/StoredProcedureParameter2_1AnnotationTests.java @@ -0,0 +1,153 @@ +/******************************************************************************* + * Copyright (c) 2013 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.tests.internal.jpa2_1.resource.java; + +import java.util.Iterator; +import org.eclipse.jdt.core.ICompilationUnit; +import org.eclipse.jpt.common.core.resource.java.JavaResourceType; +import org.eclipse.jpt.common.utility.internal.iterator.ArrayIterator; +import org.eclipse.jpt.jpa.core.jpa2_1.resource.java.JPA2_1; +import org.eclipse.jpt.jpa.core.jpa2_1.resource.java.NamedStoredProcedureQuery2_1Annoation; +import org.eclipse.jpt.jpa.core.jpa2_1.resource.java.ParameterMode2_1; +import org.eclipse.jpt.jpa.core.jpa2_1.resource.java.StoredProcedureParameter2_1Annotation; + +public class StoredProcedureParameter2_1AnnotationTests + extends JavaResourceModel2_1TestCase +{ + + public StoredProcedureParameter2_1AnnotationTests(String name) { + super(name); + } + + private ICompilationUnit createTestNamedStoredProcedureQueryWithParameters() throws Exception { + return this.createTestType(new DefaultAnnotationWriter() { + @Override + public Iterator imports() { + return new ArrayIterator(JPA2_1.NAMED_STORED_PROCEDURE_QUERY, JPA2_1.NAMED_STORED_PROCEDURE_PARAMETER, JPA2_1.PARAMETER_MODE); + } + @Override + public void appendTypeAnnotationTo(StringBuilder sb) { + sb.append("@NamedStoredProcedureQuery(parameters = {@StoredProcedureParameter(name = \"MyParameter\", mode = ParameterMode.IN, type = MyType.class), @StoredProcedureParameter})"); + } + }); + } + + public void testNamedStoredProcedureQueryWithParameters() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueryWithParameters(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + assertEquals(2, namedQuery.getParametersSize()); + StoredProcedureParameter2_1Annotation parameter = namedQuery.parameterAt(0); + assertEquals("MyParameter", parameter.getName()); + parameter = namedQuery.parameterAt(1); + assertNull(parameter.getName()); + } + + // ******** name ********* + + public void testGetName() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueryWithParameters(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + StoredProcedureParameter2_1Annotation parameter = namedQuery.parameterAt(0); + assertEquals("MyParameter", parameter.getName()); + } + + public void testSetName() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueryWithParameters(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + StoredProcedureParameter2_1Annotation parameter = namedQuery.parameterAt(0); + assertEquals("MyParameter", parameter.getName()); + + parameter.setName("foo"); + assertEquals("foo", parameter.getName()); + + assertSourceContains("@StoredProcedureParameter(name = \"foo\", mode = ParameterMode.IN, type = MyType.class)", cu); + } + + // ******* mode ********* + + public void testGetMode() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueryWithParameters(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + StoredProcedureParameter2_1Annotation parameter = namedQuery.parameterAt(0); + assertEquals(ParameterMode2_1.IN, parameter.getMode()); + } + + public void testSetMode() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueryWithParameters(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + StoredProcedureParameter2_1Annotation parameter = namedQuery.parameterAt(0); + assertEquals(ParameterMode2_1.IN, parameter.getMode()); + + parameter.setMode(ParameterMode2_1.INOUT); + assertEquals(ParameterMode2_1.INOUT, parameter.getMode()); + assertSourceContains("@StoredProcedureParameter(name = \"MyParameter\", mode = INOUT, type = MyType.class)", cu); + + parameter.setMode(null); + assertNull(parameter.getMode()); + assertSourceDoesNotContain("mode", cu); + assertSourceContains("@StoredProcedureParameter(name = \"MyParameter\", type = MyType.class)", cu); + } + + // ******* type ********* + public void testGetType() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueryWithParameters(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + StoredProcedureParameter2_1Annotation parameter = namedQuery.parameterAt(0); + + assertEquals("MyType", parameter.getType()); + } + + public void testSetType() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueryWithParameters(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + StoredProcedureParameter2_1Annotation parameter = namedQuery.parameterAt(0); + + assertEquals("MyType", parameter.getType()); + + parameter.setType("Foo"); + assertEquals("Foo", parameter.getType()); + assertSourceContains("@StoredProcedureParameter(name = \"MyParameter\", mode = ParameterMode.IN, type = Foo.class", cu); + + parameter.setType(null); + assertNull(parameter.getType()); + assertSourceDoesNotContain("type", cu); + assertSourceContains("@StoredProcedureParameter(name = \"MyParameter\", mode = ParameterMode.IN", cu); + } + + public void testGetFullyQualifiedClass() throws Exception { + ICompilationUnit cu = this.createTestNamedStoredProcedureQueryWithParameters(); + JavaResourceType resourceType = buildJavaResourceType(cu); + + NamedStoredProcedureQuery2_1Annoation namedQuery = (NamedStoredProcedureQuery2_1Annoation) resourceType.getAnnotation(0, JPA2_1.NAMED_STORED_PROCEDURE_QUERY); + StoredProcedureParameter2_1Annotation parameter = namedQuery.parameterAt(0); + + assertNotNull(parameter.getType()); + assertEquals("MyType", parameter.getFullyQualifiedTypeName()); + + parameter.setType(TYPE_NAME); + + assertEquals(FULLY_QUALIFIED_TYPE_NAME, parameter.getFullyQualifiedTypeName()); + assertSourceContains("@StoredProcedureParameter(name = \"MyParameter\", mode = ParameterMode.IN, type = " + TYPE_NAME + ".class)", cu); + } +} diff --git src/org/eclipse/jpt/jpa/core/tests/internal/resource/JptJpaCoreResourceModelTests.java src/org/eclipse/jpt/jpa/core/tests/internal/resource/JptJpaCoreResourceModelTests.java index 856925d..9a38379 100644 --- src/org/eclipse/jpt/jpa/core/tests/internal/resource/JptJpaCoreResourceModelTests.java +++ src/org/eclipse/jpt/jpa/core/tests/internal/resource/JptJpaCoreResourceModelTests.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2007, 2010 Oracle. All rights reserved. + * Copyright (c) 2007, 2013 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. @@ -14,6 +14,7 @@ import junit.framework.TestSuite; import org.eclipse.jpt.jpa.core.tests.internal.JptJpaCoreTests; import org.eclipse.jpt.jpa.core.tests.internal.jpa2.resource.java.JavaResource2_0Tests; +import org.eclipse.jpt.jpa.core.tests.internal.jpa2_1.resource.java.JavaResource2_1Tests; import org.eclipse.jpt.jpa.core.tests.internal.resource.java.JptJavaResourceTests; /** @@ -29,6 +30,7 @@ if (JptJpaCoreTests.requiredJarsExists()) { suite.addTest(JptJavaResourceTests.suite()); suite.addTest(JavaResource2_0Tests.suite()); + suite.addTest(JavaResource2_1Tests.suite()); } else { suite.addTest(TestSuite.warning(JptJpaCoreTests.buildMissingJarErrorMessage())); }