Skip to content

feat(px-datasource): Always make sure that there is a contents variable #378

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

Merged
merged 3 commits into from
May 28, 2025
Merged
Show file tree
Hide file tree
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
68 changes: 68 additions & 0 deletions PxWeb/Code/Api2/DataSource/PxFile/PxFileBuilder2.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
using System.Globalization;

using PCAxis.Paxiom;

namespace PxWeb.Code.Api2.DataSource.PxFile
{
internal class PxFileBuilder2 : PXFileBuilder
{
public override bool BuildForSelection()
{
var meta = base.Model.Meta;
var result = base.BuildForSelection();
if (meta.ContentVariable is null)
{
// If there is no content variable, we create one.

try
{
// The name of the content variable is localized based on the language of the metadata.
var name = PCAxis.Paxiom.Localization.PxResourceManager.GetResourceManager()
.GetString("ApiContentsVariableName", new CultureInfo(meta.Language));

// Create the content variable
var contentVariable = new Variable(name, "CONTENTS", PlacementType.Stub, meta.NumberOfLanguages);
contentVariable.IsContentVariable = true;

// Create the value for the content variable from the meta contents
var value = new Value(meta.Contents, meta.NumberOfLanguages);
PaxiomUtil.SetCode(value, "content");
// Move the contentInfo from tablelevel to the content value
value.ContentInfo = meta.ContentInfo;
meta.ContentInfo = null;
contentVariable.Values.Add(value);

// Insert the content variable as the first variable in the stub
meta.Stub.Insert(0, contentVariable);
meta.Variables.Insert(0, contentVariable);
meta.ContentVariable = contentVariable;

// Add text and ContentInfo for all languages
var languages = meta.GetAllLanguages();
var currentLanguage = meta.CurrentLanguage;
for (int i = 0; i < languages.Length; i++)
{
var lang = languages[i];

meta.SetLanguage(i);

// Add the label for each language
contentVariable.Name = PCAxis.Paxiom.Localization.PxResourceManager.GetResourceManager()
.GetString("ApiContentsVariableName", new CultureInfo(lang));

// Set the value text in diffrent languages
value.Value = meta.Contents;
}
meta.SetLanguage(currentLanguage);
}
catch (Exception)
{
result = false;
}
}

return result;

}
}
}
2 changes: 1 addition & 1 deletion PxWeb/Code/Api2/DataSource/PxFile/PxFileDataSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
{
public class PxFileDataSource : IDataSource
{
private readonly IPxFileConfigurationService _pxFileConfigurationService;

Check warning on line 18 in PxWeb/Code/Api2/DataSource/PxFile/PxFileDataSource.cs

View workflow job for this annotation

GitHub Actions / build

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

Check warning on line 18 in PxWeb/Code/Api2/DataSource/PxFile/PxFileDataSource.cs

View workflow job for this annotation

GitHub Actions / build

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

Check warning on line 18 in PxWeb/Code/Api2/DataSource/PxFile/PxFileDataSource.cs

View workflow job for this annotation

GitHub Actions / build

Remove this unread private field '_pxFileConfigurationService' or refactor the code to use its value. (https://rules.sonarsource.com/csharp/RSPEC-4487)
private readonly IItemSelectionResolver _itemSelectionResolver;
private readonly ITablePathResolver _tablePathResolver;
private readonly IPxHost _hostingEnvironment;
Expand All @@ -38,7 +38,7 @@
/// <returns>Builder object, null if builder could not be created</returns>
public IPXModelBuilder? CreateBuilder(string id, string language)
{
var builder = new PCAxis.Paxiom.PXFileBuilder();
var builder = new PxFileBuilder2();

var path = _tablePathResolver.Resolve(language, id, out bool selectionExists);

Expand Down Expand Up @@ -103,7 +103,7 @@
menu.SetCurrentItemBySelection(itmSel.Menu, itmSel.Selection);

// Fix selection for subitems - we only want the last part...
if (menu.CurrentItem is PxMenuItem)

Check warning on line 106 in PxWeb/Code/Api2/DataSource/PxFile/PxFileDataSource.cs

View workflow job for this annotation

GitHub Actions / build

Replace this type-check-and-cast sequence to use pattern matching. (https://rules.sonarsource.com/csharp/RSPEC-3247)

Check warning on line 106 in PxWeb/Code/Api2/DataSource/PxFile/PxFileDataSource.cs

View workflow job for this annotation

GitHub Actions / build

Replace this type-check-and-cast sequence to use pattern matching. (https://rules.sonarsource.com/csharp/RSPEC-3247)

Check warning on line 106 in PxWeb/Code/Api2/DataSource/PxFile/PxFileDataSource.cs

View workflow job for this annotation

GitHub Actions / build

Replace this type-check-and-cast sequence to use pattern matching. (https://rules.sonarsource.com/csharp/RSPEC-3247)
{
foreach (var item in ((PxMenuItem)(menu.CurrentItem)).SubItems)
{
Expand Down Expand Up @@ -154,7 +154,7 @@

}

private string GetIdentifierWithoutPath(string id)

Check warning on line 157 in PxWeb/Code/Api2/DataSource/PxFile/PxFileDataSource.cs

View workflow job for this annotation

GitHub Actions / build

Make 'GetIdentifierWithoutPath' a static method. (https://rules.sonarsource.com/csharp/RSPEC-2325)

Check warning on line 157 in PxWeb/Code/Api2/DataSource/PxFile/PxFileDataSource.cs

View workflow job for this annotation

GitHub Actions / build

Make 'GetIdentifierWithoutPath' a static method. (https://rules.sonarsource.com/csharp/RSPEC-2325)

Check warning on line 157 in PxWeb/Code/Api2/DataSource/PxFile/PxFileDataSource.cs

View workflow job for this annotation

GitHub Actions / build

Make 'GetIdentifierWithoutPath' a static method. (https://rules.sonarsource.com/csharp/RSPEC-2325)
{
if (id.Contains('/'))
{
Expand Down
3 changes: 2 additions & 1 deletion PxWeb/wwwroot/Languages/pxlang.no.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<language xmlns="http://www.pc-axis.scb.se/" name="no">
<sentence name="PxcYear" value="år" />
<sentence name="PxcHalfyear" value="halvår" />
Expand Down Expand Up @@ -55,4 +55,5 @@
<sentence name="PxcKeywordSeasAdj" value="Sesongjustert" />
<sentence name="PxcKeywordDatabase" value="Database" />
<sentence name="PxcKeywordMatrix" value="Intern referansekode" />
<sentence name="ApiContentsVariableName" value="statistikkvariabel" />
</language>
5 changes: 3 additions & 2 deletions PxWeb/wwwroot/Languages/pxlang.sv.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8"?>
<?xml version="1.0" encoding="utf-8"?>
<language xmlns="http://www.pc-axis.scb.se/" name="sv">
<sentence name="PxcYear" value="år" />
<sentence name="PxcHalfyear" value="halvår" />
Expand Down Expand Up @@ -51,4 +51,5 @@
<sentence name="PxcKeywordSeasAdj" value="Säsongrensning" />
<sentence name="PxcKeywordDatabase" value="Databas" />
<sentence name="PxcKeywordMatrix" value="Intern referenskod" />
</language>
<sentence name="ApiContentsVariableName" value="tabellinnehåll" />
</language>
3 changes: 2 additions & 1 deletion PxWeb/wwwroot/Languages/pxlang.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8" ?>
<language xmlns="http://www.pc-axis.scb.se/" name="Default">
<sentence name="PxcYear" value="year" />
<sentence name="PxcHalfyear" value="halfyear" />
Expand Down Expand Up @@ -111,4 +111,5 @@
<sentence name="PxcKeywordSeasAdj" value="Seasonal adjusted"/>
<sentence name="PxcKeywordDatabase" value="Database"/>
<sentence name="PxcKeywordMatrix" value="Internal reference code"/>
<sentence name="ApiContentsVariableName" value="contents" />
</language>