diff --git a/src/Storage/Repository/PgA2Repository.cs b/src/Storage/Repository/PgA2Repository.cs index 7b779b2d..58953e98 100644 --- a/src/Storage/Repository/PgA2Repository.cs +++ b/src/Storage/Repository/PgA2Repository.cs @@ -105,20 +105,31 @@ 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); - 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 +244,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" }; + } + } } }