From 636662cf1a92c4f081158805b5ac5e3aa5521792 Mon Sep 17 00:00:00 2001 From: Will Price Date: Fri, 29 May 2015 13:14:50 +0000 Subject: [PATCH] Updated DD4TModelBuilder to support arbitrary Multimedia links. This cleans up the code significantly as we now can use GetMultiComponentLinks for all multimedia links and no longer need: GetMultiMediaLinks, GetYoutubeVideos, GetImages, GetDownloads. Note that there is a new virtual protected method; ProcessType which contains logic to determine a more specific sub type if a model has a generic Media item property. This logic was previously handled in the GetMultiMediaLinks method and is needed for Teaser, and Article.ArticleBody, which have generic Media properties which can contain Image, YouTube or Download. This method can be overridden in your own ModelBuilder if you want to extend the logic. --- .../Sdl.Web.DD4T/Mapping/DD4TModelBuilder.cs | 108 +++++++----------- 1 file changed, 44 insertions(+), 64 deletions(-) diff --git a/web-application/Sdl.Web.DD4T/Mapping/DD4TModelBuilder.cs b/web-application/Sdl.Web.DD4T/Mapping/DD4TModelBuilder.cs index bde9706..f87e3b4 100644 --- a/web-application/Sdl.Web.DD4T/Mapping/DD4TModelBuilder.cs +++ b/web-application/Sdl.Web.DD4T/Mapping/DD4TModelBuilder.cs @@ -72,7 +72,7 @@ protected virtual object CreateEntity(object sourceEntity, Type type, List { mapData.SourceEntity }, propertyType, multival)); + pi.SetValue(model, GetMultiComponentLinks(new List { mapData.SourceEntity }, propertyType, multival)); processed = true; } else if (propertyType == typeof(Link) || propertyType == typeof(String)) @@ -263,7 +288,7 @@ private object GetFieldValues(IField field, Type propertyType, bool multival, Ma case (FieldType.Number): return GetNumbers(field, propertyType, multival); case (FieldType.MultiMediaLink): - return GetMultiMediaLinks(field, propertyType, multival); + return GetMultiComponentLinks(field, propertyType, multival); case (FieldType.ComponentLink): return GetMultiComponentLinks(field, propertyType, multival); case (FieldType.Embedded): @@ -354,50 +379,6 @@ private static object GetNumbers(IField field, Type modelType, bool multival) return null; } - private static object GetMultiMediaLinks(IField field, Type modelType, bool multival) - { - return GetMultiMediaLinks(field.LinkedComponentValues, modelType, multival); - } - - private static object GetMultiMediaLinks(IEnumerable items, Type modelType, bool multival) - { - var components = items as IList ?? items.ToList(); - if (components.Any()) - { - // TODO find better way to determine image or video - string schemaTitle = components.First().Schema.Title; - if (modelType.IsAssignableFrom(typeof(YouTubeVideo)) && schemaTitle.ToLower().Contains("youtube")) - { - if (multival) - { - return GetYouTubeVideos(components); - } - - return GetYouTubeVideos(components)[0]; - } - if (modelType.IsAssignableFrom(typeof(Download)) && schemaTitle.ToLower().Contains("download")) - { - if (multival) - { - return GetDownloads(components); - } - - return GetDownloads(components)[0]; - } - if (modelType.IsAssignableFrom(typeof(Image))) - { - if (multival) - { - return GetImages(components); - } - - return GetImages(components)[0]; - } - // TODO handle other types - } - return null; - } - private object GetMultiKeywords(IField field, Type linkedItemType, bool multival) { return GetMultiKeywords(field.Keywords, linkedItemType, multival); @@ -501,7 +482,22 @@ private object GetMultiComponentLinks(IEnumerable items, Type linked //TODO is reflection the only way to do this? MethodInfo method = GetType().GetMethod("GetCompLink" + (multival ? "s" : String.Empty), BindingFlags.NonPublic | BindingFlags.Instance); method = method.MakeGenericMethod(new[] { linkedItemType }); - return method.Invoke(this, new object[] { items, linkedItemType }); + return method.Invoke(this, new object[] { items }); + } + + private List GetCompLinks(IEnumerable components) + { + List list = new List(); + foreach (var comp in components) + { + list.Add((T)Create(comp, typeof(T))); + } + return list; + } + + private T GetCompLink(IEnumerable components) + { + return GetCompLinks(components)[0]; } private object GetMultiEmbedded(IField field, Type propertyType, bool multival, MappingData mapData) @@ -557,22 +553,6 @@ object GetStrings(IField field, Type modelType, bool multival) return null; } - private static List GetImages(IEnumerable components) - { - return components.Select(c => new Image { Url = c.Multimedia.Url, FileName = c.Multimedia.FileName, FileSize = c.Multimedia.Size, MimeType = c.Multimedia.MimeType}).ToList(); - } - - private static List GetYouTubeVideos(IEnumerable components) - { - return components.Select(c => new YouTubeVideo { Url = c.Multimedia.Url, FileSize = c.Multimedia.Size, MimeType = c.Multimedia.MimeType, YouTubeId = c.MetadataFields["youTubeId"].Value }).ToList(); - } - - private static List GetDownloads(IEnumerable components) - { - //todo this contains hardcoded metadata while we would expect this to semantiaclly ma - return components.Select(c => new Download { Url = c.Multimedia.Url, FileName = c.Multimedia.FileName, FileSize = c.Multimedia.Size, MimeType = c.Multimedia.MimeType, Description = (c.MetadataFields.ContainsKey("description") ? c.MetadataFields["description"].Value : null) }).ToList(); - } - protected Dictionary GetAllFieldsAsDictionary(IComponent component) { Dictionary values = new Dictionary();