using System; using Umbraco.Core.PropertyEditors; namespace Umbraco.Core.Models.PublishedContent { /// /// /// Represents a published property that has a unique invariant-neutral value /// and caches conversion results locally. /// /// /// Conversions results are stored within the property and will not /// be refreshed, so this class is not suitable for cached properties. /// Does not support variations: the ctor throws if the property type /// supports variations. /// internal class RawValueProperty : PublishedPropertyBase { private readonly object _sourceValue; //the value in the db private readonly Lazy _objectValue; private readonly Lazy _xpathValue; // RawValueProperty does not (yet?) support variants, // only manages the current "default" value public override object GetSourceValue(string culture = null, string segment = null) => string.IsNullOrEmpty(culture) & string.IsNullOrEmpty(segment) ? _sourceValue : null; public override bool HasValue(string culture = null, string segment = null) { var sourceValue = GetSourceValue(culture, segment); return sourceValue is string s ? !string.IsNullOrWhiteSpace(s) : sourceValue != null; } public override object GetValue(string culture = null, string segment = null) => string.IsNullOrEmpty(culture) & string.IsNullOrEmpty(segment) ? _objectValue.Value : null; public override object GetXPathValue(string culture = null, string segment = null) => string.IsNullOrEmpty(culture) & string.IsNullOrEmpty(segment) ? _xpathValue.Value : null; public RawValueProperty(PublishedPropertyType propertyType, IPublishedElement content, object sourceValue, bool isPreviewing = false) : base(propertyType, PropertyCacheLevel.Unknown) // cache level is ignored { if (propertyType.Variations != ContentVariation.InvariantNeutral) throw new ArgumentException("Property types with variations are not supported here.", nameof(propertyType)); _sourceValue = sourceValue; var interValue = new Lazy(() => PropertyType.ConvertSourceToInter(content, _sourceValue, isPreviewing)); _objectValue = new Lazy(() => PropertyType.ConvertInterToObject(content, PropertyCacheLevel.Unknown, interValue.Value, isPreviewing)); _xpathValue = new Lazy(() => PropertyType.ConvertInterToXPath(content, PropertyCacheLevel.Unknown, interValue.Value, isPreviewing)); } } }