Skip to content

ReferenceEntities

// RB2 Core Connect
using System.Globalization;
namespace CoreConnect.Pim;
public record PimReferenceEntity
{
public required string Code { get; set; }
public required List<PimReferenceEntityAttribute> Attributes { get; set; }
public required List<PimReferenceEntityRecord> Records { get; set; }
}
public record PimReferenceEntityAttribute
{
public required string Code { get; set; }
public required Dictionary<string, string> Labels { get; set; }
public required string Type { get; set; }
public bool IsSet { get; set; }
public bool IsLocalized { get; set; }
public bool IsImage { get; set; }
public required List<PimReferenceEntityAttributeOption> Options { get; set; }
}
public record PimReferenceEntityAttributeOption
{
public required string Code { get; set; }
public required Dictionary<string, string> Labels { get; set; }
}
public class PimReferenceEntityRecordValue
{
public required string Locale { get; set; }
public required string Channel { get; set; }
public required object Data { get; set; }
}
public record PimReferenceEntityRecord
{
public required string Code { get; set; }
public required Dictionary<string, List<PimReferenceEntityRecordValue>> Values { get; set; }
public object? GetValue(string valueCode, PimReferenceEntityAttribute attribute)
{
if (attribute.IsSet)
{
if (attribute.Options != null && attribute.Options.Count > 0)
{
return GetOptionListValue(valueCode, attribute.Options);
}
else
{
return GetSimpleListValue(valueCode);
}
}
else
{
if (attribute.Options != null && attribute.Options.Count > 0)
{
return GetOptionValue(valueCode, attribute.Options);
}
else
{
if (attribute.IsLocalized)
{
return GetLocalizedStringValue(valueCode);
}
else
{
return GetSimpleValue(valueCode, attribute);
}
}
}
}
private object? GetSimpleValue(string valueCode, PimReferenceEntityAttribute attribute)
{
if (!Values.TryGetValue(valueCode, out var dataValues) || dataValues[0].Data == null)
{
return null;
}
if (attribute.Type == "number")
{
if (int.TryParse((string)dataValues[0].Data, NumberStyles.Integer, CultureInfo.InvariantCulture, out var parsedValue))
{
return parsedValue;
}
}
else
{
return dataValues[0].Data;
}
return null;
}
private List<object> GetSimpleListValue(string valueCode)
{
return Values[valueCode].Select(a => a.Data).ToList();
}
private Dictionary<string, string?> GetLocalizedStringValue(string valueCode)
{
return Values[valueCode].ToDictionary(a => a.Locale.Replace('_', '-'), a => a.Data.ToString());
}
private Dictionary<string, string> GetOptionValue(string valueCode, List<PimReferenceEntityAttributeOption> Options)
{
var optionCode = Values[valueCode].FirstOrDefault()?.Data.ToString();
var option = Options.Find(a => a.Code == optionCode);
if (option != null)
{
return option.Labels.ToDictionary(a => a.Key.Replace('_', '-'), a => a.Value);
}
return new Dictionary<string, string>();
}
private List<Dictionary<string, string>> GetOptionListValue(string valueCode,
List<PimReferenceEntityAttributeOption> Options)
{
var optionCodes = Values[valueCode].FirstOrDefault()?.Data as List<string>;
List<Dictionary<string, string>> data = [];
if (optionCodes != null)
{
foreach (var optionCode in optionCodes)
{
var option = Options.Find(a => a.Code == optionCode);
if (option != null && option.Labels != null)
{
data.Add(option.Labels.ToDictionary(a => a.Key.Replace('_', '-'), a => a.Value));
}
}
}
return data;
}
}