public override IEnumerable <(CultureInfo, IDocument)> BuildIndexDocuments(IndexQueueItem item)
        {
            var page = _pageService.Get(item.SystemId);

            if (page == null)
            {
                yield break;
            }

            var cultureContentCache = new ConcurrentDictionary <CultureInfo, ISet <string> >();
            var permissions         = _searchPermissionService.GetPermissions(page);
            var blocks  = page.Blocks.Items.SelectMany(x => x.Items).Select(y => _blockService.Get(((BlockItemLink)y).BlockSystemId)).ToList();
            var isBrand = false;
            var isNews  = false;

            if (_keyLookupService.TryGetId <PageFieldTemplate>(page.FieldTemplateSystemId, out var templateId))
            {
                isBrand = PageTemplateNameConstants.Brand.Equals(templateId, StringComparison.OrdinalIgnoreCase);
                isNews  = PageTemplateNameConstants.News.Equals(templateId, StringComparison.OrdinalIgnoreCase);
            }

            foreach (var channelLink in page.ChannelLinks)
            {
                var channel = _channelService.Get(channelLink.ChannelSystemId);
                if (channel is null)
                {
                    // Orphaned category link exists, skip to create new index document.
                    continue;
                }

                var cultureInfo = _languageService.Get(channel.WebsiteLanguageSystemId.GetValueOrDefault())?.CultureInfo;
                if (cultureInfo is null)
                {
                    // Channel does not have a culture.
                    continue;
                }

                var localization = page.Localizations[cultureInfo];
                yield return(cultureInfo, new PageDocument
                {
                    PageSystemId = page.SystemId,
                    Content = cultureContentCache.GetOrAdd(cultureInfo, _ => _contentBuilderService.BuildContent <PageFieldTemplate, WebsiteArea>(page.FieldTemplateSystemId, cultureInfo, page.Fields)),
                    PublishDateTime = page.PublishedAtUtc ?? DateTimeOffset.MinValue,
                    Name = localization.Name,
                    ChannelSystemId = channel.SystemId,
                    WebsiteSystemId = page.WebsiteSystemId,
                    ChannelStartDateTime = channelLink.StartDateTimeUtc ?? DateTimeOffset.MinValue,
                    ChannelEndDateTime = channelLink.EndDateTimeUtc ?? DateTimeOffset.MaxValue,
                    Permissions = permissions,
                    Blocks = GetBlocks(blocks, cultureInfo, channel.SystemId),
                    IsBrand = isBrand,
                    IsNews = isNews,
                    NewsDate = isNews ? page.Fields.GetValue <DateTimeOffset>(PageFieldNameConstants.NewsDate) : DateTimeOffset.MinValue,
                    ParentPages = GetParenPages(page)
                });
            }
        }
Ejemplo n.º 2
0
        private void DoWork()
        {
            while (!shutdown)
            {
                IndexQueueItem item = null;
                try
                {
                    if (_indexQueue.Queue.TryDequeue(out item))
                    {
                        try
                        {
                            //_logger.LogDebug("Dequeued item - " + item.EntityID);

                            //IIndexStore indexStore = _services.GetService<IIndexStore>();
                            IIndexStore   indexStore = IoC.Container.Resolve <IIndexStore>();
                            IDbContext    primaryDC  = (IoC.Container.Resolve(item.ContextType) as IDbContext);
                            IIndexBuilder indexer    = new IndexBuilder(indexStore, primaryDC, _indexQueue, _registry); //TODO: fix
                            indexer.IndexEntity(item.EntityType, item.EntityID, item.Recursive, item.ContextType);
                            indexStore.SaveChanges();
                            indexStore.Dispose();
                            primaryDC.Dispose();
                        }
                        catch (OptimisticConcurrencyException ex)
                        {
                            _indexQueue.Queue.Enqueue(item);
                            //_logger.LogError("Error indexing (" + item.EntityType + ", " + item.EntityID.ToString() + "): " + ex.Message, ex);
                        }
                        catch (Exception ex)
                        {
                            //todo: add error reporting
                            //_logger.LogError("Error indexing (" + item.EntityType + ", " + item.EntityID.ToString() + "): " + ex.Message, ex);
                        }
                    }
                    else
                    {
                        System.Threading.Thread.Sleep(500); //no work
                    }
                }
                catch (Exception ex)
                {
                    //todo: add error reporting
                    //_logger.LogError("Error indexing (" + item.EntityType + ", " + item.EntityID.ToString() + "): " + ex.Message, ex);
                }
            }
        }
Ejemplo n.º 3
0
        public override IEnumerable <(CultureInfo, IDocument)> BuildIndexDocuments(IndexQueueItem item)
        {
            var category = _categoryService.Get(item.SystemId);

            if (category == null)
            {
                yield break;
            }

            var cultureContentCache = new ConcurrentDictionary <CultureInfo, ISet <string> >();
            var permissions         = _searchPermissionService.GetPermissions(category);

            foreach (var channelLink in category.ChannelLinks)
            {
                var channel = _channelService.Get(channelLink.ChannelSystemId);
                if (channel is null)
                {
                    // Orphaned category link exists, skip to create new index document.
                    continue;
                }

                var cultureInfo = _languageService.Get(channel.WebsiteLanguageSystemId.GetValueOrDefault())?.CultureInfo;
                if (cultureInfo is null)
                {
                    // Channel does not have a culture.
                    continue;
                }

                var localization = category.Localizations[cultureInfo];
                yield return(cultureInfo, new CategoryDocument
                {
                    CategorySystemId = category.SystemId,
                    Content = cultureContentCache.GetOrAdd(cultureInfo, _ => _contentBuilderService.BuildContent <CategoryFieldTemplate, ProductArea>(category.FieldTemplateSystemId, cultureInfo, category.Fields)),
                    Name = localization.Name,
                    ChannelSystemId = channel.SystemId,
                    Permissions = permissions,
                    Assortment = category.AssortmentSystemId,
                    Organizations = category.Fields.GetValue <IList <PointerItem> >(ProductFieldNameConstants.OrganizationsPointer)?.Select(x => x.EntitySystemId).ToHashSet()
                                    ?? new HashSet <Guid> {
                        Guid.Empty
                    }
                });
            }
        }
        public override IEnumerable <(CultureInfo, IDocument)> BuildIndexDocuments(IndexQueueItem item)
        {
            var channels = _channelService.GetAll().Where(x => x.ProductLanguageSystemId.HasValue && x.ProductLanguageSystemId.Value != Guid.Empty).ToList();

            var baseProduct = _baseProductService.Get(item.SystemId);

            if (baseProduct == null)
            {
                // Product is removed.
                // BuildRemoveIndexDocuments will be invoked by the removing of product.
                yield break;
            }

            var productFieldTemplate = _fieldTemplateService.Get <ProductFieldTemplate>(baseProduct.FieldTemplateSystemId);

            if (productFieldTemplate == null)
            {
                // Field template is removed, all products with this template is also removed.
                // BuildRemoveIndexDocuments will be invoked by the removing of product.
                yield break;
            }

            var displayTemplate = _displayTemplateService.Get <ProductDisplayTemplate>(productFieldTemplate.DisplayTemplateSystemId);

            if (displayTemplate == null)
            {
                // Display template is removed, all products with template that using the display template is also removed.
                // BuildRemoveIndexDocuments will be invoked by the removing of product.
                yield break;
            }

            var variants = _variantService.GetByBaseProduct(baseProduct.SystemId).ToList();

            if (variants.Count == 0)
            {
                // No variants exists for the BaseProduct, remove any existing document from index
                foreach (var channel in channels.GroupBy(x => x.ProductLanguageSystemId))
                {
                    var cultureInfo = _languageService.Get(channel.First().ProductLanguageSystemId.GetValueOrDefault())?.CultureInfo;
                    if (cultureInfo is null)
                    {
                        continue;
                    }
                    yield return(cultureInfo, RemoveByFieldDocument.Create <ProductDocument, Guid>(x => x.BaseProductSystemId, item.SystemId));
                }
                yield break;
            }

            var productDocuments     = CreateModelsPerChannel(baseProduct, productFieldTemplate, displayTemplate, ref variants);
            var usedVariantSystemIds = productDocuments.SelectMany(x => x.VariantSystemIds).ToHashSet();
            var usedChannelSystemIds = productDocuments.Select(x => x.ChannelSystemId).ToHashSet();

            // Remove all documents for all channel-combinations
            foreach (var channel in channels.Where(x => !usedChannelSystemIds.Contains(x.SystemId)))
            {
                var cultureInfo = _languageService.Get(channel.ProductLanguageSystemId.GetValueOrDefault())?.CultureInfo;
                if (cultureInfo is null)
                {
                    continue;
                }

                yield return(cultureInfo, new RemoveDocument(new ProductDocument {
                    ArticleNumber = baseProduct.Id, ChannelSystemId = channel.SystemId
                }));

                foreach (var variant in variants)
                {
                    yield return(cultureInfo, new RemoveDocument(new ProductDocument {
                        ArticleNumber = variant.Id, ChannelSystemId = channel.SystemId
                    }));
                }
            }

            var context = new Context(_contentBuilderService)
            {
                BaseProduct   = baseProduct,
                UsedVariants  = variants.Where(x => usedVariantSystemIds.Contains(x.SystemId)).ToList(),
                Permissions   = _searchPermissionService.GetPermissions(baseProduct),
                FieldTemplate = productFieldTemplate
            };

            context.Prices = BuildPriceData(context);

            foreach (var model in productDocuments)
            {
                var currentVariants = variants.Where(x => model.VariantSystemIds.Contains(x.SystemId)).ToList();
                var channel         = _channelService.Get(model.ChannelSystemId);
                if (channel is null)
                {
                    // Orphaned category link exists, skip to create new index document.
                    continue;
                }

                var cultureInfo = _languageService.Get(channel.ProductLanguageSystemId.GetValueOrDefault())?.CultureInfo;
                if (cultureInfo is null || currentVariants.Count == 0)
                {
                    // Channel does not have a culture or we don't have any variants in this channel.
                    continue;
                }

                PopulateProductDocument(model, cultureInfo, currentVariants, context, !displayTemplate.UseVariantUrl);
                yield return(cultureInfo, model);
            }
        }
 public override IEnumerable <IDocument> BuildRemoveIndexDocuments(IndexQueueItem item)
 {
     yield return(RemoveByFieldDocument.Create <ProductDocument, Guid>(x => x.BaseProductSystemId, item.SystemId));
 }