using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using Newtonsoft.Json; | |
using Newtonsoft.Json.Linq; | |
using Sitecore.Collections; | |
using Sitecore.Commerce.Engine.Connect.DataProvider; | |
using Sitecore.Commerce.Engine.Connect.SitecoreDataProvider.Extensions; | |
using Sitecore.Configuration; | |
using Sitecore.Data; | |
using Sitecore.Data.DataProviders; | |
using Sitecore.Data.IDTables; | |
using Sitecore.Data.Items; | |
using Sitecore.Data.Managers; | |
using Sitecore.Diagnostics; | |
using Sitecore.Globalization; | |
namespace Sitecore.Services.Examples.DataProvider | |
{ | |
public class ReadOnlyBrandDataProvider : Data.DataProviders.DataProvider | |
{ | |
public string BrandTemplateId | |
{ | |
get; | |
set; | |
} | |
public string BrandFolderTemplateId | |
{ | |
get; | |
set; | |
} | |
public string Prefix => "CommerceBrands"; | |
public Database ContentDb => Factory.GetDatabase(this.TargetDatabaseName); | |
public string TargetDatabaseName | |
{ | |
get; | |
set; | |
} | |
public ReadOnlyBrandDataProvider(string targetDatabaseName, string brandTemplateId, string brandFolderTemplateId) | |
{ | |
TargetDatabaseName = targetDatabaseName; | |
BrandTemplateId = brandTemplateId; | |
BrandFolderTemplateId = brandFolderTemplateId; | |
} | |
public override ItemDefinition GetItemDefinition(ID itemId, CallContext context) | |
{ | |
ItemDefinition itemDefinition = null; | |
var key = string.Empty; | |
if (!GetEntityId(itemId, ref key)) | |
return null; | |
var itemName = ItemUtil.ProposeValidItemName(key); | |
itemDefinition = new ItemDefinition(itemId, itemName, ID.Parse(BrandTemplateId), ID.Null); | |
return itemDefinition; | |
} | |
private bool GetEntityId(ID itemId, ref string key) | |
{ | |
var idEntry = IDTable.GetKeys(Prefix, itemId); | |
if (idEntry.Any()) | |
key = idEntry[0].Key; | |
return key.Contains("Entity-Brand-"); | |
} | |
private bool TryGetParentId(ID itemId, ref ID parentId) | |
{ | |
var idEntry = IDTable.GetKeys(Prefix, itemId); | |
if (idEntry.Any()) | |
parentId = ID.Parse(idEntry[0].ParentID); | |
return parentId.IsNull; | |
} | |
public override FieldList GetItemFields(ItemDefinition item, VersionUri version, CallContext context) | |
{ | |
FieldList fieldLists = null; | |
try | |
{ | |
if (CanProcessItem(item)) | |
{ | |
var template = TemplateManager.GetTemplate(item.TemplateID, ContentDb); | |
if (template != null) | |
{ | |
var key = string.Empty; | |
if (GetEntityId(item.ID, ref key)) | |
{ | |
var entity = GetEntity(key, version.Version.Number); | |
if (entity != null) | |
{ | |
fieldLists = new FieldList(); | |
var displayName = FieldIDs.DisplayName; | |
var displayNameValue = entity["DisplayName"]?.Value<string>(); | |
fieldLists.Add(displayName, displayNameValue); | |
foreach (var templateField in template.GetFields().Where(ItemUtil.IsDataField)) | |
{ | |
var value = entity[templateField.Name]?.Value<string>(); | |
fieldLists.Add(templateField.ID, value); | |
} | |
fieldLists.Add(FieldIDs.Created, entity.GetEntityValue("DateCreated")); | |
fieldLists.Add(FieldIDs.Updated, entity.GetEntityValue("DateUpdated")); | |
fieldLists.Add(FieldIDs.Security, "ar|Everyone|pe|+item:read|pd|+item:read|"); | |
var createdBy = entity.GetEntityValue("CreatedBy"); | |
var updatedBy = entity.GetEntityValue("UpdatedBy"); | |
if (!string.IsNullOrEmpty(createdBy)) | |
{ | |
fieldLists.Add(FieldIDs.CreatedBy, createdBy); | |
fieldLists.Add(FieldIDs.Owner, createdBy); | |
if (string.IsNullOrEmpty(updatedBy)) | |
{ | |
updatedBy = createdBy; | |
} | |
} | |
if (!string.IsNullOrEmpty(updatedBy)) | |
{ | |
fieldLists.Add(FieldIDs.UpdatedBy, updatedBy); | |
} | |
} | |
} | |
else | |
{ | |
Log.Error($"Could not find the combined entity ID for Item ID {item.ID} with template ID {item.TemplateID}", this); | |
} | |
} | |
} | |
} | |
catch (Exception exception) | |
{ | |
var message = | |
$"There was an error in GetItemFields. ItemDefinition ID: {item.ID} Template ID: {item.TemplateID}.\r\nError StackTrace: {exception.StackTrace}"; | |
Log.Error(message, this); | |
fieldLists = null; | |
} | |
return fieldLists; | |
} | |
private bool CanProcessItem(ItemDefinition item) | |
{ | |
return item.TemplateID.ToString().Equals(BrandTemplateId, StringComparison.OrdinalIgnoreCase); | |
} | |
public override ID GetParentID(ItemDefinition itemDefinition, CallContext context) | |
{ | |
if (!CanProcessItem(itemDefinition)) | |
return null; | |
var key = ID.Null; | |
context.Abort(); | |
TryGetParentId(itemDefinition.ID, ref key); | |
return key.IsNull ? null : key; | |
} | |
public bool CanProcessBrandParent(ItemDefinition parentItem) | |
{ | |
return parentItem.TemplateID.ToString().Equals(BrandFolderTemplateId, StringComparison.OrdinalIgnoreCase); | |
} | |
public override IDList GetChildIDs(ItemDefinition parentItem, CallContext context) | |
{ | |
if (!CanProcessBrandParent(parentItem)) | |
return base.GetChildIDs(parentItem, context); | |
var childIdStrings = new List<string>(); | |
var guid = parentItem.ID.Guid; | |
foreach (var listItem in GetListItems(guid.ToString(), "Brands", | |
"Sitecore.Services.Examples.Entities.Entities.Brand, Sitecore.Services.Examples.Entities", 1000)) | |
{ | |
childIdStrings.Add(listItem); | |
} | |
if (!childIdStrings.Any()) | |
return base.GetChildIDs(parentItem, context); | |
var childIDs = new IDList(); | |
foreach (var childIdString in childIdStrings) | |
{ | |
childIDs.Add(ID.Parse(childIdString)); | |
} | |
return childIDs; | |
} | |
public List<string> GetListItems(string parentId, string listName, string typeName, int take) | |
{ | |
var ids = new List<string>(); | |
var catalogRepository = new CatalogRepository(); | |
var results = catalogRepository.InvokeHttpClientGet( | |
$"GetList(id='{listName}',type='{typeName}',skip=0,take={take})?$expand=Items($expand=Components($expand=ChildComponents($expand=ChildComponents)))"); | |
if (string.IsNullOrEmpty(results)) | |
return ids; | |
var jObjects = JsonConvert.DeserializeObject<JObject>(results, CatalogRepository.JsonSettings); | |
var value = (JArray)jObjects.GetValue("Items", StringComparison.OrdinalIgnoreCase); | |
foreach (var jTokens in value) | |
{ | |
var item1 = jTokens["SitecoreId"]; | |
var sitecoreId = item1?.Value<string>(); | |
ids.Add(sitecoreId); | |
var id = jTokens["Id"]?.Value<string>(); | |
var idEntry = IDTable.GetID(Prefix, id); | |
if (idEntry != null) | |
{ | |
IDTable.RemoveKey(Prefix, id); | |
} | |
IDTable.Add(Prefix, id, ID.Parse(sitecoreId), ID.Parse(parentId)); | |
} | |
return ids; | |
} | |
public JToken GetEntity(string mixId, int? version = null) | |
{ | |
var executeString = string.Empty; | |
if (mixId.StartsWith("Entity-Brand-", StringComparison.OrdinalIgnoreCase)) | |
{ | |
var cleanedEntityId = mixId.Replace("Entity-Brand-", string.Empty); | |
executeString = $"Brands('{cleanedEntityId}')?$expand=*"; | |
} | |
var versionStrings = new Dictionary<string, string>(); | |
if (version.HasValue) | |
{ | |
versionStrings.Add("EntityVersion", version.ToString()); | |
} | |
var catalogRepository = new CatalogRepository("en"); | |
var jsonResult = catalogRepository.InvokeHttpClientGet(executeString, false, true, null, versionStrings); | |
if (string.IsNullOrEmpty(jsonResult)) | |
{ | |
return null; | |
} | |
var entity = JsonConvert.DeserializeObject<JToken>(jsonResult, CatalogRepository.JsonSettings); | |
return entity; | |
} | |
public override LanguageCollection GetLanguages(CallContext context) | |
{ | |
return null; | |
} | |
public override VersionUriList GetItemVersions(ItemDefinition item, CallContext context) | |
{ | |
if (!CanProcessItem(item)) | |
return null; | |
var versions = new VersionUriList {{Language.Current, Data.Version.First}}; | |
return versions; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment