Files
Umbraco-CMS/src/Umbraco.PublishedCache.NuCache/ContentNode.cs

185 lines
7.5 KiB
C#
Raw Normal View History

2016-05-27 14:26:28 +02:00
using System;
using System.Diagnostics;
using Umbraco.Cms.Core.Models.PublishedContent;
using Umbraco.Cms.Core.PublishedCache;
using Umbraco.Cms.Infrastructure.PublishedCache.DataSource;
using Umbraco.Extensions;
2016-05-27 14:26:28 +02:00
namespace Umbraco.Cms.Infrastructure.PublishedCache
2016-05-27 14:26:28 +02:00
{
// represents a content "node" ie a pair of draft + published versions
// internal, never exposed, to be accessed from ContentStore (only!)
[DebuggerDisplay("Id: {Id}, Path: {Path}")]
public class ContentNode
2016-05-27 14:26:28 +02:00
{
2019-04-22 17:51:07 +02:00
// special ctor for root pseudo node
public ContentNode()
{
FirstChildContentId = -1;
LastChildContentId = -1;
NextSiblingContentId = -1;
PreviousSiblingContentId = -1;
}
2019-04-22 17:51:07 +02:00
2016-05-27 14:26:28 +02:00
// special ctor with no content data - for members
2019-04-15 13:04:14 +02:00
public ContentNode(int id, Guid uid, IPublishedContentType contentType,
2016-05-27 14:26:28 +02:00
int level, string path, int sortOrder,
int parentContentId,
DateTime createDate, int creatorId)
: this()
2016-05-27 14:26:28 +02:00
{
Id = id;
Uid = uid;
ContentType = contentType;
Level = level;
Path = path;
SortOrder = sortOrder;
ParentContentId = parentContentId;
CreateDate = createDate;
CreatorId = creatorId;
}
2019-04-15 13:04:14 +02:00
public ContentNode(int id, Guid uid, IPublishedContentType contentType,
2016-05-27 14:26:28 +02:00
int level, string path, int sortOrder,
int parentContentId,
DateTime createDate, int creatorId,
2016-05-30 19:54:36 +02:00
ContentData draftData, ContentData publishedData,
IPublishedSnapshotAccessor publishedSnapshotAccessor,
IVariationContextAccessor variationContextAccessor,
IPublishedModelFactory publishedModelFactory)
2016-05-27 14:26:28 +02:00
: this(id, uid, level, path, sortOrder, parentContentId, createDate, creatorId)
{
SetContentTypeAndData(contentType, draftData, publishedData, publishedSnapshotAccessor, variationContextAccessor, publishedModelFactory);
2016-05-27 14:26:28 +02:00
}
// 2-phases ctor, phase 1
public ContentNode(int id, Guid uid,
int level, string path, int sortOrder,
int parentContentId,
DateTime createDate, int creatorId)
{
Id = id;
Uid = uid;
Level = level;
Path = path;
SortOrder = sortOrder;
ParentContentId = parentContentId;
2019-04-22 17:51:07 +02:00
FirstChildContentId = -1;
LastChildContentId = -1;
2019-04-22 17:51:07 +02:00
NextSiblingContentId = -1;
PreviousSiblingContentId = -1;
2016-05-27 14:26:28 +02:00
CreateDate = createDate;
CreatorId = creatorId;
}
// two-phase ctor, phase 2
public void SetContentTypeAndData(IPublishedContentType contentType, ContentData draftData, ContentData publishedData, IPublishedSnapshotAccessor publishedSnapshotAccessor, IVariationContextAccessor variationContextAccessor, IPublishedModelFactory publishedModelFactory)
2016-05-27 14:26:28 +02:00
{
ContentType = contentType;
if (draftData == null && publishedData == null)
throw new ArgumentException("Both draftData and publishedData cannot be null at the same time.");
_publishedSnapshotAccessor = publishedSnapshotAccessor;
_variationContextAccessor = variationContextAccessor;
_publishedModelFactory = publishedModelFactory;
2019-01-28 20:07:22 +01:00
_draftData = draftData;
_publishedData = publishedData;
2016-05-27 14:26:28 +02:00
}
2019-04-22 17:51:07 +02:00
// clone
public ContentNode(ContentNode origin, IPublishedModelFactory publishedModelFactory, IPublishedContentType contentType = null)
2016-05-27 14:26:28 +02:00
{
_publishedModelFactory = publishedModelFactory;
2016-05-27 14:26:28 +02:00
Id = origin.Id;
Uid = origin.Uid;
2019-04-22 17:51:07 +02:00
ContentType = contentType ?? origin.ContentType;
2016-05-27 14:26:28 +02:00
Level = origin.Level;
Path = origin.Path;
SortOrder = origin.SortOrder;
ParentContentId = origin.ParentContentId;
2019-04-22 17:51:07 +02:00
FirstChildContentId = origin.FirstChildContentId;
LastChildContentId = origin.LastChildContentId;
2019-04-22 17:51:07 +02:00
NextSiblingContentId = origin.NextSiblingContentId;
PreviousSiblingContentId = origin.PreviousSiblingContentId;
2016-05-27 14:26:28 +02:00
CreateDate = origin.CreateDate;
CreatorId = origin.CreatorId;
_draftData = origin._draftData;
_publishedData = origin._publishedData;
_publishedSnapshotAccessor = origin._publishedSnapshotAccessor;
_variationContextAccessor = origin._variationContextAccessor;
2016-05-27 14:26:28 +02:00
}
// everything that is common to both draft and published versions
// keep this as small as possible
2016-05-27 14:26:28 +02:00
public readonly int Id;
public readonly Guid Uid;
2019-04-15 13:04:14 +02:00
public IPublishedContentType ContentType;
2016-05-27 14:26:28 +02:00
public readonly int Level;
public readonly string Path;
public readonly int SortOrder;
public readonly int ParentContentId;
// TODO: Can we make everything readonly?? This would make it easier to debug and be less error prone especially for new developers.
// Once a Node is created and exists in the cache it is readonly so we should be able to make that happen at the API level too.
2019-04-22 17:51:07 +02:00
public int FirstChildContentId;
public int LastChildContentId;
2019-04-22 17:51:07 +02:00
public int NextSiblingContentId;
public int PreviousSiblingContentId;
2016-05-27 14:26:28 +02:00
public readonly DateTime CreateDate;
public readonly int CreatorId;
private ContentData _draftData;
private ContentData _publishedData;
private IVariationContextAccessor _variationContextAccessor;
private IPublishedSnapshotAccessor _publishedSnapshotAccessor;
public bool HasPublished => _publishedData != null;
public bool HasPublishedCulture(string culture) => _publishedData != null && _publishedData.CultureInfos.ContainsKey(culture);
2019-01-28 14:15:47 +01:00
2016-05-27 14:26:28 +02:00
// draft and published version (either can be null, but not both)
// are models not direct PublishedContent instances
private IPublishedContent _draftModel;
private IPublishedContent _publishedModel;
private IPublishedModelFactory _publishedModelFactory;
private IPublishedContent GetModel(ref IPublishedContent model, ContentData contentData)
{
if (model != null) return model;
if (contentData == null) return null;
// create the model - we want to be fast, so no lock here: we may create
// more than 1 instance, but the lock below ensures we only ever return
// 1 unique instance - and locking is a nice explicit way to ensure this
var m = new PublishedContent(this, contentData, _publishedSnapshotAccessor, _variationContextAccessor, _publishedModelFactory).CreateModel(_publishedModelFactory);
// locking 'this' is not a best-practice but ContentNode is internal and
// we know what we do, so it is fine here and avoids allocating an object
lock (this)
{
return model = model ?? m;
}
}
public IPublishedContent DraftModel => GetModel(ref _draftModel, _draftData);
public IPublishedContent PublishedModel => GetModel(ref _publishedModel, _publishedData);
2016-05-27 14:26:28 +02:00
public ContentNodeKit ToKit()
2019-04-22 17:51:07 +02:00
=> new ContentNodeKit
{
Node = this,
ContentTypeId = ContentType.Id,
DraftData = _draftData,
PublishedData = _publishedData
2019-04-22 17:51:07 +02:00
};
2016-05-27 14:26:28 +02:00
}
}