Skip to content

Commit

Permalink
[JPA] Ajout des getters et setters sur les pk composites
Browse files Browse the repository at this point in the history
  • Loading branch information
gideruette committed Oct 12, 2023
1 parent 0d99022 commit 0a850aa
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 33 deletions.
35 changes: 4 additions & 31 deletions TopModel.Generator.Jpa/JpaModelGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ private JpaModelPropertyGenerator JpaModelPropertyGenerator
{
get
{
_jpaModelPropertyGenerator ??= new JpaModelPropertyGenerator(Config, Classes);
_jpaModelPropertyGenerator ??= new JpaModelPropertyGenerator(Config, Classes, _newableTypes);
return _jpaModelPropertyGenerator;
}
}
Expand Down Expand Up @@ -92,7 +92,7 @@ protected override void HandleClass(string fileName, Class classe, string tag)
}

JpaModelPropertyGenerator.WriteProperties(fw, classe, tag);
JpaModelPropertyGenerator.WriteCompositePrimaryKeyClass(fw, classe);
JpaModelPropertyGenerator.WriteCompositePrimaryKeyClass(fw, classe, tag);
JpaModelConstructorGenerator.WriteNoArgConstructor(fw, classe);
if (Config.MappersInClass)
{
Expand Down Expand Up @@ -414,27 +414,7 @@ private void WriteGetters(JavaWriter fw, Class classe, string tag)
var properties = Config.UseJdbc ? classe.Properties : classe.GetProperties(AvailableClasses);
foreach (var property in properties)
{
var propertyName = Config.UseJdbc ? property.NameCamel : property.NameByClassCamel;
fw.WriteLine();
fw.WriteDocStart(1, $"Getter for {propertyName}");
fw.WriteReturns(1, $"value of {{@link {classe.GetImport(Config, tag)}#{propertyName} {propertyName}}}");
fw.WriteDocEnd(1);

var getterPrefix = Config.GetType(property, Classes, true) == "boolean" ? "is" : "get";
fw.WriteLine(1, @$"public {Config.GetType(property, useClassForAssociation: classe.IsPersistent && !Config.UseJdbc)} {propertyName.ToPascalCase().WithPrefix(getterPrefix)}() {{");
if (property is AssociationProperty ap && ap.Type.IsToMany())
{
var type = Config.GetType(ap, AvailableClasses, useClassForAssociation: classe.IsPersistent && !Config.UseJdbc).Split('<').First();
if (_newableTypes.TryGetValue(type, out var newableType))
{
fw.WriteLine(2, $"if(this.{propertyName} == null)");
fw.AddImport($"java.util.{newableType}");
fw.WriteLine(3, $"this.{propertyName} = new {newableType}<>();");
}
}

fw.WriteLine(2, @$"return this.{propertyName};");
fw.WriteLine(1, "}");
_jpaModelPropertyGenerator!.WriteGetter(fw, classe, tag, property);
}
}

Expand Down Expand Up @@ -486,14 +466,7 @@ private void WriteSetters(JavaWriter fw, Class classe, string tag)
var properties = Config.UseJdbc ? classe.Properties : classe.GetProperties(AvailableClasses);
foreach (var property in properties)
{
var propertyName = Config.UseJdbc ? property.NameCamel : property.NameByClassCamel;
fw.WriteLine();
fw.WriteDocStart(1, $"Set the value of {{@link {classe.GetImport(Config, tag)}#{propertyName} {propertyName}}}");
fw.WriteLine(1, $" * @param {propertyName} value to set");
fw.WriteDocEnd(1);
fw.WriteLine(1, @$"public void {propertyName.WithPrefix("set")}({Config.GetType(property, useClassForAssociation: classe.IsPersistent && !Config.UseJdbc)} {propertyName}) {{");
fw.WriteLine(2, @$"this.{propertyName} = {propertyName};");
fw.WriteLine(1, "}");
_jpaModelPropertyGenerator!.WriteSetter(fw, classe, tag, property);
}
}

Expand Down
50 changes: 48 additions & 2 deletions TopModel.Generator.Jpa/JpaModelPropertyGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ public class JpaModelPropertyGenerator
{
private readonly IEnumerable<Class> _classes;
private readonly JpaConfig _config;
private readonly Dictionary<string, string> _newableTypes;

public JpaModelPropertyGenerator(JpaConfig config, IEnumerable<Class> classes)
public JpaModelPropertyGenerator(JpaConfig config, IEnumerable<Class> classes, Dictionary<string, string> newableTypes)
{
_classes = classes;
_config = config;
_newableTypes = newableTypes;
}

public void WriteCompositePrimaryKeyClass(JavaWriter fw, Class classe)
public void WriteCompositePrimaryKeyClass(JavaWriter fw, Class classe, string tag)
{
if (classe.PrimaryKey.Count() <= 1 || !classe.IsPersistent)
{
Expand All @@ -35,6 +37,13 @@ public void WriteCompositePrimaryKeyClass(JavaWriter fw, Class classe)
fw.WriteLine();
}

foreach (var pk in classe.PrimaryKey)
{
WriteGetter(fw, classe, tag, pk, 2);
WriteSetter(fw, classe, tag, pk, 2);
}

fw.WriteLine();
fw.WriteLine(2, "public boolean equals(Object o) {");
fw.WriteLine(3, $@"if(!(o instanceof {classe.NamePascal}Id)) {{");
fw.WriteLine(4, "return false;");
Expand All @@ -53,6 +62,31 @@ public void WriteCompositePrimaryKeyClass(JavaWriter fw, Class classe)
fw.WriteLine(1, "}");
}

public void WriteGetter(JavaWriter fw, Class classe, string tag, IProperty property, int indentLevel = 1)
{
var propertyName = _config.UseJdbc ? property.NameCamel : property.NameByClassCamel;
fw.WriteLine();
fw.WriteDocStart(indentLevel, $"Getter for {propertyName}");
fw.WriteReturns(indentLevel, $"value of {{@link {classe.GetImport(_config, tag)}#{propertyName} {propertyName}}}");
fw.WriteDocEnd(indentLevel);

var getterPrefix = _config.GetType(property, _classes, true) == "boolean" ? "is" : "get";
fw.WriteLine(indentLevel, @$"public {_config.GetType(property, useClassForAssociation: classe.IsPersistent && !_config.UseJdbc)} {propertyName.ToPascalCase().WithPrefix(getterPrefix)}() {{");
if (property is AssociationProperty ap && ap.Type.IsToMany())
{
var type = _config.GetType(ap, _classes, useClassForAssociation: classe.IsPersistent && !_config.UseJdbc).Split('<').First();
if (_newableTypes.TryGetValue(type, out var newableType))
{
fw.WriteLine(indentLevel + 1, $"if(this.{propertyName} == null)");
fw.AddImport($"java.util.{newableType}");
fw.WriteLine(indentLevel + 2, $"this.{propertyName} = new {newableType}<>();");
}
}

fw.WriteLine(indentLevel + 1, @$"return this.{propertyName};");
fw.WriteLine(indentLevel, "}");
}

public void WriteProperties(JavaWriter fw, Class classe, string tag)
{
var properties = _config.UseJdbc ? classe.Properties : classe.GetProperties(_classes);
Expand Down Expand Up @@ -86,6 +120,18 @@ public void WriteProperty(JavaWriter fw, Class classe, IProperty property, strin
}
}

public void WriteSetter(JavaWriter fw, Class classe, string tag, IProperty property, int indentLevel = 1)
{
var propertyName = _config.UseJdbc ? property.NameCamel : property.NameByClassCamel;
fw.WriteLine();
fw.WriteDocStart(indentLevel, $"Set the value of {{@link {classe.GetImport(_config, tag)}#{propertyName} {propertyName}}}");
fw.WriteLine(indentLevel, $" * @param {propertyName} value to set");
fw.WriteDocEnd(indentLevel);
fw.WriteLine(indentLevel, @$"public void {propertyName.WithPrefix("set")}({_config.GetType(property, useClassForAssociation: classe.IsPersistent && !_config.UseJdbc)} {propertyName}) {{");
fw.WriteLine(indentLevel + 1, @$"this.{propertyName} = {propertyName};");
fw.WriteLine(indentLevel, "}");
}

private void WriteManyToMany(JavaWriter fw, Class classe, AssociationProperty property)
{
var role = property.Role is not null ? "_" + property.Role.ToConstantCase() : string.Empty;
Expand Down

0 comments on commit 0a850aa

Please sign in to comment.