Ejemplo n.º 1
0
 private bool HasRequiredFields(ProductCsvImportLine csvLine, CsvProductInfo csvProduct)
 {
     if (string.IsNullOrEmpty(csvProduct.ImportAction))
     {
         csvLine.Status    = ProductImportStatus.Error;
         csvLine.StatusMsg = "ImportAction is required";
         return(false);
     }
     else if (importAction == ImportActionType.UNKNOWN)
     {
         csvLine.Status    = ProductImportStatus.Error;
         csvLine.StatusMsg = "Unknown ImportAction. Valid values are: 'C', 'R', 'U', 'D'";
         return(false);
     }
     if (string.IsNullOrEmpty(csvProduct.Name))
     {
         csvLine.Status    = ProductImportStatus.Error;
         csvLine.StatusMsg = "Name is required";
         return(false);
     }
     if (string.IsNullOrEmpty(csvProduct.UrlName))
     {
         csvLine.Status    = ProductImportStatus.Error;
         csvLine.StatusMsg = "UrlName is required";
         return(false);
     }
     return(true);
 }
Ejemplo n.º 2
0
        public ProductCsvImporter(int storeId)
        {
            this.storeId = storeId;

            this.productPhotoFolderFileRoot = StoreUrls.GetProductPhotoFolderFileRoot();
            CreateDirIfNotExists(productPhotoFolderFileRoot);
            this.productFilesFolderFileRoot = StoreUrls.GetModuleFolderFileRoot() + @"ProductFiles\";
            CreateDirIfNotExists(productFilesFolderFileRoot);

            this.importFilesFolderFileRoot = StoreUrls.GetModuleFolderFileRoot() + @"ImportFiles\";
            CreateDirIfNotExists(importFilesFolderFileRoot);
            filesInImportDirectory = new List <string>(Directory.GetFiles(importFilesFolderFileRoot));

            csvLine = new ProductCsvImportLine();
        }
Ejemplo n.º 3
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);
            }
        }