/// <summary>
        /// The execute.
        /// </summary>
        /// <param name="arg">
        /// The pipeline argument.
        /// </param>
        /// <param name="context">
        /// The context.
        /// </param>
        /// <returns>
        /// The <see cref="PipelineArgument"/>.
        /// </returns>
        public async override Task <IEnumerable <LocalizedTerm> > Run(LocalizedProductFeaturesArgument arg, CommercePipelineExecutionContext context)
        {
            var cachePolicy = context.GetPolicy <ProductFeaturesCachePolicy>();

            if (!cachePolicy.AllowCaching)
            {
                context.CommerceContext.AddUniqueObjectByType((object)new KeyValuePair <string, bool>("IsFromCache", false));
                return(null);
            }

            var policy = context.GetPolicy <ProductFeaturesControlPanelPolicy>();

            string cacheKey      = policy.AvailableProductFeaturesPath + "|" + context.CommerceContext.CurrentLanguage();
            var    cachePipeline = this.Commander.Pipeline <IGetEnvironmentCachePipeline>();

            EnvironmentCacheArgument environmentCacheArgument = new EnvironmentCacheArgument();

            environmentCacheArgument.CacheName = cachePolicy.CacheName;

            var result = (await cachePipeline.Run(environmentCacheArgument, context))?.Get(cacheKey)?.Result as IEnumerable <LocalizedTerm>;

            if (result == null)
            {
                context.CommerceContext.AddUniqueObjectByType((object)new KeyValuePair <string, bool>("IsFromCache", false));
                return(null);
            }
            context.CommerceContext.AddUniqueObjectByType((object)new KeyValuePair <string, bool>("IsFromCache", true));
            context.Logger.LogDebug("Mgmt.GetLocMsg." + cacheKey);

            return(result);
        }
Exemple #2
0
        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="arg">
        /// The pipeline argument.
        /// </param>
        /// <param name="context">
        /// The context.
        /// </param>
        /// <returns>
        /// The <see cref="PipelineArgument"/>.
        /// </returns>
        public async override Task <LocalizedTerm> Run(LocalizedTerm arg, CommercePipelineExecutionContext context)
        {
            if (arg == null)
            {
                return(null);
            }

            var argument = context.CommerceContext.GetObject <LocalizedProductFeaturesConfigurationArgument>();

            if (string.IsNullOrEmpty(argument?.LocalizationKey))
            {
                return(arg);
            }

            KeyValuePair <string, bool> keyValuePair = context.CommerceContext.GetObject <KeyValuePair <string, bool> >((Func <KeyValuePair <string, bool>, bool>)(k => k.Key.Equals("IsFromCache", StringComparison.OrdinalIgnoreCase)));

            string currentLanguage = context.CommerceContext.CurrentLanguage();
            var    cachePolicy     = context.GetPolicy <ProductFeaturesCachePolicy>();

            if (cachePolicy.AllowCaching && !keyValuePair.Value)
            {
                string cacheKey = argument.LocalizationKey + "|" + currentLanguage;
                IGetEnvironmentCachePipeline cachePipeline            = this.Commander.Pipeline <IGetEnvironmentCachePipeline>();
                EnvironmentCacheArgument     environmentCacheArgument = new EnvironmentCacheArgument();
                environmentCacheArgument.CacheName = cachePolicy.CacheName;
                await(await cachePipeline.Run(environmentCacheArgument, context)).Set(cacheKey, (ICachable) new Cachable <LocalizedTerm>(arg, 1L), cachePolicy.GetCacheEntryOptions());
                cacheKey = (string)null;
            }

            return(arg);
        }
Exemple #3
0
        /// <summary>
        /// The execute.
        /// </summary>
        /// <param name="arg">
        /// The pipeline argument.
        /// </param>
        /// <param name="context">
        /// The context.
        /// </param>
        /// <returns>
        /// The <see cref="PipelineArgument"/>.
        /// </returns>
        public async override Task <LocalizedTerm> Run(LocalizedProductFeaturesConfigurationArgument arg, CommercePipelineExecutionContext context)
        {
            Condition.Requires <LocalizedProductFeaturesConfigurationArgument>(arg).IsNotNull <LocalizedProductFeaturesConfigurationArgument>(this.Name + ": pipeline block argument can not be null");
            Condition.Requires <string>(arg.LocalizationKey).IsNotNullOrEmpty(Name + ": pipeline block argument LocalizationKey cannot be null or empty");

            context.CommerceContext.AddUniqueObjectByType((object)arg);

            var cachePolicy = context.GetPolicy <ProductFeaturesCachePolicy>();

            if (!cachePolicy.AllowCaching)
            {
                context.CommerceContext.AddUniqueObjectByType((object)new KeyValuePair <string, bool>("IsFromCache", false));
                return((LocalizedTerm)null);
            }

            string cacheKey      = arg.LocalizationKey + "|" + context.CommerceContext.CurrentLanguage();
            var    cachePipeline = this.Commander.Pipeline <IGetEnvironmentCachePipeline>();

            EnvironmentCacheArgument environmentCacheArgument = new EnvironmentCacheArgument();

            environmentCacheArgument.CacheName = cachePolicy.CacheName;

            LocalizedTerm result = (await cachePipeline.Run(environmentCacheArgument, context))?.Get(cacheKey)?.Result as LocalizedTerm;

            if (result == null)
            {
                context.CommerceContext.AddUniqueObjectByType((object)new KeyValuePair <string, bool>("IsFromCache", false));
                return((LocalizedTerm)null);
            }
            context.CommerceContext.AddUniqueObjectByType((object)new KeyValuePair <string, bool>("IsFromCache", true));
            context.Logger.LogDebug("Mgmt.GetLocMsg." + cacheKey);

            return(result);
        }
Exemple #4
0
        /// <summary>
        /// Processes the specified commerce context.
        /// </summary>
        /// <param name="commerceContext">The commerce context.</param>
        /// <param name="name">The name.</param>
        /// <returns></returns>
        public virtual async Task <FulfillmentFeeBook> Process(CommerceContext commerceContext, string name = "")
        {
            using (var activity = CommandActivity.Start(commerceContext, this))
            {
                var FulfillmentFeeBookName = commerceContext.GetPolicy <GlobalPolicy>().GetFulfillmentFeeBookName(name, commerceContext);
                var cachePolicy            = commerceContext.GetPolicy <FulfillmentFeeCachePolicy>();
                var cacheKey = string.Format("{0}{1}", commerceContext.CurrentLanguage(), FulfillmentFeeBookName);

                ICache             cache = null;
                FulfillmentFeeBook FulfillmentFeeBook = null;

                if (cachePolicy.AllowCaching)
                {
                    var environmentCacheArgument = new EnvironmentCacheArgument {
                        CacheName = cachePolicy.CacheName
                    };
                    cache = await _cachePipeline.Run(environmentCacheArgument, commerceContext.GetPipelineContextOptions());

                    FulfillmentFeeBook = await cache.Get(cacheKey) as FulfillmentFeeBook;
                }

                if (FulfillmentFeeBook == null)
                {
                    FulfillmentFeeBook = await this.GetFulfillmentFeeBook(commerceContext, FulfillmentFeeBookName);

                    if (cachePolicy.AllowCaching && cache != null)
                    {
                        await cache.Set(cacheKey, new Cachable <FulfillmentFeeBook>(FulfillmentFeeBook, 1L), cachePolicy.GetCacheEntryOptions());
                    }
                }

                return(FulfillmentFeeBook);
            }
        }
        public override async Task <PersistEntityArgument> Run(PersistEntityArgument arg, CommercePipelineExecutionContext context)
        {
            var itemKey = string.IsNullOrEmpty(arg.Entity.CompositeKey) ? $"{ arg.Entity.Id}" : $"{ arg.Entity.CompositeKey}";
            var model   = context.GetModel((Func <CacheRequest, bool>)(x => x.EntityId.Equals(arg.Entity.Id, StringComparison.OrdinalIgnoreCase)));

            if (model?.Version != null)
            {
                itemKey += $"-{model.Version}";
            }

            context.RemoveModel(model);

            EntityMemoryCachingPolicy memoryCachingPolicy1 = null;

            if (!string.IsNullOrEmpty(arg.Entity?.GetType().FullName))
            {
                memoryCachingPolicy1 = context.CommerceContext.Environment.Policies.OfType <EntityMemoryCachingPolicy>().FirstOrDefault(p => p.EntityFullName.Equals(arg.Entity.GetType().FullName, StringComparison.OrdinalIgnoreCase));
            }

            var memoryCachingPolicy2 = memoryCachingPolicy1 ?? context.CommerceContext.Environment.Policies.OfType <EntityMemoryCachingPolicy>().FirstOrDefault(p => p.EntityFullName.Equals("*", StringComparison.OrdinalIgnoreCase));

            if (memoryCachingPolicy2 != null)
            {
                var cachePipeline            = _cachePipeline;
                var environmentCacheArgument = new EnvironmentCacheArgument
                {
                    CacheName = memoryCachingPolicy2.CacheName
                };
                var pipelineContextOptions = context.CommerceContext.GetPipelineContextOptions();
                await(await cachePipeline.Run(environmentCacheArgument, pipelineContextOptions)).Remove(itemKey);
                context.Logger.LogDebug($"Core.MemCache.Remove: {itemKey}", Array.Empty <object>());
            }
            return(arg);
        }
Exemple #6
0
        public override async Task <string> Run(string arg, CommercePipelineExecutionContext context)
        {
            var priceBookName = arg;

            if (!context.GetPolicy <PriceBookPolicy>().UseCommerceShopPriceBook)
            {
                return(await Task.FromResult(priceBookName));
            }

            Sitecore.Commerce.Plugin.Shops.Shop shop = context.CommerceContext.Objects.OfType <Sitecore.Commerce.Plugin.Shops.Shop>().FirstOrDefault();
            if (shop == null)
            {
                return(await Task.FromResult(priceBookName));
            }

            string storefrontItemPath         = $"{context.GetPolicy<SitecoreControlPanelItemsPolicy>().StorefrontsPath}/{shop.Name}";
            string cacheKey                   = storefrontItemPath;
            ManagementCachePolicy cachePolicy = context.GetPolicy <ManagementCachePolicy>();
            ICache    cache                   = null;
            ItemModel storefrontItemModel;

            if (cachePolicy.AllowCaching)
            {
                EnvironmentCacheArgument environmentCacheArgument = new EnvironmentCacheArgument();
                environmentCacheArgument.CacheName = cachePolicy.ItemsCacheName;
                cache = await _cachePipeline.Run(environmentCacheArgument, context);

                storefrontItemModel = await cache.Get(cacheKey) as ItemModel;

                if (storefrontItemModel != null)
                {
                    return(await Task.FromResult(ResolvePriceBookName(priceBookName, GetPricebookName(storefrontItemModel))));
                }
            }

            context.Logger.LogInformation($"Feature.Pricing.GetStorefront.{cacheKey}");

            try
            {
                ItemModelArgument itemModelArgument = new ItemModelArgument(storefrontItemPath);
                itemModelArgument.Language = context.GetPolicy <GlobalEnvironmentPolicy>().DefaultLocale;
                storefrontItemModel        = await _getItemByPathPipeline.Run(itemModelArgument, context);

                if (storefrontItemModel != null)
                {
                    if (cachePolicy.AllowCaching)
                    {
                        await cache.Set(cacheKey, new Cachable <ItemModel>(storefrontItemModel, 1L), cachePolicy.GetCacheEntryOptions());
                    }

                    return(await Task.FromResult(ResolvePriceBookName(priceBookName, GetPricebookName(storefrontItemModel))));
                }
            }
            catch (Exception ex)
            {
                string str = await context.CommerceContext.AddMessage(context.GetPolicy <KnownResultCodes>().Error, "CommerceTermsNotFound", new object[2]
                {
                    storefrontItemPath,
                    ex
                }, string.Format("Sitecore commerce terms at {0} were not found.", storefrontItemPath));
            }

            return(await Task.FromResult(priceBookName));
        }
Exemple #7
0
        public override async Task <List <string> > Run(string catalogName, CommercePipelineExecutionContext context)
        {
            List <InventorySet> inventorySets = new List <InventorySet>();

            GetProductsToUpdateInventoryBlock getProductsToUpdateInventoryBlock = this;

            // get the sitecoreid from catalog based on catalogname
            var catalogSitecoreId = "";
            FindEntitiesInListArgument entitiesInListArgumentCatalog = await getProductsToUpdateInventoryBlock._findEntitiesInListPipeline.Run(new FindEntitiesInListArgument(typeof(Catalog), string.Format("{0}", (object)CommerceEntity.ListName <Catalog>()), 0, int.MaxValue), context);

            foreach (CommerceEntity commerceEntity in (await getProductsToUpdateInventoryBlock._findEntitiesInListPipeline.Run(entitiesInListArgumentCatalog, (IPipelineExecutionContextOptions)context.ContextOptions)).List.Items)
            {
                var item = commerceEntity as Catalog;

                if (item.Name == catalogName)
                {
                    catalogSitecoreId = item.SitecoreId;
                    break;
                }
            }


            if (string.IsNullOrEmpty(catalogSitecoreId))
            {
                return(null);
            }

            List <string> productIds = new List <string>();


            string              cacheKey      = string.Format("{0}|{1}|{2}", context.CommerceContext.Environment.Name, context.CommerceContext.CurrentLanguage(), context.CommerceContext.CurrentShopName() ?? "");
            CatalogCachePolicy  cachePolicy   = context.GetPolicy <CatalogCachePolicy>();
            ICache              cache         = null;
            List <SellableItem> sellableItems = null;

            if (cachePolicy.AllowCaching)
            {
                IGetEnvironmentCachePipeline cachePipeline            = getProductsToUpdateInventoryBlock._cachePipeline;
                EnvironmentCacheArgument     environmentCacheArgument = new EnvironmentCacheArgument();
                environmentCacheArgument.CacheName = cachePolicy.CatalogsCacheName;
                CommercePipelineExecutionContext context1 = context;
                cache = await cachePipeline.Run(environmentCacheArgument, context1).ConfigureAwait(false);

                sellableItems = await cache.Get(cacheKey).ConfigureAwait(false) as List <SellableItem>;

                if (sellableItems != null)
                {
                    foreach (var item in sellableItems)
                    {
                        await GetProductId(context, getProductsToUpdateInventoryBlock, catalogSitecoreId, productIds, item);
                    }
                }
            }
            else
            {
                sellableItems = new List <SellableItem>();
                FindEntitiesInListArgument entitiesInListArgument = new FindEntitiesInListArgument(typeof(SellableItem), string.Format("{0}", (object)CommerceEntity.ListName <SellableItem>()), 0, int.MaxValue);
                foreach (CommerceEntity commerceEntity in (await getProductsToUpdateInventoryBlock._findEntitiesInListPipeline.Run(entitiesInListArgument, context.ContextOptions).ConfigureAwait(false)).List.Items)
                {
                    await GetProductId(context, getProductsToUpdateInventoryBlock, catalogSitecoreId, productIds, commerceEntity as SellableItem).ConfigureAwait(false);
                }

                if (cachePolicy.AllowCaching)
                {
                    if (cache != null)
                    {
                        await cache.Set(cacheKey, new Cachable <List <SellableItem> >(sellableItems, 1L), cachePolicy.GetCacheEntryOptions()).ConfigureAwait(false);
                    }
                }
            }

            return(productIds);
        }