private ShopifyData ReadData(ShopifyImportParams importParams, ShopifyImportNotification notification)
        {
            var result = new ShopifyData();

            if (importParams.ImportProducts)
            {
                ReadProducts(notification, result);
            }

            if (importParams.ImportCollections)
            {
                ReadCollections(notification, result);
            }

            if (importParams.ImportThemes)
            {
                ReadThemes(notification, result);
            }

            if (importParams.ImportPages)
            {
                ReadPages(notification, result);
            }

            //TODO read another data
            return(result);
        }
        private void ReadCollections(ShopifyImportNotification notification, ShopifyData result)
        {
            notification.Description = "Reading collects from shopify...";
            _notifier.Upsert(notification);
            result.Collects = _shopifyRepository.GetShopifyCollects();

            notification.Description = "Reading collections from shopify...";
            _notifier.Upsert(notification);
            result.Collections = _shopifyRepository.GetShopifyCollections();

            notification.Progresses.Add("Collections", new ShopifyImportProgress()
            {
                TotalCount = result.Collections.Count()
            });
        }
        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 UpdateProgresses(ShopifyImportParams importParams, ShopifyImportNotification notification, ShopifyData shopifyData)
        {
            notification.Progresses.Clear();

            if (importParams.ImportProducts)
            {
                notification.Progresses.Add(ProductsKey, new ShopifyImportProgress()
                {
                    TotalCount = shopifyData.Products.Count()
                });

                notification.Progresses.Add(PropertiesKey, new ShopifyImportProgress()
                {
                    TotalCount = shopifyData.Products.SelectMany(product => product.Options).GroupBy(o => o.Name).Count()
                });
            }
            if (importParams.ImportCollections)
            {
                notification.Progresses.Add(CollectionsKey, new ShopifyImportProgress()
                {
                    TotalCount = shopifyData.Collections.Count()
                });
            }

            if (importParams.ImportThemes)
            {
                notification.Progresses.Add(ThemesKey, new ShopifyImportProgress()
                {
                    TotalCount = shopifyData.Themes.Count()
                });
            }

            if (importParams.ImportPages)
            {
                notification.Progresses.Add(PagesKey, new ShopifyImportProgress()
                {
                    TotalCount = shopifyData.Pages.Count()
                });
            }

            _notifier.Upsert(notification);
        }
 private void ReadProducts(ShopifyImportNotification notification, ShopifyData result)
 {
     notification.Description = "Reading products from shopify...";
     _notifier.Upsert(notification);
     result.Products = _shopifyRepository.GetShopifyProducts();
 }
        private void ReadThemes(ShopifyImportNotification notification, ShopifyData result)
        {
            result.Themes            = new Dictionary <ShopifyTheme, Stream>();
            notification.Description = "Reading themes from shopify...";
            var themes = _shopifyRepository.GetShopifyThemes().ToList();

            notification.Progresses.Clear();
            _notifier.Upsert(notification);


            foreach (var theme in themes)
            {
                var assets = _shopifyRepository.GetShopifyAssets(theme.Id).ToList();

                var themeProgress = new ShopifyImportProgress()
                {
                    TotalCount = assets.Count
                };
                notification.Progresses.Add(string.Format("{0} theme assets downloading ", theme.Name), themeProgress);
                _notifier.Upsert(notification);

                var stream = new MemoryStream();
                using (var zip = new ZipArchive(stream, ZipArchiveMode.Create, true))
                {
                    foreach (var asset in assets)
                    {
                        try
                        {
                            var downloadedAsset = _shopifyRepository.DownloadShopifyAsset(theme.Id, asset);
                            var entry           = zip.CreateEntry(string.Format("{0}/{1}", theme.Id, asset.Key));
                            using (var entryStream = entry.Open())
                            {
                                if (downloadedAsset.Value != null)
                                {
                                    using (var writer = new StreamWriter(entryStream))
                                    {
                                        writer.Write(downloadedAsset.Value);
                                    }
                                }
                                else
                                {
                                    if (downloadedAsset.Attachment != null)
                                    {
                                        var data = Convert.FromBase64String(downloadedAsset.Attachment);
                                        entryStream.Write(data, 0, data.Length);
                                    }
                                }
                            }
                        }
                        catch (Exception ex)
                        {
                            themeProgress.ErrorCount++;
                            notification.ErrorCount++;
                            notification.Errors.Add(ex.ToString());
                            _notifier.Upsert(notification);
                        }
                        finally
                        {
                            //Raise notification each notifyProductSizeLimit category
                            themeProgress.ProcessedCount++;

                            if (themeProgress.ProcessedCount % NotifySizeLimit == 0 ||
                                themeProgress.ProcessedCount + themeProgress.ErrorCount == themeProgress.TotalCount)
                            {
                                _notifier.Upsert(notification);
                            }
                        }
                    }
                }
                stream.Seek(0, SeekOrigin.Begin);
                result.Themes.Add(theme, stream);
            }
        }
Ejemplo n.º 7
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);
        }