From a299fa5d49e6eeb2df38be830b038f9b1e1b34f1 Mon Sep 17 00:00:00 2001 From: Tim Haasdyk Date: Thu, 16 Jan 2025 15:03:26 +0100 Subject: [PATCH] Sort complex forms alphabetically --- .../Api/UpdateProxy/UpdateEntryProxy.cs | 11 +---- ...elSnapshotTests.VerifyDbModel.verified.txt | 2 +- backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs | 8 +++- backend/FwLite/LcmCrdt/QueryHelpers.cs | 42 +++++++++++++++---- backend/FwLite/MiniLcm/Models/Entry.cs | 2 +- .../FwLite/MiniLcm/Models/WritingSystemId.cs | 2 + 6 files changed, 46 insertions(+), 21 deletions(-) diff --git a/backend/FwLite/FwDataMiniLcmBridge/Api/UpdateProxy/UpdateEntryProxy.cs b/backend/FwLite/FwDataMiniLcmBridge/Api/UpdateProxy/UpdateEntryProxy.cs index 0972893ad..629552bf4 100644 --- a/backend/FwLite/FwDataMiniLcmBridge/Api/UpdateProxy/UpdateEntryProxy.cs +++ b/backend/FwLite/FwDataMiniLcmBridge/Api/UpdateProxy/UpdateEntryProxy.cs @@ -57,16 +57,9 @@ public override List Components set => throw new NotImplementedException(); } - public override IList ComplexForms + public override List ComplexForms { - get => - new UpdateListProxy( - component => _lexboxLcmApi.AddComplexFormComponent(_lexboxLcmApi.EntriesRepository.GetObject(component.ComplexFormEntryId), component), - component => _lexboxLcmApi.RemoveComplexFormComponent(_lexboxLcmApi.EntriesRepository.GetObject(component.ComplexFormEntryId), component), - //todo this does not handle complex forms which reference a sense - i => _lazyComplexForms.Value[i], - _lazyComplexForms.Value.Length - ); + get => throw new NotImplementedException(); set => throw new NotImplementedException(); } diff --git a/backend/FwLite/LcmCrdt.Tests/DataModelSnapshotTests.VerifyDbModel.verified.txt b/backend/FwLite/LcmCrdt.Tests/DataModelSnapshotTests.VerifyDbModel.verified.txt index b48c54f32..ffe1be224 100644 --- a/backend/FwLite/LcmCrdt.Tests/DataModelSnapshotTests.VerifyDbModel.verified.txt +++ b/backend/FwLite/LcmCrdt.Tests/DataModelSnapshotTests.VerifyDbModel.verified.txt @@ -97,7 +97,7 @@ Relational:ColumnType: jsonb SnapshotId (no field, Guid?) Shadow FK Index Navigations: - ComplexForms (IList) Collection ToDependent ComplexFormComponent + ComplexForms (List) Collection ToDependent ComplexFormComponent Components (List) Collection ToDependent ComplexFormComponent Senses (List) Collection ToDependent Sense Keys: diff --git a/backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs b/backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs index 658f6cefc..cba5e73ef 100644 --- a/backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs +++ b/backend/FwLite/LcmCrdt/CrdtMiniLcmApi.cs @@ -305,7 +305,7 @@ private async IAsyncEnumerable GetEntries( var entries = queryable.AsAsyncEnumerable(); await foreach (var entry in entries) { - entry.ApplySortOrder(); + entry.ApplySortOrder(sortWs); yield return entry; } } @@ -320,7 +320,11 @@ private async IAsyncEnumerable GetEntries( .LoadWith(e => e.Components) .AsQueryable() .SingleOrDefaultAsync(e => e.Id == id); - entry?.ApplySortOrder(); + if (entry is not null) + { + var sortWs = await GetWritingSystem(WritingSystemId.Default, WritingSystemType.Vernacular); + entry.ApplySortOrder(sortWs); + } return entry; } diff --git a/backend/FwLite/LcmCrdt/QueryHelpers.cs b/backend/FwLite/LcmCrdt/QueryHelpers.cs index a06f33bf4..e91038f96 100644 --- a/backend/FwLite/LcmCrdt/QueryHelpers.cs +++ b/backend/FwLite/LcmCrdt/QueryHelpers.cs @@ -1,11 +1,15 @@ +using System.Globalization; + namespace LcmCrdt; public static class QueryHelpers { - public static void ApplySortOrder(this Entry entry) + public static void ApplySortOrder(this Entry entry, WritingSystem? sortWs) { + var sortCompareInfo = GetCompareInfo(sortWs); entry.Senses.ApplySortOrder(); entry.Components.ApplySortOrder(); + entry.ComplexForms.Sort((a, b) => ComplexFormsComparison(a, b, sortCompareInfo)); foreach (var sense in entry.Senses) { sense.ApplySortOrder(); @@ -19,14 +23,36 @@ public static void ApplySortOrder(this Sense sense) private static void ApplySortOrder(this List items) where T : IOrderable { - items.Sort((x, y) => + items.Sort((a, b) => { - var result = x.Order.CompareTo(y.Order); - if (result == 0) - { - result = x.Id.CompareTo(y.Id); - } - return result; + var result = a.Order.CompareTo(b.Order); + if (result != 0) return result; + return a.Id.CompareTo(b.Id); }); } + + private static int ComplexFormsComparison(ComplexFormComponent a, ComplexFormComponent b, CompareInfo compareInfo) + { + var result = compareInfo.Compare(a.ComplexFormHeadword, b.ComplexFormHeadword, CompareOptions.IgnoreCase); + if (result != 0) return result; + return a.ComplexFormEntryId.CompareTo(b.ComplexFormEntryId); + } + + private static CompareInfo GetCompareInfo(WritingSystem? writingSystem) + { + if (writingSystem == null || writingSystem.WsId == WritingSystemId.Default) + { + return CultureInfo.InvariantCulture.CompareInfo; + } + + try + { + //todo use ICU/SLDR instead + return CultureInfo.CreateSpecificCulture(writingSystem.WsId.Code).CompareInfo; + } + catch + { + return CultureInfo.InvariantCulture.CompareInfo; + } + } } diff --git a/backend/FwLite/MiniLcm/Models/Entry.cs b/backend/FwLite/MiniLcm/Models/Entry.cs index 8eb0becf5..c5f1f0ace 100644 --- a/backend/FwLite/MiniLcm/Models/Entry.cs +++ b/backend/FwLite/MiniLcm/Models/Entry.cs @@ -22,7 +22,7 @@ public record Entry : IObjectWithId /// /// This entry is a part of these complex forms /// - public virtual IList ComplexForms { get; set; } = []; + public virtual List ComplexForms { get; set; } = []; public virtual IList ComplexFormTypes { get; set; } = []; diff --git a/backend/FwLite/MiniLcm/Models/WritingSystemId.cs b/backend/FwLite/MiniLcm/Models/WritingSystemId.cs index c12509fbb..3d132ea24 100644 --- a/backend/FwLite/MiniLcm/Models/WritingSystemId.cs +++ b/backend/FwLite/MiniLcm/Models/WritingSystemId.cs @@ -31,6 +31,8 @@ public override void WriteAsPropertyName(Utf8JsonWriter writer, WritingSystemId { public string Code { get; init; } + public static readonly WritingSystemId Default = "default"; + public WritingSystemId(string code) { //__key is used by the LfClassicMiniLcmApi to smuggle non guid ids with possibilitie lists