This is an automated email from the git hooks/post-receive script. New commit to branch feature/3692 in repository topia. See http://git.nuiton.org/topia.git commit 33c9c72b8185d78fb3402a3099831dd3985eaacc Author: Brendan Le Ny <bleny@codelutin.com> Date: Fri Jun 19 16:32:27 2015 +0200 Improve generated index names --- .../templates/EntityHibernateMappingGenerator.java | 21 ++++--- .../nuiton/topia/templates/TopiaGeneratorUtil.java | 1 - .../topia/templates/TopiaTemplateHelper.java | 70 ++++++++++++++++++++++ 3 files changed, 84 insertions(+), 8 deletions(-) diff --git a/topia-templates/src/main/java/org/nuiton/topia/templates/EntityHibernateMappingGenerator.java b/topia-templates/src/main/java/org/nuiton/topia/templates/EntityHibernateMappingGenerator.java index 573f270..ed54f73 100644 --- a/topia-templates/src/main/java/org/nuiton/topia/templates/EntityHibernateMappingGenerator.java +++ b/topia-templates/src/main/java/org/nuiton/topia/templates/EntityHibernateMappingGenerator.java @@ -268,26 +268,33 @@ public class EntityHibernateMappingGenerator extends ObjectModelGenerator { // add database-object to create and drop index + // add schema if exist (http://nuiton.org/issues/2052) + String schema = templateHelper.getDbSchemaNameTagValue(clazz, model); + boolean withSchema = StringUtils.isNotEmpty(schema); + String tableName; - String indexName = "idx_" + clazz.getName() + "_" + attribute.getName(); String propertyName; - if (GeneratorUtil.isNMultiplicity(attribute.getReverseMaxMultiplicity())) { // many to many tableName = templateHelper.getManyToManyTableName(attribute); - propertyName = templateHelper.getDbName(attribute.getReverseAttribute()); + propertyName = templateHelper.getReverseDbNameOnReverseAttribute(attribute); } else { // one to many tableName =templateHelper.getDbName(attribute.getClassifier()); - propertyName = templateHelper.getDbName(attribute.getReverseAttribute()); + propertyName = templateHelper.getReverseDbNameOnReverseAttribute(attribute); } - // add schema if exist (http://nuiton.org/issues/2052) - String schema = topiaTagValues.getDbSchemaNameTagValue(clazz, model); - if (StringUtils.isNotEmpty(schema)) { + String indexName = "idx"; + if (withSchema) { + indexName += '_' + schema; + } + indexName += '_' + tableName + '_' + propertyName; + indexName = indexName.toLowerCase(); + + if (withSchema) { tableName = schema + "." + tableName; } /*{ <database-object> diff --git a/topia-templates/src/main/java/org/nuiton/topia/templates/TopiaGeneratorUtil.java b/topia-templates/src/main/java/org/nuiton/topia/templates/TopiaGeneratorUtil.java index f335bff..24b8a1e 100644 --- a/topia-templates/src/main/java/org/nuiton/topia/templates/TopiaGeneratorUtil.java +++ b/topia-templates/src/main/java/org/nuiton/topia/templates/TopiaGeneratorUtil.java @@ -27,7 +27,6 @@ package org.nuiton.topia.templates; import com.google.common.base.Joiner; import com.google.common.base.Preconditions; import com.google.common.collect.Lists; - import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.logging.Log; diff --git a/topia-templates/src/main/java/org/nuiton/topia/templates/TopiaTemplateHelper.java b/topia-templates/src/main/java/org/nuiton/topia/templates/TopiaTemplateHelper.java index 1d0020d..fa64a8c 100644 --- a/topia-templates/src/main/java/org/nuiton/topia/templates/TopiaTemplateHelper.java +++ b/topia-templates/src/main/java/org/nuiton/topia/templates/TopiaTemplateHelper.java @@ -46,6 +46,7 @@ import org.nuiton.eugene.models.object.ObjectModelPackage; import org.nuiton.eugene.models.object.ObjectModelParameter; import org.nuiton.eugene.models.tagvalue.TagValueDefinitionProvider; import org.nuiton.eugene.models.tagvalue.TagValueDefinitionProviderManagerExtension; +import org.nuiton.eugene.models.tagvalue.TagValues; import java.util.ArrayList; import java.util.Arrays; @@ -892,6 +893,75 @@ public class TopiaTemplateHelper { return extension.getTagValueDefinitionProvider(providerType); } + /** + * Obtain the value of the {@link TopiaTagValues#TAG_SCHEMA_NAME} + * tag value on the given classifier. + * <p/> + * + * @param classifier classifier to seek + * @param model model to seek + * @return the none empty value of the found tag value or {@code null} if not found nor empty. + * @see TopiaTagValues#TAG_SCHEMA_NAME + * @since 3.0 + */ + public static String getDbSchemaNameTagValue(ObjectModelClassifier classifier, ObjectModel model) { + ObjectModelPackage aPackage = null; + if (classifier!=null) { + aPackage = model.getPackage(classifier); + } + String value = TagValues.findTagValue(TopiaTagValues.TAG_SCHEMA_NAME, model, aPackage, classifier); + return value; + } + + /** + * Obtain the reverse db name of a reverse attribute. + * + * <strong>Note that the reverse attribute can't be null here.</strong> + * <ul> + * <li>Try first to get the reverse db Name from the ReverseDbname tag-value</li> + * <li>If not found, try then the ReverseDbname tag-value on the same attribute but from this other side of the relation</li> + * <li>If not found, try then just get the name of the reverse attribute</li> + * </ul> + * @param attr the attribute to seek + * @return the value of the reverse db name on the revser attribute + * @since 3.0 + */ + public static String getReverseDbNameOnReverseAttribute(ObjectModelAttribute attr) { + ObjectModelAttribute reverseAttribute = attr.getReverseAttribute(); + if (reverseAttribute == null) { + throw new IllegalArgumentException("The reverse attribute can't be null, but was on " + attr); + } + String result = getReverseDbNameTagValue(reverseAttribute); + if (StringUtils.isEmpty(result)) { + // Try to get it from the other site of the relation + ObjectModelAttribute reverseAttribute2 = reverseAttribute.getClassifier().getAttribute(attr.getName()); + result = getReverseDbNameTagValue(reverseAttribute2); + } + if (StringUtils.isEmpty(result)) { + result = GeneratorUtil.toLowerCaseFirstLetter(reverseAttribute.getName()); + } + return result; + + } + + /** + * Obtain the value of the {@link TopiaTagValues#TAG_REVERSE_DB_NAME} + * tag value on the given element. + * <p/> + * + * Note that it won't and search on declaring element or anywhere else than on the given element. + * See https://forge.nuiton.org/issues/2342 + * + * @param element element to seek + * @return the none empty value of the found tag value or {@code null} if not found nor empty. + * @see TopiaTagValues#TAG_REVERSE_DB_NAME + * @since 3.0 + */ + public static String getReverseDbNameTagValue(ObjectModelAttribute element) { + String value = element.getTagValue(TopiaTagValues.TAG_REVERSE_DB_NAME); + return value; + } + //--------------------------------------------------------------------------------------------------------------- //-- DEPRECATED API TO REMOVE --------------------------------------------------------------------------------- //--------------------------------------------------------------------------------------------------------------- -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.