From 03474062860715a49706eaf6e29e0efb31df2a43 Mon Sep 17 00:00:00 2001 From: Henning Normann Date: Fri, 4 Oct 2024 00:08:28 +0200 Subject: [PATCH 1/2] Support for missing language when fetching style sheet --- src/Storage/Repository/PgA2Repository.cs | 37 ++++++++++++++++++++---- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/src/Storage/Repository/PgA2Repository.cs b/src/Storage/Repository/PgA2Repository.cs index 7b779b2d..a7477f3b 100644 --- a/src/Storage/Repository/PgA2Repository.cs +++ b/src/Storage/Repository/PgA2Repository.cs @@ -110,15 +110,31 @@ public async Task CreateImage(string name, byte[] image) pgcom.Parameters.AddWithValue("_lformid", NpgsqlDbType.Integer, lformId); pgcom.Parameters.AddWithValue("_language", NpgsqlDbType.Text, language); pgcom.Parameters.AddWithValue("_xsltype", NpgsqlDbType.Integer, xslType); - using TelemetryTracker tracker = new(_telemetryClient, pgcom); - await using NpgsqlDataReader reader = await pgcom.ExecuteReaderAsync(); - while (await reader.ReadAsync()) + // Loop until language match + foreach (string languageToTry in GetOrderedLanguages(language)) { - xsls.Add((reader.GetFieldValue("xsl"), reader.GetFieldValue("isportrait"))); + pgcom.Parameters.Clear(); + pgcom.Parameters.AddWithValue("_org", NpgsqlDbType.Text, org); + pgcom.Parameters.AddWithValue("_app", NpgsqlDbType.Text, app); + pgcom.Parameters.AddWithValue("_lformid", NpgsqlDbType.Integer, lformId); + pgcom.Parameters.AddWithValue("_language", NpgsqlDbType.Text, languageToTry); + pgcom.Parameters.AddWithValue("_xsltype", NpgsqlDbType.Integer, xslType); + using TelemetryTracker tracker = new(_telemetryClient, pgcom); + + await using NpgsqlDataReader reader = await pgcom.ExecuteReaderAsync(); + while (await reader.ReadAsync()) + { + xsls.Add((reader.GetFieldValue("xsl"), reader.GetFieldValue("isportrait"))); + } + + tracker.Track(); + if (xsls.Count > 0) + { + return xsls; + } } - tracker.Track(); return xsls; } @@ -233,5 +249,16 @@ public async Task GetMigrationInstanceId(int a2ArchiveReference) tracker.Track(); return instanceId; } + + private static List GetOrderedLanguages(string language) + { + switch (language) + { + case "nb": return new List { "nb", "nn", "en" }; + case "nn": return new List { "nn", "nb", "en" }; + case "en": return new List { "en", "nb", "nn" }; + default: return new List { "nb", "nn", "en" }; + } + } } } From 1264296661b9e525231796331a262ad2f98563a6 Mon Sep 17 00:00:00 2001 From: Henning Normann Date: Fri, 4 Oct 2024 11:01:03 +0200 Subject: [PATCH 2/2] Removed unnecessary.parameter population --- src/Storage/Repository/PgA2Repository.cs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/src/Storage/Repository/PgA2Repository.cs b/src/Storage/Repository/PgA2Repository.cs index a7477f3b..58953e98 100644 --- a/src/Storage/Repository/PgA2Repository.cs +++ b/src/Storage/Repository/PgA2Repository.cs @@ -105,11 +105,6 @@ public async Task CreateImage(string name, byte[] image) List<(string Xsl, bool IsPortraitl)> xsls = []; await using NpgsqlCommand pgcom = _dataSource.CreateCommand(_readXslSql); - pgcom.Parameters.AddWithValue("_org", NpgsqlDbType.Text, org); - pgcom.Parameters.AddWithValue("_app", NpgsqlDbType.Text, app); - pgcom.Parameters.AddWithValue("_lformid", NpgsqlDbType.Integer, lformId); - pgcom.Parameters.AddWithValue("_language", NpgsqlDbType.Text, language); - pgcom.Parameters.AddWithValue("_xsltype", NpgsqlDbType.Integer, xslType); // Loop until language match foreach (string languageToTry in GetOrderedLanguages(language))