-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JAVA] Enumérations représentant totalement l'entité
Fixes #373
- Loading branch information
1 parent
074ca2d
commit d94e3aa
Showing
21 changed files
with
417 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
using Microsoft.Extensions.Logging; | ||
using TopModel.Core; | ||
using TopModel.Generator.Core; | ||
|
||
namespace TopModel.Generator.Jpa; | ||
|
||
/// <summary> | ||
/// Générateur de DAOs JPA. | ||
/// </summary> | ||
public class JpaConverterGenerator : ClassGeneratorBase<JpaConfig> | ||
{ | ||
private readonly ILogger<JpaConverterGenerator> _logger; | ||
|
||
public JpaConverterGenerator(ILogger<JpaConverterGenerator> logger) | ||
: base(logger) | ||
{ | ||
_logger = logger; | ||
} | ||
|
||
public override string Name => "JpaConverterGen"; | ||
|
||
protected override bool FilterClass(Class classe) | ||
{ | ||
return classe.IsPersistent && (!Config.UseJdbc || classe.PrimaryKey.Count() <= 1) && Config.CanClassUseEnums(classe) && Config.EnumsAsEnum; | ||
} | ||
|
||
protected override string GetFileName(Class classe, string tag) | ||
{ | ||
string path = $"{classe.NamePascal}Converter.java"; | ||
return Path.Combine( | ||
Config.OutputDirectory, | ||
Config.ResolveVariables(Config.EnumConverterPath!, tag, module: classe.Namespace.Module).ToFilePath(), | ||
path); | ||
} | ||
|
||
protected override void HandleClass(string fileName, Class classe, string tag) | ||
{ | ||
var packageName = Config.ResolveVariables( | ||
Config.EnumConverterPath!, | ||
tag, | ||
module: classe.Namespace.Module).ToPackageName(); | ||
|
||
using var fw = new JavaWriter(fileName, _logger, packageName, null); | ||
fw.WriteLine(); | ||
fw.AddImport(classe.GetImport(Config, tag)); | ||
var javaOrJakarta = Config.PersistenceMode.ToString().ToLower(); | ||
fw.AddImport($"{javaOrJakarta}.persistence.AttributeConverter"); | ||
List<string> implements = new() | ||
{ | ||
$"AttributeConverter<{classe.NamePascal}, String>" | ||
}; | ||
fw.WriteClassDeclaration($"{classe.NamePascal}Converter", null, null, implements); | ||
|
||
fw.WriteLine(); | ||
fw.WriteLine(1, "@Override"); | ||
fw.WriteLine(1, $@"public String convertToDatabaseColumn({classe.NamePascal} item) {{"); | ||
fw.WriteLine(2, $"return item == null ? null : item.get{classe.EnumKey.NamePascal}().name();"); | ||
Check warning on line 57 in TopModel.Generator.Jpa/JpaConverterGenerator.cs GitHub Actions / build
|
||
fw.WriteLine(1, "}"); | ||
WriteEnumEntityToAttribute(fw, classe); | ||
fw.WriteLine("}"); | ||
} | ||
|
||
private void WriteEnumEntityToAttribute(JavaWriter fw, Class classe) | ||
{ | ||
fw.WriteLine(); | ||
fw.WriteLine(1, "@Override"); | ||
fw.WriteLine(1, $"public {classe.NamePascal} convertToEntityAttribute(String {classe.EnumKey!.NameCamel}) {{"); | ||
fw.WriteLine(2, $@"return {classe.NamePascal}.from({classe.EnumKey!.NameCamel});"); | ||
|
||
fw.WriteLine(1, $"}}"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.