public XDocument CreateUpdateDocument(Entity channelEntity, Entity updatedEntity, Configuration config) { int count = 0; List <XElement> skus = new List <XElement>(); if (config.ItemsToSkus && updatedEntity.EntityType.Id == "Item") { skus = _epiElement.GenerateSkuItemElemetsFromItem(updatedEntity, config); count += skus.Count; } XElement updatedNode = null; XElement updatedEntry = null; if (updatedEntity.EntityType.Id == "ChannelNode") { string parentId = channelEntity.Id.ToString(CultureInfo.InvariantCulture); Link nodeLink = updatedEntity.Links.Find(l => l.Source.Id == channelEntity.Id); int sortOrder = 0; if (nodeLink != null) { sortOrder = nodeLink.Index; } updatedNode = _epiElement.CreateNodeElement(updatedEntity, parentId, sortOrder, config); } else if (!(updatedEntity.EntityType.Id == "Item" && !config.UseThreeLevelsInCommerce && config.ItemsToSkus)) { updatedEntry = _epiElement.InRiverEntityToEpiEntry(updatedEntity, config); Link specLink = updatedEntity.OutboundLinks.Find(l => l.Target.EntityType.Id == "Specification"); if (specLink != null) { XElement metaField = new XElement("MetaField", new XElement("Name", "SpecificationField"), new XElement("Type", "LongHtmlString")); foreach (KeyValuePair <CultureInfo, CultureInfo> culturePair in config.LanguageMapping) { string htmlData = _context.ExtensionManager.DataService.GetSpecificationAsHtml(specLink.Target.Id, updatedEntity.Id, culturePair.Value); metaField.Add(new XElement("Data", new XAttribute("language", culturePair.Key.Name.ToLower()), new XAttribute("value", htmlData))); } XElement element = updatedEntry.Descendants().FirstOrDefault(f => f.Name == "MetaFields"); if (element != null) { element.Add(metaField); } } count += 1; } XElement catalogElement = _epiElement.CreateCatalogElement(channelEntity, config); catalogElement.Add( new XElement("Sites", new XElement("Site", _channelHelper.GetChannelGuid(channelEntity, config).ToString())), new XElement("Nodes", updatedNode), new XElement("Entries", new XAttribute("totalCount", count), updatedEntry, skus), new XElement("Relations"), new XElement("Associations")); return(CreateDocument(catalogElement, null, null, config)); }
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"); }