public async Task AddUpdateLuceneIndex(Product product)
        {
            var analyzer = new StandardAnalyzer(LuceneVersion.LUCENE_48);
            var iwc      = new IndexWriterConfig(LuceneVersion.LUCENE_48, analyzer);

            iwc.OpenMode = OpenMode.CREATE_OR_APPEND;

            var productTagNames = (await _productTagService.GetAllProductTagsByProductIdAsync(product.Id)).Select(x => x.Name);

            var productCategories = await _categoryService.GetProductCategoriesByProductIdAsync(product.Id);

            var categories = await _categoryService.GetCategoriesByIdsAsync(productCategories.Select(x => x.CategoryId).ToArray());

            var productManufacturers = await _manufacturerService.GetProductManufacturersByProductIdAsync(product.Id);

            var manufacturers = from m in await _manufacturerService.GetAllManufacturersAsync()
                                join pm in productManufacturers on m.Id equals pm.ManufacturerId
                                select m;

            using (var writer = new IndexWriter(LuceneDirectory, iwc))
            {
                InternalAddToLuceneIndex(product, productTagNames, categories, manufacturers, writer);

                writer.Dispose();
            }
        }
Exemple #2
0
        public async Task <ProductDto> PrepareProductDTOAsync(Product product)
        {
            var productDto      = product.ToDto();
            var productPictures = await _productService.GetProductPicturesByProductIdAsync(product.Id);

            await PrepareProductImagesAsync(productPictures, productDto);

            productDto.SeName = await _urlRecordService.GetSeNameAsync(product);

            productDto.DiscountIds     = (await _discountService.GetAppliedDiscountsAsync(product)).Select(discount => discount.Id).ToList();
            productDto.ManufacturerIds = (await _manufacturerService.GetProductManufacturersByProductIdAsync(product.Id)).Select(pm => pm.Id).ToList();
            productDto.RoleIds         = (await _aclService.GetAclRecordsAsync(product)).Select(acl => acl.CustomerRoleId).ToList();
            productDto.StoreIds        = (await _storeMappingService.GetStoreMappingsAsync(product)).Select(mapping => mapping.StoreId).ToList();
            productDto.Tags            = (await _productTagService.GetAllProductTagsByProductIdAsync(product.Id)).Select(tag => tag.Name).ToList();

            productDto.AssociatedProductIds = (await _productService.GetAssociatedProductsAsync(product.Id, showHidden: true))
                                              .Select(associatedProduct => associatedProduct.Id)
                                              .ToList();

            var allLanguages = await _languageService.GetAllLanguagesAsync();

            productDto.LocalizedNames = new List <LocalizedNameDto>();

            foreach (var language in allLanguages)
            {
                var localizedNameDto = new LocalizedNameDto
                {
                    LanguageId    = language.Id,
                    LocalizedName = await _localizationService.GetLocalizedAsync(product, x => x.Name, language.Id)
                };

                productDto.LocalizedNames.Add(localizedNameDto);
            }

            return(productDto);
        }
Exemple #3
0
        /// <summary>
        /// Create a copy of product with all depended data
        /// </summary>
        /// <param name="product">The product to copy</param>
        /// <param name="newName">The name of product duplicate</param>
        /// <param name="isPublished">A value indicating whether the product duplicate should be published</param>
        /// <param name="copyImages">A value indicating whether the product images should be copied</param>
        /// <param name="copyAssociatedProducts">A value indicating whether the copy associated products</param>
        /// <returns>
        /// A task that represents the asynchronous operation
        /// The task result contains the product copy
        /// </returns>
        public virtual async Task <Product> CopyProductAsync(Product product, string newName,
                                                             bool isPublished = true, bool copyImages = true, bool copyAssociatedProducts = true)
        {
            if (product == null)
            {
                throw new ArgumentNullException(nameof(product));
            }

            if (string.IsNullOrEmpty(newName))
            {
                throw new ArgumentException("Product name is required");
            }

            var productCopy = await CopyBaseProductDataAsync(product, newName, isPublished);

            //localization
            await CopyLocalizationDataAsync(product, productCopy);

            //copy product tags
            foreach (var productTag in await _productTagService.GetAllProductTagsByProductIdAsync(product.Id))
            {
                await _productTagService.InsertProductProductTagMappingAsync(new ProductProductTagMapping { ProductTagId = productTag.Id, ProductId = productCopy.Id });
            }

            await _productService.UpdateProductAsync(productCopy);

            //copy product pictures
            var originalNewPictureIdentifiers = await CopyProductPicturesAsync(product, newName, copyImages, productCopy);

            //quantity change history
            await _productService.AddStockQuantityHistoryEntryAsync(productCopy, product.StockQuantity, product.StockQuantity, product.WarehouseId,
                                                                    string.Format(await _localizationService.GetResourceAsync("Admin.StockQuantityHistory.Messages.CopyProduct"), product.Id));

            //product specifications
            await CopyProductSpecificationsAsync(product, productCopy);

            //product <-> warehouses mappings
            await CopyWarehousesMappingAsync(product, productCopy);

            //product <-> categories mappings
            await CopyCategoriesMappingAsync(product, productCopy);

            //product <-> manufacturers mappings
            await CopyManufacturersMappingAsync(product, productCopy);

            //product <-> related products mappings
            await CopyRelatedProductsMappingAsync(product, productCopy);

            //product <-> cross sells mappings
            await CopyCrossSellsMappingAsync(product, productCopy);

            //product <-> attributes mappings
            await CopyAttributesMappingAsync(product, productCopy, originalNewPictureIdentifiers);

            //product <-> discounts mapping
            await CopyDiscountsMappingAsync(product, productCopy);

            //store mapping
            var selectedStoreIds = await _storeMappingService.GetStoresIdsWithAccessAsync(product);

            foreach (var id in selectedStoreIds)
            {
                await _storeMappingService.InsertStoreMappingAsync(productCopy, id);
            }

            //tier prices
            await CopyTierPricesAsync(product, productCopy);

            //update "HasTierPrices" and "HasDiscountsApplied" properties
            await _productService.UpdateHasTierPricesPropertyAsync(productCopy);

            await _productService.UpdateHasDiscountsAppliedAsync(productCopy);

            //associated products
            await CopyAssociatedProductsAsync(product, isPublished, copyImages, copyAssociatedProducts, productCopy);

            return(productCopy);
        }