From 54857eabd5330304135fac89d32230013a69b1c7 Mon Sep 17 00:00:00 2001 From: Denis Challas Date: Mon, 21 Oct 2024 15:45:17 +0200 Subject: [PATCH 1/4] =?UTF-8?q?[JAVA]=20Enum=C3=A9rations=20repr=C3=A9sent?= =?UTF-8?q?ant=20totalement=20l'entit=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TopModel.Generator.Jpa/JpaEnumGenerator.cs | 82 ++++++++++++++++++- .../JpaModelConstructorGenerator.cs | 53 ++++++------ TopModel.Generator.Jpa/JpaModelGenerator.cs | 4 +- .../demo/dtos/securite/profil/ProfilRead.java | 4 +- .../dtos/securite/profil/ProfilWrite.java | 4 +- .../securite/utilisateur/UtilisateurRead.java | 4 +- .../utilisateur/UtilisateurWrite.java | 4 +- .../demo/entities/securite/profil/Droit.java | 24 +----- .../demo/entities/securite/profil/Profil.java | 4 +- .../entities/securite/profil/TypeDroit.java | 16 +--- .../securite/utilisateur/TypeUtilisateur.java | 16 +--- .../securite/utilisateur/Utilisateur.java | 4 +- .../demo/enums/securite/profil/DroitCode.java | 43 ++++++++-- .../enums/securite/profil/TypeDroitCode.java | 28 +++++-- .../utilisateur/TypeUtilisateurCode.java | 26 +++++- samples/model/jpa.topmodel.lock | 2 +- 16 files changed, 209 insertions(+), 109 deletions(-) diff --git a/TopModel.Generator.Jpa/JpaEnumGenerator.cs b/TopModel.Generator.Jpa/JpaEnumGenerator.cs index 9a9e5015..481d4793 100644 --- a/TopModel.Generator.Jpa/JpaEnumGenerator.cs +++ b/TopModel.Generator.Jpa/JpaEnumGenerator.cs @@ -2,6 +2,7 @@ using TopModel.Core; using TopModel.Core.FileModel; using TopModel.Generator.Core; +using TopModel.Utils; namespace TopModel.Generator.Jpa; @@ -80,22 +81,95 @@ private void WriteEnum(IProperty property, Class classe, string tag) var i = 0; var refs = GetAllValues(classe) - .OrderBy(x => x.Name, StringComparer.Ordinal) .ToList(); - foreach (var value in refs) + var properties = classe.Properties.Where(p => p != codeProperty); + foreach (var refValue in refs) { i++; var isLast = i == refs.Count(); if (classe.DefaultProperty != null) { - fw.WriteDocStart(1, $"{value.Value[classe.DefaultProperty]}"); + fw.WriteDocStart(1, $"{refValue.Value[classe.DefaultProperty]}"); fw.WriteDocEnd(1); } - fw.WriteLine(1, $"{value.Value[property]}{(isLast ? string.Empty : ",")}"); + List enumAsString = new List { }; + enumAsString.Add($"{refValue.Value[property]}("); + foreach (var prop in 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 && codeProperty.PrimaryKey && ap.Association.Values.Any(r => r.Value.ContainsKey(ap.Property) && r.Value[ap.Property] == value)) + { + value = ap.Association.NamePascal + ap.Association.EnumKey + "." + 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 == properties.Last() ? string.Empty : ", ")}"); + } + + enumAsString.Add($"){(isLast ? ";" : ",")} "); + fw.WriteLine(1, enumAsString.Aggregate(string.Empty, (acc, curr) => acc + curr)); + } + + foreach (var prop in properties) + { + fw.WriteLine(); + fw.WriteDocStart(1, $@"{prop.NameByClassPascal}"); + fw.WriteDocEnd(1); + fw.WriteLine(1, $@"private final {Config.GetType(prop)} {prop.NameByClassCamel};"); + } + + WriteConstructor(property, classe, fw, properties); + + foreach (var prop in properties) + { + fw.WriteLine(); + fw.WriteDocStart(1, "Getter"); + fw.WriteDocEnd(1); + fw.WriteLine(1, $@"public {Config.GetType(prop)} get{prop.NameByClassCamel.ToFirstUpper()}(){{"); + fw.WriteLine(2, $@"return this.{prop.NameByClassCamel};"); + fw.WriteLine(1, $@"}}"); } fw.WriteLine("}"); } + + private void WriteConstructor(IProperty property, Class classe, JavaWriter fw, IEnumerable properties) + { + // Constructeur + fw.WriteDocStart(1, "Enum constructor"); + fw.WriteDocEnd(1); + List constructorAsString = new List(); + constructorAsString.Add($@"{Config.GetEnumName(property, classe)}("); + + constructorAsString.Add(properties.Select((prop, index) => + $@"final {Config.GetType(prop)} {prop.NameByClassCamel} {(prop == properties.Last() ? string.Empty : ",")}") + .Aggregate(string.Empty, (acc, curr) => acc + curr)); + + constructorAsString.Add("){"); + fw.WriteLine(1, constructorAsString.Aggregate(string.Empty, (acc, curr) => acc + curr)); + foreach (var prop in properties) + { + // Constructeur set + fw.WriteLine(2, $@" this.{prop.NameByClassCamel} = {prop.NameByClassCamel};"); + } + + fw.WriteLine(1, "}"); + } } \ No newline at end of file diff --git a/TopModel.Generator.Jpa/JpaModelConstructorGenerator.cs b/TopModel.Generator.Jpa/JpaModelConstructorGenerator.cs index 96cd21c0..58f0e351 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; @@ -31,40 +32,34 @@ public void WriteEnumConstructor(JavaWriter fw, Class classe, List availa fw.WriteLine(2, $@"this.{classe.EnumKey!.NameCamel} = {classe.EnumKey!.NameCamel};"); if (classe.GetProperties(availableClasses).Count > 1) { - fw.WriteLine(2, $@"switch({classe.EnumKey!.NameCamel}) {{"); - foreach (var refValue in classe.Values.OrderBy(x => x.Name, StringComparer.Ordinal)) + foreach (var prop in classe.GetProperties(availableClasses).Where(p => p != codeProperty)) { - var code = refValue.Value[codeProperty]; - fw.WriteLine(2, $@"case {code} :"); - foreach (var prop in classe.GetProperties(availableClasses).Where(p => p != codeProperty)) + string val; + var getterPrefix = _config.GetType(prop!) == "boolean" ? "is" : "get"; + if (prop is AssociationProperty ap && codeProperty.PrimaryKey && ap.Association.Values.Any(r => r.Value.ContainsKey(ap.Property))) { - var isString = _config.GetType(prop) == "String"; - var value = refValue.Value.ContainsKey(prop) ? refValue.Value[prop] : "null"; - if (prop is AssociationProperty ap && codeProperty.PrimaryKey && 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 (_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; - fw.WriteLine(3, $@"this.{prop.NameByClassCamel} = {val};"); + var javaType = _config.GetType(prop, useClassForAssociation: classe.IsPersistent && !_config.UseJdbc && prop is AssociationProperty asp && asp.Association.IsPersistent); + javaType = javaType.Split("<")[0]; + val = $@"{classe.EnumKey!.NameCamel}.{prop.NameByClassCamel.WithPrefix(getterPrefix)}() != null ? new {javaType}({classe.EnumKey!.NameCamel}.{prop.NameByClassCamel.WithPrefix(getterPrefix)}()) : null"; + } + else if (_config.CanClassUseEnums(classe, prop: prop)) + { + var javaType = _config.GetType(prop, useClassForAssociation: classe.IsPersistent && !_config.UseJdbc && prop is AssociationProperty asp && asp.Association.IsPersistent); + javaType = javaType.Split("<")[0]; + val = $@"{classe.EnumKey!.NameCamel}.{prop.NameByClassCamel.WithPrefix(getterPrefix)}() != null ? new {javaType}({classe.EnumKey!.NameCamel}.{prop.NameByClassCamel.WithPrefix(getterPrefix)}()) : null"; + } + else if (_config.TranslateReferences == true && classe.DefaultProperty == prop && !_config.CanClassUseEnums(classe, prop: prop)) + { + val = $@"{classe.EnumKey!.NameCamel}.{prop.NameByClassCamel.WithPrefix(getterPrefix)}()"; + } + else + { + val = $@"{classe.EnumKey!.NameCamel}.{prop.NameByClassCamel.WithPrefix(getterPrefix)}()"; } - fw.WriteLine(3, $@"break;"); - } + fw.WriteLine(2, $@"this.{prop.NameByClassCamel} = {val};"); - fw.WriteLine(2, $@"}}"); + } } fw.WriteLine(1, $"}}"); diff --git a/TopModel.Generator.Jpa/JpaModelGenerator.cs b/TopModel.Generator.Jpa/JpaModelGenerator.cs index be5006d1..99218dec 100644 --- a/TopModel.Generator.Jpa/JpaModelGenerator.cs +++ b/TopModel.Generator.Jpa/JpaModelGenerator.cs @@ -406,9 +406,9 @@ private void WriteFieldsEnum(JavaWriter fw, Class classe, string tag) fw.WriteLine(); - fw.WriteLine(2, "private Class type;"); + fw.WriteLine(2, "private final Class type;"); fw.WriteLine(); - fw.WriteLine(2, "private Fields(Class type) {"); + fw.WriteLine(2, "Fields(Class type) {"); fw.WriteLine(3, "this.type = type;"); fw.WriteLine(2, "}"); diff --git a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/dtos/securite/profil/ProfilRead.java b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/dtos/securite/profil/ProfilRead.java index 84312ad0..7282cf96 100644 --- a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/dtos/securite/profil/ProfilRead.java +++ b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/dtos/securite/profil/ProfilRead.java @@ -192,9 +192,9 @@ public enum Fields { DATE_MODIFICATION(LocalDateTime.class), // UTILISATEURS(List.class); - private Class type; + private final Class type; - private Fields(Class type) { + Fields(Class type) { this.type = type; } diff --git a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/dtos/securite/profil/ProfilWrite.java b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/dtos/securite/profil/ProfilWrite.java index fa524c0a..53f62957 100644 --- a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/dtos/securite/profil/ProfilWrite.java +++ b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/dtos/securite/profil/ProfilWrite.java @@ -86,9 +86,9 @@ public enum Fields { LIBELLE(String.class), // DROITS(List.class); - private Class type; + private final Class type; - private Fields(Class type) { + Fields(Class type) { this.type = type; } diff --git a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/dtos/securite/utilisateur/UtilisateurRead.java b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/dtos/securite/utilisateur/UtilisateurRead.java index f4a86f6c..0ffa2277 100644 --- a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/dtos/securite/utilisateur/UtilisateurRead.java +++ b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/dtos/securite/utilisateur/UtilisateurRead.java @@ -319,9 +319,9 @@ public enum Fields { DATE_CREATION(LocalDateTime.class), // DATE_MODIFICATION(LocalDateTime.class); - private Class type; + private final Class type; - private Fields(Class type) { + Fields(Class type) { this.type = type; } diff --git a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/dtos/securite/utilisateur/UtilisateurWrite.java b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/dtos/securite/utilisateur/UtilisateurWrite.java index ff8f04df..5e27993a 100644 --- a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/dtos/securite/utilisateur/UtilisateurWrite.java +++ b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/dtos/securite/utilisateur/UtilisateurWrite.java @@ -237,9 +237,9 @@ public enum Fields { PROFIL_ID(Integer.class), // TYPE_UTILISATEUR_CODE(TypeUtilisateurCode.class); - private Class type; + private final Class type; - private Fields(Class type) { + Fields(Class type) { this.type = type; } 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..ee9243eb 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 @@ -75,24 +75,8 @@ public Droit() { */ 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.libelle = code.getLibelle(); + this.typeDroit = code.getTypeDroit() != null ? new TypeDroit(code.getTypeDroit()) : null; } /** @@ -130,9 +114,9 @@ public enum Fields { LIBELLE(String.class), // TYPE_DROIT(TypeDroit.class); - private Class type; + private final Class type; - private Fields(Class type) { + Fields(Class type) { this.type = type; } diff --git a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/entities/securite/profil/Profil.java b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/entities/securite/profil/Profil.java index b57b1972..9fc736ac 100644 --- a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/entities/securite/profil/Profil.java +++ b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/entities/securite/profil/Profil.java @@ -207,9 +207,9 @@ public enum Fields { DATE_MODIFICATION(LocalDateTime.class), // UTILISATEURS(Utilisateur.class); - private Class type; + private final Class type; - private Fields(Class type) { + Fields(Class type) { this.type = type; } 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..e906e8f5 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 @@ -63,17 +63,7 @@ public TypeDroit() { */ 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.libelle = code.getLibelle(); } /** @@ -101,9 +91,9 @@ public enum Fields { CODE(TypeDroitCode.class), // LIBELLE(String.class); - private Class type; + private final Class type; - private Fields(Class type) { + Fields(Class type) { this.type = type; } 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..77945923 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 @@ -63,17 +63,7 @@ public TypeUtilisateur() { */ 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.libelle = code.getLibelle(); } /** @@ -101,9 +91,9 @@ public enum Fields { CODE(TypeUtilisateurCode.class), // LIBELLE(String.class); - private Class type; + private final Class type; - private Fields(Class type) { + Fields(Class type) { this.type = type; } diff --git a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/entities/securite/utilisateur/Utilisateur.java b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/entities/securite/utilisateur/Utilisateur.java index 0c268563..292161c9 100644 --- a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/entities/securite/utilisateur/Utilisateur.java +++ b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/entities/securite/utilisateur/Utilisateur.java @@ -312,9 +312,9 @@ public enum Fields { DATE_CREATION(LocalDateTime.class), // DATE_MODIFICATION(LocalDateTime.class); - private Class type; + private final Class type; - private Fields(Class type) { + Fields(Class type) { this.type = type; } diff --git a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/enums/securite/profil/DroitCode.java b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/enums/securite/profil/DroitCode.java index 9e9945b6..a3ec630d 100644 --- a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/enums/securite/profil/DroitCode.java +++ b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/enums/securite/profil/DroitCode.java @@ -11,17 +11,48 @@ public enum DroitCode { /** * Création. */ - CREATE, + CREATE("securite.profil.droit.values.Create", TypeDroitCode.WRITE), + /** + * Lecture. + */ + READ("securite.profil.droit.values.Read", TypeDroitCode.READ), + /** + * Mise à jour. + */ + UPDATE("securite.profil.droit.values.Update", TypeDroitCode.WRITE), /** * Suppression. */ - DELETE, + DELETE("securite.profil.droit.values.Delete", TypeDroitCode.ADMIN); + /** - * Lecture. + * Libelle. + */ + private final String libelle; + + /** + * TypeDroit. */ - READ, + private final TypeDroitCode typeDroit; /** - * Mise à jour. + * Enum constructor. + */ + DroitCode(final String libelle ,final TypeDroitCode typeDroit ){ + this.libelle = libelle; + this.typeDroit = typeDroit; + } + + /** + * Getter. + */ + public String getLibelle(){ + return this.libelle; + } + + /** + * Getter. */ - UPDATE + public TypeDroitCode getTypeDroit(){ + return this.typeDroit; + } } diff --git a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/enums/securite/profil/TypeDroitCode.java b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/enums/securite/profil/TypeDroitCode.java index 73fd73d0..9afb3304 100644 --- a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/enums/securite/profil/TypeDroitCode.java +++ b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/enums/securite/profil/TypeDroitCode.java @@ -8,16 +8,34 @@ * Enumération des valeurs possibles de la propriété Code de la classe TypeDroit. */ public enum TypeDroitCode { + /** + * Lecture. + */ + READ("securite.profil.typeDroit.values.Read"), + /** + * Ecriture. + */ + WRITE("securite.profil.typeDroit.values.Write"), /** * Administration. */ - ADMIN, + ADMIN("securite.profil.typeDroit.values.Admin"); + /** - * Lecture. + * Libelle. */ - READ, + private final String libelle; /** - * Ecriture. + * Enum constructor. + */ + TypeDroitCode(final String libelle ){ + this.libelle = libelle; + } + + /** + * Getter. */ - WRITE + public String getLibelle(){ + return this.libelle; + } } diff --git a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/enums/securite/utilisateur/TypeUtilisateurCode.java b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/enums/securite/utilisateur/TypeUtilisateurCode.java index 4bc828f7..4efcf93b 100644 --- a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/enums/securite/utilisateur/TypeUtilisateurCode.java +++ b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/enums/securite/utilisateur/TypeUtilisateurCode.java @@ -11,13 +11,31 @@ public enum TypeUtilisateurCode { /** * Administrateur. */ - ADMIN, + ADMIN("securite.utilisateur.typeUtilisateur.values.Admin"), + /** + * Gestionnaire. + */ + GEST("securite.utilisateur.typeUtilisateur.values.Gestionnaire"), /** * Client. */ - CLIENT, + CLIENT("securite.utilisateur.typeUtilisateur.values.Client"); + /** - * Gestionnaire. + * Libelle. + */ + private final String libelle; + /** + * Enum constructor. + */ + TypeUtilisateurCode(final String libelle ){ + this.libelle = libelle; + } + + /** + * Getter. */ - GEST + public String getLibelle(){ + return this.libelle; + } } diff --git a/samples/model/jpa.topmodel.lock b/samples/model/jpa.topmodel.lock index a9c1274d..06dbc233 100644 --- a/samples/model/jpa.topmodel.lock +++ b/samples/model/jpa.topmodel.lock @@ -4,7 +4,7 @@ version: 2.1.2 custom: - ../../../TopModel.Generator.Jpa: b1ab441870ce2af095217a3622f3f615 + ../../../TopModel.Generator.Jpa: 3507a5e9995725086a8473dfef9acf32 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 From 267b1b3ad65e1587de72d1776e9787285f7e9b56 Mon Sep 17 00:00:00 2001 From: Denis Challas Date: Thu, 24 Oct 2024 00:18:25 +0200 Subject: [PATCH 2/4] =?UTF-8?q?[JAVA]=20Enum=C3=A9rations=20repr=C3=A9sent?= =?UTF-8?q?ant=20totalement=20l'entit=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TopModel.Generator.Jpa/JpaEnumGenerator.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/TopModel.Generator.Jpa/JpaEnumGenerator.cs b/TopModel.Generator.Jpa/JpaEnumGenerator.cs index 481d4793..b19b42cc 100644 --- a/TopModel.Generator.Jpa/JpaEnumGenerator.cs +++ b/TopModel.Generator.Jpa/JpaEnumGenerator.cs @@ -105,6 +105,7 @@ private void WriteEnum(IProperty property, Class classe, string tag) if (prop is AssociationProperty ap && codeProperty.PrimaryKey && ap.Association.Values.Any(r => r.Value.ContainsKey(ap.Property) && r.Value[ap.Property] == value)) { + fw.AddImport($"{Config.GetEnumPackageName(ap.Association.EnumKey.Class, tag)}.{ap.Association.NamePascal + ap.Association.EnumKey}"); value = ap.Association.NamePascal + ap.Association.EnumKey + "." + value; isString = false; } From 406c2e9f607b1da2ecbcd0b5d87d1bf28fb40c46 Mon Sep 17 00:00:00 2001 From: Denis Challas Date: Thu, 24 Oct 2024 00:21:59 +0200 Subject: [PATCH 3/4] =?UTF-8?q?[JAVA]=20Enum=C3=A9rations=20repr=C3=A9sent?= =?UTF-8?q?ant=20totalement=20l'entit=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- samples/model/jpa.topmodel.lock | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/samples/model/jpa.topmodel.lock b/samples/model/jpa.topmodel.lock index 06dbc233..1ac2145f 100644 --- a/samples/model/jpa.topmodel.lock +++ b/samples/model/jpa.topmodel.lock @@ -4,7 +4,7 @@ version: 2.1.2 custom: - ../../../TopModel.Generator.Jpa: 3507a5e9995725086a8473dfef9acf32 + ../../../TopModel.Generator.Jpa: 0cb6ca08aebf5b6bd3e7ad6858854160 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 From 626942b5a81dcd5d15e456465d1f9626648280fc Mon Sep 17 00:00:00 2001 From: Denis Challas Date: Thu, 24 Oct 2024 00:45:13 +0200 Subject: [PATCH 4/4] =?UTF-8?q?[JAVA]=20Enum=C3=A9rations=20repr=C3=A9sent?= =?UTF-8?q?ant=20totalement=20l'entit=C3=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TopModel.Generator.Jpa/JpaEnumGenerator.cs | 37 ++++++++++++++++--- .../JpaModelConstructorGenerator.cs | 3 +- .../demo/entities/securite/profil/Droit.java | 2 +- .../demo/enums/securite/profil/DroitCode.java | 10 ++--- samples/model/jpa.topmodel.lock | 2 +- 5 files changed, 40 insertions(+), 14 deletions(-) diff --git a/TopModel.Generator.Jpa/JpaEnumGenerator.cs b/TopModel.Generator.Jpa/JpaEnumGenerator.cs index b19b42cc..f549feb4 100644 --- a/TopModel.Generator.Jpa/JpaEnumGenerator.cs +++ b/TopModel.Generator.Jpa/JpaEnumGenerator.cs @@ -133,7 +133,13 @@ private void WriteEnum(IProperty property, Class classe, string tag) fw.WriteLine(); fw.WriteDocStart(1, $@"{prop.NameByClassPascal}"); fw.WriteDocEnd(1); - fw.WriteLine(1, $@"private final {Config.GetType(prop)} {prop.NameByClassCamel};"); + var fieldName = prop.NameByClassCamel; + if (prop is AssociationProperty ap) + { + fieldName = $"{ap.Association.NameCamel}{ap.Association.EnumKey}"; + } + + fw.WriteLine(1, $@"private final {Config.GetType(prop)} {fieldName};"); } WriteConstructor(property, classe, fw, properties); @@ -143,8 +149,14 @@ private void WriteEnum(IProperty property, Class classe, string tag) fw.WriteLine(); fw.WriteDocStart(1, "Getter"); fw.WriteDocEnd(1); - fw.WriteLine(1, $@"public {Config.GetType(prop)} get{prop.NameByClassCamel.ToFirstUpper()}(){{"); - fw.WriteLine(2, $@"return this.{prop.NameByClassCamel};"); + var fieldName = prop.NameByClassCamel; + if (prop is AssociationProperty ap) + { + fieldName = $"{ap.Association.NameCamel}{ap.Association.EnumKey}"; + } + + fw.WriteLine(1, $@"public {Config.GetType(prop)} get{fieldName.ToFirstUpper()}(){{"); + fw.WriteLine(2, $@"return this.{fieldName};"); fw.WriteLine(1, $@"}}"); } @@ -160,15 +172,28 @@ private void WriteConstructor(IProperty property, Class classe, JavaWriter fw, I constructorAsString.Add($@"{Config.GetEnumName(property, classe)}("); constructorAsString.Add(properties.Select((prop, index) => - $@"final {Config.GetType(prop)} {prop.NameByClassCamel} {(prop == properties.Last() ? string.Empty : ",")}") - .Aggregate(string.Empty, (acc, curr) => acc + curr)); + { + var fieldName = prop.NameByClassCamel; + if (prop is AssociationProperty ap) + { + fieldName = $"{ap.Association.NameCamel}{ap.Association.EnumKey}"; + } + + return $@"final {Config.GetType(prop)} {fieldName} {(prop == properties.Last() ? string.Empty : ",")}"; + }).Aggregate(string.Empty, (acc, curr) => acc + curr)); constructorAsString.Add("){"); fw.WriteLine(1, constructorAsString.Aggregate(string.Empty, (acc, curr) => acc + curr)); foreach (var prop in properties) { + var fieldName = prop.NameByClassCamel; + if (prop is AssociationProperty ap) + { + fieldName = $"{ap.Association.NameCamel}{ap.Association.EnumKey}"; + } + // Constructeur set - fw.WriteLine(2, $@" this.{prop.NameByClassCamel} = {prop.NameByClassCamel};"); + fw.WriteLine(2, $@" this.{fieldName} = {fieldName};"); } fw.WriteLine(1, "}"); diff --git a/TopModel.Generator.Jpa/JpaModelConstructorGenerator.cs b/TopModel.Generator.Jpa/JpaModelConstructorGenerator.cs index 58f0e351..8e9e3922 100644 --- a/TopModel.Generator.Jpa/JpaModelConstructorGenerator.cs +++ b/TopModel.Generator.Jpa/JpaModelConstructorGenerator.cs @@ -40,7 +40,8 @@ public void WriteEnumConstructor(JavaWriter fw, Class classe, List availa { var javaType = _config.GetType(prop, useClassForAssociation: classe.IsPersistent && !_config.UseJdbc && prop is AssociationProperty asp && asp.Association.IsPersistent); javaType = javaType.Split("<")[0]; - val = $@"{classe.EnumKey!.NameCamel}.{prop.NameByClassCamel.WithPrefix(getterPrefix)}() != null ? new {javaType}({classe.EnumKey!.NameCamel}.{prop.NameByClassCamel.WithPrefix(getterPrefix)}()) : null"; + var fieldName = $"{ap.Association.NameCamel}{ap.Association.EnumKey}"; + val = $@"{classe.EnumKey!.NameCamel}.{fieldName.WithPrefix(getterPrefix)}() != null ? new {javaType}({classe.EnumKey!.NameCamel}.{fieldName.WithPrefix(getterPrefix)}()) : null"; } else if (_config.CanClassUseEnums(classe, prop: prop)) { 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 ee9243eb..8e370d5e 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 @@ -76,7 +76,7 @@ public Droit() { public Droit(DroitCode code) { this.code = code; this.libelle = code.getLibelle(); - this.typeDroit = code.getTypeDroit() != null ? new TypeDroit(code.getTypeDroit()) : null; + this.typeDroit = code.getTypeDroitCode() != null ? new TypeDroit(code.getTypeDroitCode()) : null; } /** diff --git a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/enums/securite/profil/DroitCode.java b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/enums/securite/profil/DroitCode.java index a3ec630d..e4fd5eaa 100644 --- a/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/enums/securite/profil/DroitCode.java +++ b/samples/generators/jpa/src/main/javagen/topmodel/jpa/sample/demo/enums/securite/profil/DroitCode.java @@ -33,13 +33,13 @@ public enum DroitCode { /** * TypeDroit. */ - private final TypeDroitCode typeDroit; + private final TypeDroitCode typeDroitCode; /** * Enum constructor. */ - DroitCode(final String libelle ,final TypeDroitCode typeDroit ){ + DroitCode(final String libelle ,final TypeDroitCode typeDroitCode ){ this.libelle = libelle; - this.typeDroit = typeDroit; + this.typeDroitCode = typeDroitCode; } /** @@ -52,7 +52,7 @@ public String getLibelle(){ /** * Getter. */ - public TypeDroitCode getTypeDroit(){ - return this.typeDroit; + public TypeDroitCode getTypeDroitCode(){ + return this.typeDroitCode; } } diff --git a/samples/model/jpa.topmodel.lock b/samples/model/jpa.topmodel.lock index 1ac2145f..e116f845 100644 --- a/samples/model/jpa.topmodel.lock +++ b/samples/model/jpa.topmodel.lock @@ -4,7 +4,7 @@ version: 2.1.2 custom: - ../../../TopModel.Generator.Jpa: 0cb6ca08aebf5b6bd3e7ad6858854160 + ../../../TopModel.Generator.Jpa: b33f821a19a58bf955992b62067567d2 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