/// <summary> /// Executes the workflow to retrieve changed products. /// </summary> /// <param name="request">The request.</param> /// <returns>The response.</returns> protected override ChangedProductsSearchResponse Process(ChangedProductsSearchRequest request) { ThrowIf.Null(request, "request"); if (request.RequestForChanges == null) { throw new DataValidationException(DataValidationErrors.Microsoft_Dynamics_Commerce_Runtime_RequiredValueNotFound, "The query criteria must be specified."); } long channelId = this.Context.GetPrincipal().ChannelId; if (request.RequestForChanges.Context.IsRemoteLookup(channelId)) { string message = string.Format( CultureInfo.InvariantCulture, "The specified context (Channel={0}, Catalog={1}) is not supported when retrieving changed products.", request.RequestForChanges.Context.ChannelId, request.RequestForChanges.Context.CatalogId); throw new NotSupportedException(message); } GetChangedProductsDataRequest dataRequest = new GetChangedProductsDataRequest(request.RequestForChanges, request.QueryResultSettings); var products = this.Context.Runtime.Execute <SingleEntityDataServiceResponse <ChangedProductsSearchResult> >(dataRequest, this.Context).Entity; if (products.Results.Count > 0) { // retrieve and update prices var priceRequest = new GetProductPricesServiceRequest(products.Results); var priceResponse = this.Context.Execute <GetProductPricesServiceResponse>(priceRequest); var productMap = new Dictionary <long, Product>(products.Results.Count); var productIdLookupMap = new Dictionary <long, long>(); foreach (var product in products.Results) { productMap[product.RecordId] = product; productIdLookupMap[product.RecordId] = product.RecordId; if (product.IsMasterProduct) { foreach (var variant in product.GetVariants()) { productIdLookupMap[variant.DistinctProductVariantId] = product.RecordId; } } } // update prices on the products SearchProductsRequestHandler.SetProductPrices(priceResponse.ProductPrices.Results, productMap, productIdLookupMap); } return(new ChangedProductsSearchResponse(products)); }
/// <summary> /// Executes the workflow to retrieve products using the specified criteria. /// </summary> /// <param name="request">The request.</param> /// <returns>The response.</returns> protected override ProductSearchResponse Process(ProductSearchRequest request) { ThrowIf.Null(request, "request"); if (request.QueryCriteria == null) { throw new DataValidationException(DataValidationErrors.Microsoft_Dynamics_Commerce_Runtime_RequiredValueNotFound, "The query criteria must be specified."); } if (request.QueryCriteria.Context == null || request.QueryCriteria.Context.ChannelId.HasValue == false) { throw new DataValidationException(DataValidationErrors.Microsoft_Dynamics_Commerce_Runtime_RequiredValueNotFound, "The channel identifier on the query criteria context must be specified."); } long currentChannelId = this.Context.GetPrincipal().ChannelId; var searchRequest = new ProductSearchServiceRequest(request.QueryCriteria, request.QueryResultSettings); ProductSearchServiceResponse searchResponse = this.Context.Runtime.Execute <ProductSearchServiceResponse>(searchRequest, this.Context); ProductSearchResultContainer results = searchResponse.ProductSearchResult; if (results.Results.Any() && !request.QueryCriteria.Context.IsRemoteLookup(currentChannelId)) { // retrieve and update prices var priceRequest = new GetProductPricesServiceRequest(results.Results); var priceResponse = this.Context.Execute <GetProductPricesServiceResponse>(priceRequest); var productMap = new Dictionary <long, Product>(results.Results.Count); foreach (var product in results.Results) { productMap[product.RecordId] = product; } // update prices on the products SearchProductsRequestHandler.SetProductPrices(priceResponse.ProductPrices.Results, productMap, results.ProductIdLookupMap); } return(new ProductSearchResponse(results)); }