diff --git a/TopModel.Generator.Jpa/JpaModelGenerator.cs b/TopModel.Generator.Jpa/JpaModelGenerator.cs index 93bcc4ce..3424b5f9 100644 --- a/TopModel.Generator.Jpa/JpaModelGenerator.cs +++ b/TopModel.Generator.Jpa/JpaModelGenerator.cs @@ -47,7 +47,7 @@ private JpaModelPropertyGenerator JpaModelPropertyGenerator { get { - _jpaModelPropertyGenerator ??= new JpaModelPropertyGenerator(Config, Classes); + _jpaModelPropertyGenerator ??= new JpaModelPropertyGenerator(Config, Classes, _newableTypes); return _jpaModelPropertyGenerator; } } @@ -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) { @@ -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); } } @@ -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); } } diff --git a/TopModel.Generator.Jpa/JpaModelPropertyGenerator.cs b/TopModel.Generator.Jpa/JpaModelPropertyGenerator.cs index e06e041d..94080374 100644 --- a/TopModel.Generator.Jpa/JpaModelPropertyGenerator.cs +++ b/TopModel.Generator.Jpa/JpaModelPropertyGenerator.cs @@ -12,14 +12,16 @@ public class JpaModelPropertyGenerator { private readonly IEnumerable _classes; private readonly JpaConfig _config; + private readonly Dictionary _newableTypes; - public JpaModelPropertyGenerator(JpaConfig config, IEnumerable classes) + public JpaModelPropertyGenerator(JpaConfig config, IEnumerable classes, Dictionary 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) { @@ -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;"); @@ -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); @@ -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;