Skip to content

Commit

Permalink
Merge pull request #363 from klee-contrib/gideruette/issue197
Browse files Browse the repository at this point in the history
[JPA] Supprimer la règle qui empêche de faire des associations dans des classes non persistées
  • Loading branch information
gideruette authored Jun 19, 2024
2 parents 31f4df6 + a868be8 commit e18eb85
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 36 deletions.
1 change: 1 addition & 0 deletions TopModel.Generator.Core/GeneratorUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ public static List<AssociationProperty> GetReverseProperties(this Class classe,
.OfType<AssociationProperty>()
.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))
Expand Down
2 changes: 1 addition & 1 deletion TopModel.Generator.Jpa/ImportsJpaExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ private static List<string> GetTypeImports(this AssociationProperty ap, JpaConfi
var imports = new List<string>();

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));
}
Expand Down
26 changes: 17 additions & 9 deletions TopModel.Generator.Jpa/JpaMapperGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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))
{
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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) {{");
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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)
Expand Down
25 changes: 6 additions & 19 deletions TopModel.Generator.Jpa/JpaModelGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<AssociationProperty>())
{
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)
Expand Down Expand Up @@ -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))
{
Expand Down Expand Up @@ -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");
}
Expand Down Expand Up @@ -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)";
});

Expand Down
22 changes: 15 additions & 7 deletions TopModel.Generator.Jpa/JpaModelPropertyGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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}}}");
Expand All @@ -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)");
Expand Down Expand Up @@ -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, "}");
}
Expand Down Expand Up @@ -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
{
Expand Down Expand Up @@ -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};");
}
}

0 comments on commit e18eb85

Please sign in to comment.