Skip to content
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

[JAVA] Enumérations représentant totalement l'entité #401

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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
108 changes: 104 additions & 4 deletions TopModel.Generator.Jpa/JpaEnumGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@
using TopModel.Core;
using TopModel.Core.FileModel;
using TopModel.Generator.Core;
using TopModel.Utils;

namespace TopModel.Generator.Jpa;

/// <summary>
/// Générateur de fichiers de modèles JPA.
/// </summary>
public class JpaEnumGenerator : GeneratorBase<JpaConfig>

Check warning on line 12 in TopModel.Generator.Jpa/JpaEnumGenerator.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 12 in TopModel.Generator.Jpa/JpaEnumGenerator.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 ILogger<JpaEnumGenerator> _logger;

Expand Down Expand Up @@ -80,22 +81,121 @@
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<string> enumAsString = new List<string> { };
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))
{
fw.AddImport($"{Config.GetEnumPackageName(ap.Association.EnumKey.Class, tag)}.{ap.Association.NamePascal + ap.Association.EnumKey}");

Check warning on line 108 in TopModel.Generator.Jpa/JpaEnumGenerator.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.

Check warning on line 108 in TopModel.Generator.Jpa/JpaEnumGenerator.cs

View workflow job for this annotation

GitHub Actions / build

Dereference of a possibly null reference.
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);
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);

foreach (var prop in properties)
{
fw.WriteLine();
fw.WriteDocStart(1, "Getter");
fw.WriteDocEnd(1);
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, $@"}}");
}

fw.WriteLine("}");
}

private void WriteConstructor(IProperty property, Class classe, JavaWriter fw, IEnumerable<IProperty> properties)
{
// Constructeur
fw.WriteDocStart(1, "Enum constructor");
fw.WriteDocEnd(1);
List<string> constructorAsString = new List<string>();
constructorAsString.Add($@"{Config.GetEnumName(property, classe)}(");

constructorAsString.Add(properties.Select((prop, index) =>
{
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.{fieldName} = {fieldName};");
}

fw.WriteLine(1, "}");
}
}
54 changes: 25 additions & 29 deletions TopModel.Generator.Jpa/JpaModelConstructorGenerator.cs
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 Down Expand Up @@ -31,40 +32,35 @@
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];
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))
{
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, $@"}}");
}

Check warning on line 63 in TopModel.Generator.Jpa/JpaModelConstructorGenerator.cs

View workflow job for this annotation

GitHub Actions / build

Check warning on line 63 in TopModel.Generator.Jpa/JpaModelConstructorGenerator.cs

View workflow job for this annotation

GitHub Actions / build

}

fw.WriteLine(1, $"}}");
Expand Down
4 changes: 2 additions & 2 deletions TopModel.Generator.Jpa/JpaModelGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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, "}");

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.getTypeDroitCode() != null ? new TypeDroit(code.getTypeDroitCode()) : null;
}

/**
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Loading
Loading