private void DeleteEntityThatStillExistInChannel(Entity channelEntity, Entity targetEntity, Entity parentEnt, string linkTypeId, List <StructureEntity> existingEntities, string channelIdentifier, string folderDateTime, string resourceZipFile) { var channelHelper = new ChannelHelper(_context); Dictionary <string, Dictionary <string, bool> > entitiesToUpdate = new Dictionary <string, Dictionary <string, bool> >(); var channelNodes = _context.ExtensionManager.ChannelService.GetAllChannelStructureEntitiesForType(channelEntity.Id, "ChannelNode").ToList(); if (!channelNodes.Any() && parentEnt.EntityType.Id == "Channel") { channelNodes.Add(_context.ExtensionManager.ChannelService.GetAllStructureEntitiesForEntityInChannel(channelEntity.Id, parentEnt.Id).First()); } List <string> linkEntityIds = new List <string>(); if (channelHelper.LinkTypeHasLinkEntity(linkTypeId)) { DeleteUtilConfig.ChannelStructureEntities = channelHelper.GetAllEntitiesInChannel( channelEntity.Id, DeleteUtilConfig.ExportEnabledEntityTypes); List <StructureEntity> newEntityNodes = channelHelper.FindEntitiesElementInStructure(DeleteUtilConfig.ChannelStructureEntities, parentEnt.Id, targetEntity.Id, linkTypeId); List <string> pars = new List <string>(); if (parentEnt.EntityType.Id == "Item" && DeleteUtilConfig.ItemsToSkus) { pars = _epiElement.SkuItemIds(parentEnt, DeleteUtilConfig); if (DeleteUtilConfig.UseThreeLevelsInCommerce) { pars.Add(parentEnt.Id.ToString(CultureInfo.InvariantCulture)); } } else { pars.Add(parentEnt.Id.ToString(CultureInfo.InvariantCulture)); } List <string> targets = new List <string>(); if (targetEntity.EntityType.Id == "Item" && DeleteUtilConfig.ItemsToSkus) { targets = _epiElement.SkuItemIds(targetEntity, DeleteUtilConfig); if (DeleteUtilConfig.UseThreeLevelsInCommerce) { targets.Add(targetEntity.Id.ToString(CultureInfo.InvariantCulture)); } } else { targets.Add(targetEntity.Id.ToString(CultureInfo.InvariantCulture)); } linkEntityIds = _epiApi.GetLinkEntityAssociationsForEntity(linkTypeId, channelEntity.Id, channelEntity, DeleteUtilConfig, pars, targets); linkEntityIds.RemoveAll(i => newEntityNodes.Any(n => i == _channelPrefixHelper.GetEPiCodeWithChannelPrefix(n.ParentId, DeleteUtilConfig))); } // Add the removed entity element together with all the underlying entity elements List <XElement> elementList = new List <XElement>(); foreach (StructureEntity existingEntity in existingEntities) { XElement copyOfElement = new XElement(existingEntity.Type + "_" + existingEntity.EntityId); if (elementList.All(p => p.Name.LocalName != copyOfElement.Name.LocalName)) { elementList.Add(copyOfElement); } if (DeleteUtilConfig.ChannelEntities.ContainsKey(existingEntity.EntityId)) { foreach (Link outboundLinks in DeleteUtilConfig.ChannelEntities[existingEntity.EntityId].OutboundLinks) { XElement copyOfDescendant = new XElement(outboundLinks.Target.EntityType.Id + "_" + outboundLinks.Target.Id); if (elementList.All(p => p.Name.LocalName != copyOfDescendant.Name.LocalName)) { elementList.Add(copyOfDescendant); } } } } List <XElement> updatedElements = elementList; foreach (XElement element in updatedElements) { string elementEntityType = element.Name.LocalName.Split('_')[0]; string elementEntityId = element.Name.LocalName.Split('_')[1]; Dictionary <string, bool> shouldExsistInChannelNodes = channelHelper.ShouldEntityExistInChannelNodes(int.Parse(elementEntityId), channelNodes, channelEntity.Id); if (elementEntityType == "Link") { continue; } if (elementEntityType == "Item" && DeleteUtilConfig.ItemsToSkus) { Entity deletedEntity = null; try { deletedEntity = _context.ExtensionManager.DataService.GetEntity( int.Parse(elementEntityId), LoadLevel.DataOnly); } catch (Exception ex) { _context.Log(LogLevel.Warning, "Error when getting entity:" + ex); } if (deletedEntity != null) { List <XElement> skus = _epiElement.GenerateSkuItemElemetsFromItem(deletedEntity, DeleteUtilConfig); foreach (XElement sku in skus) { XElement skuCode = sku.Element("Code"); if (skuCode != null && !entitiesToUpdate.ContainsKey(skuCode.Value)) { entitiesToUpdate.Add(skuCode.Value, shouldExsistInChannelNodes); } } } if (!DeleteUtilConfig.UseThreeLevelsInCommerce) { continue; } } if (!entitiesToUpdate.ContainsKey(elementEntityId)) { entitiesToUpdate.Add(elementEntityId, shouldExsistInChannelNodes); } } List <string> parents = new List <string> { parentEnt.Id.ToString(CultureInfo.InvariantCulture) }; if (parentEnt.EntityType.Id == "Item") { if (DeleteUtilConfig.ItemsToSkus) { parents = _epiElement.SkuItemIds(parentEnt, DeleteUtilConfig); if (DeleteUtilConfig.UseThreeLevelsInCommerce) { parents.Add(parentEnt.Id.ToString(CultureInfo.InvariantCulture)); } } } XDocument updateXml = new XDocument(new XElement("xml", new XAttribute("action", "updated"))); if (updateXml.Root != null) { List <XElement> parentElements = channelHelper.GetParentXElements(parentEnt, DeleteUtilConfig); foreach (var parentElement in parentElements) { updateXml.Root.Add(parentElement); } } foreach (KeyValuePair <string, Dictionary <string, bool> > entityIdToUpdate in entitiesToUpdate) { foreach (string parentId in parents) { _epiApi.UpdateEntryRelations(entityIdToUpdate.Key, channelEntity.Id, channelEntity, DeleteUtilConfig, parentId, entityIdToUpdate.Value, linkTypeId, linkEntityIds); } updateXml.Root?.Add(new XElement("entry", _channelPrefixHelper.GetEPiCodeWithChannelPrefix(entityIdToUpdate.Key, DeleteUtilConfig))); } if (!DocumentFileHelper.ZipDocumentAndUploadToAzure(XmlDocumentType.Catalog, updateXml, DeleteUtilConfig, folderDateTime)) { _context.Log(LogLevel.Information, "Failed to zip and upload the catalog file to azure from delete utility DeleteEntityThatStillExistInChannel() method"); } _context.Log(LogLevel.Debug, "catalog saved"); }