Skip to content

Commit

Permalink
[JPA] Fix multiple :
Browse files Browse the repository at this point in the history
- Erreur de compilation dans le cas d'alias vers des associations
- Correction javadoc lorsqu'une classe non accessible est utilisée
  • Loading branch information
gideruette committed Jul 29, 2024
1 parent b54cab3 commit eb6f4ef
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 9 deletions.
10 changes: 8 additions & 2 deletions TopModel.Core/Model/AliasProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,15 @@ public string Name
: (_name?.ToPascalCase() ?? _property?.NamePascal))
+ (Suffix ?? string.Empty);

public string NameByClassPascal => NamePascal;
public string NameByClassPascal => Class.IsPersistent ? (Prefix?.ToFirstUpper() ?? string.Empty)
+ (_name?.ToPascalCase() ?? _property?.NameByClassPascal)
+ (Suffix ?? string.Empty) : NamePascal;

public string NameByClassCamel => NameCamel;
public string NameByClassCamel => Class.IsPersistent ? (Prefix?.ToFirstLower() ?? string.Empty)
+ (string.IsNullOrWhiteSpace(Prefix)
? (_name?.ToCamelCase() ?? _property?.NameByClassCamel)
: (_name?.ToPascalCase() ?? _property?.NameByClassPascal))
+ (Suffix ?? string.Empty) : NameCamel;

public string? Label
{
Expand Down
11 changes: 8 additions & 3 deletions TopModel.Generator.Jpa/JpaMapperGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,16 @@ 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
|| propertySource is AliasProperty psAlp && psAlp.Property is AssociationProperty
|| propertyTarget is AssociationProperty
|| propertySource is AliasProperty ptAlp && ptAlp.Property is AssociationProperty))
{
getter = $"{sourceName}.{propertySource.NameByClassPascal.WithPrefix(getterPrefix)}()";
}
else if (propertySource.Class.IsPersistent && !propertyTarget.Class.IsPersistent && propertySource is AssociationProperty apSource && apSource.Association.IsPersistent)
else if (propertySource.Class.IsPersistent && (!propertyTarget.Class.IsPersistent || !(propertyTarget is AssociationProperty)) && (propertySource is AssociationProperty apSource && apSource.Association.IsPersistent || propertySource is AliasProperty alpSource && alpSource.Property is AssociationProperty apSource2 && apSource2.Association.IsPersistent))
{
apSource = propertySource is AssociationProperty ? (AssociationProperty)propertySource : (AssociationProperty)((AliasProperty)propertySource).Property;
checkSourceNull = true;
if (propertyTarget is CompositionProperty cp)
{
Expand Down Expand Up @@ -160,8 +164,9 @@ protected override void HandleFile(string fileName, string tag, IList<(Class Cla
}
}
}
else if (!propertySource.Class.IsPersistent && propertyTarget.Class.IsPersistent && propertyTarget is AssociationProperty apTarget && apTarget.Association.IsPersistent)
else if ((!propertySource.Class.IsPersistent || !(propertySource is AssociationProperty)) && propertyTarget.Class.IsPersistent && (propertyTarget is AssociationProperty apTarget && apTarget.Association.IsPersistent || propertyTarget is AliasProperty ptAp && ptAp.Property is AssociationProperty ptApAss && ptApAss.Association.IsPersistent))
{
apTarget = propertyTarget is AssociationProperty ? (AssociationProperty)propertyTarget : (AssociationProperty)((AliasProperty)propertyTarget).Property;
if (Config.CanClassUseEnums(apTarget.Property.Class))
{
if (!propertySource.Class.IsPersistent)
Expand Down
55 changes: 51 additions & 4 deletions TopModel.Generator.Jpa/JpaModelPropertyGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,9 @@ public void WriteProperty(JavaWriter fw, Class classe, IProperty property, strin
case AssociationProperty { Association.IsPersistent: true } ap:
WriteAssociationProperty(fw, classe, ap, tag);
break;
case AliasProperty alp:
WriteAliasProperty(fw, classe, alp, tag);
break;
case IFieldProperty fp:
WriteIFieldProperty(fw, classe, fp, tag);
break;
Expand Down Expand Up @@ -325,10 +328,6 @@ private void WriteDomainAnnotations(JavaWriter fw, IFieldProperty property, stri
private void WriteIFieldProperty(JavaWriter fw, Class classe, IFieldProperty property, string tag)
{
var javaOrJakarta = _config.PersistenceMode.ToString().ToLower();
if (property is AliasProperty alp)
{
fw.WriteLine(1, $" * Alias of {{@link {alp.Property.Class.GetImport(_config, tag)}#get{alp.Property.NameCamel.ToFirstUpper()}() {alp.Property.Class.NamePascal}#get{alp.Property.NameCamel.ToFirstUpper()}()}} ");
}

fw.WriteDocEnd(1);
if (property.PrimaryKey && classe.IsPersistent)
Expand Down Expand Up @@ -363,6 +362,54 @@ private void WriteIFieldProperty(JavaWriter fw, Class classe, IFieldProperty pro
fw.WriteLine(1, $"private {_config.GetType(property, useClassForAssociation: useClassForAssociation)} {(isAssociationNotPersistent ? property.NameCamel : property.NameByClassCamel)}{suffix};");
}

private void WriteAliasProperty(JavaWriter fw, Class classe, AliasProperty property, string tag)
{
if (_classes.Contains(property.Property.Class))
{
fw.WriteLine(1, $" * Alias of {{@link {property.Property.Class.GetImport(_config, tag)}#get{property.Property.NameCamel.ToFirstUpper()}() {property.Property.Class.NamePascal}#get{property.Property.NameCamel.ToFirstUpper()}()}} ");
}

fw.WriteDocEnd(1);
var javaOrJakarta = _config.PersistenceMode.ToString().ToLower();
var shouldWriteAssociation = classe.IsPersistent && property.Property is AssociationProperty;

if (property.PrimaryKey && classe.IsPersistent)
{
WriteIdAnnotation(fw, classe, property);
}

if (!shouldWriteAssociation && ShouldWriteColumnAnnotation(classe, property) && (_config.UseJdbc || !(property.PrimaryKey && classe.PrimaryKey.Count() > 1)))
{
WriteColumnAnnotation(fw, property, 1);
}

if (shouldWriteAssociation)
{
WriteAssociationAnnotations(fw, classe, (AssociationProperty)property.Property, 1);
}

if (property.Required && !property.PrimaryKey && (!classe.IsPersistent || _config.UseJdbc))
{
WriteValidationAnnotations(fw, javaOrJakarta);
}

if (_config.CanClassUseEnums(property.Property.Class) && property.Property.PrimaryKey && classe.IsPersistent && !_config.UseJdbc)
{
WriteEnumAnnotation(fw, javaOrJakarta);
}

if (!property.PrimaryKey || classe.PrimaryKey.Count() <= 1)
{
WriteDomainAnnotations(fw, property, tag, 1);
}

var defaultValue = _config.GetValue(property, _classes);
var suffix = defaultValue != "null" ? $" = {defaultValue}" : string.Empty;
var isAssociationNotPersistent = property.Property is AssociationProperty ap && !ap.Association.IsPersistent;
var useClassForAssociation = classe.IsPersistent && !isAssociationNotPersistent && !_config.UseJdbc;
fw.WriteLine(1, $"private {_config.GetType(property, useClassForAssociation: useClassForAssociation)} {(isAssociationNotPersistent && !shouldWriteAssociation ? property.NameCamel : property.NameByClassCamel)}{suffix};");
}

private void WriteIdAnnotation(JavaWriter fw, Class classe, IFieldProperty property)
{
var javaOrJakarta = _config.PersistenceMode.ToString().ToLower();
Expand Down

0 comments on commit eb6f4ef

Please sign in to comment.