Esempio n. 1
0
 /// <summary>
 /// Initializes a new instance of the <see cref="CatalogItemWithPriceModel"/> class.
 /// </summary>
 /// <param name="item">The item.</param>
 /// <param name="price">The price.</param>
 /// <param name="availability">The availability.</param>
 public CatalogItemWithPriceModel(ItemModel item, PriceModel price, ItemAvailabilityModel availability)
 {
     _item         = item;
     _price        = price;
     _availability = availability;
     _itemReviews  = new ReviewTotals {
         ItemId = item.ItemId
     };
 }
Esempio n. 2
0
        /// <summary>
        /// Creates the catalog model.
        /// </summary>
        /// <param name="itemId">The item identifier.</param>
        /// <param name="parentItemId">The parent item identifier.</param>
        /// <param name="associationType">Type of the association.</param>
        /// <param name="forcedActive">if set to <c>true</c> [forced active].</param>
        /// <param name="responseGroups">The response groups.</param>
        /// <param name="display">The display.</param>
        /// <param name="byItemCode">if set to <c>true</c> gets item by code.</param>
        /// <returns>
        /// CatalogItemWithPriceModel.
        /// </returns>
        public static CatalogItemWithPriceModel CreateCatalogModel(string itemId,
            string parentItemId = null,
            string associationType = null,
            bool forcedActive = false,
            ItemResponseGroups responseGroups = ItemResponseGroups.ItemLarge,
            ItemDisplayOptions display = ItemDisplayOptions.ItemLarge,
            bool byItemCode = false)
        {

            var dbItem = CatalogClient.GetItem(itemId, responseGroups,
                                              UserHelper.CustomerSession.CatalogId, bycode: byItemCode);
            if (dbItem != null)
            {

                if (dbItem.IsActive || forcedActive)
                {
                    PriceModel priceModel = null;
                    PropertySet propertySet = null;
                    //ItemRelation[] variations = null;
                    ItemAvailabilityModel itemAvaiability = null;

                    if (display.HasFlag(ItemDisplayOptions.ItemPropertySets))
                    {
                        propertySet = CatalogClient.GetPropertySet(dbItem.PropertySetId);
                        //variations = CatalogClient.GetItemRelations(itemId);
                    }

                    var itemModel = CreateItemModel(dbItem, propertySet);

                    if (display.HasFlag(ItemDisplayOptions.ItemAvailability))
                    {
                        var fulfillmentCenter = UserHelper.StoreClient.GetCurrentStore().FulfillmentCenterId;
                        var availability = CatalogClient.GetItemAvailability(dbItem.ItemId, fulfillmentCenter);
                        itemAvaiability = new ItemAvailabilityModel(availability);
                    }

                    if (display.HasFlag(ItemDisplayOptions.ItemPrice))
                    {
                        var lowestPrice = PriceListClient.GetLowestPrice(dbItem.ItemId, itemAvaiability != null ? itemAvaiability.MinQuantity : 1);
                        var outlines = OutlineBuilder.BuildCategoryOutline(CatalogClient.CustomerSession.CatalogId, dbItem.ItemId);
                        var tags = new Hashtable
							{
								{
									"Outline",
                                    outlines.ToString()
                                }
							};
                        priceModel = MarketingHelper.GetItemPriceModel(dbItem, lowestPrice, tags);
                        itemModel.CatalogOutlines = outlines;

                        // get the category name
                        if (outlines.Count > 0)
                        {
                            var outline = outlines[0];
                            if (outline.Categories.Count > 0)
                            {
                                var category = outline.Categories.OfType<Category>().Reverse().FirstOrDefault();
                                if (category != null)
                                {
                                    itemModel.CategoryName = category.Name;
                                }
                            }
                        }
                    }

                    itemModel.ParentItemId = parentItemId;

                    return string.IsNullOrEmpty(associationType)
                               ? new CatalogItemWithPriceModel(itemModel, priceModel, itemAvaiability)
                               : new AssociatedCatalogItemWithPriceModel(itemModel, priceModel, itemAvaiability, associationType);
                }
            }

            return null;
        }
Esempio n. 3
0
		/// <summary>
		/// Initializes a new instance of the <see cref="CatalogItemWithPriceModel"/> class.
		/// </summary>
		/// <param name="item">The item.</param>
		/// <param name="price">The price.</param>
		/// <param name="availability">The availability.</param>
        public CatalogItemWithPriceModel(ItemModel item, PriceModel price, ItemAvailabilityModel availability)
        {
            _item = item;
            _price = price;
            _availability = availability;
		    _itemReviews = new ReviewTotals {ItemId = item.ItemId};
        }
Esempio n. 4
0
		/// <summary>
		/// Initializes a new instance of the <see cref="AssociatedCatalogItemWithPriceModel"/> class.
		/// </summary>
		/// <param name="item">The item.</param>
		/// <param name="price">The price.</param>
		/// <param name="availability">The availability.</param>
		/// <param name="associationType">Type of the association.</param>
        public AssociatedCatalogItemWithPriceModel(ItemModel item, PriceModel price, ItemAvailabilityModel availability,
                                                   string associationType) :
            base(item, price, availability)
        {
            _associationType = associationType;
        }
        /// <summary>
        /// Creates the data model.
        /// </summary>
        /// <param name="criteria">The criteria.</param>
        /// <param name="parameters">The parameters.</param>
        /// <param name="cacheResults">if set to <c>true</c> [cache results].</param>
        /// <returns>CatalogItemSearchModel.</returns>
        private CatalogItemSearchModel CreateDataModel(CatalogItemSearchCriteria criteria, SearchParameters parameters,
                                                       bool cacheResults)
        {
            var session = UserHelper.CustomerSession;

            // Create a model
            var dataSource = new CatalogItemSearchModel();

            // Now fill in filters
            var filters = _searchFilter.Filters;

            // Add all filters
            foreach (var filter in filters)
            {
                criteria.Add(filter);
            }

            // Get selected filters
            var facets = parameters.Facets;
            dataSource.SelectedFilters = new List<SelectedFilterModel>();
            if (facets.Count != 0)
            {
                foreach (var key in facets.Keys)
                {
                    var filter = filters.SingleOrDefault(x => x.Key.Equals(key, StringComparison.OrdinalIgnoreCase)
                        && (!(x is PriceRangeFilter) || ((PriceRangeFilter)x).Currency.Equals(StoreHelper.CustomerSession.Currency, StringComparison.OrdinalIgnoreCase)));

                    var appliedFilter = _searchFilter.Convert(filter, facets[key]);

                    foreach (var val in appliedFilter.GetValues())
                    {
                        criteria.Apply(appliedFilter);
                        dataSource.SelectedFilters.Add(
                            new SelectedFilterModel(_searchFilter.Convert(filter), _searchFilter.Convert(val)));
                    }
                }
            }

            // Perform search
            var sort = string.IsNullOrEmpty(parameters.Sort) ? "position" : parameters.Sort;
            var sortOrder = parameters.SortOrder;

            var isDescending = "desc".Equals(sortOrder, StringComparison.OrdinalIgnoreCase);

            SearchSort sortObject = null;

            switch (sort.ToLowerInvariant())
            {
                case "price":
                    if (session.Pricelists != null)
                    {
                        sortObject = new SearchSort(session.Pricelists.Select(priceList =>
                            new SearchSortField(
                                String.Format("price_{0}_{1}",
                                    criteria.Currency.ToLower(),
                                    priceList.ToLower()))
                            {
                                IgnoredUnmapped = true,
                                IsDescending = isDescending,
                                DataType = SearchSortField.DOUBLE
                            })
                            .ToArray());
                    }
                    break;
                case "position":
                    sortObject = new SearchSort(new SearchSortField(string.Format("sort{0}{1}",session.CatalogId,session.CategoryId).ToLower())
                    {
                        IgnoredUnmapped = true,
                        IsDescending = isDescending
                    });
                    break;
                case "name":
                    sortObject = new SearchSort("name", isDescending);
                    break;
                case "rating":
                    sortObject = new SearchSort(criteria.ReviewsAverageField, isDescending);
                    break;
                case "reviews":
                    sortObject = new SearchSort(criteria.ReviewsTotalField, isDescending);
                    break;
                default:
                    sortObject = CatalogItemSearchCriteria.DefaultSortOrder;
                    break;

            }

            criteria.Sort = sortObject;
            CatalogItemSearchResults results;
            // Search using criteria, it will only return IDs of the items
            var items = Search(criteria, cacheResults, out results).ToArray();
            var itemsIdsArray = items.Select(i => i.ItemId).ToArray();

            // Now load items with appropriate 
            var itemModelList = new List<CatalogItemWithPriceModel>();
            if (items.Any())
            {
                // Now convert it to the model
                var prices = _priceListClient.GetLowestPrices(session.Pricelists, itemsIdsArray, 1);
                var availabilities = _catalogClient.GetItemAvailability(itemsIdsArray,
                                                   UserHelper.StoreClient.GetCurrentStore().FulfillmentCenterId);

                foreach (var item in items)
                {
                    PriceModel priceModel = null;
                    ItemAvailabilityModel availabilityModel = null;
                    var searchTags = results.Items[item.ItemId.ToLower()];

                    var currentOutline = this.GetItemOutlineUsingContext(searchTags[criteria.OutlineField].ToString());

                    //Cache outline
                    HttpContext.Items["browsingoutline_" + item.ItemId.ToLower()] = StripCatalogFromOutline(currentOutline);

                    if (prices != null && prices.Any())
                    {
                        var lowestPrice = (prices.Where(p => p.ItemId.Equals(item.ItemId, StringComparison.OrdinalIgnoreCase))).SingleOrDefault();
                        if (lowestPrice != null)
                        {
                            var tags = new Hashtable
							{
								{
									"Outline",
									currentOutline
								}
							};
                            priceModel = _marketing.GetItemPriceModel(item, lowestPrice, tags);
                        }
                    }

                    if (availabilities != null && availabilities.Any())
                    {
                        var availability =
                            (from a in availabilities
                             where a.ItemId.Equals(item.ItemId, StringComparison.OrdinalIgnoreCase)
                             select a).SingleOrDefault();

                        availabilityModel = new ItemAvailabilityModel(availability);
                    }

                    var itemModel = new CatalogItemWithPriceModel(CatalogHelper.CreateItemModel(item), priceModel, availabilityModel)
                    {
                        SearchOutline = currentOutline
                    };

                    try
                    {
                        itemModel.ItemReviewTotals.AverageRating = double.Parse(searchTags[criteria.ReviewsAverageField].ToString());
                        itemModel.ItemReviewTotals.TotalReviews = int.Parse(searchTags[criteria.ReviewsTotalField].ToString());
                    }
                    catch
                    {
                        //There are no reviews indexed?
                    }
                    itemModelList.Add(itemModel);
                }
            }

            dataSource.FilterGroups = _searchFilter.Convert(results.FacetGroups);
            dataSource.CatalogItems = itemModelList.ToArray();
            dataSource.Criteria = criteria;

            // Create pager
            var pager = new PagerModel
            {
                TotalCount = results.TotalCount,
                CurrentPage = criteria.StartingRecord / criteria.RecordsToRetrieve + 1,
                RecordsPerPage = criteria.RecordsToRetrieve,
                StartingRecord = criteria.StartingRecord,
                DisplayStartingRecord = criteria.StartingRecord + 1,
                SortValues = new[] { "Position", "Name", "Price", "Rating", "Reviews" },
                SelectedSort = sort,
                SortOrder = isDescending ? "desc" : "asc"
            };

            var end = criteria.StartingRecord + criteria.RecordsToRetrieve;
            pager.DisplayEndingRecord = end > results.TotalCount ? results.TotalCount : end;

            dataSource.Pager = pager;
            return dataSource;
        }
        /// <summary>
        /// Creates the data model.
        /// </summary>
        /// <param name="criteria">The criteria.</param>
        /// <param name="parameters">The parameters.</param>
        /// <param name="cacheResults">if set to <c>true</c> [cache results].</param>
        /// <returns>CatalogItemSearchModel.</returns>
        private CatalogItemSearchModel CreateDataModel(CatalogItemSearchCriteria criteria, SearchParameters parameters,
                                                       bool cacheResults)
        {
            var session = UserHelper.CustomerSession;

            // Create a model
            var dataSource = new CatalogItemSearchModel();

            // Now fill in filters
            var searchHelper = new SearchHelper(_storeClient.GetCurrentStore());

            var filters = searchHelper.Filters;

            // Add all filters
            foreach (var filter in filters)
            {
                // Check if we already filtering
                if (parameters.Facets.Keys.Any(k => filter.Key.Equals(k, StringComparison.OrdinalIgnoreCase)))
                    continue;

                criteria.Add(filter);
            }

            // Get selected filters
            var facets = parameters.Facets;
            dataSource.SelectedFilters = new List<SelectedFilterModel>();
            if (facets.Count != 0)
            {
                foreach (var key in facets.Keys)
                {
                    var filter = filters.SingleOrDefault(x => x.Key.Equals(key, StringComparison.OrdinalIgnoreCase)
                        && (!(x is PriceRangeFilter) || ((PriceRangeFilter)x).Currency.Equals(StoreHelper.CustomerSession.Currency, StringComparison.OrdinalIgnoreCase)));
                    var val =
                        (from v in searchHelper.GetFilterValues(filter) where v.Id == facets[key] select v)
                            .SingleOrDefault();
                    if (val != null)
                    {
                        criteria.Add(filter, val);
                        dataSource.SelectedFilters.Add(new SelectedFilterModel(searchHelper.Convert(filter),
                                                                               searchHelper.Convert(val)));
                    }
                }
            }

            // Perform search
            var sort = string.IsNullOrEmpty(parameters.Sort) ? "position" : parameters.Sort;
            var sortOrder = parameters.SortOrder;

            bool isDescending = "desc".Equals(sortOrder, StringComparison.OrdinalIgnoreCase);

            SearchSort sortObject = null;

            if (!sort.Equals("position", StringComparison.OrdinalIgnoreCase))
            {
                if (sort.Equals("price", StringComparison.OrdinalIgnoreCase))
                {
                    if (session.Pricelists != null)
                    {
                        sortObject = new SearchSort(session.Pricelists.Select(priceList =>
                            new SearchSortField(
                                String.Format("price_{0}_{1}",
                                    criteria.Currency.ToLower(),
                                    priceList.ToLower()))
                            {
                                IgnoredUnmapped = true,
                                IsDescending = isDescending,
                                DataType = SearchSortField.DOUBLE
                            })
                            .ToArray());
                    }
                }
                else
                {
                    sortObject = new SearchSort(sort.ToLower(), isDescending);
                }
            }

            // Put default sort order if none is set
            if (sortObject == null)
            {
                sortObject = CatalogItemSearchCriteria.DefaultSortOrder;
            }

            criteria.Sort = sortObject;
            CatalogItemSearchResults results;
            // Search using criteria, it will only return IDs of the items
            var items = Search(criteria, cacheResults, out results).ToArray();
            var itemsIdsArray = items.Select(i => i.ItemId).ToArray();

            // Now load items with appropriate 
            var itemModelList = new List<CatalogItemWithPriceModel>();
            if (items.Any())
            {

                // Now convert it to the model

                var prices = _priceListClient.GetLowestPrices(session.Pricelists, itemsIdsArray, 1);
                var availabilities = _catalogClient.GetItemAvailability(itemsIdsArray,
                                                   UserHelper.StoreClient.GetCurrentStore().FulfillmentCenterId);

                foreach (var item in items)
                {
                    PriceModel priceModel = null;
                    ItemAvailabilityModel availabilityModel = null;
                    var catalogIdPath = UserHelper.CustomerSession.CatalogId + "/";
                    var searchTags = results.Items[item.ItemId.ToLower()].ToPropertyDictionary();

                    //Cache outline
                    HttpContext.Items["browsingoutline_" + item.Code.ToLower()] = searchTags[criteria.BrowsingOutlineField].ToString();
                    var currentOutline = searchTags[criteria.OutlineField].ToString().Split(new[] { ';' }, StringSplitOptions.RemoveEmptyEntries)
                        .FirstOrDefault(x => x.StartsWith(catalogIdPath, StringComparison.OrdinalIgnoreCase)) ?? string.Empty;

                    if (prices != null && prices.Any())
                    {
                        var lowestPrice =
                            (from p in prices
                             where p.ItemId.Equals(item.ItemId, StringComparison.OrdinalIgnoreCase)
                             select p).SingleOrDefault();
                        if (lowestPrice != null)
                        {
                            var tags = new Hashtable
							{
								{
									"Outline",
									currentOutline
								}
							};
                            priceModel = _marketing.GetItemPriceModel(item, lowestPrice, tags);
                        }
                    }

                    if (availabilities != null && availabilities.Any())
                    {
                        var availability =
                            (from a in availabilities
                             where a.ItemId.Equals(item.ItemId, StringComparison.OrdinalIgnoreCase)
                             select a).SingleOrDefault();

                        availabilityModel = new ItemAvailabilityModel(availability);
                    }

                    var itemModel = new CatalogItemWithPriceModel(CatalogHelper.CreateItemModel(item), priceModel, availabilityModel)
                    {
                        SearchOutline = currentOutline
                    };
                    itemModelList.Add(itemModel);
                }
            }

            dataSource.FilterGroups = searchHelper.Convert(results.FacetGroups);
            dataSource.CatalogItems = itemModelList.ToArray();
            dataSource.Criteria = criteria;

            // Create pager
            var pager = new PagerModel
            {
                TotalCount = results.TotalCount,
                CurrentPage = criteria.StartingRecord / criteria.RecordsToRetrieve + 1,
                RecordsPerPage = criteria.RecordsToRetrieve,
                StartingRecord = criteria.StartingRecord,
                DisplayStartingRecord = criteria.StartingRecord + 1,
                SortValues = new[] { "Position", "Name", "Price" },
                SelectedSort = sort,
                SortOrder = isDescending ? "desc" : "asc"
            };

            var end = criteria.StartingRecord + criteria.RecordsToRetrieve;
            pager.DisplayEndingRecord = end > results.TotalCount ? results.TotalCount : end;

            dataSource.Pager = pager;

            // Query similar words
            /*
            if (count == 0)
                dataSource.Suggestions = GetSuggestions();
             * */
            //}

            return dataSource;
        }
Esempio n. 7
0
		/// <summary>
		/// Initializes a new instance of the <see cref="CatalogItemWithPriceModel"/> class.
		/// </summary>
		/// <param name="item">The item.</param>
		/// <param name="price">The price.</param>
		/// <param name="availability">The availability.</param>
        public CatalogItemWithPriceModel(ItemModel item, PriceModel price, ItemAvailabilityModel availability)
        {
            _item = item;
            _price = price;
            _availability = availability;
        }
Esempio n. 8
0
 /// <summary>
 /// Initializes a new instance of the <see cref="AssociatedCatalogItemWithPriceModel"/> class.
 /// </summary>
 /// <param name="item">The item.</param>
 /// <param name="price">The price.</param>
 /// <param name="availability">The availability.</param>
 /// <param name="associationType">Type of the association.</param>
 public AssociatedCatalogItemWithPriceModel(ItemModel item, PriceModel price, ItemAvailabilityModel availability,
                                            string associationType) :
     base(item, price, availability)
 {
     _associationType = associationType;
 }