From a868be8fb52feabbe890abae6d7f8aeaf7993995 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gild=C3=A9ric=20DERUETTE?= Date: Wed, 19 Jun 2024 18:45:40 +0200 Subject: [PATCH] =?UTF-8?q?[JPA]=20Supprimer=20la=20r=C3=A8gle=20qui=20emp?= =?UTF-8?q?=C3=AAche=20de=20faire=20des=20associations=20dans=20des=20clas?= =?UTF-8?q?ses=20non=20persist=C3=A9es=20Fixes=20#197?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- TopModel.Generator.Core/GeneratorUtils.cs | 1 + .../ImportsJpaExtensions.cs | 2 +- TopModel.Generator.Jpa/JpaMapperGenerator.cs | 26 ++++++++++++------- TopModel.Generator.Jpa/JpaModelGenerator.cs | 25 +++++------------- .../JpaModelPropertyGenerator.cs | 22 +++++++++++----- 5 files changed, 40 insertions(+), 36 deletions(-) diff --git a/TopModel.Generator.Core/GeneratorUtils.cs b/TopModel.Generator.Core/GeneratorUtils.cs index caf1ac40..427937f2 100644 --- a/TopModel.Generator.Core/GeneratorUtils.cs +++ b/TopModel.Generator.Core/GeneratorUtils.cs @@ -60,6 +60,7 @@ public static List GetReverseProperties(this Class classe, .OfType() .Where(p => p is not ReverseAssociationProperty) .Where(p => p.Type != AssociationType.OneToOne) + .Where(p => p.Class.IsPersistent) .Where(p => p.Association.PrimaryKey.Count() == 1 || p.Type == AssociationType.ManyToOne) .Where(p => p.Association == classe && (p.Type == AssociationType.OneToMany || p.Class.Namespace.RootModule == classe.Namespace.RootModule)) diff --git a/TopModel.Generator.Jpa/ImportsJpaExtensions.cs b/TopModel.Generator.Jpa/ImportsJpaExtensions.cs index a7000309..39b52e17 100644 --- a/TopModel.Generator.Jpa/ImportsJpaExtensions.cs +++ b/TopModel.Generator.Jpa/ImportsJpaExtensions.cs @@ -50,7 +50,7 @@ private static List GetTypeImports(this AssociationProperty ap, JpaConfi var imports = new List(); imports.AddRange(config.GetDomainImports(ap, tag)); - if (!config.UseJdbc && ap.Class != null) + if (!config.UseJdbc && ap.Class != null && ap.Association.IsPersistent) { imports.Add(ap.Association.GetImport(config, tag)); } diff --git a/TopModel.Generator.Jpa/JpaMapperGenerator.cs b/TopModel.Generator.Jpa/JpaMapperGenerator.cs index 149ea1c7..d8709744 100644 --- a/TopModel.Generator.Jpa/JpaMapperGenerator.cs +++ b/TopModel.Generator.Jpa/JpaMapperGenerator.cs @@ -101,11 +101,11 @@ protected override void HandleFile(string fileName, string tag, IList<(Class Cla var checkSourceNull = false; if ((!propertySource.Class.IsPersistent && !propertyTarget.Class.IsPersistent) - || !(propertySource is AssociationProperty || propertyTarget is AssociationProperty)) + || !(propertySource is AssociationProperty || propertyTarget is AssociationProperty)) { getter = $"{sourceName}.{propertySource.NameByClassPascal.WithPrefix(getterPrefix)}()"; } - else if (propertySource.Class.IsPersistent && !propertyTarget.Class.IsPersistent && propertySource is AssociationProperty apSource) + else if (propertySource.Class.IsPersistent && !propertyTarget.Class.IsPersistent && propertySource is AssociationProperty apSource && apSource.Association.IsPersistent) { checkSourceNull = true; if (propertyTarget is CompositionProperty cp) @@ -156,7 +156,7 @@ protected override void HandleFile(string fileName, string tag, IList<(Class Cla } } } - else if (!propertySource.Class.IsPersistent && propertyTarget.Class.IsPersistent && propertyTarget is AssociationProperty apTarget) + else if (!propertySource.Class.IsPersistent && propertyTarget.Class.IsPersistent && propertyTarget is AssociationProperty apTarget && apTarget.Association.IsPersistent) { if (Config.CanClassUseEnums(apTarget.Property.Class)) { @@ -220,7 +220,14 @@ protected override void HandleFile(string fileName, string tag, IList<(Class Cla } else { - getter = $"{sourceName}.{propertySource.NameByClassPascal.WithPrefix(getterPrefix)}()"; + if (propertySource is AssociationProperty ap && !ap.Association.IsPersistent) + { + getter = $"{sourceName}.{propertySource.NamePascal.WithPrefix(getterPrefix)}()"; + } + else + { + getter = $"{sourceName}.{propertySource.NameByClassPascal.WithPrefix(getterPrefix)}()"; + } } return (Getter: Config.GetConvertedValue( @@ -253,7 +260,8 @@ private void WriteFromMapper(Class classe, FromMapper mapper, JavaWriter fw, str fw.WriteReturns(1, $"Une nouvelle instance de '{classe.NamePascal}' ou bien l'instance passée en paramètres sur lesquels les champs sources ont été mappée"); fw.WriteDocEnd(1); - var entryParams = mapper.ClassParams.Select(p => $"{p.Class} {p.Name.ToCamelCase()}").Concat(mapper.PropertyParams.Select(p => $"{Config.GetType(p.Property, Classes)} {p.Property.NameCamel}")); + var useClassForAssociation = (IProperty p) => classe.IsPersistent && !Config.UseJdbc && p is AssociationProperty ap && ap.Association.IsPersistent; + var entryParams = mapper.ClassParams.Select(p => $"{p.Class} {p.Name.ToCamelCase()}").Concat(mapper.PropertyParams.Select(p => $"{Config.GetType(p.Property, Classes, useClassForAssociation: useClassForAssociation(p.Property))} {p.Property.NameCamel}")); var entryParamImports = mapper.PropertyParams.Select(p => p.Property.GetTypeImports(Config, tag)).SelectMany(p => p); fw.AddImports(entryParamImports.ToList()); fw.WriteLine(1, $"public static {classe.NamePascal} create{classe.NamePascal}({string.Join(", ", entryParams)}, {classe.NamePascal} target) {{"); @@ -292,7 +300,7 @@ private void WriteFromMapper(Class classe, FromMapper mapper, JavaWriter fw, str { if (param.Property.Required && !classe.Abstract) { - if (param.TargetProperty is AssociationProperty && classe.IsPersistent) + if (param.TargetProperty is AssociationProperty atg && atg.Association.IsPersistent && classe.IsPersistent) { continue; } @@ -320,7 +328,7 @@ private void WriteFromMapper(Class classe, FromMapper mapper, JavaWriter fw, str var propertySource = mapping.Value!; var getterPrefix = Config.GetType(propertyTarget!) == "boolean" ? "is" : "get"; var (getter, checkSourceNull) = GetSourceGetter(propertySource, propertyTarget, classe, fw, param.Name.ToCamelCase(), tag); - var propertyTargetName = Config.UseJdbc ? propertyTarget.NamePascal : propertyTarget.NameByClassPascal; + var propertyTargetName = Config.UseJdbc || propertyTarget is AssociationProperty ap && !ap.Association.IsPersistent ? propertyTarget.NamePascal : propertyTarget.NameByClassPascal; if (classe.Abstract) { if (!isFirst) @@ -371,7 +379,7 @@ private void WriteFromMapper(Class classe, FromMapper mapper, JavaWriter fw, str foreach (var param in mapper.PropertyParams) { var propertyTargetName = Config.UseJdbc ? param.TargetProperty.NamePascal : param.TargetProperty.NameByClassPascal; - if (param.TargetProperty is AssociationProperty && classe.IsPersistent) + if (param.TargetProperty is AssociationProperty apTg && apTg.Association.IsPersistent && classe.IsPersistent) { continue; } @@ -450,7 +458,7 @@ private void WriteToMapper(Class classe, ClassMappings mapper, JavaWriter fw, st var propertySource = mapping.Key; var getterPrefix = Config.GetType(propertyTarget!) == "boolean" ? "is" : "get"; var (getter, checkSourceNull) = GetSourceGetter(propertySource, propertyTarget!, classe, fw, "source", tag); - var propertyTargetName = Config.UseJdbc ? propertyTarget!.NamePascal : propertyTarget!.NameByClassPascal; + var propertyTargetName = Config.UseJdbc || propertyTarget is AssociationProperty asp && !asp.Association.IsPersistent ? propertyTarget!.NamePascal : propertyTarget!.NameByClassPascal; if (mapper.Class.Abstract) { if (!isFirst) diff --git a/TopModel.Generator.Jpa/JpaModelGenerator.cs b/TopModel.Generator.Jpa/JpaModelGenerator.cs index f357662b..74e334de 100644 --- a/TopModel.Generator.Jpa/JpaModelGenerator.cs +++ b/TopModel.Generator.Jpa/JpaModelGenerator.cs @@ -148,19 +148,6 @@ private void CheckClass(Class classe) throw new ModelException(property, $"La propriété ${property} persistée ne peut pas faire l'objet d'une composition dans la classe {classe.Name} car elle ne l'est pas"); } } - - foreach (var property in classe.GetProperties(AvailableClasses).OfType()) - { - if (!classe.IsPersistent) - { - throw new ModelException(classe, $"La classe {classe} est non persistée, elle ne peut donc pas être associée à {property.Association.Name}"); - } - - if (!property.Association.IsPersistent) - { - throw new ModelException(classe, $"La classe {property.Association} est non persistée, elle ne peut donc pas être associée à {classe.Name}"); - } - } } private void WriteAdders(JavaWriter fw, Class classe, string tag) @@ -312,7 +299,7 @@ private void WriteEnumShortcuts(JavaWriter fw, Class classe, string tag) } else { - var type = Config.GetType(ap, AvailableClasses, useClassForAssociation: classe.IsPersistent && !Config.UseJdbc).Split('<').First(); + var type = Config.GetType(ap, AvailableClasses, useClassForAssociation: classe.IsPersistent && !Config.UseJdbc && ap.Association.IsPersistent).Split('<').First(); if (_newableTypes.TryGetValue(type, out var newableType)) { @@ -348,7 +335,7 @@ private void WriteEnumShortcuts(JavaWriter fw, Class classe, string tag) } else { - var listOrSet = Config.GetType(ap, AvailableClasses, useClassForAssociation: classe.IsPersistent && !Config.UseJdbc).Split('<').First(); + var listOrSet = Config.GetType(ap, AvailableClasses, useClassForAssociation: classe.IsPersistent && !Config.UseJdbc && ap.Association.IsPersistent).Split('<').First(); fw.WriteLine(2, @$"return this.{ap.NameByClassCamel} != null ? this.{ap.NameByClassCamel}.stream().map({ap.Association.NamePascal}::get{ap.Property.NameCamel.ToFirstUpper()}).collect(Collectors.to{listOrSet}()) : null;"); fw.AddImport("java.util.stream.Collectors"); } @@ -385,17 +372,17 @@ private void WriteFieldsEnum(JavaWriter fw, Class classe, string tag) var props = classe.GetProperties(AvailableClasses).Select(prop => { string name; - if (prop is AssociationProperty ap && !Config.UseJdbc) + if (prop is AssociationProperty ap && ap.Association.IsPersistent && !Config.UseJdbc) { name = ap.NameByClassCamel.ToConstantCase(); } else { - name = prop.Name.ToConstantCase(); + name = prop.NameCamel.ToConstantCase(); } - var javaType = Config.GetType(prop, useClassForAssociation: classe.IsPersistent && !Config.UseJdbc); - javaType = javaType.Split("<").First(); + var javaType = Config.GetType(prop, useClassForAssociation: classe.IsPersistent && !Config.UseJdbc && prop is AssociationProperty asp && asp.Association.IsPersistent); + javaType = javaType.Split("<")[0]; return $" {name}({javaType}.class)"; }); diff --git a/TopModel.Generator.Jpa/JpaModelPropertyGenerator.cs b/TopModel.Generator.Jpa/JpaModelPropertyGenerator.cs index 71c0dc4a..f6f4d46b 100644 --- a/TopModel.Generator.Jpa/JpaModelPropertyGenerator.cs +++ b/TopModel.Generator.Jpa/JpaModelPropertyGenerator.cs @@ -64,7 +64,8 @@ public void WriteCompositePrimaryKeyClass(JavaWriter fw, Class classe, string ta public void WriteGetter(JavaWriter fw, Class classe, string tag, IProperty property, int indentLevel = 1) { - var propertyName = _config.UseJdbc ? property.NameCamel : property.NameByClassCamel; + var isAssociationNotPersistent = property is AssociationProperty apr && !apr.Association.IsPersistent; + var propertyName = _config.UseJdbc || isAssociationNotPersistent ? property.NameCamel : property.NameByClassCamel; fw.WriteLine(); fw.WriteDocStart(indentLevel, $"Getter for {propertyName}"); fw.WriteReturns(indentLevel, $"value of {{@link {classe.GetImport(_config, tag)}#{propertyName} {propertyName}}}"); @@ -77,10 +78,11 @@ public void WriteGetter(JavaWriter fw, Class classe, string tag, IProperty prope getterName = propertyName.ToFirstUpper().WithPrefix(getterPrefix); } - fw.WriteLine(indentLevel, @$"public {_config.GetType(property, useClassForAssociation: classe.IsPersistent && !_config.UseJdbc)} {getterName}() {{"); + var useClassForAssociation = classe.IsPersistent && !isAssociationNotPersistent && !_config.UseJdbc; + fw.WriteLine(indentLevel, @$"public {_config.GetType(property, useClassForAssociation: useClassForAssociation)} {getterName}() {{"); if (property is AssociationProperty ap && ap.Type.IsToMany()) { - var type = _config.GetType(ap, _classes, useClassForAssociation: classe.IsPersistent && !_config.UseJdbc).Split('<').First(); + var type = _config.GetType(ap, _classes, useClassForAssociation: useClassForAssociation).Split('<').First(); if (_newableTypes.TryGetValue(type, out var newableType)) { fw.WriteLine(indentLevel + 1, $"if(this.{propertyName} == null)"); @@ -128,12 +130,14 @@ 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; + var isAssociationNotPersistent = property is AssociationProperty apr && !apr.Association.IsPersistent; + var propertyName = _config.UseJdbc || isAssociationNotPersistent ? 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}) {{"); + var useClassForAssociation = classe.IsPersistent && !isAssociationNotPersistent && !_config.UseJdbc; + fw.WriteLine(indentLevel, @$"public void {propertyName.WithPrefix("set")}({_config.GetType(property, useClassForAssociation: useClassForAssociation)} {propertyName}) {{"); fw.WriteLine(indentLevel + 1, @$"this.{propertyName} = {propertyName};"); fw.WriteLine(indentLevel, "}"); } @@ -255,7 +259,9 @@ private void WriteProperty(JavaWriter fw, Class classe, AssociationProperty prop fw.WriteLine(1, "@Id"); } - fw.WriteLine(1, $"private {_config.GetType(property, useClassForAssociation: classe.IsPersistent)} {property.NameByClassCamel}{suffix};"); + var isAssociationNotPersistent = !property.Association.IsPersistent; + var useClassForAssociation = classe.IsPersistent && !isAssociationNotPersistent && !_config.UseJdbc; + fw.WriteLine(1, $"private {_config.GetType(property, useClassForAssociation: useClassForAssociation)} {property.NameByClassCamel}{suffix};"); } else { @@ -380,6 +386,8 @@ private void WriteProperty(JavaWriter fw, Class classe, IFieldProperty property, var defaultValue = _config.GetValue(property, _classes); var suffix = defaultValue != "null" ? $" = {defaultValue}" : string.Empty; - fw.WriteLine(1, $"private {_config.GetType(property, useClassForAssociation: classe.IsPersistent && !_config.UseJdbc)} {property.NameByClassCamel}{suffix};"); + var isAssociationNotPersistent = property is AssociationProperty ap && !ap.Association.IsPersistent; + var useClassForAssociation = classe.IsPersistent && !isAssociationNotPersistent && !_config.UseJdbc; + fw.WriteLine(1, $"private {_config.GetType(property, useClassForAssociation: useClassForAssociation)} {(isAssociationNotPersistent ? property.NameCamel : property.NameByClassCamel)}{suffix};"); } }