Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for missing language when fetching style sheet #518

Merged
merged 2 commits into from
Oct 4, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 32 additions & 10 deletions src/Storage/Repository/PgA2Repository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

private readonly NpgsqlDataSource _dataSource;
private readonly TelemetryClient _telemetryClient;
private readonly ILogger<PgA2Repository> _logger;

Check warning on line 35 in src/Storage/Repository/PgA2Repository.cs

View workflow job for this annotation

GitHub Actions / Build, test & analyze

Remove this unread private field '_logger' or refactor the code to use its value. (https://rules.sonarsource.com/csharp/RSPEC-4487)

/// <summary>
/// Initializes a new instance of the <see cref="PgA2Repository"/> class.
Expand All @@ -59,7 +59,7 @@
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);

Check warning on line 62 in src/Storage/Repository/PgA2Repository.cs

View workflow job for this annotation

GitHub Actions / Build, test & analyze

Define a constant instead of using this literal '_language' 4 times. (https://rules.sonarsource.com/csharp/RSPEC-1192)
pgcom.Parameters.AddWithValue("_pagenumber", NpgsqlDbType.Integer, pageNumber);
pgcom.Parameters.AddWithValue("_xsl", NpgsqlDbType.Text, xsl);
pgcom.Parameters.AddWithValue("_xsltype", NpgsqlDbType.Integer, xslType);
Expand All @@ -75,7 +75,7 @@
public async Task CreateCodelist(string name, string language, int version, string codelist)
{
await using NpgsqlCommand pgcom = _dataSource.CreateCommand(_insertCodelistSql);
pgcom.Parameters.AddWithValue("_name", NpgsqlDbType.Text, name);

Check warning on line 78 in src/Storage/Repository/PgA2Repository.cs

View workflow job for this annotation

GitHub Actions / Build, test & analyze

Define a constant instead of using this literal '_name' 4 times. (https://rules.sonarsource.com/csharp/RSPEC-1192)
pgcom.Parameters.AddWithValue("_language", NpgsqlDbType.Text, language);
pgcom.Parameters.AddWithValue("_version", NpgsqlDbType.Integer, version);
pgcom.Parameters.AddWithValue("_codelist", NpgsqlDbType.Text, codelist);
Expand Down Expand Up @@ -105,20 +105,31 @@
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<string>("xsl"), reader.GetFieldValue<bool>("isportrait")));
pgcom.Parameters.Clear();
HenningNormann marked this conversation as resolved.
Show resolved Hide resolved
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<string>("xsl"), reader.GetFieldValue<bool>("isportrait")));

Check warning on line 123 in src/Storage/Repository/PgA2Repository.cs

View workflow job for this annotation

GitHub Actions / Build, test & analyze

Await GetFieldValueAsync instead. (https://rules.sonarsource.com/csharp/RSPEC-6966)
HenningNormann marked this conversation as resolved.
Show resolved Hide resolved
}

tracker.Track();
if (xsls.Count > 0)
{
return xsls;
}
}

tracker.Track();
return xsls;
}

Expand Down Expand Up @@ -233,5 +244,16 @@
tracker.Track();
return instanceId;
}

private static List<string> GetOrderedLanguages(string language)
{
switch (language)
{
case "nb": return new List<string> { "nb", "nn", "en" };
case "nn": return new List<string> { "nn", "nb", "en" };
case "en": return new List<string> { "en", "nb", "nn" };
default: return new List<string> { "nb", "nn", "en" };
}
}
}
}
Loading