public void DeleteAssociation(string sourceCode, string targetCode) { _logger.Debug($"Deleting association between {sourceCode} and {targetCode}."); ContentReference sourceReference = _referenceConverter.GetContentLink(sourceCode); ContentReference targetReference = _referenceConverter.GetContentLink(targetCode); IEnumerable <Association> associations = _associationRepository.GetAssociations(sourceReference); Association existingAssociation = associations.FirstOrDefault(x => x.Target.Equals(targetReference)); if (existingAssociation != null) { _associationRepository.RemoveAssociation(existingAssociation); } }
// ToDo: clean up here ... have an Aggergation for this part ... could use as demo private IEnumerable <ContentReference> GetAssociatedEntries(EntryContentBase currentContent) { // using linksRep is gone IAssociationRepository _linksRep = ServiceLocator.Current.GetInstance <IAssociationRepository>(); IEnumerable <EPiServer.Commerce.Catalog.Linking.Association> linksRepAssoc = _linksRep.GetAssociations(currentContent.ContentLink).Where(l => l.Group.Name == "CrossSell"); // would like to be able to filter when calling, instead of .Where() // would like to get the metadata out ... like type and group... and probaly treat them differently IEnumerable <EPiServer.Commerce.Catalog.Linking.Association> assoc = currentContent.GetAssociations(); List <ContentReference> refs = new List <ContentReference>(); foreach (EPiServer.Commerce.Catalog.Linking.Association item in assoc) { refs.Add(item.Target); } return(refs); }
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; } } } }
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; } } } }