Skip to content

Commit

Permalink
[JPA] Mauvais type lorsque qu'une contrainte d'unicité est ajouté à u…
Browse files Browse the repository at this point in the history
…n champ d'une liste de ref qui n'est pas la PK

Fix : #268
  • Loading branch information
Gildéric DERUETTE committed Sep 29, 2023
1 parent c2b722b commit 31df881
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 23 deletions.
12 changes: 6 additions & 6 deletions TopModel.Generator.Jpa/ImportsJpaExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ private static List<string> GetTypeImports(this AliasProperty ap, JpaConfig conf
{
var imports = new List<string>();

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));
Expand All @@ -96,9 +96,9 @@ private static List<string> 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;
Expand Down
8 changes: 4 additions & 4 deletions TopModel.Generator.Jpa/JpaConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
54 changes: 46 additions & 8 deletions TopModel.Generator.Jpa/JpaEnumGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
using Microsoft.Extensions.Logging;
using TopModel.Core;
using TopModel.Core.FileModel;
using TopModel.Generator.Core;

namespace TopModel.Generator.Jpa;

/// <summary>
/// Générateur de fichiers de modèles JPA.
/// </summary>
public class JpaEnumGenerator : ClassGeneratorBase<JpaConfig>
public class JpaEnumGenerator : GeneratorBase<JpaConfig>
{
private readonly ILogger<JpaEnumGenerator> _logger;

Expand All @@ -19,25 +20,62 @@ public JpaEnumGenerator(ILogger<JpaEnumGenerator> 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<string> 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<IFieldProperty> GetEnumProperties(Class classe)
{
List<IFieldProperty> 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<ModelFile> 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))
{
Expand All @@ -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("}");
Expand Down
10 changes: 7 additions & 3 deletions TopModel.Generator.Jpa/JpaModelConstructorGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public void WriteEnumConstructor(JavaWriter fw, Class classe, List<Class> 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();
Expand All @@ -43,7 +43,7 @@ public void WriteEnumConstructor(JavaWriter fw, Class classe, List<Class> 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<IFieldProperty>()
.Where(p => p != codeProperty))
{
var isString = _config.GetType((IFieldProperty)prop) == "String";
Expand All @@ -54,8 +54,12 @@ public void WriteEnumConstructor(JavaWriter fw, Class classe, List<Class> 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;
}
Expand Down
2 changes: 1 addition & 1 deletion TopModel.Generator.Jpa/JpaModelGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@ private void WriteEnumShortcuts(JavaWriter fw, Class classe, string tag)
{
foreach (var ap in classe.GetProperties(AvailableClasses).OfType<AssociationProperty>().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;
Expand Down
2 changes: 1 addition & 1 deletion TopModel.Generator.Jpa/JpaModelPropertyGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down

0 comments on commit 31df881

Please sign in to comment.