/// <summary> /// Converts a <see cref="T:Tridion.ContentDelivery.Taxonomies.Keyword" /> to a <see cref="T:TcmCDService.Contracts.Keyword" /> data contract. /// </summary> /// <param name="keyword"><see cref="T:Tridion.ContentDelivery.Taxonomies.Keyword" /></param> /// <param name="linkingKeyword"><see cref="T:TcmCDService.Contracts.Keyword" /> from where the conversion is called.</param> /// <returns> /// <see cref="T:TcmCDService.Contracts.Keyword" /> /// </returns> internal static Contracts.Keyword ToContract(this Keyword keyword, Contracts.Keyword linkingKeyword) { if (keyword != null) { Contracts.Keyword result = new Contracts.Keyword() { HasChildren = keyword.HasChildren, IsAbstract = keyword.IsAbstract, IsNavigable = keyword.IsNavigable, IsUsedForNavigation = keyword.IsUsedForIdentification, Depth = keyword.KeywordDepth, Description = keyword.KeywordDescription, Key = keyword.KeywordKey, Left = keyword.KeywordLeft, Meta = ToContract(keyword.KeywordMeta), Name = keyword.KeywordName, Right = keyword.KeywordRight, Uri = keyword.KeywordUri, ReferencedContentCount = keyword.ReferencedContentCount, TaxonomyUri = keyword.TaxonomyUri, RelatedKeywordUris = keyword.GetRelatedKeywordUris() }; // If the parent keyword is equal to the linkingKeyword, do not recursive, but assign it manually result.ParentKeyword = keyword .ParentKeyword .IsEqual(linkingKeyword) ? linkingKeyword : ToContract(keyword.ParentKeyword, result); // Retrieve all children which are not the same as the "linking keyword" IEnumerable <Keyword> children = keyword.KeywordChildren.OfType <Keyword>(); result.Children = children.Where(k => !k.IsEqual(linkingKeyword)).Select(k => ToContract(k, result)); // If the linking keyword is part of the children, add it manually to the children collection (To prevent recursing endlessly) if (children.Any(k => k.IsEqual(linkingKeyword))) { result.Children = result.Children.Concat(new Contracts.Keyword[] { linkingKeyword }); } IEnumerable <Keyword> parents = keyword.ParentKeywords.OfType <Keyword>(); result.ParentKeywords = parents.Where(k => !k.IsEqual(linkingKeyword)).Select(k => ToContract(k, linkingKeyword)); // If the linking keyword is part of the parents, add it manually to the parents collection (To prevent recursing endlessly) if (parents.Any(k => k.IsEqual(linkingKeyword))) { result.ParentKeywords = result.ParentKeywords.Concat(new Contracts.Keyword[] { linkingKeyword }); } return(result); } return(null); }