/// <summary> /// Gets the product item by URL. /// </summary> /// <param name="url">The full URL.</param> /// <returns>The product item by URL.</returns> public virtual Item GetProductItem(string url) { Assert.ArgumentNotNullOrEmpty(url, "url"); object value; lock (this.ProductsUrlsCollection.SyncRoot) { value = this.ProductsUrlsCollection[System.Web.HttpUtility.UrlDecode(url)]; } if (value == null) { Log.Debug("Unable to parse product url '{0}'.".FormatWith(url), this); this.ProductCatalogItem = default(Item); this.ProductPresentationItem = default(Item); return(default(Item)); } ProductUriLine productUriLine = (ProductUriLine)value; Item productItem = Database.GetItem(new ItemUri(productUriLine.ProductItemUri)); this.ProductPresentationItem = Database.GetItem(new ItemUri(productUriLine.ProductPresentationItemUri)); this.ProductCatalogItem = Database.GetItem(new ItemUri(productUriLine.ProductCatalogItemUri)); return(productItem); }
/// <summary> /// Gets the virtual product URL. /// </summary> /// <param name="catalogItem">The catalog item.</param> /// <param name="productItem">The product item.</param> /// <returns>The virtual product URL.</returns> public virtual string GetVirtualProductUrl(Item catalogItem, Item productItem) { Assert.ArgumentNotNull(catalogItem, "catalogItem"); Assert.ArgumentNotNull(productItem, "productItem"); if (catalogItem.Template != null && ProductRepositoryUtil.IsBasedOnTemplate(catalogItem.Template, this.productBaseTemplateId)) { if (this.ProductCatalogItem != default(Item) && !ProductRepositoryUtil.IsBasedOnTemplate(this.ProductCatalogItem.Template, this.productBaseTemplateId)) { catalogItem = this.ProductCatalogItem; } else { lock (this.ProductsUrlsCollection.SyncRoot) { foreach (DictionaryEntry dictionaryEntry in this.ProductsUrlsCollection) { var uriLine = (ProductUriLine)dictionaryEntry.Value; if (string.Compare(uriLine.ProductItemUri, catalogItem.Uri.ToString(), true) == 0) { catalogItem = Database.GetItem(new ItemUri(uriLine.ProductCatalogItemUri)); } } } } } string key = this.GetDisplayProductsModeKey(catalogItem); // NOTE: Check whether items are presented in the url collection lock (this.ProductsUrlsCollection.SyncRoot) { foreach (DictionaryEntry entry in this.ProductsUrlsCollection) { ProductUriLine line = (ProductUriLine)entry.Value; if (line.ProductItemUri == productItem.Uri.ToString() && line.ProductCatalogItemUri == catalogItem.Uri.ToString() && line.DisplayProductsMode == key) { return System.Web.HttpUtility.UrlPathEncode(entry.Key as string); } } } ProductUrlProcessor productUrlBuilder; if (Context.Entity.HasRegistration(typeof(ProductUrlProcessor), key)) { productUrlBuilder = Context.Entity.Resolve<ProductUrlProcessor>(key); } else { Log.Warn(string.Format("ProductUrlProcessor is not defined with the name: {0}. Check Unity.config file.", key), this); return LinkManager.GetItemUrl(productItem); } string url = productUrlBuilder.GetProductUrl(catalogItem, productItem).ToString(); Item productPresentationItem = this.GetProductPresentationItem(catalogItem); Assert.IsNotNull(productPresentationItem, "Product presentation item is null"); ProductUriLine productUriLine = new ProductUriLine { ProductItemUri = productItem.Uri.ToString(), ProductCatalogItemUri = catalogItem.Uri.ToString(), ProductPresentationItemUri = productPresentationItem.Uri.ToString(), DisplayProductsMode = key, }; string result = System.Web.HttpUtility.UrlDecode(url); lock (this.ProductsUrlsCollection.SyncRoot) { this.ProductsUrlsCollection[result] = productUriLine; } return System.Web.HttpUtility.UrlPathEncode(url); }
/// <summary> /// Gets the virtual product URL. /// </summary> /// <param name="catalogItem">The catalog item.</param> /// <param name="productItem">The product item.</param> /// <returns>The virtual product URL.</returns> public virtual string GetVirtualProductUrl(Item catalogItem, Item productItem) { Assert.ArgumentNotNull(catalogItem, "catalogItem"); Assert.ArgumentNotNull(productItem, "productItem"); if (catalogItem.Template != null && ProductRepositoryUtil.IsBasedOnTemplate(catalogItem.Template, this.productBaseTemplateId)) { if (this.ProductCatalogItem != default(Item) && !ProductRepositoryUtil.IsBasedOnTemplate(this.ProductCatalogItem.Template, this.productBaseTemplateId)) { catalogItem = this.ProductCatalogItem; } else { lock (this.ProductsUrlsCollection.SyncRoot) { foreach (DictionaryEntry dictionaryEntry in this.ProductsUrlsCollection) { var uriLine = (ProductUriLine)dictionaryEntry.Value; if (string.Compare(uriLine.ProductItemUri, catalogItem.Uri.ToString(), true) == 0) { catalogItem = Database.GetItem(new ItemUri(uriLine.ProductCatalogItemUri)); } } } } } string key = this.GetDisplayProductsModeKey(catalogItem); // NOTE: Check whether items are presented in the url collection lock (this.ProductsUrlsCollection.SyncRoot) { foreach (DictionaryEntry entry in this.ProductsUrlsCollection) { ProductUriLine line = (ProductUriLine)entry.Value; if (line.ProductItemUri == productItem.Uri.ToString() && line.ProductCatalogItemUri == catalogItem.Uri.ToString() && line.DisplayProductsMode == key) { return(System.Web.HttpUtility.UrlPathEncode(entry.Key as string)); } } } ProductUrlProcessor productUrlBuilder; if (Context.Entity.HasRegistration(typeof(ProductUrlProcessor), key)) { productUrlBuilder = Context.Entity.Resolve <ProductUrlProcessor>(key); } else { Log.Warn(string.Format("ProductUrlProcessor is not defined with the name: {0}. Check Unity.config file.", key), this); return(LinkManager.GetItemUrl(productItem)); } string url = productUrlBuilder.GetProductUrl(catalogItem, productItem).ToString(); Item productPresentationItem = this.GetProductPresentationItem(catalogItem); Assert.IsNotNull(productPresentationItem, "Product presentation item is null"); ProductUriLine productUriLine = new ProductUriLine { ProductItemUri = productItem.Uri.ToString(), ProductCatalogItemUri = catalogItem.Uri.ToString(), ProductPresentationItemUri = productPresentationItem.Uri.ToString(), DisplayProductsMode = key, }; string result = System.Web.HttpUtility.UrlDecode(url); lock (this.ProductsUrlsCollection.SyncRoot) { this.ProductsUrlsCollection[result] = productUriLine; } return(System.Web.HttpUtility.UrlPathEncode(url)); }