From d746a371e5a689bb82d410df88e22d8a29a6885e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gild=C3=A9ric=20DERUETTE?= Date: Wed, 13 Nov 2024 22:17:18 +0100 Subject: [PATCH] =?UTF-8?q?Fix=20#373=20:=20[JAVA]=20Enum=C3=A9rations=20r?= =?UTF-8?q?epr=C3=A9sentant=20totalement=20l'entit=C3=A9=20#373?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../GeneratorRegistration.cs | 7 + TopModel.Generator.Jpa/JpaConfig.cs | 27 ++- .../JpaEnumValueGenerator.cs | 213 ++++++++++++++++++ .../JpaModelConstructorGenerator.cs | 71 +++--- TopModel.Generator.Jpa/JpaModelGenerator.cs | 6 +- .../JpaModelPropertyGenerator.cs | 2 +- TopModel.Generator.Jpa/jpa.config.json | 5 + .../demo/entities/securite/profil/Droit.java | 42 ++-- .../entities/securite/profil/TypeDroit.java | 31 +-- .../securite/utilisateur/TypeUtilisateur.java | 31 +-- .../enums/securite/profil/Droit_Value.java | 74 ++++++ .../securite/profil/TypeDroit_Value.java | 56 +++++ .../utilisateur/TypeUtilisateur_Value.java | 56 +++++ samples/model/angular.topmodel.lock | 2 +- samples/model/csharp.topmodel.lock | 2 +- samples/model/focus.topmodel.lock | 2 +- samples/model/jpa.topmodel.lock | 7 +- samples/model/pg.topmodel.lock | 2 +- samples/model/php.topmodel.lock | 2 +- samples/model/ssdt.topmodel.lock | 2 +- samples/model/translation.topmodel.lock | 2 +- 21 files changed, 540 insertions(+), 102 deletions(-) create mode 100644 TopModel.Generator.Jpa/JpaEnumValueGenerator.cs create mode 100644 samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/enums/securite/profil/Droit_Value.java create mode 100644 samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/enums/securite/profil/TypeDroit_Value.java create mode 100644 samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/enums/securite/utilisateur/TypeUtilisateur_Value.java diff --git a/TopModel.Generator.Jpa/GeneratorRegistration.cs b/TopModel.Generator.Jpa/GeneratorRegistration.cs index 82c73edc..61372007 100644 --- a/TopModel.Generator.Jpa/GeneratorRegistration.cs +++ b/TopModel.Generator.Jpa/GeneratorRegistration.cs @@ -10,10 +10,16 @@ public class GeneratorRegistration : IGeneratorRegistration /// public void Register(IServiceCollection services, JpaConfig config, int number) { + if (config.EnumsValuesPath == "default") + { + config.EnumsValuesPath = config.EnumsPath; + } + TrimSlashes(config, c => c.EntitiesPath); TrimSlashes(config, c => c.DaosPath); TrimSlashes(config, c => c.DtosPath); TrimSlashes(config, c => c.EnumsPath); + TrimSlashes(config, c => c.EnumsValuesPath); TrimSlashes(config, c => c.ApiPath); TrimSlashes(config, c => c.ResourcesPath); @@ -23,6 +29,7 @@ public void Register(IServiceCollection services, JpaConfig config, int number) services.AddGenerator(config, number); services.AddGenerator(config, number); services.AddGenerator(config, number); + services.AddGenerator(config, number); if (config.DaosPath != null) { services.AddGenerator(config, number); diff --git a/TopModel.Generator.Jpa/JpaConfig.cs b/TopModel.Generator.Jpa/JpaConfig.cs index cda9e144..085aedf9 100644 --- a/TopModel.Generator.Jpa/JpaConfig.cs +++ b/TopModel.Generator.Jpa/JpaConfig.cs @@ -15,10 +15,15 @@ public class JpaConfig : GeneratorConfigBase public string EntitiesPath { get; set; } = "javagen:{app}/entities/{module}"; /// - /// Localisation des classes persistées du modèle, relative au répertoire de génération. Par défaut, 'javagen/{app}/entities/{module}'. + /// Localisation des enums, relative au répertoire de génération. Par défaut, 'javagen:{app}/enums/{module}'. /// public string EnumsPath { get; set; } = "javagen:{app}/enums/{module}"; + /// + /// Localisation des enums de valeurs, relative au répertoire de génération. Par défaut, 'javagen:{app}/enums/{module}'. + /// + public string EnumsValuesPath { get; set; } = "default"; + /// /// Localisation des DAOs, relative au répertoire de génération. /// @@ -158,6 +163,7 @@ public class JpaConfig : GeneratorConfigBase nameof(DtosPath), nameof(ApiPath), nameof(EnumsPath), + nameof(EnumsValuesPath), nameof(ApiGeneration), nameof(ResourcesPath), nameof(DbSchema) @@ -170,6 +176,7 @@ public class JpaConfig : GeneratorConfigBase nameof(DtosPath), nameof(ApiPath), nameof(EnumsPath), + nameof(EnumsValuesPath), nameof(DataFlowsPath) ]; @@ -232,6 +239,14 @@ public string GetEnumFileName(IProperty property, Class classe, string tag) $"{GetEnumName(property, classe)}.java"); } + public string GetEnumValueFileName(IProperty property, Class classe, string tag) + { + return Path.Combine( + OutputDirectory, + ResolveVariables(EnumsValuesPath, tag, module: classe.Namespace.Module).ToFilePath(), + $"{classe.NamePascal}_Value.java"); + } + public string GetEnumName(IProperty property, Class classe) { return $"{classe.NamePascal}{property.Name.ToPascalCase()}"; @@ -242,6 +257,11 @@ public string GetEnumPackageName(Class classe, string tag) return GetPackageName(classe.Namespace, EnumsPath, tag); } + public string GetEnumValuePackageName(Class classe, string tag) + { + return GetPackageName(classe.Namespace, EnumsValuesPath, tag); + } + public string GetMapperFilePath((Class Classe, FromMapper Mapper) mapper, string tag) { var (ns, modelPath) = GetMapperLocation(mapper); @@ -333,4 +353,9 @@ protected override bool IsEnumNameValid(string name) { return base.IsEnumNameValid(name) && !Regex.IsMatch(name ?? string.Empty, "(?<=[^$\\w'\"\\])(?!(abstract|assert|boolean|break|byte|case|catch|char|class|const|continue|default|double|do|else|enum|extends|false|final|finally|float|for|goto|if|implements|import|instanceof|int|interface|long|native|new|null|package|private|protected|public|return|short|static|strictfp|super|switch|synchronized|this|throw|throws|transient|true|try|void|volatile|while|_\\b))([A-Za-z_$][$\\w]*)"); } + + public bool IsEnumNameJavaValid(string name) + { + return IsEnumNameValid(name); + } } \ No newline at end of file diff --git a/TopModel.Generator.Jpa/JpaEnumValueGenerator.cs b/TopModel.Generator.Jpa/JpaEnumValueGenerator.cs new file mode 100644 index 00000000..a44ecde8 --- /dev/null +++ b/TopModel.Generator.Jpa/JpaEnumValueGenerator.cs @@ -0,0 +1,213 @@ +using Microsoft.Extensions.Logging; +using TopModel.Core; +using TopModel.Core.FileModel; +using TopModel.Generator.Core; +using TopModel.Utils; + +namespace TopModel.Generator.Jpa; + +/// +/// Générateur de fichiers de modèles JPA. +/// +public class JpaEnumValueGenerator : GeneratorBase +{ + private readonly ILogger _logger; + + public JpaEnumValueGenerator(ILogger logger, ModelConfig modelConfig) + : base(logger) + { + _logger = logger; + } + + public override string Name => "JpaEnumValuesGen"; + + public override IEnumerable GeneratedFiles => Files + .Values + .SelectMany(f => f.Classes.Where(FilterClass)) + .SelectMany(c => Config.Tags.Intersect(c.Tags).SelectMany(tag => GetEnumProperties(c).Select(p => GetFileName(p, c, tag)))).Distinct(); + + protected bool FilterClass(Class classe) + { + return !classe.Abstract + && Config.CanClassUseEnums(classe, Classes.ToList()) + && GetAllValues(classe).All(v => Config.IsEnumNameJavaValid(v.Name)); + } + + protected string GetFileName(IProperty property, Class classe, string tag) + { + return Config.GetEnumValueFileName(property, classe, tag); + } + + protected void HandleClass(Class classe, string tag) + { + foreach (var p in GetEnumProperties(classe)) + { + WriteEnum(p, classe, tag); + } + } + + protected override void HandleFiles(IEnumerable files) + { + foreach (var file in files) + { + foreach (var classe in file.Classes.Where(FilterClass)) + { + foreach (var tag in Config.Tags.Intersect(classe.Tags)) + { + HandleClass(classe, tag); + } + } + } + } + + private IEnumerable GetEnumProperties(Class classe) + { + List result = new(); + if (classe.EnumKey != null && Config.CanClassUseEnums(classe, prop: classe.EnumKey) && !(classe.Extends != null && Config.CanClassUseEnums(classe.Extends, Classes, prop: classe.EnumKey))) + { + result.Add(classe.EnumKey); + } + + var uks = classe.UniqueKeys.Where(uk => uk.Count == 1 && Config.CanClassUseEnums(classe, Classes, uk.Single()) && !(classe.Extends != null && Config.CanClassUseEnums(classe.Extends, Classes, prop: classe.EnumKey))).Select(uk => uk.Single()); + result.AddRange(uks); + return result; + } + + private void WriteEnum(IProperty property, Class classe, string tag) + { + var packageName = Config.GetEnumValuePackageName(classe, tag); + using var fw = new JavaWriter(Config.GetEnumValueFileName(property, classe, tag), _logger, packageName, null); + fw.WriteLine(); + var codeProperty = classe.EnumKey!; + fw.WriteDocStart(0, $"Enumération des valeurs possibles de la classe {classe.NamePascal}"); + fw.WriteDocEnd(0); + fw.WriteLine($@"public enum {classe.NamePascal}_Value {{"); + var i = 0; + + var refs = GetAllValues(classe) + .ToList(); + + foreach (var refValue in refs) + { + if (i > 0) + { + fw.WriteLine(); + } + + i++; + var isLast = i == refs.Count(); + if (classe.DefaultProperty != null) + { + fw.WriteDocStart(1, $"{refValue.Value[classe.DefaultProperty]}"); + fw.WriteDocEnd(1); + } + + List enumAsString = [$"{refValue.Name.ToConstantCase()}("]; + foreach (var prop in classe.Properties) + { + var isString = Config.GetType(prop) == "String"; + var isInt = Config.GetType(prop) == "int"; + var isBoolean = Config.GetType(prop) == "Boolean"; + var value = refValue.Value.ContainsKey(prop) ? refValue.Value[prop] : "null"; + + if (prop is AssociationProperty ap && ap.Association.Values.Any(r => r.Value.ContainsKey(ap.Property) && r.Value[ap.Property] == value)) + { + fw.AddImport($"{Config.GetEnumValuePackageName(ap.Association.EnumKey!.Class, tag)}.{ap.Association.NamePascal}_Value"); + value = ap.Association.NamePascal + "_Value." + value; + isString = false; + } + else if (Config.CanClassUseEnums(classe, prop: prop)) + { + value = Config.GetType(prop) + "." + value; + } + + if (Config.TranslateReferences == true && classe.DefaultProperty == prop && !Config.CanClassUseEnums(classe, prop: prop)) + { + value = refValue.ResourceKey; + } + + var quote = isString ? "\"" : string.Empty; + var val = quote + value + quote; + enumAsString.Add($@"{val}{(prop == classe.Properties.Last() ? string.Empty : ", ")}"); + } + + enumAsString.Add($"){(isLast ? ";" : ",")} "); + fw.WriteLine(1, enumAsString.Aggregate(string.Empty, (acc, curr) => acc + curr)); + } + + foreach (var prop in classe.Properties) + { + fw.WriteLine(); + fw.WriteDocStart(1, $@"{prop.NameByClassPascal}"); + fw.WriteDocEnd(1); + var fieldName = prop.NameByClassCamel; + if (prop is AssociationProperty ap) + { + fieldName = $"{ap.NameByClassCamel}Value"; + fw.WriteLine(1, $@"private final {ap.Association.NamePascal}_Value {fieldName};"); + } + else + { + fw.WriteLine(1, $@"private final {Config.GetType(prop)} {fieldName};"); + } + } + + WriteConstructor(classe, fw, classe.Properties); + + foreach (var prop in classe.Properties) + { + fw.WriteLine(); + var fieldName = prop.NameByClassCamel; + fw.WriteDocStart(1, $"Getter for {fieldName}"); + fw.WriteDocEnd(1); + if (prop is AssociationProperty ap) + { + fieldName = $"{ap.NameByClassCamel}Value"; + fw.WriteLine(1, $@"public {ap.Association.NamePascal}_Value get{fieldName.ToFirstUpper()}() {{"); + } + else + { + fw.WriteLine(1, $@"public {Config.GetType(prop)} get{fieldName.ToFirstUpper()}() {{"); + } + + fw.WriteLine(2, $@"return this.{fieldName};"); + fw.WriteLine(1, $@"}}"); + } + + fw.WriteLine("}"); + } + + private void WriteConstructor(Class classe, JavaWriter fw, IEnumerable properties) + { + // Constructeur + fw.WriteDocStart(1, "Enum constructor"); + fw.WriteDocEnd(1); + string constructorAsString = + $@"{classe.NamePascal}_Value(" + + string.Join(", ", properties.Select((prop, index) => + { + var fieldName = prop.NameByClassCamel; + if (prop is AssociationProperty ap) + { + fieldName = $"{ap.NameByClassCamel}Value"; + return $@"final {ap.Association.NamePascal}_Value {fieldName}"; + } + + return $@"final {Config.GetType(prop)} {fieldName}"; + })) + "){"; + fw.WriteLine(1, constructorAsString); + foreach (var prop in properties) + { + var fieldName = prop.NameByClassCamel; + if (prop is AssociationProperty ap) + { + fieldName = $"{ap.NameByClassCamel}Value"; + } + + // Constructeur set + fw.WriteLine(2, $@" this.{fieldName} = {fieldName};"); + } + + fw.WriteLine(1, "}"); + } +} \ No newline at end of file diff --git a/TopModel.Generator.Jpa/JpaModelConstructorGenerator.cs b/TopModel.Generator.Jpa/JpaModelConstructorGenerator.cs index 4a545d78..9184ffe3 100644 --- a/TopModel.Generator.Jpa/JpaModelConstructorGenerator.cs +++ b/TopModel.Generator.Jpa/JpaModelConstructorGenerator.cs @@ -1,5 +1,6 @@ using TopModel.Core; using TopModel.Generator.Core; +using TopModel.Utils; namespace TopModel.Generator.Jpa; @@ -22,52 +23,48 @@ public void WriteEnumConstructor(JavaWriter fw, Class classe, List availa fw.WriteDocStart(1, "Enum constructor"); fw.WriteParam(classe.EnumKey!.NameCamel, "Code dont on veut obtenir l'instance"); fw.WriteDocEnd(1); - fw.WriteLine(1, $"public {classe.NamePascal}({_config.GetType(classe.EnumKey!)} {classe.EnumKey!.NameCamel}) {{"); + fw.WriteLine(1, $"public {classe.NamePascal}({_config.GetType(classe.EnumKey!)} {codeProperty.Name.ToCamelCase()}) {{"); if (classe.Extends != null || classe.Decorators.Any(d => _config.GetImplementation(d.Decorator)?.Extends is not null)) { fw.WriteLine(2, $"super();"); } - fw.WriteLine(2, $@"this.{classe.EnumKey!.NameCamel} = {classe.EnumKey!.NameCamel};"); - if (classe.GetProperties(availableClasses).Count > 1) + fw.AddImport("java.util.Arrays"); + fw.AddImport($"{_config.GetEnumValuePackageName(classe, tag)}.{classe.NamePascal}_Value"); + fw.WriteLine(2, $"this(Arrays.stream({classe.NamePascal}_Value.values()).filter(t -> t.get{codeProperty.NamePascal}() == {codeProperty.Name.ToCamelCase()}).findFirst().orElseThrow());"); + fw.WriteLine(1, $"}}"); + } + + public void WriteEnumValueConstructor(JavaWriter fw, Class classe, List availableClasses, string tag, ModelConfig modelConfig) + { + var codeProperty = classe.EnumKey!; + fw.WriteLine(); + fw.WriteDocStart(1, "Enum constructor"); + fw.WriteParam($"{classe.NameCamel}Value", "Enum de valeur dont on veut obtenir l'entité"); + fw.WriteDocEnd(1); + fw.WriteLine(1, $"public {classe.NamePascal}({classe.NamePascal}_Value {classe.NameCamel}Value) {{"); + if (classe.Extends != null || classe.Decorators.Any(d => _config.GetImplementation(d.Decorator)?.Extends is not null)) { - fw.WriteLine(2, $@"switch({classe.EnumKey!.NameCamel}) {{"); - foreach (var refValue in classe.Values.OrderBy(x => x.Name, StringComparer.Ordinal)) - { - var code = refValue.Value[codeProperty]; - fw.WriteLine(2, $@"case {code} :"); - foreach (var prop in classe.GetProperties(availableClasses).Where(p => p != codeProperty)) - { - var isString = _config.GetType(prop) == "String"; - var value = refValue.Value.ContainsKey(prop) ? refValue.Value[prop] : "null"; - if (value == "null") - { - isString = false; - } - else if (prop is AssociationProperty ap && _config.CanClassUseEnums(ap.Association, prop: ap.Property) && ap.Association.Values.Any(r => r.Value.ContainsKey(ap.Property) && r.Value[ap.Property] == value)) - { - value = ap.Association.NamePascal + "." + value; - isString = false; - fw.AddImport(ap.Association.GetImport(_config, tag)); - } - else if (prop is AliasProperty alp && _config.CanClassUseEnums(alp.Property.Class, prop: alp.Property)) - { - value = _config.GetType(alp.Property) + "." + value; - } - else if (_config.TranslateReferences == true && classe.DefaultProperty == prop && !_config.CanClassUseEnums(classe, prop: prop)) - { - value = refValue.ResourceKey; - } - - var quote = isString ? "\"" : string.Empty; - var val = quote + value + quote; - fw.WriteLine(3, $@"this.{prop.NameByClassCamel} = {val};"); - } + fw.WriteLine(2, $"super();"); + } - fw.WriteLine(3, $@"break;"); + foreach (var prop in classe.GetProperties(availableClasses)) + { + var value = $"{classe.NameCamel}Value.get{prop.NameByClassPascal}()"; + if (prop is AssociationProperty ap && _config.CanClassUseEnums(ap.Association, prop: ap.Property)) + { + value = $"new {ap.Association.NamePascal}({classe.NameCamel}Value.get{prop.NameByClassPascal}Value())"; + fw.AddImport(ap.Association.GetImport(_config, tag)); + fw.AddImport($"{_config.GetEnumValuePackageName(ap.Association, tag)}.{ap.Association.NamePascal}_Value"); + fw.WriteLine(2, $@"if ({classe.NameCamel}Value.get{prop.NameByClassPascal}Value() != null) {{"); + fw.WriteLine(3, $@"this.{prop.NameByClassCamel} = {value};"); + fw.WriteLine(2, "}"); + } + else + { + fw.WriteLine(2, $@"this.{prop.NameByClassCamel} = {value};"); } - fw.WriteLine(2, $@"}}"); } fw.WriteLine(1, $"}}"); diff --git a/TopModel.Generator.Jpa/JpaModelGenerator.cs b/TopModel.Generator.Jpa/JpaModelGenerator.cs index be5006d1..1e54c5dd 100644 --- a/TopModel.Generator.Jpa/JpaModelGenerator.cs +++ b/TopModel.Generator.Jpa/JpaModelGenerator.cs @@ -101,14 +101,15 @@ protected override void HandleClass(string fileName, Class classe, string tag) var javaOrJakarta = Config.PersistenceMode.ToString().ToLower(); foreach (var refValue in classe.Values.OrderBy(x => x.Name, StringComparer.Ordinal)) { - var code = refValue.Value[codeProperty]; + var code = refValue.Name.ToConstantCase(); if (classe.IsPersistent) { fw.AddImport($"{javaOrJakarta}.persistence.Transient"); fw.WriteLine(1, "@Transient"); } - fw.WriteLine(1, $@"public static final {classe.NamePascal} {code} = new {classe.NamePascal}({Config.GetEnumName(codeProperty, classe)}.{code});"); + fw.AddImport($"{Config.GetEnumValuePackageName(classe, tag)}.{classe.NamePascal}_Value"); + fw.WriteLine(1, $@"public static final {classe.NamePascal} {code} = new {classe.NamePascal}({classe.NamePascal}_Value.{code});"); } } @@ -135,6 +136,7 @@ protected override void HandleClass(string fileName, Class classe, string tag) if (Config.CanClassUseEnums(classe, Classes)) { JpaModelConstructorGenerator.WriteEnumConstructor(fw, classe, AvailableClasses, tag, _modelConfig); + JpaModelConstructorGenerator.WriteEnumValueConstructor(fw, classe, AvailableClasses, tag, _modelConfig); } WriteGetters(fw, classe, tag); diff --git a/TopModel.Generator.Jpa/JpaModelPropertyGenerator.cs b/TopModel.Generator.Jpa/JpaModelPropertyGenerator.cs index bbfc4e24..177824e9 100644 --- a/TopModel.Generator.Jpa/JpaModelPropertyGenerator.cs +++ b/TopModel.Generator.Jpa/JpaModelPropertyGenerator.cs @@ -324,7 +324,7 @@ private void WriteAssociationProperty(JavaWriter fw, Class classe, AssociationPr var defaultValue = _config.GetValue(property, _classes); if (defaultValue != "null") { - fw.AddImport($"{_config.GetEnumPackageName(classe, _config.GetBestClassTag(classe, tag))}.{_config.GetType(property.Association.PrimaryKey.Single())}"); + fw.AddImport($"{_config.GetEnumValuePackageName(classe, _config.GetBestClassTag(classe, tag))}.{_config.GetType(property.Association.PrimaryKey.Single())}"); suffix = $" = new {property.Association.NamePascal}({defaultValue})"; } } diff --git a/TopModel.Generator.Jpa/jpa.config.json b/TopModel.Generator.Jpa/jpa.config.json index 03a63598..416da508 100644 --- a/TopModel.Generator.Jpa/jpa.config.json +++ b/TopModel.Generator.Jpa/jpa.config.json @@ -91,6 +91,11 @@ "description": "Localisation des enums du modèle, relative au répertoire de génération.", "default": "javagen:{app}/enums/{module}" }, + "enumsValuesPath": { + "type": "string", + "description": "Localisation des enums de valeur du modèle, relative au répertoire de génération.", + "default": "javagen:{app}/enums/{module}" + }, "apiPath": { "type": "string", "description": "Localisation du l'API générée (client ou serveur), relative au répertoire de génération. Par défaut, 'javagen:{app}/api/{module}'.", diff --git a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/entities/securite/profil/Droit.java b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/entities/securite/profil/Droit.java index a2f2ce67..c507739d 100644 --- a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/entities/securite/profil/Droit.java +++ b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/entities/securite/profil/Droit.java @@ -4,6 +4,8 @@ package topmodel.jpa.sample.demo.entities.securite.profil; +import java.util.Arrays; + import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; import org.hibernate.annotations.Immutable; @@ -20,7 +22,9 @@ import jakarta.persistence.Table; import jakarta.persistence.Transient; +import topmodel.jpa.sample.demo.enums.securite.profil.Droit_Value; import topmodel.jpa.sample.demo.enums.securite.profil.DroitCode; +import topmodel.jpa.sample.demo.enums.securite.profil.TypeDroit_Value; /** * Droits de l'application. @@ -33,13 +37,13 @@ public class Droit { @Transient - public static final Droit CREATE = new Droit(DroitCode.CREATE); + public static final Droit CREATE = new Droit(Droit_Value.CREATE); @Transient - public static final Droit DELETE = new Droit(DroitCode.DELETE); + public static final Droit DELETE = new Droit(Droit_Value.DELETE); @Transient - public static final Droit READ = new Droit(DroitCode.READ); + public static final Droit READ = new Droit(Droit_Value.READ); @Transient - public static final Droit UPDATE = new Droit(DroitCode.UPDATE); + public static final Droit UPDATE = new Droit(Droit_Value.UPDATE); /** * Code du droit. @@ -74,24 +78,18 @@ public Droit() { * @param code Code dont on veut obtenir l'instance. */ public Droit(DroitCode code) { - this.code = code; - switch(code) { - case CREATE : - this.libelle = "securite.profil.droit.values.Create"; - this.typeDroit = TypeDroit.WRITE; - break; - case DELETE : - this.libelle = "securite.profil.droit.values.Delete"; - this.typeDroit = TypeDroit.ADMIN; - break; - case READ : - this.libelle = "securite.profil.droit.values.Read"; - this.typeDroit = TypeDroit.READ; - break; - case UPDATE : - this.libelle = "securite.profil.droit.values.Update"; - this.typeDroit = TypeDroit.WRITE; - break; + this(Arrays.stream(Droit_Value.values()).filter(t -> t.getCode() == code).findFirst().orElseThrow()); + } + + /** + * Enum constructor. + * @param droitValue Enum de valeur dont on veut obtenir l'entité. + */ + public Droit(Droit_Value droitValue) { + this.code = droitValue.getCode(); + this.libelle = droitValue.getLibelle(); + if (droitValue.getTypeDroitValue() != null) { + this.typeDroit = new TypeDroit(droitValue.getTypeDroitValue()); } } diff --git a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/entities/securite/profil/TypeDroit.java b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/entities/securite/profil/TypeDroit.java index 5f11c138..c054c27e 100644 --- a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/entities/securite/profil/TypeDroit.java +++ b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/entities/securite/profil/TypeDroit.java @@ -4,6 +4,8 @@ package topmodel.jpa.sample.demo.entities.securite.profil; +import java.util.Arrays; + import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; import org.hibernate.annotations.Immutable; @@ -17,6 +19,7 @@ import jakarta.persistence.Table; import jakarta.persistence.Transient; +import topmodel.jpa.sample.demo.enums.securite.profil.TypeDroit_Value; import topmodel.jpa.sample.demo.enums.securite.profil.TypeDroitCode; /** @@ -30,11 +33,11 @@ public class TypeDroit { @Transient - public static final TypeDroit ADMIN = new TypeDroit(TypeDroitCode.ADMIN); + public static final TypeDroit ADMIN = new TypeDroit(TypeDroit_Value.ADMIN); @Transient - public static final TypeDroit READ = new TypeDroit(TypeDroitCode.READ); + public static final TypeDroit READ = new TypeDroit(TypeDroit_Value.READ); @Transient - public static final TypeDroit WRITE = new TypeDroit(TypeDroitCode.WRITE); + public static final TypeDroit WRITE = new TypeDroit(TypeDroit_Value.WRITE); /** * Code du type de droit. @@ -62,18 +65,16 @@ public TypeDroit() { * @param code Code dont on veut obtenir l'instance. */ public TypeDroit(TypeDroitCode code) { - this.code = code; - switch(code) { - case ADMIN : - this.libelle = "securite.profil.typeDroit.values.Admin"; - break; - case READ : - this.libelle = "securite.profil.typeDroit.values.Read"; - break; - case WRITE : - this.libelle = "securite.profil.typeDroit.values.Write"; - break; - } + this(Arrays.stream(TypeDroit_Value.values()).filter(t -> t.getCode() == code).findFirst().orElseThrow()); + } + + /** + * Enum constructor. + * @param typeDroitValue Enum de valeur dont on veut obtenir l'entité. + */ + public TypeDroit(TypeDroit_Value typeDroitValue) { + this.code = typeDroitValue.getCode(); + this.libelle = typeDroitValue.getLibelle(); } /** diff --git a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/entities/securite/utilisateur/TypeUtilisateur.java b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/entities/securite/utilisateur/TypeUtilisateur.java index 945d5045..b9912ad5 100644 --- a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/entities/securite/utilisateur/TypeUtilisateur.java +++ b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/entities/securite/utilisateur/TypeUtilisateur.java @@ -4,6 +4,8 @@ package topmodel.jpa.sample.demo.entities.securite.utilisateur; +import java.util.Arrays; + import org.hibernate.annotations.Cache; import org.hibernate.annotations.CacheConcurrencyStrategy; import org.hibernate.annotations.Immutable; @@ -17,6 +19,7 @@ import jakarta.persistence.Table; import jakarta.persistence.Transient; +import topmodel.jpa.sample.demo.enums.securite.utilisateur.TypeUtilisateur_Value; import topmodel.jpa.sample.demo.enums.securite.utilisateur.TypeUtilisateurCode; /** @@ -30,11 +33,11 @@ public class TypeUtilisateur { @Transient - public static final TypeUtilisateur ADMIN = new TypeUtilisateur(TypeUtilisateurCode.ADMIN); + public static final TypeUtilisateur ADMIN = new TypeUtilisateur(TypeUtilisateur_Value.ADMIN); @Transient - public static final TypeUtilisateur CLIENT = new TypeUtilisateur(TypeUtilisateurCode.CLIENT); + public static final TypeUtilisateur CLIENT = new TypeUtilisateur(TypeUtilisateur_Value.CLIENT); @Transient - public static final TypeUtilisateur GEST = new TypeUtilisateur(TypeUtilisateurCode.GEST); + public static final TypeUtilisateur GESTIONNAIRE = new TypeUtilisateur(TypeUtilisateur_Value.GESTIONNAIRE); /** * Code du type d'utilisateur. @@ -62,18 +65,16 @@ public TypeUtilisateur() { * @param code Code dont on veut obtenir l'instance. */ public TypeUtilisateur(TypeUtilisateurCode code) { - this.code = code; - switch(code) { - case ADMIN : - this.libelle = "securite.utilisateur.typeUtilisateur.values.Admin"; - break; - case CLIENT : - this.libelle = "securite.utilisateur.typeUtilisateur.values.Client"; - break; - case GEST : - this.libelle = "securite.utilisateur.typeUtilisateur.values.Gestionnaire"; - break; - } + this(Arrays.stream(TypeUtilisateur_Value.values()).filter(t -> t.getCode() == code).findFirst().orElseThrow()); + } + + /** + * Enum constructor. + * @param typeUtilisateurValue Enum de valeur dont on veut obtenir l'entité. + */ + public TypeUtilisateur(TypeUtilisateur_Value typeUtilisateurValue) { + this.code = typeUtilisateurValue.getCode(); + this.libelle = typeUtilisateurValue.getLibelle(); } /** diff --git a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/enums/securite/profil/Droit_Value.java b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/enums/securite/profil/Droit_Value.java new file mode 100644 index 00000000..c228d50d --- /dev/null +++ b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/enums/securite/profil/Droit_Value.java @@ -0,0 +1,74 @@ +//// +//// ATTENTION CE FICHIER EST GENERE AUTOMATIQUEMENT ! +//// + +package topmodel.jpa.sample.demo.enums.securite.profil; + +/** + * Enumération des valeurs possibles de la classe Droit. + */ +public enum Droit_Value { + /** + * Création. + */ + CREATE(DroitCode.CREATE, "securite.profil.droit.values.Create", TypeDroit_Value.WRITE), + + /** + * Lecture. + */ + READ(DroitCode.READ, "securite.profil.droit.values.Read", TypeDroit_Value.READ), + + /** + * Mise à jour. + */ + UPDATE(DroitCode.UPDATE, "securite.profil.droit.values.Update", TypeDroit_Value.WRITE), + + /** + * Suppression. + */ + DELETE(DroitCode.DELETE, "securite.profil.droit.values.Delete", TypeDroit_Value.ADMIN); + + /** + * Code. + */ + private final DroitCode code; + + /** + * Libelle. + */ + private final String libelle; + + /** + * TypeDroit. + */ + private final TypeDroit_Value typeDroitValue; + /** + * Enum constructor. + */ + Droit_Value(final DroitCode code, final String libelle, final TypeDroit_Value typeDroitValue){ + this.code = code; + this.libelle = libelle; + this.typeDroitValue = typeDroitValue; + } + + /** + * Getter for code. + */ + public DroitCode getCode() { + return this.code; + } + + /** + * Getter for libelle. + */ + public String getLibelle() { + return this.libelle; + } + + /** + * Getter for typeDroit. + */ + public TypeDroit_Value getTypeDroitValue() { + return this.typeDroitValue; + } +} diff --git a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/enums/securite/profil/TypeDroit_Value.java b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/enums/securite/profil/TypeDroit_Value.java new file mode 100644 index 00000000..18506132 --- /dev/null +++ b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/enums/securite/profil/TypeDroit_Value.java @@ -0,0 +1,56 @@ +//// +//// ATTENTION CE FICHIER EST GENERE AUTOMATIQUEMENT ! +//// + +package topmodel.jpa.sample.demo.enums.securite.profil; + +/** + * Enumération des valeurs possibles de la classe TypeDroit. + */ +public enum TypeDroit_Value { + /** + * Lecture. + */ + READ(TypeDroitCode.READ, "securite.profil.typeDroit.values.Read"), + + /** + * Ecriture. + */ + WRITE(TypeDroitCode.WRITE, "securite.profil.typeDroit.values.Write"), + + /** + * Administration. + */ + ADMIN(TypeDroitCode.ADMIN, "securite.profil.typeDroit.values.Admin"); + + /** + * Code. + */ + private final TypeDroitCode code; + + /** + * Libelle. + */ + private final String libelle; + /** + * Enum constructor. + */ + TypeDroit_Value(final TypeDroitCode code, final String libelle){ + this.code = code; + this.libelle = libelle; + } + + /** + * Getter for code. + */ + public TypeDroitCode getCode() { + return this.code; + } + + /** + * Getter for libelle. + */ + public String getLibelle() { + return this.libelle; + } +} diff --git a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/enums/securite/utilisateur/TypeUtilisateur_Value.java b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/enums/securite/utilisateur/TypeUtilisateur_Value.java new file mode 100644 index 00000000..b0a46611 --- /dev/null +++ b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/enums/securite/utilisateur/TypeUtilisateur_Value.java @@ -0,0 +1,56 @@ +//// +//// ATTENTION CE FICHIER EST GENERE AUTOMATIQUEMENT ! +//// + +package topmodel.jpa.sample.demo.enums.securite.utilisateur; + +/** + * Enumération des valeurs possibles de la classe TypeUtilisateur. + */ +public enum TypeUtilisateur_Value { + /** + * Administrateur. + */ + ADMIN(TypeUtilisateurCode.ADMIN, "securite.utilisateur.typeUtilisateur.values.Admin"), + + /** + * Gestionnaire. + */ + GESTIONNAIRE(TypeUtilisateurCode.GEST, "securite.utilisateur.typeUtilisateur.values.Gestionnaire"), + + /** + * Client. + */ + CLIENT(TypeUtilisateurCode.CLIENT, "securite.utilisateur.typeUtilisateur.values.Client"); + + /** + * Code. + */ + private final TypeUtilisateurCode code; + + /** + * Libelle. + */ + private final String libelle; + /** + * Enum constructor. + */ + TypeUtilisateur_Value(final TypeUtilisateurCode code, final String libelle){ + this.code = code; + this.libelle = libelle; + } + + /** + * Getter for code. + */ + public TypeUtilisateurCode getCode() { + return this.code; + } + + /** + * Getter for libelle. + */ + public String getLibelle() { + return this.libelle; + } +} diff --git a/samples/model/angular.topmodel.lock b/samples/model/angular.topmodel.lock index 6f637188..d8101746 100644 --- a/samples/model/angular.topmodel.lock +++ b/samples/model/angular.topmodel.lock @@ -2,7 +2,7 @@ # ATTENTION CE FICHIER EST GENERE AUTOMATIQUEMENT ! # -version: 2.1.2 +version: 2.1.3 custom: ../../../TopModel.Generator.Javascript: 813bd1d0b6e6a08aff8900e52ef483ae generatedFiles: diff --git a/samples/model/csharp.topmodel.lock b/samples/model/csharp.topmodel.lock index 4cecf819..036a073b 100644 --- a/samples/model/csharp.topmodel.lock +++ b/samples/model/csharp.topmodel.lock @@ -2,7 +2,7 @@ # ATTENTION CE FICHIER EST GENERE AUTOMATIQUEMENT ! # -version: 2.1.2 +version: 2.1.3 custom: ../../../TopModel.Generator.Csharp: 121e8b5ce6a8be344945262bf8960580 generatedFiles: diff --git a/samples/model/focus.topmodel.lock b/samples/model/focus.topmodel.lock index 685db183..ab25e0f0 100644 --- a/samples/model/focus.topmodel.lock +++ b/samples/model/focus.topmodel.lock @@ -2,7 +2,7 @@ # ATTENTION CE FICHIER EST GENERE AUTOMATIQUEMENT ! # -version: 2.1.2 +version: 2.1.3 custom: ../../../TopModel.Generator.Javascript: 813bd1d0b6e6a08aff8900e52ef483ae generatedFiles: diff --git a/samples/model/jpa.topmodel.lock b/samples/model/jpa.topmodel.lock index d307af2e..d66d9fd0 100644 --- a/samples/model/jpa.topmodel.lock +++ b/samples/model/jpa.topmodel.lock @@ -2,9 +2,9 @@ # ATTENTION CE FICHIER EST GENERE AUTOMATIQUEMENT ! # -version: 2.1.2 +version: 2.1.3 custom: - ../../../TopModel.Generator.Jpa: b7b41bad6aa1f4963dda4549de26efcd + ../../../TopModel.Generator.Jpa: 415d42dd4a10b557ea3c47d748141584 generatedFiles: - ../generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/api/client/securite/profil/ProfilClient.java - ../generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/api/client/securite/utilisateur/UtilisateurClient.java @@ -25,8 +25,11 @@ generatedFiles: - ../generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/entities/securite/utilisateur/SecuriteUtilisateurMappers.java - ../generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/entities/securite/utilisateur/TypeUtilisateur.java - ../generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/entities/securite/utilisateur/Utilisateur.java + - ../generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/enums/securite/profil/Droit_Value.java - ../generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/enums/securite/profil/DroitCode.java + - ../generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/enums/securite/profil/TypeDroit_Value.java - ../generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/enums/securite/profil/TypeDroitCode.java + - ../generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/enums/securite/utilisateur/TypeUtilisateur_Value.java - ../generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/enums/securite/utilisateur/TypeUtilisateurCode.java - ../generators/jpa/src/main/resources/i18n/model/common.properties - ../generators/jpa/src/main/resources/i18n/model/securite.properties diff --git a/samples/model/pg.topmodel.lock b/samples/model/pg.topmodel.lock index 0cadab1c..f4ee53c7 100644 --- a/samples/model/pg.topmodel.lock +++ b/samples/model/pg.topmodel.lock @@ -2,7 +2,7 @@ # ATTENTION CE FICHIER EST GENERE AUTOMATIQUEMENT ! # -version: 2.1.2 +version: 2.1.3 custom: ../../../TopModel.Generator.Sql: 6c49105633a94d33dc5f26d6fece3a43 generatedFiles: diff --git a/samples/model/php.topmodel.lock b/samples/model/php.topmodel.lock index ec83f209..7b08b827 100644 --- a/samples/model/php.topmodel.lock +++ b/samples/model/php.topmodel.lock @@ -2,7 +2,7 @@ # ATTENTION CE FICHIER EST GENERE AUTOMATIQUEMENT ! # -version: 2.1.2 +version: 2.1.3 custom: ../../../TopModel.Generator.Php: 8029e36a5806c385b25b725c2e00ecdc generatedFiles: diff --git a/samples/model/ssdt.topmodel.lock b/samples/model/ssdt.topmodel.lock index 963823c9..cdb4f9f4 100644 --- a/samples/model/ssdt.topmodel.lock +++ b/samples/model/ssdt.topmodel.lock @@ -2,7 +2,7 @@ # ATTENTION CE FICHIER EST GENERE AUTOMATIQUEMENT ! # -version: 2.1.2 +version: 2.1.3 custom: ../../../TopModel.Generator.Sql: 6c49105633a94d33dc5f26d6fece3a43 generatedFiles: diff --git a/samples/model/translation.topmodel.lock b/samples/model/translation.topmodel.lock index acbc0cb0..64ceb118 100644 --- a/samples/model/translation.topmodel.lock +++ b/samples/model/translation.topmodel.lock @@ -2,7 +2,7 @@ # ATTENTION CE FICHIER EST GENERE AUTOMATIQUEMENT ! # -version: 2.1.2 +version: 2.1.3 custom: ../../../TopModel.Generator.Translation: 9f15a161c1cd09a554e17eb18f624495 generatedFiles: