diff options
author | nhauge | 2011-07-12 16:38:55 +0000 |
---|---|---|
committer | nhauge | 2011-07-12 16:38:55 +0000 |
commit | d7e05f62919d46ec73eef009c4e00d5b031b9b51 (patch) | |
tree | 5aa99a6db2d2231fd074242337edc98765d82b71 | |
parent | 629a1694eb44fdcfc9e4309cec3dae842e6ff96d (diff) | |
download | webtools.dali-d7e05f62919d46ec73eef009c4e00d5b031b9b51.tar.gz webtools.dali-d7e05f62919d46ec73eef009c4e00d5b031b9b51.tar.xz webtools.dali-d7e05f62919d46ec73eef009c4e00d5b031b9b51.zip |
[348773] Refactored validation for duplicate entity names. Patch from Nan.
6 files changed, 113 insertions, 193 deletions
diff --git a/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/context/persistence/PersistenceUnit.java b/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/context/persistence/PersistenceUnit.java index ae16400632..44afac5c51 100644 --- a/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/context/persistence/PersistenceUnit.java +++ b/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/context/persistence/PersistenceUnit.java @@ -11,8 +11,6 @@ package org.eclipse.jpt.jpa.core.context.persistence; import java.util.Iterator; import java.util.ListIterator; -import java.util.Map; -import java.util.Set; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFolder; import org.eclipse.core.runtime.IPath; @@ -729,58 +727,6 @@ public interface PersistenceUnit */ boolean containsOffset(int textOffset); - /** - * Return a map with entity names as the key and class names as the value. - * Duplicate class names are eliminated. - */ - // TODO bjv - Map<String, Set<String>> mapEntityNameToClassNames(); - - /** - * Return the class names of all the mapped orm classes cross the persistent unit - */ - // TODO bjv probably should re-work, but also should use "mapping file" instead of - // "orm"... - Iterable<String> getOrmMappedClassNames(); - - /** - * Return all the entities defined in both the implied and specified mapping files - * of a persistence unit - */ - // TODO bjv probably should re-work, but also should use "mapping file" instead of - // "orm"... - Iterable<Entity> getOrmEntities(); - - /** - * Return the entity names of all the entities defined in both the implied and specified mapping files - * of a persistence unit - */ - // TODO bjv probably should re-work, but also should use "mapping file" instead of - // "orm"... - Iterator<String> ormEntityNames(); - - /** - * Return all the entities defined with both the implied and specified Java classes - * of a persistence unit - */ - // TODO bjv remove - Iterable<Entity> getJavaEntities(); - - /** - * Return the entity names of all the entities defined with both the implied and specified Java classes - * of a persistence unit - */ - // TODO bjv remove - Iterator<String> javaEntityNames(); - - /** - * Return the entity names of entities only defined with mapped Java classes of a persistence unit. - * The names of Java entities overridden by entities defined in the mapping files are excluded. - */ - // TODO bjv - Iterator<String> javaEntityNamesExclOverridden(); - - // ********** validation ********** /** diff --git a/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/context/java/AbstractJavaEntity.java b/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/context/java/AbstractJavaEntity.java index 7c4b6a5d9c..f8af81331e 100644 --- a/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/context/java/AbstractJavaEntity.java +++ b/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/context/java/AbstractJavaEntity.java @@ -18,7 +18,6 @@ import org.eclipse.jdt.core.dom.CompilationUnit; import org.eclipse.jpt.common.core.utility.TextRange; import org.eclipse.jpt.common.utility.Filter; import org.eclipse.jpt.common.utility.internal.CollectionTools; -import org.eclipse.jpt.common.utility.internal.HashBag; import org.eclipse.jpt.common.utility.internal.NotNullFilter; import org.eclipse.jpt.common.utility.internal.StringTools; import org.eclipse.jpt.common.utility.internal.Tools; @@ -1349,7 +1348,6 @@ public abstract class AbstractJavaEntity this.attributeOverrideContainer.validate(messages, reporter, astRoot); this.associationOverrideContainer.validate(messages, reporter, astRoot); this.validateEntityName(messages, astRoot); - this.validateDuplicateEntityNames(messages, astRoot); this.idClassReference.validate(messages, reporter, astRoot); } @@ -1372,29 +1370,6 @@ public abstract class AbstractJavaEntity } } - protected void validateDuplicateEntityNames(List<IMessage> messages, CompilationUnit astRoot) { - HashBag<String> javaEntityNamesExclOverridden = CollectionTools.bag(this.getPersistenceUnit().javaEntityNamesExclOverridden()); - HashSet<String> ormEntityNames = CollectionTools.set(this.getPersistenceUnit().ormEntityNames()); - String javaEntityName = this.getName(); - if ((javaEntityName != null) - // Check whether or not this entity name has duplicates among - // the java entities that are not overridden by orm entities - && ((javaEntityNamesExclOverridden.count(javaEntityName) > 1) - //Check whether or not this entity name has duplicates - //with the names of orm entities - || (ormEntityNames.contains(javaEntityName)))) { - messages.add( - DefaultJpaValidationMessages.buildMessage( - IMessage.HIGH_SEVERITY, - JpaValidationMessages.ENTITY_NAME_DUPLICATED, - new String[] {javaEntityName}, - this, - this.getMappingAnnotation().getNameTextRange(astRoot) - ) - ); - } - } - protected void validatePrimaryKey(List<IMessage> messages, IReporter reporter, CompilationUnit astRoot) { this.buildPrimaryKeyValidator(astRoot).validate(messages, reporter); } diff --git a/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/context/orm/AbstractOrmEntity.java b/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/context/orm/AbstractOrmEntity.java index 4831eebcd7..6db18c8d9f 100644 --- a/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/context/orm/AbstractOrmEntity.java +++ b/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/context/orm/AbstractOrmEntity.java @@ -1804,7 +1804,6 @@ public abstract class AbstractOrmEntity<X extends XmlEntity> this.generatorContainer.validate(messages, reporter); this.queryContainer.validate(messages, reporter); this.validateEntityName(messages); - this.validateDuplicateEntityNames(messages); this.idClassReference.validate(messages, reporter); } @@ -1822,27 +1821,6 @@ public abstract class AbstractOrmEntity<X extends XmlEntity> } } - protected void validateDuplicateEntityNames(List<IMessage> messages) { - HashSet<String> javaEntityNamesExclOverridden = CollectionTools.set(this.getPersistenceUnit().javaEntityNamesExclOverridden()); - Map<String, Set<String>> map = this.getPersistenceUnit().mapEntityNameToClassNames(); - Set<String> classNames = map.get(this.getName()); - // Check whether or not this entity name has duplicates among the orm entities defined with different classes - if (((classNames != null) && (classNames.size() > 1)) || - // Check whether or not this entity name has duplicates among - // the java entities that are not defined in the mapping files - (javaEntityNamesExclOverridden.contains(this.getName()))) { - messages.add( - DefaultJpaValidationMessages.buildMessage( - IMessage.HIGH_SEVERITY, - JpaValidationMessages.ENTITY_NAME_DUPLICATED, - new String[] {this.getName()}, - this, - this.getNameTextRange() - ) - ); - } - } - protected void validatePrimaryKey(List<IMessage> messages, IReporter reporter) { this.buildPrimaryKeyValidator().validate(messages, reporter); } diff --git a/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/context/orm/AbstractOrmTypeMapping.java b/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/context/orm/AbstractOrmTypeMapping.java index 31778e7022..de5b1394d1 100644 --- a/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/context/orm/AbstractOrmTypeMapping.java +++ b/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/context/orm/AbstractOrmTypeMapping.java @@ -15,7 +15,6 @@ import org.eclipse.jdt.core.IPackageFragment; import org.eclipse.jdt.core.IType; import org.eclipse.jpt.common.core.utility.TextRange; import org.eclipse.jpt.common.utility.internal.CollectionTools; -import org.eclipse.jpt.common.utility.internal.HashBag; import org.eclipse.jpt.common.utility.internal.StringTools; import org.eclipse.jpt.common.utility.internal.Tools; import org.eclipse.jpt.common.utility.internal.iterables.EmptyIterable; @@ -462,20 +461,6 @@ public abstract class AbstractOrmTypeMapping<X extends XmlTypeMapping> this.getClassTextRange() ) ); - } else { - HashBag<String> ormMappedClassNames = new HashBag<String>(); - CollectionTools.addAll(ormMappedClassNames, this.getPersistenceUnit().getOrmMappedClassNames()); - if (ormMappedClassNames.count(this.class_) > 1) { - messages.add( - DefaultJpaValidationMessages.buildMessage( - IMessage.NORMAL_SEVERITY, - JpaValidationMessages.PERSISTENT_TYPE_DUPLICATE_CLASS, - new String[] {this.class_}, - this, - this.getClassTextRange() - ) - ); - } return; } this.buildTypeMappingValidator().validate(messages, reporter); diff --git a/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/context/persistence/AbstractPersistenceUnit.java b/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/context/persistence/AbstractPersistenceUnit.java index 8b51ef7b49..9dac1cf5ab 100644 --- a/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/context/persistence/AbstractPersistenceUnit.java +++ b/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/context/persistence/AbstractPersistenceUnit.java @@ -20,6 +20,7 @@ import java.util.ListIterator; import java.util.Map; import java.util.Set; import java.util.Vector; + import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IFolder; import org.eclipse.core.runtime.CoreException; @@ -64,9 +65,11 @@ import org.eclipse.jpt.jpa.core.context.Query; import org.eclipse.jpt.jpa.core.context.QueryContainer; import org.eclipse.jpt.jpa.core.context.ReadOnlyPersistentAttribute; import org.eclipse.jpt.jpa.core.context.TypeMapping; +import org.eclipse.jpt.jpa.core.context.java.JavaEntity; import org.eclipse.jpt.jpa.core.context.java.JavaGenerator; import org.eclipse.jpt.jpa.core.context.java.JavaQuery; import org.eclipse.jpt.jpa.core.context.orm.EntityMappings; +import org.eclipse.jpt.jpa.core.context.orm.OrmEntity; import org.eclipse.jpt.jpa.core.context.orm.OrmGenerator; import org.eclipse.jpt.jpa.core.context.orm.OrmPersistentType; import org.eclipse.jpt.jpa.core.context.orm.OrmQuery; @@ -2036,18 +2039,38 @@ public abstract class AbstractPersistenceUnit }; } - - // ********** mapping file type mappings ********** + /** + * Filters out the entities from the active type mappings + */ + protected Iterable<Entity> getActiveEntities() { + return filterToEntities(this.getActiveTypeMappings()); + } /** - * Return a map of the entities defined in the persistence unit's mapping files, - * keyed by entity name. Since there can be (erroneously) duplicate entity - * names, each entity name is mapped to a <em>list</em> of entities. + * Returns the "active" type mappings, i.e. the mapping file type mappings and + * the Java type mappings that are not "overridden" by mapping file + * type mappings (by full qualified class name). */ - protected HashMap<String, ArrayList<Entity>> mapMappingFileEntitiesByName() { - return this.mapTypeMappingsByName(this.getMappingFileEntities()); + protected Iterable<TypeMapping> getActiveTypeMappings(){ + ArrayList<TypeMapping> typeMappingList = new ArrayList<TypeMapping>(); + CollectionTools.addAll(typeMappingList, this.getMappingFileTypeMappings()); + + HashMap<String, ArrayList<TypeMapping>> mappingFileTypeMappings = this.mapTypeMappingsByClassName(this.getMappingFileTypeMappings()); + HashMap<String, ArrayList<TypeMapping>> javaTypeMappings = this.mapTypeMappingsByClassName(this.getJavaTypeMappings()); + for (Map.Entry<String, ArrayList<TypeMapping>> javaTypeMappingEntry : javaTypeMappings.entrySet()) { + if (mappingFileTypeMappings.get(javaTypeMappingEntry.getKey()) == null) { + typeMappingList.addAll(javaTypeMappingEntry.getValue()); + } + } + + return typeMappingList; } + /** + * Return a map of the type mappings keyed by type mapping name (short class name). + * Since there can be duplicate (erroneously) type mapping names, + * each type mapping name is mapped to a <em>list</em> of type mappings. + */ protected <M extends TypeMapping> HashMap<String, ArrayList<M>> mapTypeMappingsByName(Iterable<M> typeMappings) { HashMap<String, ArrayList<M>> map = new HashMap<String, ArrayList<M>>(); for (M typeMapping : typeMappings) { @@ -2063,6 +2086,27 @@ public abstract class AbstractPersistenceUnit } /** + * Return a map of the type mappings keyed by full qualified class name. + * Since there can be duplicate (erroneously) class names, + * each class name is mapped to a <em>list</em> of type mappings. + */ + protected <M extends TypeMapping> HashMap<String, ArrayList<M>> mapTypeMappingsByClassName(Iterable<M> typeMappings) { + HashMap<String, ArrayList<M>> map = new HashMap<String, ArrayList<M>>(); + for (M typeMapping : typeMappings) { + String typeMappingName = typeMapping.getPersistentType().getName(); + ArrayList<M> list = map.get(typeMappingName); + if (list == null) { + list = new ArrayList<M>(); + map.put(typeMappingName, list); + } + list.add(typeMapping); + } + return map; + } + + // ********** mapping file type mappings ********** + + /** * Return all the entities defined in the persistence unit's mapping files * (i.e. excluding the Java entities). */ @@ -2083,8 +2127,7 @@ public abstract class AbstractPersistenceUnit }; } - // TODO remove VVVVVVVVVVVVVVVVV - public Iterable<String> getOrmMappedClassNames() { + public Iterable<String> getMappingFileMappedClassNames() { return new TransformationIterable<PersistentType, String>(this.getMappingFilePersistentTypes()) { @Override protected String transform(PersistentType persistentType) { @@ -2093,43 +2136,8 @@ public abstract class AbstractPersistenceUnit }; } - public Map<String, Set<String>> mapEntityNameToClassNames() { - HashMap<String, ArrayList<Entity>> mappingFileEntitiesByName = this.mapMappingFileEntitiesByName(); - HashMap<String, Set<String>> map = new HashMap<String, Set<String>>(mappingFileEntitiesByName.size()); - for (Map.Entry<String, ArrayList<Entity>> entry : mappingFileEntitiesByName.entrySet()) { - String entityName = entry.getKey(); - ArrayList<Entity> entities = entry.getValue(); - HashSet<String> entityClassNames = new HashSet<String>(entities.size()); - for (Entity entity : entities) { - entityClassNames.add(entity.getPersistentType().getName()); - } - map.put(entityName, entityClassNames); - } - return map; - } - - public Iterator<String> ormEntityNames() { - return this.getMappingFileEntityNames().iterator(); - } - - protected Iterable<String> getMappingFileEntityNames() { - return new TransformationIterable<Entity, String>(this.getMappingFileEntities()) { - @Override - protected String transform(Entity entity) { - return entity.getName(); - } - }; - } - - public Iterable<Entity> getOrmEntities() { - return this.getMappingFileEntities(); - } - // remove ^^^^^^^^^^^^^^^^^ - - // ********** Java type mappings ********** - // TODO remove VVVVVVVVVVVVVVVVV /** * These may be overridden in the mapping files. * @see #getJavaPersistentTypes() @@ -2151,38 +2159,6 @@ public abstract class AbstractPersistenceUnit }; } - protected Iterator<String> javaEntityClassNames(){ - return new TransformationIterator<Entity, String>(this.getJavaEntities()) { - @Override - protected String transform(Entity javaEntity) { - return javaEntity.getPersistentType().getName(); - } - }; - } - - public Iterator<String> javaEntityNamesExclOverridden() { - HashSet<String> ormMappedClassNames = CollectionTools.set(this.getOrmMappedClassNames()); - List<String> javaEntityNamesExclOverridden = new ArrayList<String>(); - for (Iterator<String> javaEntityClassNames = this.javaEntityClassNames(); javaEntityClassNames.hasNext();){ - String javaEntityClassName = javaEntityClassNames.next(); - if (!ormMappedClassNames.contains(javaEntityClassName)) { - javaEntityNamesExclOverridden.add((this.getEntity(javaEntityClassName)).getName()); - } - } - return javaEntityNamesExclOverridden.iterator(); - } - - public Iterator<String> javaEntityNames(){ - return new TransformationIterator<Entity, String>(this.getJavaEntities()) { - @Override - protected String transform(Entity javaEntity) { - return javaEntity.getName(); - } - }; - } - // remove ^^^^^^^^^^^^^^^^^ - - // ********** misc ********** public XmlPersistenceUnit getXmlPersistenceUnit() { @@ -2230,6 +2206,7 @@ public abstract class AbstractPersistenceUnit this.validateProperties(messages, reporter); this.validateGenerators(messages, reporter); this.validateQueries(messages, reporter); + this.validateEntityNames(messages, reporter); } protected void validateMappingFiles(List<IMessage> messages, IReporter reporter) { @@ -2238,6 +2215,7 @@ public abstract class AbstractPersistenceUnit for (MappingFileRef mappingFileRef : this.getMappingFileRefs()) { mappingFileRef.validate(messages, reporter); } + this.checkForDuplicateMappingFileClasses(messages); } protected void checkForMultiplePersistenceUnitMetadata(List<IMessage> messages) { @@ -2284,6 +2262,25 @@ public abstract class AbstractPersistenceUnit } } + protected void checkForDuplicateMappingFileClasses(List<IMessage> messages) { + HashBag<String> classNames = CollectionTools.bag(this.getMappingFileMappedClassNames()); + for (PersistentType persistentType : this.getMappingFilePersistentTypes()) { + String className = persistentType.getName(); + if ((className != null) && (!StringTools.stringIsEmpty(className)) + &&(classNames.count(className) > 1)) { + messages.add( + DefaultJpaValidationMessages.buildMessage( + IMessage.NORMAL_SEVERITY, + JpaValidationMessages.PERSISTENT_TYPE_DUPLICATE_CLASS, + new String[] {className}, + persistentType, + persistentType.getValidationTextRange() + ) + ); + } + } + } + protected void validateClassRefs(List<IMessage> messages, IReporter reporter) { this.checkForDuplicateClasses(messages); for (ClassRef classRef : this.getClassRefs()) { @@ -2334,7 +2331,7 @@ public abstract class AbstractPersistenceUnit } } - protected void validateProperties(@SuppressWarnings("unused") List<IMessage> messages, @SuppressWarnings("unused") IReporter reporter) { + protected void validateProperties(List<IMessage> messages, IReporter reporter) { // do nothing by default } @@ -2447,6 +2444,38 @@ public abstract class AbstractPersistenceUnit } } + protected void validateEntityNames(List<IMessage> messages, IReporter reporter) { + this.checkforDuplicateEntityNames(messages); + } + + protected void checkforDuplicateEntityNames(List<IMessage> messages) { + HashMap<String, ArrayList<Entity>> activeEntityNames = this.mapTypeMappingsByName(this.getActiveEntities()); + for (ArrayList<Entity> dups : activeEntityNames.values()) { + if (dups.size() > 1) { + for (Entity dup : dups) { + if (!StringTools.stringIsEmpty(dup.getName())) { + messages.add( + DefaultJpaValidationMessages.buildMessage( + IMessage.HIGH_SEVERITY, + JpaValidationMessages.ENTITY_NAME_DUPLICATED, + new String[] {dup.getName()}, + dup, + this.extractNameTextRange(dup) + ) + ); + } + + } + } + } + } + + protected TextRange extractNameTextRange(Entity entity) { + return (entity instanceof OrmEntity) ? + ((OrmEntity) entity).getXmlTypeMapping().getNameTextRange(): + ((JavaEntity) entity).getMappingAnnotation().getNameTextRange(null); + } + public boolean validatesAgainstDatabase() { return this.connectionProfileIsActive(); } diff --git a/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/resource/java/source/SourceEntityAnnotation.java b/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/resource/java/source/SourceEntityAnnotation.java index 50fb7d3e9f..37bd89f01c 100644 --- a/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/resource/java/source/SourceEntityAnnotation.java +++ b/jpa/plugins/org.eclipse.jpt.jpa.core/src/org/eclipse/jpt/jpa/core/internal/resource/java/source/SourceEntityAnnotation.java @@ -34,6 +34,7 @@ public final class SourceEntityAnnotation private static final DeclarationAnnotationElementAdapter<String> NAME_ADAPTER = buildNameAdapter(); private final AnnotationElementAdapter<String> nameAdapter; private String name; + TextRange nameTextRange; public SourceEntityAnnotation(JavaResourcePersistentType parent, Type type) { @@ -47,10 +48,12 @@ public final class SourceEntityAnnotation public void initialize(CompilationUnit astRoot) { this.name = this.buildName(astRoot); + this.nameTextRange = this.buildNameTextRange(astRoot); } public void synchronizeWith(CompilationUnit astRoot) { this.syncName(this.buildName(astRoot)); + this.nameTextRange = this.buildNameTextRange(astRoot); } @Override @@ -90,6 +93,10 @@ public final class SourceEntityAnnotation } public TextRange getNameTextRange(CompilationUnit astRoot) { + return this.nameTextRange; + } + + private TextRange buildNameTextRange(CompilationUnit astRoot) { return this.getElementTextRange(NAME_ADAPTER, astRoot); } |