Exemplo n.º 1
0
        internal ProductCsvImportResult ImportProducts(Stream fileStream)
        {
            ProductCsvImportResult result = new ProductCsvImportResult();

            try
            {
                IList <CsvProductInfo> csvProducts;
                using (StreamReader csvStreamReader = new StreamReader(fileStream))
                {
                    var csvParser  = new CsvParser(csvStreamReader);
                    var csvOptions = new CsvReaderOptions()
                    {
                        HasHeaderRecord = true, Strict = false
                    };
                    using (CsvReader csvReader = new CsvReader(csvParser, csvOptions))
                    {
                        csvProducts = csvReader.GetRecords <CsvProductInfo>();
                    }
                }

                if (csvProducts.Count == 0)
                {
                    result.Messages.Add("Did not find any products in import file");
                }
                else
                {
                    result.Messages.Add(string.Format(@"Read {0:N0} products from CSV file", csvProducts.Count));

                    ImportProductsToDatabase(csvProducts, ref result);
                }
            }
            catch (Exception ex)
            {
                result.Messages.Add(string.Format(@"ERROR: {0}. {1}", ex.Message, ex.StackTrace));
            }

            return(result);
        }
Exemplo n.º 2
0
        private void ImportProductsToDatabase(IEnumerable <CsvProductInfo> csvProducts, ref ProductCsvImportResult result)
        {
            int lineNo = 1;

            foreach (var csvProduct in csvProducts)
            {
                lineNo++;
                csvLine = new ProductCsvImportLine()
                {
                    CsvLineNumber  = lineNo,
                    ProductName    = csvProduct.Name,
                    ProductSku     = csvProduct.Sku,
                    ProductUrlName = csvProduct.UrlName,
                    StatusMsg      = ""
                };

                string actionString = (csvProduct.ImportAction ?? "").ToUpper();
                importAction = ImportActionType.UNKNOWN;
                if (actionString == "C")
                {
                    importAction = ImportActionType.Create;
                }
                if (actionString == "R")
                {
                    importAction = ImportActionType.Replace;
                }
                if (actionString == "U")
                {
                    importAction = ImportActionType.Update;
                }
                if (actionString == "D")
                {
                    importAction = ImportActionType.Delete;
                }

                //--- Check for missing/invalid fields
                if (!HasRequiredFields(csvLine, csvProduct))
                {
                    result.CsvLines.Add(csvLine);
                    continue;
                }

                // try to find the product
                Product p = null;
                if (csvProduct.ProductId.HasValue)
                {
                    p = Product.GetProduct(csvProduct.ProductId.Value);
                }
                else
                {
                    p = Product.GetBySlug(storeId, csvProduct.UrlName) ??
                        Product.GetByName(storeId, csvProduct.Name) ?? Product.GetBySku(storeId, csvProduct.Sku);
                }
                bool productExists = (p != null);

                try
                {
                    switch (importAction)
                    {
                    case ImportActionType.Create:
                        if (productExists)
                        {
                            csvLine.Status    = ProductImportStatus.Skipped;
                            csvLine.StatusMsg = "Product already exists. Specify 'R' or 'U' to replace/update this product.";
                        }
                        else
                        {
                            csvLine.Status = ProductImportStatus.Created;
                            CreateProduct(csvProduct);
                        }
                        break;

                    case ImportActionType.Replace:
                        if (productExists)
                        {
                            csvLine.Status = ProductImportStatus.Replaced;
                            ReplaceProduct(p, csvProduct);
                        }
                        else
                        {
                            csvLine.Status = ProductImportStatus.Created;
                            CreateProduct(csvProduct);
                        }
                        break;

                    case ImportActionType.Update:
                        if (productExists)
                        {
                            csvLine.Status = ProductImportStatus.Updated;
                            UpdateProduct(p, csvProduct);
                        }
                        else
                        {
                            csvLine.Status = ProductImportStatus.Created;
                            CreateProduct(csvProduct);
                        }
                        break;

                    case ImportActionType.Delete:
                        if (productExists)
                        {
                            csvLine.Status = ProductImportStatus.Deleted;
                            DeleteProduct(p);
                        }
                        else
                        {
                            csvLine.Status    = ProductImportStatus.Skipped;
                            csvLine.StatusMsg = "Unable to find Product to delete";
                        }
                        break;
                    }
                }
                catch (Exception ex)
                {
                    csvLine.Status     = ProductImportStatus.Error;
                    csvLine.StatusMsg += " ERROR: " + ex.ToString();
                }

                result.CsvLines.Add(csvLine);
            }
        }