public CatalogItemParentAssociationModel(string itemId, string catalogId, CatalogItemBase parent)
 {
     ItemId           = itemId;
     CatalogId        = catalogId;
     ParentId         = parent.Id;
     ParentSitecoreId = parent.SitecoreId;
 }
        protected virtual async Task <List <string> > GetBreadcrumbs(CatalogItemBase catalogItem, CommercePipelineExecutionContext context)
        {
            var breadcrumbs  = new List <string>();
            var entityIdList = this.GetParentEntityList(catalogItem, context);

            if (entityIdList == null || entityIdList.Count == 0)
            {
                breadcrumbs.Add(this.GetEntityLink(catalogItem));

                return(breadcrumbs);
            }

            foreach (var id in entityIdList)
            {
                var parentCatalogItem = await this.GetParentCatalogItem(id, context).ConfigureAwait(false);

                if (parentCatalogItem == null)
                {
                    continue;
                }
                var parentBreadcrumbs = await GetBreadcrumbs(parentCatalogItem, context).ConfigureAwait(false);

                foreach (var breadcrumb in parentBreadcrumbs)
                {
                    breadcrumbs.Add($"{breadcrumb} > {this.GetEntityLink(catalogItem)}");
                }
            }

            return(await Task.FromResult(breadcrumbs).ConfigureAwait(false));
        }
Exemple #3
0
        public static BreadcrumbModel ToBreadcrumbModel(this CatalogItemBase item, int version)
        {
            var result = new BreadcrumbModel
            {
                DisplayName = item.DisplayName,
                Name        = item.Name,
                IsActive    = true,
                Href        = $"/entityView/Master/{version}/{item.Name}",
                EntityId    = item.Id
            };

            if (item is Category)
            {
                result.Icon = CategoryIconPath;
            }
            else if (item is SellableItem)
            {
                result.Icon = ProductIconPath;
            }
            else if (item is Catalog)
            {
                result.Icon = CatalogIconPath;
            }

            return(result);
        }
        public virtual string GetCatalogId(CatalogItemBase catalogItem)
        {
            if (string.IsNullOrWhiteSpace(catalogItem.FriendlyId))
            {
                return(null);
            }

            var strArray = catalogItem.FriendlyId.Split('-');

            return(strArray[0]?.ToEntityId <Catalog>());
        }
        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);
        }
Exemple #6
0
        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);
        }
 protected virtual List <string> GetParentEntityList(CatalogItemBase catalogItem, CommercePipelineExecutionContext context)
 {
     if (context.CommerceContext.GetPolicy <CatalogNavigationPolicy>().UseUglySitecoreIds)
     {
         var parentList = this.ToParentList(catalogItem.ParentCategoryList);
         if ((parentList == null || !parentList.Any()) && !(catalogItem is Catalog))
         {
             return(new List <string>()
             {
                 this.GetCatalogId(catalogItem)
             });
         }
         return(parentList);
     }
     else
     {
         var categoryRelationshipsComponent = catalogItem.GetComponent <CategoryHierarchyComponent>();
         return(this.ToParentList(categoryRelationshipsComponent.ParentEntityList));
     }
 }
        protected virtual async Task AddCatalogNavigationView(EntityView entityView, CatalogItemBase catalogItem, CommercePipelineExecutionContext context)
        {
            var parentCategoriesView = new EntityView {
                Name = "Catalog Navigation", Icon = "elements_hierarchy", UiHint = "Table"
            };

            entityView.ChildViews.Insert(0, parentCategoriesView);

            var breadcrumbs = await this.GetBreadcrumbs(catalogItem, context).ConfigureAwait(false);

            breadcrumbs.Sort();
            foreach (var breadcrumb in breadcrumbs)
            {
                var breadcrumbView = new EntityView {
                    Name = "Breadcrumbs", Icon = null
                };
                breadcrumbView.Properties.Add(new ViewProperty()
                {
                    Name = "Breadcrumb", Value = breadcrumb, UiType = "Html"
                });
                parentCategoriesView.ChildViews.Add(breadcrumbView);
            }
        }
 protected virtual string GetEntityLink(CatalogItemBase catalogItem)
 {
     return($"<a href=\"/entityView/Master/{catalogItem.EntityVersion}/{catalogItem.Id}\">{catalogItem.DisplayName}</a>");
 }
Exemple #10
0
        public override async Task <RelationshipArgument> Run(RelationshipArgument arg,
                                                              CommercePipelineExecutionContext context)
        {
            if (!(new[]
            {
                "CatalogToCategory",
                "CatalogToSellableItem",
                "CategoryToCategory",
                "CategoryToSellableItem"
            }).Contains(arg.RelationshipType, StringComparer.OrdinalIgnoreCase))
            {
                return(arg);
            }
            CatalogItemBase source = arg.SourceEntity as CatalogItemBase;

            if (source == null)
            {
                source = await _commerceCommander.Pipeline <IFindEntityPipeline>()
                         .Run(new FindEntityArgument(typeof(CatalogItemBase), arg.SourceName), context)
                         .ConfigureAwait(false) as CatalogItemBase;
            }

            List <Tuple <string, CatalogItemBase> > tupleList = new List <Tuple <string, CatalogItemBase> >();

            if (arg.TargetName.Contains("|"))
            {
                string targetName = arg.TargetName;
                char[] chArray    = { '|' };
                foreach (string str in targetName.Split(chArray))
                {
                    tupleList.Add(new Tuple <string, CatalogItemBase>(str, null));
                }
            }
            else
            {
                tupleList.Add(new Tuple <string, CatalogItemBase>(arg.TargetName, arg.TargetEntity as CatalogItemBase));
            }

            foreach (Tuple <string, CatalogItemBase> tuple in tupleList)
            {
                CatalogItemBase catalogItemBase = tuple.Item2 ?? await _commerceCommander.Pipeline <IFindEntityPipeline>()
                                                  .Run(new FindEntityArgument(typeof(CatalogItemBase), tuple.Item1), context).ConfigureAwait(false) as CatalogItemBase;

                if (source != null && catalogItemBase != null)
                {
                    bool changed = false;
                    var  parentEntitiesComponent = catalogItemBase.GetComponent <ParentEntitiesComponent>();

                    if (arg.Mode.HasValue)
                    {
                        if (arg.Mode.Value == RelationshipMode.Create)
                        {
                            if (!parentEntitiesComponent.EntityIds.Contains(source.Id))
                            {
                                parentEntitiesComponent.EntityIds.Add(source.Id);
                                changed = true;
                            }
                        }
                        else if (arg.Mode.Value == RelationshipMode.Delete)
                        {
                            if (parentEntitiesComponent.EntityIds.Contains(source.Id))
                            {
                                parentEntitiesComponent.EntityIds.Remove(source.Id);
                                changed = true;
                            }
                        }
                    }

                    if (changed)
                    {
                        await _commerceCommander
                        .Pipeline <IPersistEntityPipeline>().Run(new PersistEntityArgument(catalogItemBase), context)
                        .ConfigureAwait(false);
                    }
                }
            }

            return(arg);
        }
Exemple #11
0
        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);
        }