Ejemplo n.º 1
0
        public void ExportProductRecommendations(IEnumerable <EntryContentBase> entries, IExportState exportState)
        {
            if (!_configuration.ProductRecommendationsImportUrl.IsValidProductRecommendationsImportUrl())
            {
                return;
            }

            var allAssociations = new HashSet <Association>();
            var entriesToDelete = new HashSet <EntryContentBase>(ContentComparer.Default);

            foreach (EntryContentBase entry in entries
                     .Distinct(ContentComparer.Default)
                     .OfType <EntryContentBase>())
            {
                var associations = (ICollection <Association>)_associationRepository.GetAssociations(entry.ContentLink);
                if (associations.Count == 0)
                {
                    entriesToDelete.Add(entry);
                }
                else
                {
                    foreach (Association association in associations)
                    {
                        allAssociations.Add(association);
                    }
                }
            }

            foreach (var associationsByGroup in allAssociations
                     .GroupBy(a => a.Group.Name))
            {
                if (exportState != null)
                {
                    exportState.Action    = "Exported";
                    exportState.ModelName = $"product associations ({associationsByGroup.Key})";
                    exportState.Total     = associationsByGroup.Count();
                    exportState.Uploaded  = 0;
                }

                var recommendationGroups = associationsByGroup
                                           .GroupBy(a => a.Source)
                                           .Select(g => _productFactory.BuildKaChingRecommendationGroup(g.Key, g.ToArray()))
                                           .Where(x => x != null);

                foreach (var group in recommendationGroups
                         .Batch(BatchSize))
                {
                    APIFacade.Post(
                        new { products = group },
                        _configuration.ProductRecommendationsImportUrl + "&recommendation_id=" + associationsByGroup.Key.SanitizeKey());

                    if (exportState != null)
                    {
                        exportState.Uploaded += group.Count;
                    }
                }
            }

            if (entriesToDelete.Count == 0)
            {
                return;
            }

            foreach (var associationGroup in _associationGroupRepository.List())
            {
                if (exportState != null)
                {
                    exportState.Action    = "Deleted";
                    exportState.ModelName = $"product associations ({associationGroup.Name})";
                    exportState.Total     = entriesToDelete.Count;
                    exportState.Uploaded  = 0;
                }

                foreach (var batch in entriesToDelete
                         .Select(c => c.Code.SanitizeKey())
                         .Batch(BatchSize))
                {
                    // Call the external endpoint asynchronously and return immediately.
                    Task.Factory.StartNew(() =>
                                          APIFacade.DeleteObjectAsync(
                                              batch,
                                              _configuration.ProductRecommendationsImportUrl + "&recommendation_id=" + associationGroup.Name.SanitizeKey())
                                          .ConfigureAwait(false));

                    if (exportState != null)
                    {
                        exportState.Uploaded += batch.Count;
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void ExportProductRecommendations(IEnumerable <ContentReference> entryLinks, IExportState exportState)
        {
            if (!_configuration.ProductRecommendationsImportUrl.IsValidProductRecommendationsImportUrl())
            {
                return;
            }

            var allAssociations    = new List <Association>();
            var entryLinksToDelete = new HashSet <ContentReference>(ContentReferenceComparer.IgnoreVersion);

            foreach (ContentReference entryLink in entryLinks
                     .Distinct(ContentReferenceComparer.IgnoreVersion))
            {
                var associations = (ICollection <Association>)_associationRepository.GetAssociations(entryLink);
                if (associations.Count == 0)
                {
                    entryLinksToDelete.Add(entryLink);
                }
                else
                {
                    allAssociations.AddRange(associations);
                }
            }

            foreach (var associationsByGroup in allAssociations
                     .GroupBy(a => a.Group.Name))
            {
                if (exportState != null)
                {
                    exportState.Action    = "Exported";
                    exportState.ModelName = $"product associations ({associationsByGroup.Key})";
                    exportState.Total     = associationsByGroup.Count();
                    exportState.Uploaded  = 0;
                }

                var recommendationGroups = associationsByGroup
                                           .GroupBy(a => a.Source)
                                           .Select(g => _productFactory.BuildKaChingRecommendationGroup(g.Key, g.ToArray()))
                                           .Where(x => x != null);

                foreach (var group in recommendationGroups
                         .Batch(BatchSize))
                {
                    APIFacade.Post(
                        new { products = group },
                        _configuration.ProductRecommendationsImportUrl + "&recommendation_id=" + associationsByGroup.Key.SanitizeKey());

                    if (exportState != null)
                    {
                        exportState.Uploaded += group.Count;
                    }
                }
            }

            if (entryLinksToDelete.Count == 0)
            {
                return;
            }

            foreach (var associationGroup in _associationGroupRepository.List())
            {
                if (exportState != null)
                {
                    exportState.Action    = "Deleted";
                    exportState.ModelName = $"product associations ({associationGroup.Name})";
                    exportState.Total     = entryLinksToDelete.Count;
                    exportState.Uploaded  = 0;
                }

                foreach (var batch in _contentLoader
                         .GetItems(entryLinksToDelete, CultureInfo.InvariantCulture)
                         .OfType <EntryContentBase>()
                         .Select(c => c.Code.SanitizeKey())
                         .Batch(BatchSize))
                {
                    APIFacade.DeleteObject(
                        batch,
                        _configuration.ProductRecommendationsImportUrl + "&recommendation_id=" + associationGroup.Name.SanitizeKey());

                    if (exportState != null)
                    {
                        exportState.Uploaded += batch.Count;
                    }
                }
            }
        }