Skip to content

Commit 0122586

Browse files
committed
Add configurable PX parser and centralize content variable logic
Refactored PxFileBuilder2 to delegate contents variable creation to ContentsUtil.AssertContentsVariableExists, reducing code duplication. Introduced UsePxUtilsParser option in PxFileConfigurationOptions to allow runtime selection between PxBuilder and PxFileBuilder2 in PxFileDataSource. Updated class inheritance and namespaces to align with new structure.
1 parent 7277620 commit 0122586

File tree

3 files changed

+12
-56
lines changed

3 files changed

+12
-56
lines changed

PxWeb/Code/Api2/DataSource/PxFile/PxFileBuilder2.cs

Lines changed: 6 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,18 @@
1-
using System.Globalization;
1+
using PCAxis.Paxiom;
22

3-
using PCAxis.Paxiom;
4-
5-
using PxWeb.PxFile;
3+
using PxWeb.Code.PxFile;
64

75
namespace PxWeb.Code.Api2.DataSource.PxFile
86
{
9-
internal class PxFileBuilder2 : PxBuilder
7+
8+
9+
internal class PxFileBuilder2 : PXFileBuilder
1010
{
1111
public override bool BuildForSelection()
1212
{
1313
var meta = base.Model.Meta;
1414
var result = base.BuildForSelection();
15-
if (meta.ContentVariable is null)
16-
{
17-
// If there is no content variable, we create one.
18-
19-
try
20-
{
21-
// The name of the content variable is localized based on the language of the metadata.
22-
var name = PCAxis.Paxiom.Localization.PxResourceManager.GetResourceManager()
23-
.GetString("ApiContentsVariableName", new CultureInfo(meta.Language));
24-
25-
// Create the content variable
26-
var contentVariable = new Variable(name, "CONTENTS", PlacementType.Stub, meta.NumberOfLanguages);
27-
contentVariable.IsContentVariable = true;
28-
29-
// Create the value for the content variable from the meta contents
30-
var value = new Value(meta.Contents, meta.NumberOfLanguages);
31-
PaxiomUtil.SetCode(value, "content");
32-
// Move the contentInfo from tablelevel to the content value
33-
value.ContentInfo = meta.ContentInfo;
34-
meta.ContentInfo = null;
35-
contentVariable.Values.Add(value);
36-
37-
// Insert the content variable as the first variable in the stub
38-
meta.Stub.Insert(0, contentVariable);
39-
meta.Variables.Insert(0, contentVariable);
40-
meta.ContentVariable = contentVariable;
41-
42-
// Add text and ContentInfo for all languages
43-
var languages = meta.GetAllLanguages();
44-
var currentLanguage = meta.CurrentLanguage;
45-
for (int i = 0; i < languages.Length; i++)
46-
{
47-
var lang = languages[i];
48-
49-
meta.SetLanguage(i);
50-
51-
// Add the label for each language
52-
contentVariable.Name = PCAxis.Paxiom.Localization.PxResourceManager.GetResourceManager()
53-
.GetString("ApiContentsVariableName", new CultureInfo(lang));
54-
55-
// Set the value text in diffrent languages
56-
value.Value = meta.Contents;
57-
}
58-
meta.SetLanguage(currentLanguage);
59-
}
60-
catch (Exception)
61-
{
62-
result = false;
63-
}
64-
}
15+
ContentsUtil.AssertContentsVariableExists(meta);
6516

6617
return result;
6718

PxWeb/Code/Api2/DataSource/PxFile/PxFileDataSource.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
using Px.Abstractions.Interfaces;
1111

1212
using PxWeb.Mappers;
13+
using PxWeb.PxFile;
1314

1415
namespace PxWeb.Code.Api2.DataSource.PxFile
1516
{
@@ -19,13 +20,15 @@ public class PxFileDataSource : IDataSource
1920
private readonly ITablePathResolver _tablePathResolver;
2021
private readonly IPxHost _hostingEnvironment;
2122
private readonly ICodelistMapper _codelistMapper;
23+
private readonly bool _usePxUtilsParser;
2224

2325
public PxFileDataSource(IPxFileConfigurationService pxFileConfigurationService, IItemSelectionResolver itemSelectionResolver, ITablePathResolver tablePathResolver, IPxHost hostingEnvironment, ICodelistMapper codelistMapper)
2426
{
2527
_itemSelectionResolver = itemSelectionResolver;
2628
_tablePathResolver = tablePathResolver;
2729
_hostingEnvironment = hostingEnvironment;
2830
_codelistMapper = codelistMapper;
31+
_usePxUtilsParser = pxFileConfigurationService.GetConfiguration().UsePxUtilsParser;
2932
}
3033

3134
/// <summary>
@@ -36,7 +39,7 @@ public PxFileDataSource(IPxFileConfigurationService pxFileConfigurationService,
3639
/// <returns>Builder object, null if builder could not be created</returns>
3740
public IPXModelBuilder? CreateBuilder(string id, string language)
3841
{
39-
var builder = new PxFileBuilder2();
42+
IPXModelBuilder builder = _usePxUtilsParser ? new PxBuilder() : new PxFileBuilder2();
4043

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

PxWeb/Config/Api2/PxFileConfigurationOptions.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,7 @@
33
public class PxFileConfigurationOptions
44
{
55
public bool StrictAggregations { get; set; }
6+
7+
public bool UsePxUtilsParser { get; set; } = false;
68
}
79
}

0 commit comments

Comments
 (0)