Example #1
0
        public async Task Update(LocalizationEntity localizationEntity)
        {
            var languageSet = localizationEntity.LanguageSets.First();

            var basicFilter = Builders <LocalizationEntity> .Filter.Eq(l => l.DisplayKey, localizationEntity.DisplayKey);

            var elementFilter =
                Builders <LocalizationEntity> .Filter.ElemMatch(l => l.LanguageSets,
                                                                ls => ls.LangCode == languageSet.LangCode);

            FilterDefinition <LocalizationEntity> filter;
            UpdateDefinition <LocalizationEntity> update;

            var collection = _db.GetCollection <LocalizationEntity>(CollectName);
            var document   = collection.FindAsync(Builders <LocalizationEntity> .Filter.And(basicFilter, elementFilter)).Result.Current;

            if (!document.Any())
            {
                filter = basicFilter;

                update = Builders <LocalizationEntity> .Update.Push(l => l.LanguageSets,
                                                                    languageSet);
            }
            else
            {
                filter = Builders <LocalizationEntity> .Filter.And(basicFilter, elementFilter);

                update = Builders <LocalizationEntity> .Update.Set(l => l.LanguageSets.ElementAt(-1),
                                                                   languageSet);
            }

            await collection.UpdateOneAsync(filter, update);
        }
        public override async Task <ImportLocalizeContentArgument> Run(ImportLocalizeContentArgument arg, CommercePipelineExecutionContext context)
        {
            if (arg.CommerceEntity != null &&
                arg.HasLocalizationContent)
            {
                var localizationEntityId = LocalizationEntity.GetIdBasedOnEntityId(arg.CommerceEntity.Id);
                var localizationEntity   = await _findEntityPipeline.Run(new FindEntityArgument(typeof(LocalizationEntity), localizationEntityId, arg.CommerceEntity.EntityVersion), context).ConfigureAwait(false);

                arg.LocalizationEntity = localizationEntity as LocalizationEntity;
            }

            return(await Task.FromResult(arg));
        }
Example #3
0
        public async Task Modify(LocalizationEntity localEntity)
        {
            var document = await _localizationRepository.GetByCode(localEntity.DisplayKey);

            if (document == null)
            {
                await _localizationRepository.Insert(localEntity);
            }
            else
            {
                await _localizationRepository.Update(localEntity);
            }
        }
Example #4
0
        /// <summary>
        /// Ensures the existence of the Category.
        /// If it doesn't exist it creates the Category, adds DisplayNames, adds identifiers and associates the Category with the parent Category.
        /// </summary>
        /// <param name="propositionCategory"></param>
        /// <param name="catalogId"></param>
        /// <param name="catalogName"></param>
        /// <param name="policy"></param>
        /// <param name="context"></param>
        /// <param name="associateWithCatalog"></param>
        /// <returns></returns>
        private async Task <List <Category> > EnsureCategory(CollectionFolder propositionCategory, string catalogId,
                                                             string catalogName, SyncForceClientPolicy policy, CommerceContext context,
                                                             bool associateWithCatalog = false)
        {
            var result = new List <Category>();

            var categoryName = propositionCategory.Values.FirstOrDefault(l => l.Language == policy.DefaultLanguage)
                               ?.Name;
            //Added sort index to category name to make sure sorting is done right, instead of done on alphabetical order.
            var categoryId =
                $"{CommerceEntity.IdPrefix<Category>()}{catalogName}-{propositionCategory.SortIndex.ToString().PadLeft(3, '0') + propositionCategory.Id + categoryName.ProposeValidId()}";
            Category category = null;

            //Get or create category
            if (await _doesEntityExistPipeline.Run(new FindEntityArgument(typeof(Category), categoryId),
                                                   context.PipelineContextOptions))
            {
                category = await _getCategoryPipeline.Run(new GetCategoryArgument(categoryId),
                                                          context.PipelineContextOptions);
            }
            else
            {
                var createResult = await _createCategoryPipeline.Run(
                    new CreateCategoryArgument(catalogId,
                                               propositionCategory.SortIndex.ToString().PadLeft(3, '0') + propositionCategory.Id +
                                               categoryName.ProposeValidId(), categoryName, ""), context.PipelineContextOptions);

                category = createResult?.Categories?.FirstOrDefault(c => c.Id == categoryId);
            }

            if (category != null)
            {
                //Localize properties
                var localizationEntityId = LocalizationEntity.GetIdBasedOnEntityId(category.Id);
                var localizationEntity   = await _findEntityPipeline.Run(new FindEntityArgument(typeof(LocalizationEntity), localizationEntityId),
                                                                         context.PipelineContextOptions) as LocalizationEntity;

                if (localizationEntity != null)
                {
                    var displayNames = propositionCategory.Values.Select(p => new Parameter(p.Language, p.Name)).ToList();
                    localizationEntity.AddOrUpdatePropertyValue("DisplayName", displayNames);

                    var descriptions = propositionCategory.CategoryContent.Values.Select(p => new Parameter(p.Language, p.Name)).ToList();
                    localizationEntity.AddOrUpdatePropertyValue("Description", descriptions);

                    await _persistEntityPipeline.Run(new PersistEntityArgument(localizationEntity),
                                                     context.PipelineContextOptions);
                }

                //Add identifiers
                var identifiersComponent = category.GetComponent <IdentifiersComponent>();
                if (!identifiersComponent.CustomId.Any(i => i.Key.Equals(policy.CustomIdentifierKey)))
                {
                    identifiersComponent.CustomId.Add(
                        new Sitecore.Commerce.Plugin.Catalogs.SyncForce.Models.CustomIdentifier(
                            policy.CustomIdentifierKey, propositionCategory.Id.ToString()));
                }
                category.SetComponent(identifiersComponent);
                category = (await _persistEntityPipeline.Run(new PersistEntityArgument(category),
                                                             context.PipelineContextOptions))?.Entity as Category ?? category;

                //Add custom properties
                var categoryComponent = category.GetComponent <SyncForceCategoryComponent>();
                categoryComponent.Sortorder = propositionCategory.SortIndex;
                category.SetComponent(categoryComponent);
                category = (await _persistEntityPipeline.Run(new PersistEntityArgument(category), context.PipelineContextOptions))?.Entity as Category ?? category;

                //Create sub-categories
                if (propositionCategory?.ProductPropositionCategories?.Any() ?? false)
                {
                    foreach (var subCategory in propositionCategory.ProductPropositionCategories)
                    {
                        var s = await EnsureCategory(subCategory, catalogId, catalogName, policy, context);

                        result.AddRange(s);

                        foreach (var createdSubCategory in s)
                        {
                            var persistedSubCategory =
                                (await _persistEntityPipeline.Run(new PersistEntityArgument(createdSubCategory),
                                                                  context.PipelineContextOptions))?.Entity;
                            var associateResult = await _associateCategoryToParentPipeline.Run(
                                new CatalogReferenceArgument(catalogId, category.Id,
                                                             persistedSubCategory?.Id ?? createdSubCategory.Id),
                                context.PipelineContextOptions);

                            category = associateResult?.Categories?.FirstOrDefault(c => c.Id == category.Id) ??
                                       category;
                        }
                    }
                }

                //Associate with catalog if top level category
                if (associateWithCatalog)
                {
                    var associateResult = await _associateCategoryToParentPipeline.Run(
                        new CatalogReferenceArgument(catalogId, catalogId, category.Id),
                        context.PipelineContextOptions);

                    category = associateResult?.Categories
                               ?.FirstOrDefault(c => c.Id == category.Id) ?? category;
                }

                result.Add(category);
            }

            return(result);
        }
Example #5
0
 public async Task Insert(LocalizationEntity localizationEntity)
 {
     var collection = _db.GetCollection <LocalizationEntity>(CollectName);
     await collection.InsertOneAsync(localizationEntity);
 }