private void ImportLink(MappedLink mappedLink, Entity currentEntity, bool newEntity) { int linkedEntityId = _entityRepository.GetEntityIdByUniqueValue( mappedLink.LinkedUniqueFieldType, mappedLink.LinkedUniqueFieldValue); if (linkedEntityId == 0) { return; } LinkType linkType = _modelsRepository.GetLinkType(mappedLink.LinkType); if (linkType == null) { throw new InvalidOperationException("The link type '{mappedLink.LinkType}' does not exist in the model."); } Entity sourceEntity; Entity targetEntity; if (mappedLink.Direction == LinkDirection.ChildParent) { sourceEntity = new Entity { Id = linkedEntityId }; targetEntity = currentEntity; } else { sourceEntity = currentEntity; targetEntity = new Entity { Id = linkedEntityId }; } // If entity is new, there would be no existing links. // If it is not new, skip adding a link if it already exists. bool linkExists = !newEntity && _entityRepository.LinkAlreadyExists( sourceEntity.Id, targetEntity.Id, null, linkType.Id); if (linkExists) { return; } Link link = new Link { LinkType = linkType, Source = sourceEntity, Target = targetEntity, Index = mappedLink.SortIndex }; _entityRepository.AddLink(link); }
private MappedLink GetLink(XPathNavigator parentNode, LinkMapping linkMapping, bool isChildLink) { XPathExpression xPathExpression = _xPathCompiler.GetCachedExpression(linkMapping.SourcePath); string targetUniqueValue = parentNode .Evaluate(xPathExpression)? .ToString(); var mappedLink = new MappedLink(); mappedLink.LinkType = linkMapping.LinkType; mappedLink.LinkedUniqueFieldType = linkMapping.TargetUniqueFieldType; mappedLink.LinkedUniqueFieldValue = targetUniqueValue; mappedLink.Direction = isChildLink ? LinkDirection.ParentChild : LinkDirection.ChildParent; LinkEntityMapping linkEntityMapping = linkMapping.LinkEntity; if (linkEntityMapping != null) { mappedLink.LinkEntityType = linkEntityMapping.EntityType; mappedLink.Fields = GetFields(parentNode, linkEntityMapping.Fields); } return(mappedLink); }