2022-04-29 08:19:34 +02:00
|
|
|
using System;
|
2016-06-10 16:37:28 +02:00
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
2021-02-18 11:06:02 +01:00
|
|
|
using Umbraco.Cms.Core.Models.PublishedContent;
|
|
|
|
|
using Umbraco.Cms.Core.PropertyEditors;
|
2016-06-10 16:37:28 +02:00
|
|
|
|
2021-02-18 11:06:02 +01:00
|
|
|
namespace Umbraco.Cms.Core.PublishedCache
|
2016-06-10 16:37:28 +02:00
|
|
|
{
|
2016-06-29 15:28:40 +02:00
|
|
|
// notes:
|
2017-09-26 14:57:50 +02:00
|
|
|
// a published element does NOT manage any tree-like elements, neither the
|
2016-06-29 15:28:40 +02:00
|
|
|
// original NestedContent (from Lee) nor the DetachedPublishedContent POC did.
|
2016-06-10 16:37:28 +02:00
|
|
|
//
|
2017-07-21 17:04:14 +02:00
|
|
|
// at the moment we do NOT support models for sets - that would require
|
2016-06-29 15:28:40 +02:00
|
|
|
// an entirely new models factory + not even sure it makes sense at all since
|
2019-01-22 09:49:35 +01:00
|
|
|
// sets are created manually todo yes it does! - what does this all mean?
|
2016-06-10 16:37:28 +02:00
|
|
|
//
|
2020-01-30 20:05:16 +01:00
|
|
|
public class PublishedElement : IPublishedElement
|
2016-06-10 16:37:28 +02:00
|
|
|
{
|
2017-09-25 08:59:32 +02:00
|
|
|
// initializes a new instance of the PublishedElement class
|
2017-10-31 12:48:24 +01:00
|
|
|
// within the context of a published snapshot service (eg a published content property value)
|
2022-02-22 13:35:32 +01:00
|
|
|
public PublishedElement(IPublishedContentType contentType, Guid key, Dictionary<string, object?>? values, bool previewing,
|
2022-02-09 13:24:35 +01:00
|
|
|
PropertyCacheLevel referenceCacheLevel, IPublishedSnapshotAccessor? publishedSnapshotAccessor)
|
2016-06-10 16:37:28 +02:00
|
|
|
{
|
2017-09-29 15:51:33 +02:00
|
|
|
if (key == Guid.Empty) throw new ArgumentException("Empty guid.");
|
|
|
|
|
if (values == null) throw new ArgumentNullException(nameof(values));
|
2017-10-31 12:48:24 +01:00
|
|
|
if (referenceCacheLevel != PropertyCacheLevel.None && publishedSnapshotAccessor == null)
|
|
|
|
|
throw new ArgumentNullException("A published snapshot accessor is required when referenceCacheLevel != None.", nameof(publishedSnapshotAccessor));
|
2017-09-29 15:51:33 +02:00
|
|
|
|
|
|
|
|
ContentType = contentType ?? throw new ArgumentNullException(nameof(contentType));
|
2016-06-10 16:37:28 +02:00
|
|
|
Key = key;
|
|
|
|
|
|
2016-06-29 15:28:40 +02:00
|
|
|
values = GetCaseInsensitiveValueDictionary(values);
|
2016-06-10 16:37:28 +02:00
|
|
|
|
|
|
|
|
_propertiesArray = contentType
|
2022-02-09 13:24:35 +01:00
|
|
|
.PropertyTypes?
|
2016-06-10 16:37:28 +02:00
|
|
|
.Select(propertyType =>
|
|
|
|
|
{
|
2018-01-10 12:48:51 +01:00
|
|
|
values.TryGetValue(propertyType.Alias, out var value);
|
2022-04-29 08:19:34 +02:00
|
|
|
return (IPublishedProperty)new PublishedElementPropertyBase(propertyType, this, previewing, referenceCacheLevel, value, publishedSnapshotAccessor);
|
2016-06-10 16:37:28 +02:00
|
|
|
})
|
2022-04-29 08:19:34 +02:00
|
|
|
.ToArray()
|
|
|
|
|
?? new IPublishedProperty[0];
|
2016-06-29 15:28:40 +02:00
|
|
|
}
|
2016-06-10 16:37:28 +02:00
|
|
|
|
2017-09-25 08:59:32 +02:00
|
|
|
// initializes a new instance of the PublishedElement class
|
2017-10-31 12:48:24 +01:00
|
|
|
// without any context, so it's purely 'standalone' and should NOT interfere with the published snapshot service
|
2017-09-29 15:51:33 +02:00
|
|
|
// + using an initial reference cache level of .None ensures that everything will be
|
|
|
|
|
// cached at .Content level - and that reference cache level will propagate to all
|
|
|
|
|
// properties
|
2022-02-27 21:20:50 +01:00
|
|
|
public PublishedElement(IPublishedContentType contentType, Guid key, Dictionary<string, object?> values, bool previewing)
|
2017-09-29 15:51:33 +02:00
|
|
|
: this(contentType, key, values, previewing, PropertyCacheLevel.None, null)
|
|
|
|
|
{ }
|
2016-06-29 15:28:40 +02:00
|
|
|
|
2022-02-22 13:35:32 +01:00
|
|
|
private static Dictionary<string, object?> GetCaseInsensitiveValueDictionary(Dictionary<string, object?> values)
|
2016-06-29 15:28:40 +02:00
|
|
|
{
|
|
|
|
|
// ensure we ignore case for property aliases
|
|
|
|
|
var comparer = values.Comparer;
|
2017-07-21 17:04:14 +02:00
|
|
|
var ignoreCase = Equals(comparer, StringComparer.OrdinalIgnoreCase) || Equals(comparer, StringComparer.InvariantCultureIgnoreCase) || Equals(comparer, StringComparer.CurrentCultureIgnoreCase);
|
2022-02-22 13:35:32 +01:00
|
|
|
return ignoreCase ? values : new Dictionary<string, object?>(values, StringComparer.OrdinalIgnoreCase);
|
2016-06-10 16:37:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#region ContentType
|
|
|
|
|
|
2019-04-15 13:04:14 +02:00
|
|
|
public IPublishedContentType ContentType { get; }
|
2016-06-10 16:37:28 +02:00
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
2017-09-25 08:59:32 +02:00
|
|
|
#region PublishedElement
|
2016-06-10 16:37:28 +02:00
|
|
|
|
|
|
|
|
public Guid Key { get; }
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
#region Properties
|
|
|
|
|
|
2022-04-29 08:19:34 +02:00
|
|
|
private readonly IPublishedProperty[] _propertiesArray;
|
2016-06-10 16:37:28 +02:00
|
|
|
|
2022-04-29 08:19:34 +02:00
|
|
|
public IEnumerable<IPublishedProperty> Properties => _propertiesArray;
|
2016-06-10 16:37:28 +02:00
|
|
|
|
2022-02-09 13:24:35 +01:00
|
|
|
public IPublishedProperty? GetProperty(string alias)
|
2016-06-10 16:37:28 +02:00
|
|
|
{
|
|
|
|
|
var index = ContentType.GetPropertyIndex(alias);
|
2022-02-09 13:24:35 +01:00
|
|
|
var property = index < 0 ? null : _propertiesArray?[index];
|
2016-06-10 16:37:28 +02:00
|
|
|
return property;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
}
|
|
|
|
|
}
|