diff --git a/TopModel.Generator.Jpa/ImportsJpaExtensions.cs b/TopModel.Generator.Jpa/ImportsJpaExtensions.cs index 34fa6ed1..44d7282a 100644 --- a/TopModel.Generator.Jpa/ImportsJpaExtensions.cs +++ b/TopModel.Generator.Jpa/ImportsJpaExtensions.cs @@ -67,13 +67,13 @@ private static List GetTypeImports(this AliasProperty ap, JpaConfig conf { var imports = new List(); - if (config.CanClassUseEnums(ap.Property.Class)) + if (config.CanClassUseEnums(ap.Property.Class, prop: ap)) { - imports.Add($"{config.GetEnumPackageName(ap.Property.Class, tag)}.{config.GetEnumName(ap.Property.Class)}"); + imports.Add($"{config.GetEnumPackageName(ap.Property.Class, tag)}.{config.GetEnumName(ap, ap.Property.Class)}"); } - else if (ap.Property is AssociationProperty apr && config.CanClassUseEnums(apr.Property.Class)) + else if (ap.Property is AssociationProperty apr && config.CanClassUseEnums(apr.Property.Class, prop: apr.Property)) { - imports.Add($"{config.GetEnumPackageName(apr.Association, tag)}.{config.GetEnumName(apr.Association)}"); + imports.Add($"{config.GetEnumPackageName(apr.Association, tag)}.{config.GetEnumName(apr.Association.PrimaryKey.Single(), apr.Association)}"); } imports.AddRange(config.GetDomainImports(ap, tag)); @@ -96,9 +96,9 @@ private static List GetTypeImports(this IFieldProperty rp, JpaConfig con imports.AddRange(rpr.GetTypeImports(config, tag)); } - if (rp.Class != null && config.CanClassUseEnums(rp.Class) && rp == rp.Class.EnumKey) + if (rp.Class != null && config.CanClassUseEnums(rp.Class, prop: rp)) { - imports.Add($"{config.GetEnumPackageName(rp.Class, tag)}.{config.GetEnumName(rp.Class)}"); + imports.Add($"{config.GetEnumPackageName(rp.Class, tag)}.{config.GetEnumName(rp, rp.Class)}"); } return imports; diff --git a/TopModel.Generator.Jpa/JpaConfig.cs b/TopModel.Generator.Jpa/JpaConfig.cs index f3c36236..f7dfd9c8 100644 --- a/TopModel.Generator.Jpa/JpaConfig.cs +++ b/TopModel.Generator.Jpa/JpaConfig.cs @@ -180,17 +180,17 @@ public string GetDataFlowPartialFilePath(DataFlow df, string tag) $"{df.Name.ToPascalCase()}PartialFlow.java"); } - public string GetEnumFileName(Class classe, string tag) + public string GetEnumFileName(IFieldProperty property, Class classe, string tag) { return Path.Combine( OutputDirectory, ResolveVariables(EnumsPath, tag, module: classe.Namespace.Module).ToFilePath(), - $"{GetEnumName(classe)}.java"); + $"{GetEnumName(property, classe)}.java"); } - public string GetEnumName(Class classe) + public string GetEnumName(IFieldProperty property, Class classe) { - return $"{classe.NamePascal}{classe.EnumKey!.Name.ToPascalCase()}"; + return $"{classe.NamePascal}{property.Name.ToPascalCase()}"; } public string GetEnumPackageName(Class classe, string tag) diff --git a/TopModel.Generator.Jpa/JpaEnumGenerator.cs b/TopModel.Generator.Jpa/JpaEnumGenerator.cs index 35d052c1..96f3fb2a 100644 --- a/TopModel.Generator.Jpa/JpaEnumGenerator.cs +++ b/TopModel.Generator.Jpa/JpaEnumGenerator.cs @@ -1,5 +1,6 @@ using Microsoft.Extensions.Logging; using TopModel.Core; +using TopModel.Core.FileModel; using TopModel.Generator.Core; namespace TopModel.Generator.Jpa; @@ -7,7 +8,7 @@ namespace TopModel.Generator.Jpa; /// /// Générateur de fichiers de modèles JPA. /// -public class JpaEnumGenerator : ClassGeneratorBase +public class JpaEnumGenerator : GeneratorBase { private readonly ILogger _logger; @@ -19,25 +20,62 @@ public JpaEnumGenerator(ILogger logger, ModelConfig modelConfi public override string Name => "JpaEnumGen"; - protected override bool FilterClass(Class classe) + protected bool FilterClass(Class classe) { return !classe.Abstract && Config.CanClassUseEnums(classe, Classes.ToList()); } + 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 override string GetFileName(Class classe, string tag) + protected string GetFileName(IFieldProperty property, Class classe, string tag) { - return Config.GetEnumFileName(classe, tag); + return Config.GetEnumFileName(property, classe, tag); } - protected override void HandleClass(string fileName, Class classe, string tag) + private IEnumerable GetEnumProperties(Class classe) + { + List result = new(); + if (classe.EnumKey != null && Config.CanClassUseEnums(classe, prop: classe.EnumKey)) + { + result.Add(classe.EnumKey); + } + + var uks = classe.UniqueKeys.Where(uk => uk.Count == 1 && Config.CanClassUseEnums(classe, Classes, uk.Single())).Select(uk => uk.Single()); + result.AddRange(uks); + return result; + } + + 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); + } + } + } + } + + protected void HandleClass(Class classe, string tag) + { + foreach (var p in GetEnumProperties(classe)) + { + WriteEnum(p, classe, tag); + } + } + + private void WriteEnum(IFieldProperty property, Class classe, string tag) { var packageName = Config.GetEnumPackageName(classe, tag); - using var fw = new JavaWriter(Config.GetEnumFileName(classe, tag), _logger, packageName, null); + using var fw = new JavaWriter(Config.GetEnumFileName(property, classe, tag), _logger, packageName, null); fw.WriteLine(); var codeProperty = classe.EnumKey!; fw.WriteDocStart(0, $"Enumération des valeurs possibles de la propriété {codeProperty.NamePascal} de la classe {classe.NamePascal}"); fw.WriteDocEnd(0); - fw.WriteLine($@"public enum {Config.GetEnumName(classe)} {{"); + fw.WriteLine($@"public enum {Config.GetEnumName(property, classe)} {{"); var i = 0; foreach (var value in classe.Values.OrderBy(x => x.Name, StringComparer.Ordinal)) { @@ -49,7 +87,7 @@ protected override void HandleClass(string fileName, Class classe, string tag) fw.WriteDocEnd(1); } - fw.WriteLine(1, $"{value.Value[codeProperty]}{(isLast ? ";" : ",")}"); + fw.WriteLine(1, $"{value.Value[property]}{(isLast ? ";" : ",")}"); } fw.WriteLine("}"); diff --git a/TopModel.Generator.Jpa/JpaModelConstructorGenerator.cs b/TopModel.Generator.Jpa/JpaModelConstructorGenerator.cs index 22d40b6e..486cc5c0 100644 --- a/TopModel.Generator.Jpa/JpaModelConstructorGenerator.cs +++ b/TopModel.Generator.Jpa/JpaModelConstructorGenerator.cs @@ -22,7 +22,7 @@ public void WriteEnumConstructor(JavaWriter fw, Class classe, List availa foreach (var refValue in classe.Values.OrderBy(x => x.Name, StringComparer.Ordinal)) { var code = refValue.Value[codeProperty]; - fw.WriteLine(1, $@"public static final {classe.NamePascal} {code} = new {classe.NamePascal}({_config.GetEnumName(classe)}.{code});"); + fw.WriteLine(1, $@"public static final {classe.NamePascal} {code} = new {classe.NamePascal}({_config.GetEnumName(codeProperty, classe)}.{code});"); } fw.WriteLine(); @@ -43,7 +43,7 @@ public void WriteEnumConstructor(JavaWriter fw, Class classe, List availa { var code = refValue.Value[codeProperty]; fw.WriteLine(2, $@"case {code} :"); - foreach (var prop in classe.GetProperties(availableClasses) + foreach (var prop in classe.GetProperties(availableClasses).OfType() .Where(p => p != codeProperty)) { var isString = _config.GetType((IFieldProperty)prop) == "String"; @@ -54,8 +54,12 @@ public void WriteEnumConstructor(JavaWriter fw, Class classe, List availa isString = false; fw.AddImport(ap.Association.GetImport(_config, tag)); } + else if (_config.CanClassUseEnums(classe, prop: prop)) + { + value = _config.GetType(prop) + "." + value; + } - if (modelConfig.I18n.TranslateReferences && classe.DefaultProperty == prop) + if (modelConfig.I18n.TranslateReferences && classe.DefaultProperty == prop && !_config.CanClassUseEnums(classe, prop: prop)) { value = refValue.ResourceKey; } diff --git a/TopModel.Generator.Jpa/JpaModelGenerator.cs b/TopModel.Generator.Jpa/JpaModelGenerator.cs index 253a4985..993e291c 100644 --- a/TopModel.Generator.Jpa/JpaModelGenerator.cs +++ b/TopModel.Generator.Jpa/JpaModelGenerator.cs @@ -284,7 +284,7 @@ private void WriteEnumShortcuts(JavaWriter fw, Class classe, string tag) { foreach (var ap in classe.GetProperties(AvailableClasses).OfType().Where(ap => Config.CanClassUseEnums(ap.Association))) { - fw.AddImport($"{Config.GetEnumPackageName(ap.Association, tag)}.{Config.GetEnumName(ap.Association)}"); + fw.AddImport($"{Config.GetEnumPackageName(ap.Association, tag)}.{Config.GetEnumName(ap, ap.Association)}"); var isMultiple = ap.Type.IsToMany(); { var propertyName = ap.NameCamel; diff --git a/TopModel.Generator.Jpa/JpaModelPropertyGenerator.cs b/TopModel.Generator.Jpa/JpaModelPropertyGenerator.cs index 24c4de1b..eb6eba6f 100644 --- a/TopModel.Generator.Jpa/JpaModelPropertyGenerator.cs +++ b/TopModel.Generator.Jpa/JpaModelPropertyGenerator.cs @@ -188,7 +188,7 @@ private void WriteProperty(JavaWriter fw, Class classe, AssociationProperty prop var suffix = string.Empty; - if (_config.CanClassUseEnums(property.Association)) + if (_config.CanClassUseEnums(property.Association, _classes, prop: property.Association.PrimaryKey.Single())) { var defaultValue = _config.GetValue(property, _classes); if (defaultValue != "null")