using System; using System.Collections.Generic; using System.Linq; using Umbraco.Core.Models; using Umbraco.Core.Models.PublishedContent; namespace Umbraco.Web.Models { internal class DetachedContent { private readonly Dictionary _properties; /// /// Initialized a new instance of the class with properties. /// /// The properties /// Properties must be detached or nested properties ie their property type must be detached or nested. /// Such a detached content can be part of a published property value. public DetachedContent(IEnumerable properties) { _properties = properties.ToDictionary(x => x.PropertyTypeAlias, x => x, StringComparer.InvariantCultureIgnoreCase); } // don't uncomment until you know what you are doing /* public DetachedContent(IPublishedContent content) { _properties = content.Properties.ToDictionary(x => x.PropertyTypeAlias, x => x, StringComparer.InvariantCultureIgnoreCase); } */ // don't uncomment until you know what you are doing // at the moment, I don't fully /* public DetachedContent(IContent content, bool isPreviewing) { var publishedContentType = PublishedContentType.Get(PublishedItemType.Content, content.ContentType.Alias); _properties = PublishedProperty.MapProperties(publishedContentType.PropertyTypes, content.Properties, (t, v) => PublishedProperty.GetDetached(t.Detached(), v, isPreviewing)) .ToDictionary(x => x.PropertyTypeAlias, x => x, StringComparer.InvariantCultureIgnoreCase); } public DetachedContent(IMedia media, bool isPreviewing) { var publishedContentType = PublishedContentType.Get(PublishedItemType.Media, media.ContentType.Alias); _properties = PublishedProperty.MapProperties(publishedContentType.PropertyTypes, media.Properties, (t, v) => PublishedProperty.GetDetached(t.Detached(), v, isPreviewing)) .ToDictionary(x => x.PropertyTypeAlias, x => x, StringComparer.InvariantCultureIgnoreCase); } public DetachedContent(IMember member, bool isPreviewing) { var publishedContentType = PublishedContentType.Get(PublishedItemType.Member, member.ContentType.Alias); _properties = PublishedProperty.MapProperties(publishedContentType.PropertyTypes, member.Properties, (t, v) => PublishedProperty.GetDetached(t.Detached(), v, isPreviewing)) .ToDictionary(x => x.PropertyTypeAlias, x => x, StringComparer.InvariantCultureIgnoreCase); } */ public ICollection Properties { get { return _properties.Values; } } public IPublishedProperty GetProperty(string alias) { IPublishedProperty property; return _properties.TryGetValue(alias, out property) ? property : null; } public bool HasProperty(string alias) { var property = GetProperty(alias); return property != null; } public bool HasValue(string alias) { var property = GetProperty(alias); return property != null && property.HasValue; } public object GetPropertyValue(string alias) { var property = GetProperty(alias); return property == null ? null : property.Value; } public object GetPropertyValue(string alias, string defaultValue) { var property = GetProperty(alias); return property == null || property.HasValue == false ? defaultValue : property.Value; } public object GetPropertyValue(string alias, object defaultValue) { var property = GetProperty(alias); return property == null || property.HasValue == false ? defaultValue : property.Value; } public T GetPropertyValue(string alias) { var property = GetProperty(alias); if (property == null) return default(T); return property.GetValue(false, default(T)); } public T GetPropertyValue(string alias, T defaultValue) { var property = GetProperty(alias); if (property == null) return defaultValue; return property.GetValue(true, defaultValue); } } }