349 lines
14 KiB
C#
349 lines
14 KiB
C#
using System;
|
|
using System.ComponentModel;
|
|
using System.Reflection;
|
|
using System.Runtime.Serialization;
|
|
|
|
namespace Umbraco.Core.Models
|
|
{
|
|
/// <summary>
|
|
/// Represents a Content object
|
|
/// </summary>
|
|
[Serializable]
|
|
[DataContract(IsReference = true)]
|
|
public class Content : ContentBase, IContent
|
|
{
|
|
private IContentType _contentType;
|
|
private ITemplate _template;
|
|
private bool _published;
|
|
private bool? _publishedOriginal;
|
|
private string _language;
|
|
private DateTime? _releaseDate;
|
|
private DateTime? _expireDate;
|
|
private int _writer;
|
|
private string _nodeName;//NOTE Once localization is introduced this will be the non-localized Node Name.
|
|
|
|
private static readonly Lazy<PropertySelectors> Ps = new Lazy<PropertySelectors>();
|
|
|
|
/// <summary>
|
|
/// Constructor for creating a Content object
|
|
/// </summary>
|
|
/// <param name="name">Name of the content</param>
|
|
/// <param name="parent">Parent <see cref="IContent"/> object</param>
|
|
/// <param name="contentType">ContentType for the current Content object</param>
|
|
public Content(string name, IContent parent, IContentType contentType)
|
|
: this(name, parent, contentType, new PropertyCollection())
|
|
{ }
|
|
|
|
/// <summary>
|
|
/// Constructor for creating a Content object
|
|
/// </summary>
|
|
/// <param name="name">Name of the content</param>
|
|
/// <param name="parent">Parent <see cref="IContent"/> object</param>
|
|
/// <param name="contentType">ContentType for the current Content object</param>
|
|
/// <param name="properties">Collection of properties</param>
|
|
public Content(string name, IContent parent, IContentType contentType, PropertyCollection properties)
|
|
: base(name, parent, contentType, properties)
|
|
{
|
|
_contentType = contentType ?? throw new ArgumentNullException(nameof(contentType));
|
|
PublishedState = PublishedState.Unpublished;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Constructor for creating a Content object
|
|
/// </summary>
|
|
/// <param name="name">Name of the content</param>
|
|
/// <param name="parentId">Id of the Parent content</param>
|
|
/// <param name="contentType">ContentType for the current Content object</param>
|
|
public Content(string name, int parentId, IContentType contentType)
|
|
: this(name, parentId, contentType, new PropertyCollection())
|
|
{ }
|
|
|
|
/// <summary>
|
|
/// Constructor for creating a Content object
|
|
/// </summary>
|
|
/// <param name="name">Name of the content</param>
|
|
/// <param name="parentId">Id of the Parent content</param>
|
|
/// <param name="contentType">ContentType for the current Content object</param>
|
|
/// <param name="properties">Collection of properties</param>
|
|
public Content(string name, int parentId, IContentType contentType, PropertyCollection properties)
|
|
: base(name, parentId, contentType, properties)
|
|
{
|
|
_contentType = contentType ?? throw new ArgumentNullException(nameof(contentType));
|
|
PublishedState = PublishedState.Unpublished;
|
|
}
|
|
|
|
// ReSharper disable once ClassNeverInstantiated.Local
|
|
private class PropertySelectors
|
|
{
|
|
public readonly PropertyInfo TemplateSelector = ExpressionHelper.GetPropertyInfo<Content, ITemplate>(x => x.Template);
|
|
public readonly PropertyInfo PublishedSelector = ExpressionHelper.GetPropertyInfo<Content, bool>(x => x.Published);
|
|
public readonly PropertyInfo LanguageSelector = ExpressionHelper.GetPropertyInfo<Content, string>(x => x.Language);
|
|
public readonly PropertyInfo ReleaseDateSelector = ExpressionHelper.GetPropertyInfo<Content, DateTime?>(x => x.ReleaseDate);
|
|
public readonly PropertyInfo ExpireDateSelector = ExpressionHelper.GetPropertyInfo<Content, DateTime?>(x => x.ExpireDate);
|
|
public readonly PropertyInfo WriterSelector = ExpressionHelper.GetPropertyInfo<Content, int>(x => x.WriterId);
|
|
public readonly PropertyInfo NodeNameSelector = ExpressionHelper.GetPropertyInfo<Content, string>(x => x.NodeName);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets or sets the template used by the Content.
|
|
/// This is used to override the default one from the ContentType.
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// If no template is explicitly set on the Content object,
|
|
/// the Default template from the ContentType will be returned.
|
|
/// </remarks>
|
|
[DataMember]
|
|
public virtual ITemplate Template
|
|
{
|
|
get => _template ?? _contentType.DefaultTemplate;
|
|
set => SetPropertyValueAndDetectChanges(value, ref _template, Ps.Value.TemplateSelector);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the current status of the Content
|
|
/// </summary>
|
|
[IgnoreDataMember]
|
|
public ContentStatus Status
|
|
{
|
|
get
|
|
{
|
|
if(Trashed)
|
|
return ContentStatus.Trashed;
|
|
|
|
if(ExpireDate.HasValue && ExpireDate.Value > DateTime.MinValue && DateTime.Now > ExpireDate.Value)
|
|
return ContentStatus.Expired;
|
|
|
|
if(ReleaseDate.HasValue && ReleaseDate.Value > DateTime.MinValue && ReleaseDate.Value > DateTime.Now)
|
|
return ContentStatus.AwaitingRelease;
|
|
|
|
if(Published)
|
|
return ContentStatus.Published;
|
|
|
|
return ContentStatus.Unpublished;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Boolean indicating whether this Content is Published or not
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// Setting Published to true/false should be private or internal and should ONLY be used for wiring up the value
|
|
/// from the db or modifying it based on changing the published state.
|
|
/// </remarks>
|
|
[DataMember]
|
|
public bool Published
|
|
{
|
|
get => _published;
|
|
internal set
|
|
{
|
|
SetPropertyValueAndDetectChanges(value, ref _published, Ps.Value.PublishedSelector);
|
|
_publishedOriginal = _publishedOriginal ?? _published;
|
|
PublishedState = _published ? PublishedState.Published : PublishedState.Unpublished;
|
|
}
|
|
}
|
|
|
|
[IgnoreDataMember]
|
|
public bool PublishedOriginal => _publishedOriginal ?? false;
|
|
|
|
/// <summary>
|
|
/// Language of the data contained within this Content object.
|
|
/// </summary>
|
|
[Obsolete("This is not used and will be removed from the codebase in future versions")]
|
|
[EditorBrowsable(EditorBrowsableState.Never)]
|
|
public string Language
|
|
{
|
|
get => _language;
|
|
set => SetPropertyValueAndDetectChanges(value, ref _language, Ps.Value.LanguageSelector);
|
|
}
|
|
|
|
/// <summary>
|
|
/// The date this Content should be released and thus be published
|
|
/// </summary>
|
|
[DataMember]
|
|
public DateTime? ReleaseDate
|
|
{
|
|
get => _releaseDate;
|
|
set => SetPropertyValueAndDetectChanges(value, ref _releaseDate, Ps.Value.ReleaseDateSelector);
|
|
}
|
|
|
|
/// <summary>
|
|
/// The date this Content should expire and thus be unpublished
|
|
/// </summary>
|
|
[DataMember]
|
|
public DateTime? ExpireDate
|
|
{
|
|
get => _expireDate;
|
|
set => SetPropertyValueAndDetectChanges(value, ref _expireDate, Ps.Value.ExpireDateSelector);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Id of the user who wrote/updated this Content
|
|
/// </summary>
|
|
[DataMember]
|
|
public virtual int WriterId
|
|
{
|
|
get => _writer;
|
|
set => SetPropertyValueAndDetectChanges(value, ref _writer, Ps.Value.WriterSelector);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Name of the Node (non-localized).
|
|
/// </summary>
|
|
/// <remarks>
|
|
/// This Property is kept internal until localization is introduced.
|
|
/// </remarks>
|
|
[DataMember]
|
|
internal string NodeName
|
|
{
|
|
get => _nodeName;
|
|
set => SetPropertyValueAndDetectChanges(value, ref _nodeName, Ps.Value.NodeNameSelector);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the ContentType used by this content object
|
|
/// </summary>
|
|
[IgnoreDataMember]
|
|
public IContentType ContentType => _contentType;
|
|
|
|
/// <summary>
|
|
/// Changes the <see cref="ContentType"/> for the current content object
|
|
/// </summary>
|
|
/// <param name="contentType">New ContentType for this content</param>
|
|
/// <remarks>Leaves PropertyTypes intact after change</remarks>
|
|
public void ChangeContentType(IContentType contentType)
|
|
{
|
|
ContentTypeId = contentType.Id;
|
|
_contentType = contentType;
|
|
ContentTypeBase = contentType;
|
|
Properties.EnsurePropertyTypes(PropertyTypes);
|
|
Properties.CollectionChanged += PropertiesChanged;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Changes the <see cref="ContentType"/> for the current content object and removes PropertyTypes,
|
|
/// which are not part of the new ContentType.
|
|
/// </summary>
|
|
/// <param name="contentType">New ContentType for this content</param>
|
|
/// <param name="clearProperties">Boolean indicating whether to clear PropertyTypes upon change</param>
|
|
public void ChangeContentType(IContentType contentType, bool clearProperties)
|
|
{
|
|
if(clearProperties)
|
|
{
|
|
ContentTypeId = contentType.Id;
|
|
_contentType = contentType;
|
|
ContentTypeBase = contentType;
|
|
Properties.EnsureCleanPropertyTypes(PropertyTypes);
|
|
Properties.CollectionChanged += PropertiesChanged;
|
|
return;
|
|
}
|
|
|
|
ChangeContentType(contentType);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Changes the Published state of the content object
|
|
/// </summary>
|
|
public void ChangePublishedState(PublishedState state)
|
|
{
|
|
if (state == PublishedState.Published || state == PublishedState.Unpublished)
|
|
throw new ArgumentException("Invalid state.");
|
|
Published = state == PublishedState.Publishing;
|
|
PublishedState = state;
|
|
}
|
|
|
|
[DataMember]
|
|
internal PublishedState PublishedState { get; private set; }
|
|
|
|
/// <summary>
|
|
/// Gets or sets the unique identifier of the published version, if any.
|
|
/// </summary>
|
|
[IgnoreDataMember]
|
|
public Guid PublishedVersionGuid { get; internal set; }
|
|
|
|
/// <summary>
|
|
/// Gets a value indicating whether the content has a published version.
|
|
/// </summary>
|
|
public bool HasPublishedVersion => PublishedVersionGuid != default(Guid);
|
|
|
|
[IgnoreDataMember]
|
|
internal DateTime PublishedDate { get; set; }
|
|
|
|
[DataMember]
|
|
public bool IsBlueprint { get; internal set; }
|
|
|
|
public override void ResetDirtyProperties(bool rememberDirty)
|
|
{
|
|
base.ResetDirtyProperties(rememberDirty);
|
|
|
|
// take care of the published state
|
|
switch (PublishedState)
|
|
{
|
|
case PublishedState.Saving:
|
|
case PublishedState.Unpublishing:
|
|
PublishedState = PublishedState.Unpublished;
|
|
break;
|
|
case PublishedState.Publishing:
|
|
PublishedState = PublishedState.Published;
|
|
break;
|
|
}
|
|
|
|
_publishedOriginal = _published;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Method to call when Entity is being updated
|
|
/// </summary>
|
|
/// <remarks>Modified Date is set and a new Version guid is set</remarks>
|
|
internal override void UpdatingEntity()
|
|
{
|
|
base.UpdatingEntity();
|
|
Version = Guid.NewGuid();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a deep clone of the current entity with its identity and it's property identities reset
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[Obsolete("Use DeepCloneWithResetIdentities instead")]
|
|
public IContent Clone()
|
|
{
|
|
return DeepCloneWithResetIdentities();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a deep clone of the current entity with its identity and it's property identities reset
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
public IContent DeepCloneWithResetIdentities()
|
|
{
|
|
var clone = (Content)DeepClone();
|
|
clone.Key = Guid.Empty;
|
|
clone.Version = Guid.NewGuid();
|
|
clone.ResetIdentity();
|
|
|
|
foreach (var property in clone.Properties)
|
|
property.ResetIdentity();
|
|
|
|
clone.PublishedVersionGuid = Guid.Empty;
|
|
|
|
return clone;
|
|
}
|
|
|
|
public override object DeepClone()
|
|
{
|
|
var clone = (Content) base.DeepClone();
|
|
//turn off change tracking
|
|
clone.DisableChangeTracking();
|
|
//need to manually clone this since it's not settable
|
|
clone._contentType = (IContentType)ContentType.DeepClone();
|
|
//this shouldn't really be needed since we're not tracking
|
|
clone.ResetDirtyProperties(false);
|
|
//re-enable tracking
|
|
clone.EnableChangeTracking();
|
|
|
|
return clone;
|
|
|
|
}
|
|
}
|
|
}
|