From ac018388aed93616d5e56e44e33ffad1fb687314 Mon Sep 17 00:00:00 2001 From: likp Date: Mon, 26 May 2025 16:28:35 +0200 Subject: [PATCH 1/3] Added wrapper for PxFileBuilder that creates a contents variable if not present --- .../Api2/DataSource/PxFile/PxFileBuilder2.cs | 34 +++++++++++++++++++ .../DataSource/PxFile/PxFileDataSource.cs | 2 +- 2 files changed, 35 insertions(+), 1 deletion(-) create mode 100644 PxWeb/Code/Api2/DataSource/PxFile/PxFileBuilder2.cs diff --git a/PxWeb/Code/Api2/DataSource/PxFile/PxFileBuilder2.cs b/PxWeb/Code/Api2/DataSource/PxFile/PxFileBuilder2.cs new file mode 100644 index 00000000..2ffdd9c3 --- /dev/null +++ b/PxWeb/Code/Api2/DataSource/PxFile/PxFileBuilder2.cs @@ -0,0 +1,34 @@ +using PCAxis.Paxiom; + +namespace PxWeb.Code.Api2.DataSource.PxFile +{ + internal class PxFileBuilder2 : PXFileBuilder + { + public override bool BuildForSelection() + { + var meta = base.Model.Meta; + var retVAlue = base.BuildForSelection(); + if (meta.ContentVariable is null) + { + // TODO: If there is no content variable add one + Console.WriteLine("No content variable found in the model. This should be handled properly."); + + var contentVariable = new Variable("Metrics", "CONTENTS", PlacementType.Stub, meta.NumberOfLanguages); + contentVariable.IsContentVariable = true; + var value = new Value(meta.Contents, meta.NumberOfLanguages); + PaxiomUtil.SetCode(value, "content"); + value.ContentInfo = meta.ContentInfo; + meta.ContentInfo = null; + + contentVariable.Values.Add(value); + meta.Stub.Insert(0, contentVariable); + meta.Variables.Insert(0, contentVariable); + meta.ContentVariable = contentVariable; + + } + + return retVAlue; + + } + } +} diff --git a/PxWeb/Code/Api2/DataSource/PxFile/PxFileDataSource.cs b/PxWeb/Code/Api2/DataSource/PxFile/PxFileDataSource.cs index a086239a..52dae0c5 100644 --- a/PxWeb/Code/Api2/DataSource/PxFile/PxFileDataSource.cs +++ b/PxWeb/Code/Api2/DataSource/PxFile/PxFileDataSource.cs @@ -38,7 +38,7 @@ public PxFileDataSource(IPxFileConfigurationService pxFileConfigurationService, /// Builder object, null if builder could not be created 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); From deb09bd14462bed425e95a12af008e6a4a56f140 Mon Sep 17 00:00:00 2001 From: likp Date: Tue, 27 May 2025 10:52:39 +0200 Subject: [PATCH 2/3] Set the content variable name from language settings --- PxWeb/Code/Api2/DataSource/PxFile/PxFileBuilder2.cs | 10 +++++++--- PxWeb/wwwroot/Languages/pxlang.no.xml | 3 ++- PxWeb/wwwroot/Languages/pxlang.sv.xml | 5 +++-- PxWeb/wwwroot/Languages/pxlang.xml | 3 ++- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/PxWeb/Code/Api2/DataSource/PxFile/PxFileBuilder2.cs b/PxWeb/Code/Api2/DataSource/PxFile/PxFileBuilder2.cs index 2ffdd9c3..71c42c11 100644 --- a/PxWeb/Code/Api2/DataSource/PxFile/PxFileBuilder2.cs +++ b/PxWeb/Code/Api2/DataSource/PxFile/PxFileBuilder2.cs @@ -1,4 +1,6 @@ -using PCAxis.Paxiom; +using System.Globalization; + +using PCAxis.Paxiom; namespace PxWeb.Code.Api2.DataSource.PxFile { @@ -10,10 +12,12 @@ public override bool BuildForSelection() var retVAlue = base.BuildForSelection(); if (meta.ContentVariable is null) { - // TODO: If there is no content variable add one Console.WriteLine("No content variable found in the model. This should be handled properly."); - var contentVariable = new Variable("Metrics", "CONTENTS", PlacementType.Stub, meta.NumberOfLanguages); + var name = PCAxis.Paxiom.Localization.PxResourceManager.GetResourceManager() + .GetString("ApiContentsVariableName", new CultureInfo(meta.Language)); + + var contentVariable = new Variable(name, "CONTENTS", PlacementType.Stub, meta.NumberOfLanguages); contentVariable.IsContentVariable = true; var value = new Value(meta.Contents, meta.NumberOfLanguages); PaxiomUtil.SetCode(value, "content"); diff --git a/PxWeb/wwwroot/Languages/pxlang.no.xml b/PxWeb/wwwroot/Languages/pxlang.no.xml index 4080ba69..5787ffee 100644 --- a/PxWeb/wwwroot/Languages/pxlang.no.xml +++ b/PxWeb/wwwroot/Languages/pxlang.no.xml @@ -1,4 +1,4 @@ - + @@ -55,4 +55,5 @@ + diff --git a/PxWeb/wwwroot/Languages/pxlang.sv.xml b/PxWeb/wwwroot/Languages/pxlang.sv.xml index 8579618b..531c2195 100644 --- a/PxWeb/wwwroot/Languages/pxlang.sv.xml +++ b/PxWeb/wwwroot/Languages/pxlang.sv.xml @@ -1,4 +1,4 @@ - + @@ -51,4 +51,5 @@ - \ No newline at end of file + + diff --git a/PxWeb/wwwroot/Languages/pxlang.xml b/PxWeb/wwwroot/Languages/pxlang.xml index 95337f22..cfb83b9c 100644 --- a/PxWeb/wwwroot/Languages/pxlang.xml +++ b/PxWeb/wwwroot/Languages/pxlang.xml @@ -1,4 +1,4 @@ - + @@ -111,4 +111,5 @@ + From dcde5f14b2cc88fe6bc5385298ce2b58ceb65153 Mon Sep 17 00:00:00 2001 From: likp Date: Tue, 27 May 2025 16:29:13 +0200 Subject: [PATCH 3/3] Improve content variable handling in PxFileBuilder2 Updated the `BuildForSelection` method to enhance the management of content variables. Added error handling and localization for the content variable's name based on the metadata's language. The method now creates a content variable if it doesn't exist, sets its properties, and adds it to the metadata's variables and stub. Additionally, it updates the content variable's name and value for all languages, ensuring proper localization. Renamed the return value from `retVAlue` to `result` for clarity. --- .../Api2/DataSource/PxFile/PxFileBuilder2.cs | 60 ++++++++++++++----- 1 file changed, 45 insertions(+), 15 deletions(-) diff --git a/PxWeb/Code/Api2/DataSource/PxFile/PxFileBuilder2.cs b/PxWeb/Code/Api2/DataSource/PxFile/PxFileBuilder2.cs index 71c42c11..b12b8e65 100644 --- a/PxWeb/Code/Api2/DataSource/PxFile/PxFileBuilder2.cs +++ b/PxWeb/Code/Api2/DataSource/PxFile/PxFileBuilder2.cs @@ -9,29 +9,59 @@ internal class PxFileBuilder2 : PXFileBuilder public override bool BuildForSelection() { var meta = base.Model.Meta; - var retVAlue = base.BuildForSelection(); + var result = base.BuildForSelection(); if (meta.ContentVariable is null) { - Console.WriteLine("No content variable found in the model. This should be handled properly."); + // If there is no content variable, we create one. - var name = PCAxis.Paxiom.Localization.PxResourceManager.GetResourceManager() - .GetString("ApiContentsVariableName", new CultureInfo(meta.Language)); + 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)); - var contentVariable = new Variable(name, "CONTENTS", PlacementType.Stub, meta.NumberOfLanguages); - contentVariable.IsContentVariable = true; - var value = new Value(meta.Contents, meta.NumberOfLanguages); - PaxiomUtil.SetCode(value, "content"); - value.ContentInfo = meta.ContentInfo; - meta.ContentInfo = null; + // Create the content variable + var contentVariable = new Variable(name, "CONTENTS", PlacementType.Stub, meta.NumberOfLanguages); + contentVariable.IsContentVariable = true; - contentVariable.Values.Add(value); - meta.Stub.Insert(0, contentVariable); - meta.Variables.Insert(0, contentVariable); - meta.ContentVariable = contentVariable; + // 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 retVAlue; + return result; } }