Example #1
0
        /// <summary>
        /// Get some display text for a PRODUCT IMPORT RESPONSE to be shown in the console.
        /// </summary>
        /// <param name="productImportResponse"></param>
        /// <returns></returns>
        public static string GetDisplayText(this ProductImportResponse productImportResponse)
        {
            StringBuilder messageBuilder = new StringBuilder();

            messageBuilder.AppendLine("Response:");
            messageBuilder.Append(" - Import Reference: ");
            messageBuilder.AppendLine(productImportResponse.Reference);

            messageBuilder.Append(" - ");
            messageBuilder.AppendLine(productImportResponse.Summary.ToMessagesString());
            messageBuilder.AppendLine(" - Please check the status by following ");
            messageBuilder.AppendLine("   Configuration / Import Log in the admin portal.");

            if (productImportResponse.Summary.Messages.Count > 0)
            {
                var resultMessage = productImportResponse.Summary.Messages.FirstOrDefault();
                if (resultMessage != null)
                {
                    if (string.Compare(resultMessage.Code, "IM105", StringComparison.InvariantCultureIgnoreCase) == 0)
                    {
                        messageBuilder.AppendLine("   then check under Configuration / Products");
                        messageBuilder.AppendLine("   and look for the \"Example imported product\"");
                    }
                }
            }
            return(messageBuilder.ToString());
        }
        private ProductImportResponse ProcessProducts(List<ExportQueueItem> productsToProcess, List<ProductAttributeConfigItem> productAttributeConfigItems, List<Vendor> vendors, List<TaxCategory> taxCategories)
        {
            ProductImportResponse result = null;           
            List<int> productIdsToProcess = new List<int>();

            // Build a list of the product Ids to process
            for (int i = 0; i < 2; i++)
            {
                if (i == 0)
                {
                    productIdsToProcess.AddRange(
                        productsToProcess.Where(p => p.Action != ExportQueueAction.All)
                                         .Select(p => p.EntityKey)
                                         .ToList());
                }
                else
                {
                    // In theory, if we have an 'All' action, the only other items in the queue should be deletes.
                    if (productsToProcess.Any(ei => ei.Action == ExportQueueAction.All))
                    {
                        var allProducts = _productService.SearchProducts(productType: ProductType.SimpleProduct, storeId: _promoSettings.StoreId);
                        productIdsToProcess.AddRange(
                                allProducts.Select(p => p.Id)
                                           .ToList());
                    }
                }
            }

            if (productIdsToProcess.Count > 0)
            {
                int batch = 0;
                var batchProductIds = productIdsToProcess.Skip(batch).Take(_promoSettings.BatchSize);
                List<ProductImportResponse> allBatchResults = new List<ProductImportResponse>();
                bool commsError = false;

                while (batchProductIds.Count() > 0)
                {
                    var importRequest = BuildBaseProductImport();
                    var productsToAdd = new List<ProductImportRequestItem>();
                    var productMappings = new List<InternalProductVariantItem>();

                    // For all product Ids, get the import details.           
                    batchProductIds.ToList().ForEach(p =>
                    {
                        var importDetails = GetImportProduct(p, productAttributeConfigItems, vendors, taxCategories);
                        if (importDetails != null)
                        {
                            productsToAdd.AddRange(importDetails.ImportProducts);
                            productMappings.AddRange(importDetails.VariantItems);
                        }
                    });

                    importRequest.Products.AddRange(productsToAdd);

                    // Send to Promo
                    var batchResult = _qixolPromoUtilities.ImportProductsToPromoService(importRequest);

                    if(batchResult != null)
                        allBatchResults.Add(batchResult);

                    if (batchResult != null && batchResult.Summary != null && batchResult.Summary.ProcessedSuccessfully)
                    {
                        // Now update the variant mappings...
                        productMappings.ForEach(v =>
                        {
                            if (v.Delete)
                            {
                                if (v.ExistingItem != null)
                                    _productMappingService.Delete(v.ExistingItem);
                            }
                            else
                            {
                                _productMappingService.Insert(new ProductMappingItem()
                                {
                                    AttributesXml = v.AttributesXml,
                                    EntityName = EntityAttributeName.Product,
                                    EntityId = v.ProductId,
                                    VariantCode = v.VariantCode,
                                    NoVariants = v.NoVariants
                                });
                            }
                        });

                        batch += _promoSettings.BatchSize;
                        batchProductIds = productIdsToProcess.Skip(batch).Take(_promoSettings.BatchSize);
                    }
                    else
                    {
                        // This should not happen!!
                        commsError = true;
                        break;
                    }
                }

                if (!commsError)
                {
                    result = new ProductImportResponse();
                    result.Summary = new ImportResponseSummary() { ProcessedSuccessfully = allBatchResults.Where(br => br.Summary != null && !br.Summary.ProcessedSuccessfully).Count() == 0 };
                    result.Summary.Messages = new List<ImportResponseMessage>();

                    allBatchResults.ForEach(br =>
                    {
                        //result.Summary.Messages.Add(new ImportResponseMessage() { Code = br.Reference, Message = br.GetMessages() });
                    });
                }
            }
            else
            {
                result = new ProductImportResponse();
                result.Summary = new ImportResponseSummary() { ProcessedSuccessfully = true };
            }

            return result;    
        }
        private ProductImportResponse ProcessCheckoutAttributes(List<ExportQueueItem> checkoutAttributesToProcess)
        {
            var attribsToUpdate = new List<int>();
            ProductImportResponse result = null;
            List<KeyValuePair<int, bool>> caIdsToProcess = new List<KeyValuePair<int, bool>>();

            // Build a list of the product Ids to process
            for (int i = 0; i < 2; i++)
            {
                if (i == 0)
                {
                    caIdsToProcess.AddRange(
                        checkoutAttributesToProcess.Where(p => p.Action != ExportQueueAction.All)
                                                   .Select(p => new KeyValuePair<int, bool>(p.EntityKey, p.Action == ExportQueueAction.Delete))
                                                   .ToList());
                }
                else
                {
                    // In theory, if we have an 'All' action, the only other items in the queue should be deletes.
                    if (checkoutAttributesToProcess.Any(ei => ei.Action == ExportQueueAction.All))
                    {
                        var allCheckoutAttributes = _attributeValueService.RetrieveAllForAttribute(EntityAttributeName.CheckoutAttribute);
                        caIdsToProcess.AddRange(allCheckoutAttributes.ToList().Select(p => new KeyValuePair<int, bool>(p.Id, false)).ToList());
                    }
                }
            }

            if (caIdsToProcess.Count > 0)
            {
                int batch = 0;
                var batchCAIds = caIdsToProcess.Skip(batch).Take(_promoSettings.BatchSize);
                List<ProductImportResponse> allBatchResults = new List<ProductImportResponse>();
                bool commsError = false;

                while (batchCAIds.Count() > 0)
                {
                    var importRequest = BuildBaseProductImport();
                    var productsToAdd = new List<ProductImportRequestItem>();
                    var productMappings = new List<InternalProductVariantItem>();

                    // For all product Ids, get the import details.           
                    batchCAIds.ToList().ForEach(p =>
                    {
                        InternalProductImportDetails importDetails = null;
                        if (p.Value)       // Meaning DELETED
                            importDetails = GetDeletedCheckoutAttributeProduct(p.Key);
                        else
                        {
                            importDetails = GetCheckoutAttributeProduct(p.Key);
                            if(importDetails != null)
                                attribsToUpdate.Add(p.Key);
                        }

                        if (importDetails != null)
                        {
                            if (importDetails.ImportProducts != null)
                                productsToAdd.AddRange(importDetails.ImportProducts);
                            if (importDetails.VariantItems != null)
                                productMappings.AddRange(importDetails.VariantItems);
                        }
                    });

                    importRequest.Products.AddRange(productsToAdd);

                    // Send to Promo
                    var batchResult = _qixolPromoUtilities.ImportProductsToPromoService(importRequest);

                    if (batchResult != null)
                        allBatchResults.Add(batchResult);

                    if (batchResult != null && batchResult.Summary != null && batchResult.Summary.ProcessedSuccessfully)
                    {
                        // Now update the variant mappings...
                        productMappings.ForEach(v =>
                        {
                            if (v.Delete)
                            {
                                if (v.ExistingItem != null)
                                    _productMappingService.Delete(v.ExistingItem);
                            }
                            else
                            {
                                _productMappingService.Insert(new ProductMappingItem()
                                {
                                    AttributesXml = v.AttributesXml,
                                    EntityName = EntityAttributeName.CheckoutAttribute,
                                    EntityId = v.ProductId,
                                    VariantCode = v.VariantCode,
                                    NoVariants = v.NoVariants
                                });
                            }
                        });

                        batch += _promoSettings.BatchSize;
                        batchCAIds = caIdsToProcess.Skip(batch).Take(_promoSettings.BatchSize);
                    }
                    else
                    {
                        // This should not happen!!
                        commsError = true;
                        break;
                    }
                }

                if (!commsError)
                {
                    result = new ProductImportResponse();
                    result.Summary = new ImportResponseSummary() { ProcessedSuccessfully = allBatchResults.Where(br => br.Summary != null && !br.Summary.ProcessedSuccessfully).Count() == 0 };
                    result.Summary.Messages = new List<ImportResponseMessage>();

                    allBatchResults.ForEach(br =>
                    {
                        //result.Summary.Messages.Add(new ImportResponseMessage() { Code = br.Reference, Message = br.GetMessages() });
                    });

                    if(result.Summary.ProcessedSuccessfully)
                        FlagAttributeAsSynchonized(attribsToUpdate);
                }
            }
            else
            {
                result = new ProductImportResponse();
                result.Summary = new ImportResponseSummary() { ProcessedSuccessfully = true };
            }

            return result;
        }