Beispiel #1
0
 private void InitDisplayTemplates(IEnumerable <DisplayTemplate> displayTemplates)
 {
     foreach (var displayTemplate in displayTemplates)
     {
         if (IsAlreadyExecuted <DisplayTemplate>(displayTemplate.Id))
         {
             continue;
         }
         var currentField = _displayTemplateService.Get <DisplayTemplate>(displayTemplate.Id);
         if (currentField != null)
         {
             continue;
         }
         _displayTemplateService.Create(displayTemplate);
         SetAlreadyExecuted <DisplayTemplate>(displayTemplate.Id);
     }
 }
        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 <FieldTemplate> GetTemplates()
        {
            var categoryDisplayTemplateId = _displayTemplateService.Get <CategoryDisplayTemplate>("Category")?.SystemId ?? Guid.Empty;
            var productDisplayTemplateId  = _displayTemplateService.Get <ProductDisplayTemplate>("Product")?.SystemId ?? Guid.Empty;
            var productWithVariantListDisplayTemplateId = _displayTemplateService.Get <ProductDisplayTemplate>("ProductWithVariantList")?.SystemId ?? Guid.Empty;

            if (categoryDisplayTemplateId == Guid.Empty || productDisplayTemplateId == Guid.Empty || productWithVariantListDisplayTemplateId == Guid.Empty)
            {
                return(Enumerable.Empty <FieldTemplate>());
            }

            var fieldTemplates = new FieldTemplate[]
            {
                new CategoryFieldTemplate("Category", categoryDisplayTemplateId)
                {
                    CategoryFieldGroups = new[]
                    {
                        new FieldTemplateFieldGroup
                        {
                            Id            = "General",
                            Collapsed     = false,
                            Localizations =
                            {
                                ["sv-SE"] = { Name = "Allmänt" },
                                ["en-US"] = { Name = "General" }
                            },
                            Fields =
                            {
                                "_name",
                                "_description",
                                "_url",
                                "_seoTitle",
                                "_seoDescription",
                                "AcceleratorFilterFields",
                                ProductFieldNameConstants.OrganizationsPointer
                            }
                        }
                    }
                },
                new ProductFieldTemplate("ProductWithOneVariant", productDisplayTemplateId)
                {
                    ProductFieldGroups = new[]
                    {
                        new FieldTemplateFieldGroup
                        {
                            Id            = "General",
                            Collapsed     = false,
                            Localizations =
                            {
                                ["sv-SE"] = { Name = "Allmänt" },
                                ["en-US"] = { Name = "General" }
                            },
                            Fields =
                            {
                                "_name",
                                ProductFieldNameConstants.OrganizationsPointer
                            }
                        }
                    },
                    VariantFieldGroups = new[]
                    {
                        new FieldTemplateFieldGroup
                        {
                            Id            = "General",
                            Collapsed     = false,
                            Localizations =
                            {
                                ["sv-SE"] = { Name = "Allmänt" },
                                ["en-US"] = { Name = "General" }
                            },
                            Fields =
                            {
                                "_name",
                                "_description",
                                "_url",
                                "_seoTitle",
                                "_seoDescription"
                            }
                        },
                        new FieldTemplateFieldGroup
                        {
                            Id            = "Product information",
                            Collapsed     = false,
                            Localizations =
                            {
                                ["sv-SE"] = { Name = "Produktinformation"  },
                                ["en-US"] = { Name = "Product information" }
                            },
                            Fields =
                            {
                                "News",
                                "Brand",
                                "Color",
                                "Size",
                                "ProductSheet"
                            }
                        },
                        new FieldTemplateFieldGroup
                        {
                            Id            = "Product specification",
                            Collapsed     = false,
                            Localizations =
                            {
                                ["sv-SE"] = { Name = "Produktspecifikation"  },
                                ["en-US"] = { Name = "Product specification" }
                            },
                            Fields =
                            {
                                "Specification",
                                "Weight"
                            }
                        }
                    }
                },
                new ProductFieldTemplate("ProductWithVariants", productDisplayTemplateId)
                {
                    ProductFieldGroups = new[]
                    {
                        new FieldTemplateFieldGroup
                        {
                            Id            = "General",
                            Collapsed     = false,
                            Localizations =
                            {
                                ["sv-SE"] = { Name = "Allmänt" },
                                ["en-US"] = { Name = "General" }
                            },
                            Fields =
                            {
                                "_name",
                                "_description",
                                ProductFieldNameConstants.OrganizationsPointer
                            }
                        },
                        new FieldTemplateFieldGroup
                        {
                            Id            = "Product information",
                            Collapsed     = false,
                            Localizations =
                            {
                                ["sv-SE"] = { Name = "Produktinformation"  },
                                ["en-US"] = { Name = "Product information" }
                            },
                            Fields =
                            {
                                "News",
                                "Brand",
                                "ProductSheet"
                            }
                        }
                    },
                    VariantFieldGroups = new[]
                    {
                        new FieldTemplateFieldGroup
                        {
                            Id            = "General",
                            Collapsed     = false,
                            Localizations =
                            {
                                ["sv-SE"] = { Name = "Allmänt" },
                                ["en-US"] = { Name = "General" }
                            },
                            Fields =
                            {
                                "_name",
                                "_description",
                                "_url",
                                "_seoTitle",
                                "_seoDescription",
                            }
                        },
                        new FieldTemplateFieldGroup
                        {
                            Id            = "Product information",
                            Collapsed     = false,
                            Localizations =
                            {
                                ["sv-SE"] = { Name = "Produktinformation"  },
                                ["en-US"] = { Name = "Product information" }
                            },
                            Fields =
                            {
                                "Color",
                                "Size"
                            }
                        }
                    }
                },
                new ProductFieldTemplate("ProductWithVariantsList", productWithVariantListDisplayTemplateId)
                {
                    ProductFieldGroups = new[]
                    {
                        new FieldTemplateFieldGroup
                        {
                            Id            = "General",
                            Collapsed     = false,
                            Localizations =
                            {
                                ["sv-SE"] = { Name = "Allmänt" },
                                ["en-US"] = { Name = "General" }
                            },
                            Fields =
                            {
                                "Brand",
                                "_name",
                                "_description",
                                "_url",
                                "_seoTitle",
                                "_seoDescription",
                                ProductFieldNameConstants.OrganizationsPointer
                            }
                        },
                        new FieldTemplateFieldGroup
                        {
                            Id            = "Product specification",
                            Collapsed     = false,
                            Localizations =
                            {
                                ["sv-SE"] = { Name = "Produktspecifikation"  },
                                ["en-US"] = { Name = "Product specification" }
                            },
                            Fields =
                            {
                                "Specification",
                                "ProductSheet"
                            }
                        }
                    },
                    VariantFieldGroups = new[]
                    {
                        new FieldTemplateFieldGroup
                        {
                            Id            = "General",
                            Collapsed     = false,
                            Localizations =
                            {
                                ["sv-SE"] = { Name = "Allmänt" },
                                ["en-US"] = { Name = "General" }
                            },
                            Fields =
                            {
                                "_name",
                                "Color",
                                "Size"
                            }
                        }
                    }
                }
            };

            return(fieldTemplates);
        }