private void AddNodeElements(List <StructureEntity> batch) { var nodeStructureEntities = batch.Where(x => x.IsChannelNode() && x.EntityId != _config.ChannelId); foreach (var structureEntity in nodeStructureEntities) { var entity = _entityService.GetEntity(structureEntity.EntityId, LoadLevel.DataOnly); if (_config.ChannelId == structureEntity.ParentId) { _epiApi.MoveNodeToRootIfNeeded(entity.Id); } IntegrationLogger.Write(LogLevel.Debug, $"Trying to add channelNode {entity.Id} to Nodes"); var currentNodeCode = _catalogCodeGenerator.GetEpiserverCode(entity); XElement nodeElement = _epiElementContainer.Nodes.FirstOrDefault(e => e.Element("Code")?.Value == currentNodeCode); int linkIndex = structureEntity.SortOrder; if (nodeElement == null) { nodeElement = _catalogElementFactory.CreateNodeElement(entity, structureEntity.ParentId, linkIndex); _epiElementContainer.AddNode(nodeElement, currentNodeCode); IntegrationLogger.Write(LogLevel.Debug, $"Added channelNode {entity.Id} to Nodes"); } else { var parentNode = nodeElement.Element("ParentNode"); if (parentNode != null && parentNode.Value != _config.ChannelId.ToString(CultureInfo.InvariantCulture) && structureEntity.ParentId == _config.ChannelId) { parentNode.Value = _config.ChannelId.ToString(CultureInfo.InvariantCulture); var sortOrderElement = nodeElement.Element("SortOrder"); if (sortOrderElement != null) { string oldSortOrder = sortOrderElement.Value; sortOrderElement.Value = linkIndex.ToString(CultureInfo.InvariantCulture); linkIndex = int.Parse(oldSortOrder); } } var relationName = _catalogCodeGenerator.GetRelationName(entity.Id, structureEntity.ParentId); if (_epiElementContainer.HasRelation(relationName)) { continue; } var nodeRelationElement = _catalogElementFactory.CreateNodeRelation(structureEntity.ParentId, entity.Id, linkIndex); _epiElementContainer.AddRelation(nodeRelationElement, relationName); IntegrationLogger.Write(LogLevel.Debug, $"Adding relation to channelNode {entity.Id}"); } } }
private async Task AddNodeElementsAsync(IEnumerable <StructureEntity> batch) { IEnumerable <StructureEntity> nodeStructureEntities = batch.Where(x => x.IsChannelNode() && x.EntityId != _config.ChannelId); foreach (StructureEntity structureEntity in nodeStructureEntities) { Entity entity = _entityService.GetEntity(structureEntity.EntityId, LoadLevel.DataOnly); if (_config.ChannelId == structureEntity.ParentId) { try { await _epiApi.MoveNodeToRootIfNeededAsync(entity.Id); } catch (Exception ex) { IntegrationLogger.Write(LogLevel.Warning, $"Could not process entity {entity.Id}. Message: {ex.Message}"); } } IntegrationLogger.Write(LogLevel.Debug, $"Trying to add channelNode {entity.Id} to Nodes"); string currentNodeCode = _catalogCodeGenerator.GetEpiserverCode(entity); XElement nodeElement = _epiElementContainer.Nodes.FirstOrDefault(e => e.Element("Code")?.Value == currentNodeCode); int linkIndex = structureEntity.SortOrder; if (nodeElement == null) { nodeElement = _catalogElementFactory.CreateNodeElement(entity, structureEntity.ParentId, linkIndex); _epiElementContainer.AddNode(nodeElement, currentNodeCode); IntegrationLogger.Write(LogLevel.Debug, $"Added channelNode {entity.Id} to Nodes"); } else { XElement parentNode = nodeElement.Element("ParentNode"); if (parentNode != null && parentNode.Value != _config.ChannelId.ToString(CultureInfo.InvariantCulture) && structureEntity.ParentId == _config.ChannelId) { parentNode.Value = _config.ChannelId.ToString(CultureInfo.InvariantCulture); XElement sortOrderElement = nodeElement.Element("SortOrder"); if (sortOrderElement != null) { string oldSortOrder = sortOrderElement.Value; sortOrderElement.Value = linkIndex.ToString(CultureInfo.InvariantCulture); linkIndex = Int32.Parse(oldSortOrder); } } string relationName = _catalogCodeGenerator.GetRelationName(entity.Id, structureEntity.ParentId); if (_epiElementContainer.HasRelation(relationName)) { continue; } XElement nodeRelationElement = _catalogElementFactory.CreateNodeRelation(structureEntity.ParentId, entity.Id, linkIndex); _epiElementContainer.AddRelation(nodeRelationElement, relationName); IntegrationLogger.Write(LogLevel.Debug, $"Adding relation to channelNode {entity.Id}"); } } }