protected override ExportableSearchResult FetchData(CategorySearchCriteria searchCriteria)
        {
            var searchResult = _categorySearchService.SearchCategoriesAsync(searchCriteria).GetAwaiter().GetResult();

            return(new ExportableSearchResult
            {
                TotalCount = searchResult.TotalCount,
                Results = searchResult.Results.ToList <IExportable>(),
            });
        }
Exemple #2
0
        /// <summary>
        /// Try to find (create if not) categories for products with Category.Path
        /// </summary>
        private async Task SaveCategoryTree(Catalog catalog, IEnumerable <CsvProduct> csvProducts, ExportImportProgressInfo progressInfo, Action <ExportImportProgressInfo> progressCallback)
        {
            var cachedCategoryMap = new Dictionary <string, Category>();
            var outline           = new StringBuilder();

            foreach (var csvProduct in csvProducts.Where(x => x.Category != null && !string.IsNullOrEmpty(x.Category.Path)))
            {
                outline.Clear();
                var    productCategoryNames = csvProduct.Category.Path.Split(_categoryDelimiters);
                string parentCategoryId     = null;
                foreach (var categoryName in productCategoryNames)
                {
                    outline.Append($"\\{categoryName}");
                    Category category;
                    if (!cachedCategoryMap.TryGetValue(outline.ToString(), out category))
                    {
                        var searchCriteria = new CategorySearchCriteria
                        {
                            CatalogId        = catalog.Id,
                            CategoryId       = parentCategoryId,
                            SearchOnlyInRoot = parentCategoryId == null,
                            Keyword          = categoryName
                        };
                        category = (await _categorySearchService.SearchCategoriesAsync(searchCriteria)).Results.FirstOrDefault();
                    }

                    if (category == null)
                    {
                        var code = categoryName.GenerateSlug();
                        if (string.IsNullOrEmpty(code))
                        {
                            code = Guid.NewGuid().ToString("N");
                        }

                        category = new Category()
                        {
                            Name = categoryName, Code = code, CatalogId = catalog.Id, ParentId = parentCategoryId
                        };
                        await _categoryService.SaveChangesAsync(new[] { category });

                        //Raise notification each notifyCategorySizeLimit category
                        var count = progressInfo.ProcessedCount;
                        progressInfo.Description = $"Creating categories: {++count} created";
                        progressCallback(progressInfo);
                    }
                    csvProduct.CategoryId = category.Id;
                    csvProduct.Category   = category;
                    parentCategoryId      = category.Id;
                    cachedCategoryMap[outline.ToString()] = category;
                }
            }
        }
Exemple #3
0
        public async Task DoExportAsync(Stream outStream, ExportImportOptions options, Action <ExportImportProgressInfo> progressCallback, ICancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            var progressInfo = new ExportImportProgressInfo {
                Description = "loading data..."
            };

            progressCallback(progressInfo);

            using (var sw = new StreamWriter(outStream))
                using (var writer = new JsonTextWriter(sw))
                {
                    await writer.WriteStartObjectAsync();

                    #region Export catalogs
                    progressInfo.Description = "Catalogs exporting...";
                    progressCallback(progressInfo);

                    await writer.WritePropertyNameAsync("Catalogs");

                    await writer.SerializeJsonArrayWithPagingAsync(_jsonSerializer, _batchSize, async (skip, take) =>
                    {
                        var searchResult = (await _catalogService.GetCatalogsListAsync()).ToArray();
                        return(new GenericSearchResult <Catalog> {
                            Results = searchResult, TotalCount = searchResult.Length
                        });
                    }, (processedCount, totalCount) =>
                    {
                        progressInfo.Description = $"{ processedCount } of { totalCount } catalogs have been exported";
                        progressCallback(progressInfo);
                    }, cancellationToken);

                    #endregion

                    #region Export categories
                    progressInfo.Description = "Categories exporting...";
                    progressCallback(progressInfo);

                    await writer.WritePropertyNameAsync("Categories");

                    await writer.SerializeJsonArrayWithPagingAsync(_jsonSerializer, _batchSize, async (skip, take) =>
                    {
                        var searchResult = await _categorySearchService.SearchCategoriesAsync(new CategorySearchCriteria {
                            Skip = skip, Take = take
                        });
                        var categories = searchResult.Results;
                        if (options.HandleBinaryData)
                        {
                            LoadImages(categories.OfType <IHasImages>().ToArray(), progressInfo);
                        }

                        return(new GenericSearchResult <Category> {
                            Results = categories, TotalCount = searchResult.TotalCount
                        });
                    }, (processedCount, totalCount) =>
                    {
                        progressInfo.Description = $"{ processedCount } of { totalCount } Categories have been exported";
                        progressCallback(progressInfo);
                    }, cancellationToken);

                    #endregion

                    #region Export properties
                    progressInfo.Description = "Properties exporting...";
                    progressCallback(progressInfo);

                    await writer.WritePropertyNameAsync("Properties");

                    await writer.SerializeJsonArrayWithPagingAsync(_jsonSerializer, _batchSize, async (skip, take) =>
                    {
                        var searchResult = await _propertySearchService.SearchPropertiesAsync(new PropertySearchCriteria {
                            Skip = skip, Take = take
                        });
                        return(new GenericSearchResult <Property> {
                            Results = searchResult.Results, TotalCount = searchResult.TotalCount
                        });
                    }, (processedCount, totalCount) =>
                    {
                        progressInfo.Description = $"{ processedCount } of { totalCount } properties have been exported";
                        progressCallback(progressInfo);
                    }, cancellationToken);

                    #endregion

                    #region Export propertyDictionaryItems
                    progressInfo.Description = "PropertyDictionaryItems exporting...";
                    progressCallback(progressInfo);

                    await writer.WritePropertyNameAsync("PropertyDictionaryItems");

                    await writer.SerializeJsonArrayWithPagingAsync(_jsonSerializer, _batchSize, async (skip, take) =>
                    {
                        var searchResult = await _propertyDictionarySearchService.SearchAsync(new PropertyDictionaryItemSearchCriteria {
                            Skip = skip, Take = take
                        });
                        return(new GenericSearchResult <PropertyDictionaryItem> {
                            Results = searchResult.Results, TotalCount = searchResult.TotalCount
                        });
                    }, (processedCount, totalCount) =>
                    {
                        progressInfo.Description = $"{ processedCount } of { totalCount } property dictionary items have been exported";
                        progressCallback(progressInfo);
                    }, cancellationToken);

                    #endregion

                    #region Export products
                    progressInfo.Description = "Products exporting...";
                    progressCallback(progressInfo);

                    await writer.WritePropertyNameAsync("Products");

                    await writer.SerializeJsonArrayWithPagingAsync(_jsonSerializer, _batchSize, async (skip, take) =>
                    {
                        var searchResult = await _productSearchService.SearchProductsAsync(new ProductSearchCriteria {
                            Skip = skip, Take = take
                        });
                        if (options.HandleBinaryData)
                        {
                            LoadImages(searchResult.Results.OfType <IHasImages>().ToArray(), progressInfo);
                        }
                        return(new GenericSearchResult <CatalogProduct> {
                            Results = searchResult.Results, TotalCount = searchResult.TotalCount
                        });
                    }, (processedCount, totalCount) =>
                    {
                        progressInfo.Description = $"{ processedCount } of { totalCount } Products have been exported";
                        progressCallback(progressInfo);
                    }, cancellationToken);

                    #endregion

                    await writer.WriteEndObjectAsync();

                    await writer.FlushAsync();
                }
        }
Exemple #4
0
        public async Task DoExportAsync(Stream outStream, ExportImportOptions options, Action <ExportImportProgressInfo> progressCallback, ICancellationToken cancellationToken)
        {
            cancellationToken.ThrowIfCancellationRequested();
            var progressInfo = new ExportImportProgressInfo {
                Description = "loading data..."
            };

            progressCallback(progressInfo);

            using (var sw = new StreamWriter(outStream))
                using (var writer = new JsonTextWriter(sw))
                {
                    await writer.WriteStartObjectAsync();

                    #region Export properties

                    progressInfo.Description = "Properties exporting...";
                    progressCallback(progressInfo);

                    await writer.WritePropertyNameAsync("Properties");

                    await writer.SerializeJsonArrayWithPagingAsync(_jsonSerializer, _batchSize, async (skip, take) =>
                    {
                        var searchResult = await _propertySearchService.SearchPropertiesAsync(new PropertySearchCriteria {
                            Skip = skip, Take = take
                        });
                        foreach (var item in searchResult.Results)
                        {
                            ResetRedundantReferences(item);
                        }
                        return((GenericSearchResult <Property>)searchResult);
                    }
                                                                   , (processedCount, totalCount) =>
                    {
                        progressInfo.Description = $"{ processedCount } of { totalCount } properties have been exported";
                        progressCallback(progressInfo);
                    }, cancellationToken);

                    #endregion Export properties

                    #region Export propertyDictionaryItems

                    progressInfo.Description = "PropertyDictionaryItems exporting...";
                    progressCallback(progressInfo);

                    await writer.WritePropertyNameAsync("PropertyDictionaryItems");

                    await writer.SerializeJsonArrayWithPagingAsync(_jsonSerializer, _batchSize, async (skip, take) =>
                                                                   (GenericSearchResult <PropertyDictionaryItem>) await _propertyDictionarySearchService.SearchAsync(new PropertyDictionaryItemSearchCriteria {
                        Skip = skip, Take = take
                    })
                                                                   , (processedCount, totalCount) =>
                    {
                        progressInfo.Description = $"{ processedCount } of { totalCount } property dictionary items have been exported";
                        progressCallback(progressInfo);
                    }, cancellationToken);

                    #endregion Export propertyDictionaryItems

                    #region Export catalogs

                    progressInfo.Description = "Catalogs exporting...";
                    progressCallback(progressInfo);

                    await writer.WritePropertyNameAsync("Catalogs");

                    await writer.SerializeJsonArrayWithPagingAsync(_jsonSerializer, _batchSize, async (skip, take) =>
                                                                   (GenericSearchResult <Catalog>) await _catalogSearchService.SearchCatalogsAsync(new CatalogSearchCriteria {
                        Skip = skip, Take = take
                    })
                                                                   , (processedCount, totalCount) =>
                    {
                        progressInfo.Description = $"{ processedCount } of { totalCount } catalogs have been exported";
                        progressCallback(progressInfo);
                    }, cancellationToken);

                    #endregion Export catalogs

                    #region Export categories

                    progressInfo.Description = "Categories exporting...";
                    progressCallback(progressInfo);

                    await writer.WritePropertyNameAsync("Categories");

                    await writer.SerializeJsonArrayWithPagingAsync(_jsonSerializer, _batchSize, async (skip, take) =>
                    {
                        var searchResult = await _categorySearchService.SearchCategoriesAsync(new CategorySearchCriteria {
                            Skip = skip, Take = take
                        });
                        LoadImages(searchResult.Results.OfType <IHasImages>().ToArray(), progressInfo, options.HandleBinaryData);
                        foreach (var item in searchResult.Results)
                        {
                            ResetRedundantReferences(item);
                        }

                        return((GenericSearchResult <Category>)searchResult);
                    }, (processedCount, totalCount) =>
                    {
                        progressInfo.Description = $"{ processedCount } of { totalCount } Categories have been exported";
                        progressCallback(progressInfo);
                    }, cancellationToken);

                    #endregion Export categories

                    #region Export products

                    progressInfo.Description = "Products exporting...";
                    progressCallback(progressInfo);

                    await writer.WritePropertyNameAsync("Products");

                    await writer.SerializeJsonArrayWithPagingAsync(_jsonSerializer, _batchSize, async (skip, take) =>
                    {
                        var searchResult = await _productSearchService.SearchProductsAsync(new ProductSearchCriteria {
                            Skip = skip, Take = take, ResponseGroup = ItemResponseGroup.Full.ToString()
                        });
                        LoadImages(searchResult.Results.OfType <IHasImages>().ToArray(), progressInfo, options.HandleBinaryData);
                        foreach (var item in searchResult.Results)
                        {
                            ResetRedundantReferences(item);
                        }
                        return((GenericSearchResult <CatalogProduct>)searchResult);
                    }, (processedCount, totalCount) =>
                    {
                        progressInfo.Description = $"{ processedCount } of { totalCount } Products have been exported";
                        progressCallback(progressInfo);
                    }, cancellationToken);

                    #endregion Export products

                    await writer.WriteEndObjectAsync();

                    await writer.FlushAsync();
                }
        }