Skip to content

Fix #373 : [JAVA] Enumérations représentant totalement l'entité #373 #407

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions TopModel.Generator.Jpa/ClassGeneration/JavaDtoGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ protected override void HandleClass(string fileName, Class classe, string tag)

fw.WriteClassDeclaration(classe.NamePascal, null, extends, implements);

WriteStaticMembers(fw, classe);
WriteStaticMembers(fw, classe, tag);
JpaModelPropertyGenerator.WriteProperties(fw, classe, tag);
WriteConstuctors(fw, classe, tag);

Expand Down Expand Up @@ -92,7 +92,7 @@ protected virtual void WriteConstuctors(JavaWriter fw, Class classe, string tag)
}
}

protected virtual void WriteStaticMembers(JavaWriter fw, Class classe)
protected virtual void WriteStaticMembers(JavaWriter fw, Class classe, string tag)
{
fw.WriteLine(" /** Serial ID */");
fw.WriteLine(1, "private static final long serialVersionUID = 1L;");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using TopModel.Core;
using TopModel.Generator.Core;
using TopModel.Utils;

namespace TopModel.Generator.Jpa;

Expand All @@ -13,59 +14,63 @@ public JavaEnumConstructorGenerator(JpaConfig config)
{
}

public void WriteEnumConstructor(JavaWriter fw, Class classe, IEnumerable<Class> availableClasses, string tag)
public void WriteEnumCodeFinder(JavaWriter fw, Class classe, string tag)
{
var codeProperty = classe.EnumKey!;
fw.WriteLine();
fw.WriteDocStart(1, "Enum constructor");
fw.WriteDocStart(1, "Enum code finder");
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}) {{");
var method = new JavaMethod(classe.NamePascal, "from")
{
Static = true,
Visibility = "public",
};
var param = new JavaMethodParameter(Config.GetType(classe.EnumKey!), codeProperty.Name.ToCamelCase());
method.AddParameter(param);

method.AddBodyLine($@"return switch ({param.Name}) {{");
foreach (var refValue in classe.Values.OrderBy(x => x.Name, StringComparer.Ordinal))
{
var code = refValue.Name.ToConstantCase();
var codeRef = refValue.Value[classe.EnumKey!].ToConstantCase();
method.AddBodyLine(1, $@"case {codeRef} -> {code};");
}

method.AddBodyLine("};");

fw.Write(1, method);
}

public void WriteEnumValueConstructor(JavaWriter fw, Class classe, IEnumerable<Class> availableClasses, string tag)
{
var codeProperty = classe.EnumKey!;
fw.WriteLine();
fw.WriteDocStart(1, "Enum constructor");
fw.WriteParam($"{classe.NameCamel}{Config.EnumValueSuffix}", "Enum de valeur dont on veut obtenir l'entité");
fw.WriteDocEnd(1);
fw.WriteLine(1, $"public {classe.NamePascal}({classe.NamePascal}{Config.EnumValueSuffix} {classe.NameCamel}{Config.EnumValueSuffix}) {{");
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)
foreach (var prop in classe.Properties)
{
fw.WriteLine(2, $@"switch({classe.EnumKey!.NameCamel}) {{");
foreach (var refValue in classe.Values.OrderBy(x => x.Name, StringComparer.Ordinal))
var value = $"{classe.NameCamel}{Config.EnumValueSuffix}.get{prop.NameByClassPascal}()";
if (prop is AssociationProperty ap && Config.CanClassUseEnums(ap.Association, prop: ap.Property))
{
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(3, $@"break;");
value = $"new {ap.Association.NamePascal}({classe.NameCamel}{Config.EnumValueSuffix}.get{prop.NameByClassPascal}{Config.EnumValueSuffix}())";
fw.AddImport(ap.Association.GetImport(Config, tag));
fw.AddImport($"{Config.GetEnumValuePackageName(ap.Association, tag)}.{ap.Association.NamePascal}{Config.EnumValueSuffix}");
fw.WriteLine(2, $@"if ({classe.NameCamel}{Config.EnumValueSuffix}.get{prop.NameByClassPascal}{Config.EnumValueSuffix}() != 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, $"}}");
Expand Down
17 changes: 11 additions & 6 deletions TopModel.Generator.Jpa/ClassGeneration/JavaEnumDtoGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,34 @@ protected override JavaEnumConstructorGenerator ConstructorGenerator

protected override bool FilterClass(Class classe)
{
return !classe.Abstract && Config.CanClassUseEnums(classe, Classes) && !classe.IsPersistent;
return !classe.Abstract
&& Config.CanClassUseEnums(classe, Classes)
&& !Config.EnumsAsEnums
&& !classe.IsPersistent;
}

protected override void WriteConstuctors(JavaWriter fw, Class classe, string tag)
{
ConstructorGenerator.WriteNoArgConstructor(fw, classe);
ConstructorGenerator.WriteEnumConstructor(fw, classe, Classes, tag);
ConstructorGenerator.WriteEnumCodeFinder(fw, classe, tag);
ConstructorGenerator.WriteEnumValueConstructor(fw, classe, Classes, tag);
}

protected override void WriteSetters(JavaWriter fw, Class classe, string tag)
{
return;
}

protected override void WriteStaticMembers(JavaWriter fw, Class classe)
protected override void WriteStaticMembers(JavaWriter fw, Class classe, string tag)
{
base.WriteStaticMembers(fw, classe);
base.WriteStaticMembers(fw, classe, tag);
fw.WriteLine();
var codeProperty = classe.EnumKey!;
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(codeProperty, classe)}.{code});");
fw.AddImport($"{Config.GetEnumValuePackageName(classe, tag)}.{classe.NamePascal}{Config.EnumValueSuffix}");
fw.WriteLine(1, $@"public static final {classe.NamePascal} {code} = new {classe.NamePascal}({classe.NamePascal}{Config.EnumValueSuffix}.{code});");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ protected override void HandleClass(string fileName, Class classe, string tag)

if (Config.CanClassUseEnums(classe, Classes))
{
ConstructorGenerator.WriteEnumConstructor(fw, classe, Classes, tag);
ConstructorGenerator.WriteEnumCodeFinder(fw, classe, tag);
}

WriteGetters(fw, classe, tag);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/// <summary>
/// Générateur de fichiers de modèles JPA.
/// </summary>
public class JdbcModelPropertyGenerator(JpaConfig config, IEnumerable<Class> classes, Dictionary<string, string> newableTypes) : JpaModelPropertyGenerator(config, classes, newableTypes)

Check warning on line 8 in TopModel.Generator.Jpa/ClassGeneration/JdbcModelPropertyGenerator.cs

View workflow job for this annotation

GitHub Actions / build

Les éléments de la classe ne sont pas dans le bon ordre

Check warning on line 8 in TopModel.Generator.Jpa/ClassGeneration/JdbcModelPropertyGenerator.cs

View workflow job for this annotation

GitHub Actions / build

Les éléments de la classe ne sont pas dans le bon ordre
{
private readonly IEnumerable<Class> _classes = classes;
private readonly JpaConfig _config = config;
Expand All @@ -27,13 +27,9 @@
return _config.GetType(property, _classes, false);
}

public override void WriteProperties(JavaWriter fw, Class classe, string tag)
public override IList<IProperty> GetProperties(Class classe)
{
var properties = classe.Properties.Where(p => !(p is AssociationProperty ap && (ap.Type == AssociationType.OneToMany || ap.Type == AssociationType.ManyToMany)));
foreach (var property in properties)
{
WriteProperty(fw, property, tag);
}
return classe.Properties.Where(p => !(p is AssociationProperty ap && (ap.Type == AssociationType.OneToMany || ap.Type == AssociationType.ManyToMany))).ToList();
}

protected override IEnumerable<JavaAnnotation> GetAnnotations(AliasProperty property, string tag)
Expand Down
33 changes: 22 additions & 11 deletions TopModel.Generator.Jpa/ClassGeneration/JpaEnumEntityGenerator.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Extensions.Logging;
using TopModel.Core;
using TopModel.Core.Model.Implementation;
using TopModel.Utils;

namespace TopModel.Generator.Jpa;

Expand Down Expand Up @@ -32,7 +33,10 @@ protected override JavaEnumConstructorGenerator ConstructorGenerator

protected override bool FilterClass(Class classe)
{
return !classe.Abstract && Config.CanClassUseEnums(classe, Classes) && classe.IsPersistent;
return !classe.Abstract
&& Config.CanClassUseEnums(classe, Classes)
&& classe.IsPersistent
&& !Config.EnumsAsEnums;
}

protected override void HandleClass(string fileName, Class classe, string tag)
Expand All @@ -57,17 +61,15 @@ protected override void HandleClass(string fileName, Class classe, string tag)
var codeProperty = classe.EnumKey!;
foreach (var refValue in classe.Values.OrderBy(x => x.Name, StringComparer.Ordinal))
{
var code = refValue.Value[codeProperty];
if (classe.IsPersistent)
{
fw.AddImport($"{JavaxOrJakarta}.persistence.Transient");
fw.WriteLine(1, "@Transient");
}

fw.WriteLine(1, $@"public static final {classe.NamePascal} {code} = new {classe.NamePascal}({Config.GetEnumName(codeProperty, classe)}.{code});");
var code = refValue.Name.ToConstantCase();
fw.AddImport($"{JavaxOrJakarta}.persistence.Transient");
fw.WriteLine(1, "@Transient");

fw.AddImport($"{Config.GetEnumValuePackageName(classe, tag)}.{classe.NamePascal}{Config.EnumValueSuffix}");
fw.WriteLine(1, $@"public static final {classe.NamePascal} {code} = new {classe.NamePascal}({classe.NamePascal}{Config.EnumValueSuffix}.{code});");
}

JpaModelPropertyGenerator.WriteProperties(fw, classe, tag);
JpaModelPropertyGenerator.WriteProperties(fw, classe, tag, classe.Properties);
WriteConstructors(classe, tag, fw);

WriteGetters(fw, classe, tag);
Expand All @@ -88,7 +90,16 @@ protected override void HandleClass(string fileName, Class classe, string tag)
protected override void WriteConstructors(Class classe, string tag, JavaWriter fw)
{
ConstructorGenerator.WriteNoArgConstructor(fw, classe);
ConstructorGenerator.WriteEnumConstructor(fw, classe, Classes, tag);
ConstructorGenerator.WriteEnumCodeFinder(fw, classe, tag);
ConstructorGenerator.WriteEnumValueConstructor(fw, classe, Classes, tag);
}

protected override void WriteGetters(JavaWriter fw, Class classe, string tag)
{
foreach (var property in classe.Properties)
{
JpaModelPropertyGenerator.WriteGetter(fw, tag, property);
}
}

protected override void WriteSetters(JavaWriter fw, Class classe, string tag)
Expand Down
Loading
Loading