private async Task <List <CatalogItemBase> > GetChildren(CatalogItemBase item, CommercePipelineExecutionContext context) { var result = new List <CatalogItemBase>(); if (item == null || !item.HasComponent <ExtendedCatalogItemComponent>()) { return(result); } var component = item.GetComponent <ExtendedCatalogItemComponent>(); if (item.Id.StartsWith(CommerceEntity.IdPrefix <Catalog>()) || item.Id.StartsWith(CommerceEntity.IdPrefix <Category>())) { if (!string.IsNullOrEmpty(component.ChildrenSellableItemEntitiesList)) { var sellableItems = component.ChildrenSellableItemEntitiesList.Split('|'); foreach (var sellableItemId in sellableItems) { var existingSellableItem = await _findEntityPipeline.Run(new FindEntityArgument(typeof(CatalogItemBase), sellableItemId, false), context).ConfigureAwait(false) as CatalogItemBase; if (existingSellableItem != null) { result.Add(existingSellableItem); } } } if (!string.IsNullOrEmpty(component.ChildrenCategoryEntitiesList)) { var categories = component.ChildrenCategoryEntitiesList.Split('|'); foreach (var categoryItemId in categories) { var existingCategoryItem = await _findEntityPipeline.Run(new FindEntityArgument(typeof(CatalogItemBase), categoryItemId, false), context).ConfigureAwait(false) as CatalogItemBase; if (existingCategoryItem != null) { result.Add(existingCategoryItem); } } } } return(result); }
private async Task <List <CatalogItemBase> > GetParents(CatalogItemBase item, List <CatalogItemBase> model, CommercePipelineExecutionContext context) { if (item == null || !item.HasComponent <ExtendedCatalogItemComponent>()) { return(model); } var component = item.GetComponent <ExtendedCatalogItemComponent>(); if (!string.IsNullOrEmpty(component.ParentCategoryEntitiesList)) { var existingCategoryItem = await _findEntityPipeline.Run(new FindEntityArgument(typeof(CatalogItemBase), component.ParentCategoryEntitiesList, false), context) as CatalogItemBase; model.Add(existingCategoryItem); await GetParents(existingCategoryItem, model, context).ConfigureAwait(false); } return(model); }
public override async Task <RelationshipArgument> Run( RelationshipArgument arg, CommercePipelineExecutionContext context) { Condition.Requires(arg).IsNotNull(Name + ": The argument can not be null"); Condition.Requires(arg.TargetName).IsNotNullOrEmpty(Name + ": The target name can not be null or empty"); Condition.Requires(arg.SourceName).IsNotNullOrEmpty(Name + ": The source name can not be null or empty"); Condition.Requires(arg.RelationshipType).IsNotNullOrEmpty(Name + ": The relationship type can not be null or empty"); if (!((IEnumerable <string>) new string[4] { CatalogToCategory, CatalogToSellableItem, CategoryToCategory, CategoryToSellableItem }) .Contains(arg.RelationshipType, StringComparer.OrdinalIgnoreCase)) { return(arg); } CatalogItemBase source = await _findEntityPipeline.Run(new FindEntityArgument(typeof(CatalogItemBase), arg.SourceName, false), context) as CatalogItemBase; List <string> stringList = new List <string>(); if (arg.TargetName.Contains("|")) { string[] strArray = arg.TargetName.Split('|'); stringList.AddRange(strArray); } else { stringList.Add(arg.TargetName); } ValueWrapper <bool> sourceChanged = new ValueWrapper <bool>(false); if (!source.HasComponent <ExtendedCatalogItemComponent>()) { source.SetComponent(new ExtendedCatalogItemComponent()); } var sourceComponent = source.GetComponent <ExtendedCatalogItemComponent>(); foreach (string entityId in stringList) { CatalogItemBase catalogItemBase = await _findEntityPipeline.Run(new FindEntityArgument(typeof(CatalogItemBase), entityId, false), context) as CatalogItemBase; if (source != null && catalogItemBase != null) { if (!catalogItemBase.HasComponent <ExtendedCatalogItemComponent>()) { catalogItemBase.SetComponent(new ExtendedCatalogItemComponent()); } var catalogItemBaseComponent = catalogItemBase.GetComponent <ExtendedCatalogItemComponent>(); ValueWrapper <bool> changed = new ValueWrapper <bool>(false); if (arg.RelationshipType.Equals(CatalogToCategory, StringComparison.OrdinalIgnoreCase)) { sourceComponent.ChildrenCategoryEntitiesList = UpdateHierarchy(arg, catalogItemBase.Id, sourceComponent.ChildrenCategoryEntitiesList, sourceChanged); catalogItemBaseComponent.ParentCatalogEntitiesList = UpdateHierarchy(arg, source.Id, catalogItemBaseComponent.ParentCatalogEntitiesList, changed); } else if (arg.RelationshipType.Equals(CategoryToCategory, StringComparison.OrdinalIgnoreCase)) { sourceComponent.ChildrenCategoryEntitiesList = UpdateHierarchy(arg, catalogItemBase.Id, sourceComponent.ChildrenCategoryEntitiesList, sourceChanged); catalogItemBaseComponent.ParentCategoryEntitiesList = UpdateHierarchy(arg, source.Id, catalogItemBaseComponent.ParentCategoryEntitiesList, changed); catalogItemBaseComponent.ParentCatalogEntitiesList = UpdateHierarchy(arg, ExtractCatalogId(source.Id), catalogItemBaseComponent.ParentCatalogEntitiesList, changed); } else if (arg.RelationshipType.Equals(CatalogToSellableItem, StringComparison.OrdinalIgnoreCase)) { sourceComponent.ChildrenSellableItemEntitiesList = UpdateHierarchy(arg, catalogItemBase.Id, sourceComponent.ChildrenSellableItemEntitiesList, sourceChanged); catalogItemBaseComponent.ParentCatalogEntitiesList = UpdateHierarchy(arg, source.Id, catalogItemBaseComponent.ParentCatalogEntitiesList, changed); } else if (arg.RelationshipType.Equals(CategoryToSellableItem, StringComparison.OrdinalIgnoreCase)) { sourceComponent.ChildrenSellableItemEntitiesList = UpdateHierarchy(arg, catalogItemBase.Id, sourceComponent.ChildrenSellableItemEntitiesList, sourceChanged); catalogItemBaseComponent.ParentCategoryEntitiesList = UpdateHierarchy(arg, source.Id, catalogItemBaseComponent.ParentCategoryEntitiesList, changed); catalogItemBaseComponent.ParentCatalogEntitiesList = UpdateHierarchy(arg, ExtractCatalogId(source.Id), catalogItemBaseComponent.ParentCatalogEntitiesList, changed); } if (changed.Value) { PersistEntityArgument persistEntityArgument = await _persistEntityPipeline.Run(new PersistEntityArgument(catalogItemBase), context); } } } if (sourceChanged.Value) { await _persistEntityPipeline.Run(new PersistEntityArgument(source), context); } return(arg); }