In a previous post, I presented the Taxonomy Factory and a way of retrieving taxonomies and keywords from Tridion. One of the promises I made in there was to explain the conversion process from a Tridion Keyword to a specialized class MyKeyword, a subclass of DD4T.ContentModel.IKeyword.
The DD4T IKeyword class does not provide a way to navigate the taxonomy downwards -- it only provides a ParentKeywords property. Therefore, I created IMyKeyword:
Its accompanying implementation class is quite trivial:
In order to convert a Tridion.ContentDelivery.Taxonomies.Keyword to MyKeyword, I'm using the following TaxonomyConverter class, which performs a recursive deep-copy of the given Tridion Keyword to a MyKeyword instance, by populating all properties and building the Parent/Child keyword sets.
The DD4T IKeyword class does not provide a way to navigate the taxonomy downwards -- it only provides a ParentKeywords property. Therefore, I created IMyKeyword:
public interface IMyKeyword : IKeyword { IList<IMyKeyword> ChildKeywords { get; } IMyKeyword ParentKeyword { get; } new IList<IMyKeyword> ParentKeywords { get; } }
Its accompanying implementation class is quite trivial:
using dd4t = DD4T.ContentModel; public class MyKeyword : dd4t.Keyword, IMyKeyword { private IList<IMyKeyword> _childKeywords = new List<IMyKeyword>(); public IList<IMyKeyword> ChildKeywords { get { return _childKeywords; } } private IList<IMyKeyword> _parentKeywords = new List<IMyKeyword>(); public IMyKeyword ParentKeyword { get { return _parentKeywords.FirstOrDefault(); } } public new IList<IMyKeyword> ParentKeywords { get { return _parentKeywords; } } }
In order to convert a Tridion.ContentDelivery.Taxonomies.Keyword to MyKeyword, I'm using the following TaxonomyConverter class, which performs a recursive deep-copy of the given Tridion Keyword to a MyKeyword instance, by populating all properties and building the Parent/Child keyword sets.
using dd4t = DD4T.ContentModel; using tridion = Tridion.ContentDelivery.Taxonomies; public class TaxonomyConverter { public IMyKeyword ConvertToDD4T(tridion.Keyword keyword) { string publicationUri = UriHelper.GetPublicationUri(keyword.KeywordUri); dd4t.Publication publication = BuildPublication(keyword, publicationUri); IMyKeyword result = new MyKeyword() { Description = keyword.KeywordDescription, Id = keyword.KeywordUri, Key = keyword.KeywordKey, MetadataFields = BuildMetadata(keyword.KeywordMeta), OwningPublication = publication, Path = BuildPath(keyword), Publication = publication, PublicationId = publicationUri, TaxonomyId = keyword.TaxonomyUri, Title = keyword.KeywordName }; ConvertToDD4TList(result.ParentKeywords, keyword.ParentKeywords); ConvertToDD4TList(result.ChildKeywords, keyword.KeywordChildren); return result; } public void ConvertToDD4TList(IList<IMylKeyword> myKeywords, IList tridionKeywords) { if (tridionKeywords == null) { return; } foreach (tridion.Keyword tridionKeyword in tridionKeywords) { myKeywords.Add(ConvertToDD4T(tridionKeyword)); } } private dd4t.FieldSet BuildMetadata(CustomMeta customMeta) { IDictionary nameValues = customMeta.NameValues; if (customMeta == null || nameValues.IsNullOrEmpty()) { return null; } dd4t.FieldSet fieldSet = new dd4t.FieldSet(); foreach (NameValuePair pair in nameValues.Values) { string name = pair.Name; switch (pair.ValueType) { case 2: // Date fieldSet.Add(name, new dd4t.Field() { Name = name, DateTimeValues = pair.MultipleValues.Cast<DateTime>().ToList() }); break; case 3: // Numeric fieldSet.Add(name, new dd4t.Field() { Name = name, NumericValues = pair.MultipleValues.Cast<float>().Select(x => Convert.ToDouble(x.ToString())).ToList() }); break; default: // String fieldSet.Add(name, new dd4t.Field() { Name = name, Values = pair.MultipleValues.Cast<string>().ToList() }); break; } } return fieldSet; } }
Comments