private void SaveThemes(VirtoData virtoData, ShopifyImportParams importParams, ShopifyImportNotification notification)
        {
            notification.Description = "Saving themes";
            _notifier.Upsert(notification);

            foreach (var theme in virtoData.Themes)
            {
                using (ZipArchive archive = new ZipArchive(theme.Value, ZipArchiveMode.Read))
                {
                    foreach (ZipArchiveEntry entry in archive.Entries)
                    {
                        if (!entry.FullName.EndsWith("/"))
                        {
                            var fileName = String.Join("/", entry.FullName.Split('/').Skip(1));
                            using (var entryStream = entry.Open())
                                using (var targetStream = _contentStorageProvider.OpenWrite("/Themes/" + importParams.StoreId + "/" + theme.Key.Name + "/" + fileName))
                                {
                                    entryStream.CopyTo(targetStream);
                                }
                        }
                    }
                }

                notification.Progresses[ThemesKey].ProcessedCount++;
                _notifier.Upsert(notification);
            }
        }
        private void SavePages(VirtoData virtoData, ShopifyImportParams importParams, ShopifyImportNotification notification)
        {
            notification.Description = "Saving pages";
            _notifier.Upsert(notification);

            foreach (var page in virtoData.Pages)
            {
                _pagesService.SavePage(importParams.StoreId, page);

                notification.Progresses[PagesKey].ProcessedCount++;
                _notifier.Upsert(notification);
            }
        }
        private void SaveThemes(VirtoData virtoData, ShopifyImportParams importParams, ShopifyImportNotification notification)
        {
            notification.Description = "Saving themes";
            _notifier.Upsert(notification);

            foreach (var theme in virtoData.Themes)
            {
                using (var zip = new ZipArchive(theme.Value, ZipArchiveMode.Read))
                {
                    _themeService.UploadTheme(importParams.StoreId, theme.Key.Name, zip);
                }


                notification.Progresses[ThemesKey].ProcessedCount++;
                _notifier.Upsert(notification);
            }
        }
        private VirtoData ConvertData(ShopifyData shopifyData, ShopifyImportParams importParams, ShopifyImportNotification notification)
        {
            var virtoData = new VirtoData();

            if (importParams.ImportCollections)
            {
                notification.Description = "Converting categories";
                _notifier.Upsert(notification);

                virtoData.Categories =
                    shopifyData.Collections
                    .Select(collection => _shopifyConverter.Convert(collection, importParams)).ToList();
            }

            if (importParams.ImportProducts)
            {
                notification.Description = "Converting product properties";
                _notifier.Upsert(notification);

                virtoData.Properties =
                    shopifyData.Products.SelectMany(product => product.Options)
                    .Select(option => _shopifyConverter.Convert(option, importParams))
                    .ToList();

                notification.Description = "Converting products";
                _notifier.Upsert(notification);

                virtoData.Products =
                    shopifyData.Products.Select(
                        product => _shopifyConverter.Convert(product, importParams, shopifyData, virtoData)).ToList();
            }

            if (importParams.ImportThemes)
            {
                virtoData.Themes = shopifyData.Themes;
            }

            if (importParams.ImportPages)
            {
                virtoData.Pages = shopifyData.Pages.Select(page => _shopifyConverter.Convert(page)).ToList();
            }

            return(virtoData);
        }
        private void SavePages(VirtoData virtoData, ShopifyImportParams importParams, ShopifyImportNotification notification)
        {
            notification.Description = "Saving pages";
            _notifier.Upsert(notification);

            var pageBasePath = "/Pages/" + importParams.StoreId + "/";

            foreach (var page in virtoData.Pages)
            {
                var pageUrl = pageBasePath + page.Title + ".md";
                using (var targetStream = _contentStorageProvider.OpenWrite(pageUrl))
                {
                    // convert string to stream
                    byte[] byteArray = Encoding.UTF8.GetBytes(page.BodyHtml);
                    using (var sourceStream = new MemoryStream(byteArray))
                    {
                        sourceStream.CopyTo(targetStream);
                    }
                }
                notification.Progresses[PagesKey].ProcessedCount++;
                _notifier.Upsert(notification);
            }
        }
        private void SaveData(VirtoData virtoData, ShopifyImportParams importParams,
                              ShopifyImportNotification notification)
        {
            if (importParams.ImportCollections)
            {
                SaveCategories(virtoData, importParams, notification);
            }

            if (importParams.ImportProducts)
            {
                SaveProperties(virtoData, importParams, notification);
                SaveProducts(virtoData, importParams, notification);
            }

            if (importParams.ImportThemes)
            {
                SaveThemes(virtoData, importParams, notification);
            }

            if (importParams.ImportPages)
            {
                SavePages(virtoData, importParams, notification);
            }
        }
        private void SaveCategories(VirtoData virtoData, ShopifyImportParams importParams,
                                    ShopifyImportNotification notification)
        {
            var categoriesProgress = notification.Progresses[CollectionsKey];

            notification.Description = "Saving categories";
            _notifier.Upsert(notification);

            var categoriesToCreate = new List <coreModel.Category>();
            var categoriesToUpdate = new List <coreModel.Category>();

            var existingCategories = _searchService.Search(new coreModel.SearchCriteria()
            {
                CatalogId        = importParams.VirtoCatalogId,
                GetAllCategories = true,
                ResponseGroup    = coreModel.ResponseGroup.WithCategories
            }).Categories;

            foreach (var category in virtoData.Categories)
            {
                var existingCategory = existingCategories.FirstOrDefault(c => c.Code == category.Code);
                if (existingCategory != null)
                {
                    category.Id = existingCategory.Id;
                    categoriesToUpdate.Add(category);
                }
                else
                {
                    categoriesToCreate.Add(category);
                }
            }

            foreach (var category in categoriesToCreate)
            {
                try
                {
                    _categoryService.Create(category);
                }
                catch (Exception ex)
                {
                    categoriesProgress.ErrorCount++;
                    notification.ErrorCount++;
                    notification.Errors.Add(ex.ToString());
                    _notifier.Upsert(notification);
                }
                finally
                {
                    //Raise notification each notifyProductSizeLimit category
                    categoriesProgress.ProcessedCount++;
                    notification.Description = string.Format("Creating categories: {0} of {1} created",
                                                             categoriesProgress.ProcessedCount, categoriesProgress.TotalCount);
                    if (categoriesProgress.ProcessedCount % NotifySizeLimit == 0 || categoriesProgress.ProcessedCount + categoriesProgress.ErrorCount == categoriesProgress.TotalCount)
                    {
                        _notifier.Upsert(notification);
                    }
                }
            }

            if (categoriesToUpdate.Count > 0)
            {
                _categoryService.Update(categoriesToUpdate.ToArray());

                notification.Description = string.Format("Updating categories: {0} updated",
                                                         categoriesProgress.ProcessedCount = categoriesProgress.ProcessedCount + categoriesToUpdate.Count);
                _notifier.Upsert(notification);
            }

            virtoData.Categories.Clear();
            virtoData.Categories.AddRange(categoriesToCreate);
            virtoData.Categories.AddRange(categoriesToUpdate);
        }
        private void SaveProducts(VirtoData virtoData, ShopifyImportParams importParams, ShopifyImportNotification notification)
        {
            var productProgress = notification.Progresses[ProductsKey];

            if (importParams.ImportCollections)
            {
                //Update categories references
                foreach (var product in virtoData.Products)
                {
                    if (product.Category != null)
                    {
                        product.Category   = virtoData.Categories.First(category => category.Code == product.Category.Code);
                        product.CategoryId = product.Category.Id;
                    }
                }
            }

            notification.Description = "Checking existing products";
            _notifier.Upsert(notification);


            notification.Description = "Saving products";
            _notifier.Upsert(notification);

            var productsToCreate = new List <coreModel.CatalogProduct>();
            var productsToUpdate = new List <coreModel.CatalogProduct>();

            foreach (var product in virtoData.Products)
            {
                var existingProduct = _searchService.Search(new coreModel.SearchCriteria()
                {
                    CatalogId        = importParams.VirtoCatalogId,
                    GetAllCategories = true,
                    ResponseGroup    = coreModel.ResponseGroup.WithProducts,
                    Count            = int.MaxValue,
                    Code             = product.Code
                }).Products.FirstOrDefault();

                if (existingProduct != null)
                {
                    product.Id = existingProduct.Id;
                    productsToUpdate.Add(product);
                }
                else
                {
                    productsToCreate.Add(product);
                }
            }

            foreach (var product in productsToCreate)
            {
                try
                {
                    _productService.Create(product);

                    //Create price in default price list
                    if (product.Prices != null && product.Prices.Any())
                    {
                        var price = product.Prices.First();
                        price.ProductId = product.Id;
                        _pricingService.CreatePrice(price);
                    }
                }
                catch (Exception ex)
                {
                    productProgress.ErrorCount++;
                    notification.ErrorCount++;
                    notification.Errors.Add(ex.ToString());
                    _notifier.Upsert(notification);
                }
                finally
                {
                    //Raise notification each notifyProductSizeLimit category

                    productProgress.ProcessedCount++;
                    notification.Description = string.Format("Creating products: {0} of {1} created",
                                                             productProgress.ProcessedCount, productProgress.TotalCount);
                    if (productProgress.ProcessedCount % NotifySizeLimit == 0 || productProgress.ProcessedCount + productProgress.ErrorCount == productProgress.TotalCount)
                    {
                        _notifier.Upsert(notification);
                    }
                }
            }


            if (productsToUpdate.Count > 0)
            {
                try
                {
                    _productService.Update(productsToUpdate.ToArray());
                }
                catch (Exception ex)
                {
                    productProgress.ErrorCount++;
                    notification.ErrorCount++;
                    notification.Errors.Add(ex.ToString());
                    _notifier.Upsert(notification);
                }
                finally
                {
                    notification.Description = string.Format("Updating products: {0} updated",
                                                             productProgress.ProcessedCount = productProgress.ProcessedCount + productsToUpdate.Count);
                    _notifier.Upsert(notification);
                }
            }

            virtoData.Products.Clear();
            virtoData.Products.AddRange(productsToCreate);
            virtoData.Products.AddRange(productsToUpdate);
        }
        private void SaveProperties(VirtoData virtoData, ShopifyImportParams importParams,
                                    ShopifyImportNotification notification)
        {
            var propertiesProgress = notification.Progresses[PropertiesKey];

            notification.Description = "Saving properties";
            _notifier.Upsert(notification);

            var propertiesToCreate = new List <coreModel.Property>();
            var propertiesToUpdate = new List <coreModel.Property>();

            var existingProperties = _propertyService.GetCatalogProperties(importParams.VirtoCatalogId);

            foreach (var property in virtoData.Properties)
            {
                if (propertiesToCreate.All(p => p.Name != property.Name) &&
                    propertiesToUpdate.All(p => p.Name != property.Name))
                {
                    var existitngProperty = existingProperties.FirstOrDefault(c => c.Name == property.Name);
                    if (existitngProperty != null)
                    {
                        property.Id = existitngProperty.Id;
                        propertiesToUpdate.Add(property);
                    }
                    else
                    {
                        propertiesToCreate.Add(property);
                    }
                }
            }

            foreach (var property in propertiesToCreate)
            {
                try
                {
                    _propertyService.Create(property);
                }
                catch (Exception ex)
                {
                    propertiesProgress.ErrorCount++;
                    notification.ErrorCount++;
                    notification.Errors.Add(ex.ToString());
                    _notifier.Upsert(notification);
                }
                finally
                {
                    //Raise notification each notifyProductSizeLimit property
                    propertiesProgress.ProcessedCount++;
                    notification.Description = string.Format("Creating properties: {0} of {1} created",
                                                             propertiesProgress.ProcessedCount, propertiesProgress.TotalCount);
                    if (propertiesProgress.ProcessedCount % NotifySizeLimit == 0 ||
                        propertiesProgress.ProcessedCount + propertiesProgress.ErrorCount ==
                        propertiesProgress.TotalCount)
                    {
                        _notifier.Upsert(notification);
                    }
                }
            }

            if (propertiesToUpdate.Count > 0)
            {
                _propertyService.Update(propertiesToUpdate.ToArray());

                notification.Description = string.Format("Updating properties: {0} updated",
                                                         propertiesProgress.ProcessedCount = propertiesProgress.ProcessedCount + propertiesToUpdate.Count);
                _notifier.Upsert(notification);
            }
            virtoData.Properties.Clear();
            virtoData.Properties.AddRange(propertiesToCreate);
            virtoData.Properties.AddRange(propertiesToUpdate);
        }
Esempio n. 10
0
        public CatalogProduct Convert(ShopifyProduct shopifyProduct, ShopifyImportParams importParams, ShopifyData shopifyData, VirtoData virtoData)
        {
            var retVal = new CatalogProduct
            {
                Id          = shopifyProduct.Handle + "-" + shopifyProduct.Id.ToString(),
                Name        = shopifyProduct.Title,
                Code        = shopifyProduct.Handle,
                StartDate   = shopifyProduct.PublishedAt,
                IsActive    = true,
                CatalogId   = importParams.VirtoCatalogId,
                ProductType = shopifyProduct.ProductType,
                Vendor      = shopifyProduct.Vendor
            };

            //Images
            if (shopifyProduct.Image != null)
            {
                retVal.Images = new List <Image>();
                retVal.Images.Add(Convert(shopifyProduct.Image, true));
            }

            //Review
            if (shopifyProduct.BodyHtml != null)
            {
                retVal.Reviews = new List <EditorialReview>();
                var review = new EditorialReview
                {
                    Content      = shopifyProduct.BodyHtml,
                    LanguageCode = "en-US",
                };
                retVal.Reviews.Add(review);
            }

            //Seo
            retVal.SeoInfos = new List <SeoInfo>();
            var seoInfo = new SeoInfo
            {
                SemanticUrl  = shopifyProduct.Title.GenerateSlug(),
                LanguageCode = "en-US"
            };

            retVal.SeoInfos.Add(seoInfo);

            //TODO: Inventory

            //Variation
            if (shopifyProduct.Variants != null)
            {
                retVal.Variations = new List <CatalogProduct>();
                var isFirst = true;
                foreach (var shopifyVariant in shopifyProduct.Variants)
                {
                    var variation = isFirst ? retVal : new CatalogProduct();
                    variation.Name     = String.Format("{0} ({1})", retVal.Name, shopifyVariant.Title);
                    variation.Code     = (shopifyProduct.Handle + "-" + shopifyVariant.Title).GenerateSlug();
                    variation.IsActive = true;

                    variation.SeoInfos = new List <SeoInfo>();
                    seoInfo            = new SeoInfo
                    {
                        SemanticUrl  = variation.Name.GenerateSlug(),
                        LanguageCode = "en-US"
                    };
                    retVal.SeoInfos.Add(seoInfo);

                    if (shopifyVariant.ImageId != null && shopifyProduct.Images != null)
                    {
                        variation.Images = new List <Image>();
                        var image = shopifyProduct.Images.Where(x => x.Id == shopifyVariant.ImageId).Select(x => Convert(x, true)).FirstOrDefault();
                        if (image != null)
                        {
                            variation.Images.Add(image);
                        }
                    }

                    //Price
                    variation.Prices = new List <Price>();
                    variation.Prices.Add(new Price
                    {
                        Sale     = shopifyVariant.Price,
                        List     = shopifyVariant.Price,
                        Currency = "USD"
                    });


                    //Properties (need refactor)
                    variation.PropertyValues = new List <PropertyValue>();

                    var orderedProperties = shopifyProduct.Options.OrderBy(option => option.Position).ToArray();

                    if (shopifyVariant.Option1 != null)
                    {
                        var propValue = new PropertyValue
                        {
                            PropertyName = orderedProperties[0].Name,
                            Value        = shopifyVariant.Option1,
                            ValueType    = PropertyValueType.ShortText,
                        };
                        variation.PropertyValues.Add(propValue);
                    }
                    if (shopifyVariant.Option2 != null)
                    {
                        var propValue = new PropertyValue
                        {
                            PropertyName = orderedProperties[1].Name,
                            Value        = shopifyVariant.Option2,
                            ValueType    = PropertyValueType.ShortText
                        };
                        variation.PropertyValues.Add(propValue);
                    }
                    if (shopifyVariant.Option3 != null)
                    {
                        var propValue = new PropertyValue
                        {
                            PropertyName = orderedProperties[2].Name,
                            Value        = shopifyVariant.Option3,
                            ValueType    = PropertyValueType.ShortText
                        };
                        variation.PropertyValues.Add(propValue);
                    }

                    if (!isFirst)
                    {
                        retVal.Variations.Add(variation);
                    }
                    isFirst = false;
                }
            }


            if (importParams.ImportCollections)
            {
                var firstCollect = shopifyData.Collects.FirstOrDefault(collect => collect.ProductId == shopifyProduct.Id);
                if (firstCollect != null)
                {
                    retVal.Category = new Category()
                    {
                        Code =
                            shopifyData.Collections.First(collection => collection.Id == firstCollect.CollectionId)
                            .Handle
                    };
                }
            }

            return(retVal);
        }
Esempio n. 11
0
        private void SaveProducts(VirtoData virtoData, ShopifyImportParams importParams, ShopifyImportNotification notification)
        {
            var productProgress = notification.Progresses[ProductsKey];

            if (importParams.ImportCollections)
            {
                //Update categories references
                foreach (var product in virtoData.Products)
                {
                    if (product.Category != null)
                    {
                        product.Category   = virtoData.Categories.First(category => category.Code == product.Category.Code);
                        product.CategoryId = product.Category.Id;
                    }
                }
            }

            notification.Description = "Checking existing products";
            _notifier.Upsert(notification);


            notification.Description = "Saving products";
            _notifier.Upsert(notification);

            var productsToCreate = new List <coreModel.CatalogProduct>();
            var productsToUpdate = new List <coreModel.CatalogProduct>();

            var alreadyExistProducts = _productService.GetByIds(virtoData.Products.Select(x => x.Id).Where(x => x != null).ToArray(), coreModel.ItemResponseGroup.ItemInfo);

            foreach (var product in virtoData.Products)
            {
                if (alreadyExistProducts.Any(x => x.Id == product.Id))
                {
                    productsToUpdate.Add(product);
                }
                else
                {
                    productsToCreate.Add(product);
                }
            }

            foreach (var product in productsToCreate)
            {
                try
                {
                    _productService.Create(product);

                    //Create price in default price list
                    if (product.Prices != null && product.Prices.Any())
                    {
                        var price = product.Prices.First();
                        price.ProductId = product.Id;
                        _pricingService.CreatePrice(price);
                    }
                }
                catch (Exception ex)
                {
                    productProgress.ErrorCount++;
                    notification.ErrorCount++;
                    notification.Errors.Add(ex.ToString());
                    _notifier.Upsert(notification);
                }
                finally
                {
                    //Raise notification each notifyProductSizeLimit category

                    productProgress.ProcessedCount++;
                    notification.Description = string.Format("Creating products: {0} of {1} created",
                                                             productProgress.ProcessedCount, productProgress.TotalCount);
                    if (productProgress.ProcessedCount % NotifySizeLimit == 0 || productProgress.ProcessedCount + productProgress.ErrorCount == productProgress.TotalCount)
                    {
                        _notifier.Upsert(notification);
                    }
                }
            }


            if (productsToUpdate.Count > 0)
            {
                try
                {
                    _productService.Update(productsToUpdate.ToArray());
                }
                catch (Exception ex)
                {
                    productProgress.ErrorCount++;
                    notification.ErrorCount++;
                    notification.Errors.Add(ex.ToString());
                    _notifier.Upsert(notification);
                }
                finally
                {
                    notification.Description = string.Format("Updating products: {0} updated",
                                                             productProgress.ProcessedCount = productProgress.ProcessedCount + productsToUpdate.Count);
                    _notifier.Upsert(notification);
                }
            }

            virtoData.Products.Clear();
            virtoData.Products.AddRange(productsToCreate);
            virtoData.Products.AddRange(productsToUpdate);
        }