Skip to content

Commit

Permalink
Merge pull request #249 from stevetemple/feature/numeric-ids-in-pickers
Browse files Browse the repository at this point in the history
Feature/numeric ids in pickers
  • Loading branch information
KevinJump authored Nov 23, 2023
2 parents 4023271 + 97a3dc9 commit 96b3210
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 49 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,61 +6,71 @@

namespace uSync.Migrations.Migrators.Community.GibeLinkPicker
{
[SyncMigrator("Gibe.LinkPicker")]
public class GibeLinkPickerToMultiUrlPickerMigrator : SyncPropertyMigratorBase
{
public override string GetEditorAlias(SyncMigrationDataTypeProperty dataTypeProperty, SyncMigrationContext context)
=> Constants.PropertyEditors.Aliases.MultiUrlPicker;
[SyncMigrator("Gibe.LinkPicker")]
public class GibeLinkPickerToMultiUrlPickerMigrator : SyncPropertyMigratorBase
{
public override string GetEditorAlias(SyncMigrationDataTypeProperty dataTypeProperty, SyncMigrationContext context)
=> UmbConstants.PropertyEditors.Aliases.MultiUrlPicker;

public override string GetContentValue(SyncMigrationContentProperty contentProperty, SyncMigrationContext context)
{
if (contentProperty?.Value == null)
{
return contentProperty?.Value;
}
public override object? GetConfigValues(SyncMigrationDataTypeProperty dataTypeProperty, SyncMigrationContext context)
{
var config = new MultiUrlPickerConfiguration
{
MaxNumber = 1
};
return config;
}

var gibeLinkPickers = GetPickerValues(contentProperty.Value).ToArray();
if (!gibeLinkPickers.Any())
{
return contentProperty.Value;
}
public override string GetContentValue(SyncMigrationContentProperty contentProperty, SyncMigrationContext context)
{
if (contentProperty?.Value == null)
{
return contentProperty?.Value;
}

var links = new List<MultiUrlPickerValueEditor.LinkDto>();
var gibeLinkPickers = GetPickerValues(contentProperty.Value).ToArray();

foreach (var picker in gibeLinkPickers)
{
var link = new MultiUrlPickerValueEditor.LinkDto
{
Name = picker?.Name ?? string.Empty,
Url = picker?.Url ?? string.Empty,
};
if (!gibeLinkPickers.Any())
{
return contentProperty.Value;
}

if (picker?.Target == "_blank")
{
link.Target = picker.Target;
}
var links = new List<MultiUrlPickerValueEditor.LinkDto>();

links.Add(link);
}
return JsonConvert.SerializeObject(links, Formatting.Indented);
}

private IEnumerable<GibeLinkPickerData> GetPickerValues(string? contentValue)
{
if (contentValue == null)
{
return Enumerable.Empty<GibeLinkPickerData>();
}
foreach (var picker in gibeLinkPickers)
{
var link = new MultiUrlPickerValueEditor.LinkDto
{
Name = picker?.Name ?? string.Empty,
Url = picker?.Url ?? string.Empty,
Udi = picker?.Uid != null ? new GuidUdi(UmbConstants.UdiEntityType.Document, Guid.Parse(picker.Uid)) : null,
};

if (contentValue.StartsWith("["))
{
return JsonConvert.DeserializeObject<IEnumerable<GibeLinkPickerData>>(contentValue)
?? Enumerable.Empty<GibeLinkPickerData>();
}
return JsonConvert.DeserializeObject<GibeLinkPickerData>(contentValue)?.AsEnumerableOfOne()
?? Enumerable.Empty<GibeLinkPickerData>();
}
}
if (picker?.Target == "_blank")
{
link.Target = picker.Target;
}

links.Add(link);
}
return JsonConvert.SerializeObject(links, Formatting.Indented);
}

private IEnumerable<GibeLinkPickerData> GetPickerValues(string? contentValue)
{
if (contentValue == null)
{
return Enumerable.Empty<GibeLinkPickerData>();
}

if (contentValue.StartsWith("["))
{
return JsonConvert.DeserializeObject<IEnumerable<GibeLinkPickerData>>(contentValue)
?? Enumerable.Empty<GibeLinkPickerData>();
}
return JsonConvert.DeserializeObject<GibeLinkPickerData>(contentValue)?.AsEnumerableOfOne()
?? Enumerable.Empty<GibeLinkPickerData>();
}
}
}

10 changes: 10 additions & 0 deletions uSync.Migrations.Migrators/Core/ContentPickerMigrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,16 @@ public override string GetDatabaseType(SyncMigrationDataTypeProperty dataTypePro
if (Guid.TryParse(contentProperty.Value, out var guid))
{
return new GuidUdi(UmbConstants.UdiEntityType.Document, guid).ToString();
}

// Really old pickers might have numeric ids
if (int.TryParse(contentProperty.Value, out var id))
{
var possibleGuid = context.GetKey(id);
if (possibleGuid != Guid.Empty)
{
return new GuidUdi(UmbConstants.UdiEntityType.Document, possibleGuid).ToString();
}
}

return base.GetContentValue(contentProperty, context);
Expand Down
6 changes: 6 additions & 0 deletions uSync.Migrations.Migrators/Core/MediaPickerMigrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,12 @@ public override string GetDatabaseType(SyncMigrationDataTypeProperty dataTypePro
guid = udi.Guid;
}

// Really old pickers might have numeric ids
if (guid.Equals(Guid.Empty) && int.TryParse(image, out var id))
{
guid = context.GetKey(id);
}

if (guid.Equals(Guid.Empty) == false)
{
media.Add(new MediaWithCropsDto
Expand Down
10 changes: 10 additions & 0 deletions uSync.Migrations.Migrators/Core/MultiNodeTreePickerMigrator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ public override string GetDatabaseType(SyncMigrationDataTypeProperty dataTypePro
else if (UdiParser.TryParse<GuidUdi>(item, out var udi) == true)
{
values.Add(udi);
}
else if (int.TryParse(item, out var id) == true)
{
// Really old editors might have numeric ids
var possibleGuid = context.GetKey(id);
if (possibleGuid != Guid.Empty)
{
// TODO: Eeek! This might possibly be content, media or member! [LK]
values.Add(Udi.Create(UmbConstants.UdiEntityType.Document, possibleGuid));
}
}
}
}
Expand Down

0 comments on commit 96b3210

Please sign in to comment.