2012-10-03 10:26:50 -02:00
|
|
|
|
using System;
|
2012-11-02 09:11:23 -01:00
|
|
|
|
using System.Linq;
|
2012-10-03 10:26:50 -02:00
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
using System.Runtime.Serialization;
|
2012-11-05 14:42:21 -01:00
|
|
|
|
using Umbraco.Core.Models.Membership;
|
2012-10-03 10:26:50 -02:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Core.Models
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2012-10-03 11:59:49 -02:00
|
|
|
|
/// Represents a Content object
|
2012-10-03 10:26:50 -02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
[Serializable]
|
|
|
|
|
|
[DataContract(IsReference = true)]
|
2012-10-10 08:42:54 -02:00
|
|
|
|
public class Content : ContentBase, IContent
|
2012-10-03 10:26:50 -02:00
|
|
|
|
{
|
|
|
|
|
|
private IContentType _contentType;
|
|
|
|
|
|
private string _template;
|
|
|
|
|
|
private bool _published;
|
|
|
|
|
|
private string _language;
|
|
|
|
|
|
private DateTime? _releaseDate;
|
|
|
|
|
|
private DateTime? _expireDate;
|
2012-11-05 14:42:21 -01:00
|
|
|
|
private IProfile _writer;
|
2012-10-03 10:26:50 -02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Constructor for creating a Content object
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="parentId">Id of the Parent content</param>
|
|
|
|
|
|
/// <param name="contentType">ContentType for the current Content object</param>
|
|
|
|
|
|
public Content(int parentId, IContentType contentType) : this(parentId, contentType, new PropertyCollection())
|
|
|
|
|
|
{
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Constructor for creating a Content object
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <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>
|
2012-10-10 08:42:54 -02:00
|
|
|
|
public Content(int parentId, IContentType contentType, PropertyCollection properties) : base(parentId, contentType, properties)
|
2012-10-03 10:26:50 -02:00
|
|
|
|
{
|
2012-11-02 11:54:42 -01:00
|
|
|
|
Mandate.ParameterNotNull(contentType, "contentType");
|
|
|
|
|
|
|
2012-10-03 10:26:50 -02:00
|
|
|
|
_contentType = contentType;
|
|
|
|
|
|
}
|
2012-10-10 08:42:54 -02:00
|
|
|
|
|
2012-10-03 10:26:50 -02:00
|
|
|
|
private static readonly PropertyInfo TemplateSelector = ExpressionHelper.GetPropertyInfo<Content, string>(x => x.Template);
|
|
|
|
|
|
private static readonly PropertyInfo PublishedSelector = ExpressionHelper.GetPropertyInfo<Content, bool>(x => x.Published);
|
|
|
|
|
|
private static readonly PropertyInfo LanguageSelector = ExpressionHelper.GetPropertyInfo<Content, string>(x => x.Language);
|
|
|
|
|
|
private static readonly PropertyInfo ReleaseDateSelector = ExpressionHelper.GetPropertyInfo<Content, DateTime?>(x => x.ReleaseDate);
|
|
|
|
|
|
private static readonly PropertyInfo ExpireDateSelector = ExpressionHelper.GetPropertyInfo<Content, DateTime?>(x => x.ExpireDate);
|
2012-11-05 14:42:21 -01:00
|
|
|
|
private static readonly PropertyInfo WriterSelector = ExpressionHelper.GetPropertyInfo<Content, IProfile>(x => x.Writer);
|
2012-10-03 10:26:50 -02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Path to the template used by this 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 string Template
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
if (string.IsNullOrEmpty(_template) || _template == null)
|
|
|
|
|
|
return _contentType.DefaultTemplate;
|
|
|
|
|
|
|
|
|
|
|
|
return _template;
|
|
|
|
|
|
}
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
_template = value;
|
|
|
|
|
|
OnPropertyChanged(TemplateSelector);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the current status of the Content
|
|
|
|
|
|
/// </summary>
|
2012-11-01 13:48:03 -01:00
|
|
|
|
[IgnoreDataMember]
|
2012-10-03 10:26:50 -02:00
|
|
|
|
public ContentStatus Status
|
|
|
|
|
|
{
|
|
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
if(Trashed)
|
|
|
|
|
|
return ContentStatus.Trashed;
|
|
|
|
|
|
|
|
|
|
|
|
if(ExpireDate.HasValue && DateTime.UtcNow > ExpireDate.Value)
|
|
|
|
|
|
return ContentStatus.Expired;
|
|
|
|
|
|
|
|
|
|
|
|
if(ReleaseDate.HasValue && ReleaseDate.Value > DateTime.UtcNow)
|
|
|
|
|
|
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</remarks>
|
|
|
|
|
|
[DataMember]
|
|
|
|
|
|
public bool Published
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return _published; }
|
|
|
|
|
|
internal set
|
|
|
|
|
|
{
|
|
|
|
|
|
_published = value;
|
|
|
|
|
|
OnPropertyChanged(PublishedSelector);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Language of the data contained within this Content object
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[DataMember]
|
|
|
|
|
|
internal string Language
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return _language; }
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
_language = value;
|
|
|
|
|
|
OnPropertyChanged(LanguageSelector);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// The date this Content should be released and thus be published
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[DataMember]
|
|
|
|
|
|
public DateTime? ReleaseDate
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return _releaseDate; }
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
if(value.HasValue && value.Value > DateTime.UtcNow && Published)
|
2012-11-05 14:42:21 -01:00
|
|
|
|
_published = false;
|
2012-10-03 10:26:50 -02:00
|
|
|
|
|
|
|
|
|
|
if (value.HasValue && value.Value < DateTime.UtcNow && !Published)
|
2012-11-05 14:42:21 -01:00
|
|
|
|
_published = true;
|
2012-10-03 10:26:50 -02:00
|
|
|
|
|
|
|
|
|
|
_releaseDate = value;
|
|
|
|
|
|
OnPropertyChanged(ReleaseDateSelector);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// The date this Content should expire and thus be unpublished
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[DataMember]
|
|
|
|
|
|
public DateTime? ExpireDate
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return _expireDate; }
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
if(value.HasValue && DateTime.UtcNow > value.Value && Published)
|
2012-11-05 14:42:21 -01:00
|
|
|
|
_published = false;
|
2012-10-03 10:26:50 -02:00
|
|
|
|
|
|
|
|
|
|
_expireDate = value;
|
|
|
|
|
|
OnPropertyChanged(ExpireDateSelector);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-11-05 14:42:21 -01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// IProfile of the user who wrote/updated this Content
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[DataMember]
|
|
|
|
|
|
public virtual IProfile Writer
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return _writer; }
|
|
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
_writer = value;
|
|
|
|
|
|
OnPropertyChanged(WriterSelector);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2012-10-03 10:26:50 -02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the ContentType used by this content object
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[IgnoreDataMember]
|
|
|
|
|
|
public IContentType ContentType
|
|
|
|
|
|
{
|
|
|
|
|
|
get { return _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;
|
2012-10-10 08:42:54 -02:00
|
|
|
|
ContentTypeBase = contentType;
|
|
|
|
|
|
Properties.EnsurePropertyTypes(PropertyTypes);
|
|
|
|
|
|
Properties.CollectionChanged += PropertiesChanged;
|
2012-10-03 10:26:50 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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;
|
2012-10-10 08:42:54 -02:00
|
|
|
|
ContentTypeBase = contentType;
|
|
|
|
|
|
Properties.EnsureCleanPropertyTypes(PropertyTypes);
|
|
|
|
|
|
Properties.CollectionChanged += PropertiesChanged;
|
2012-10-03 10:26:50 -02:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
ChangeContentType(contentType);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Changes the Published state of the content object
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="isPublished">Boolean indicating whether content is published (true) or unpublished (false)</param>
|
2012-10-29 14:28:16 -01:00
|
|
|
|
public void ChangePublishedState(bool isPublished)
|
2012-10-03 10:26:50 -02:00
|
|
|
|
{
|
|
|
|
|
|
Published = isPublished;
|
|
|
|
|
|
//NOTE Should this be checked against the Expire/Release dates?
|
|
|
|
|
|
//TODO possibly create new (unpublished version)?
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Changes the Trashed state of the content object
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="isTrashed">Boolean indicating whether content is trashed (true) or not trashed (false)</param>
|
|
|
|
|
|
/// <param name="parentId"> </param>
|
2012-10-29 14:28:16 -01:00
|
|
|
|
public void ChangeTrashedState(bool isTrashed, int parentId = -1)
|
2012-10-03 10:26:50 -02:00
|
|
|
|
{
|
|
|
|
|
|
Trashed = isTrashed;
|
|
|
|
|
|
|
|
|
|
|
|
//If Content is trashed the parent id should be set to that of the RecycleBin
|
|
|
|
|
|
if(isTrashed)
|
|
|
|
|
|
{
|
|
|
|
|
|
ParentId = -20;
|
|
|
|
|
|
}
|
|
|
|
|
|
else//otherwise set the parent id to the optional parameter, -1 being the fallback
|
|
|
|
|
|
{
|
|
|
|
|
|
ParentId = parentId;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//If the content is trashed and is published it should be marked as unpublished
|
|
|
|
|
|
if (isTrashed && Published)
|
|
|
|
|
|
{
|
|
|
|
|
|
ChangePublishedState(false);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2012-10-10 12:13:23 -02:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Creates a clone of the current entity
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns></returns>
|
|
|
|
|
|
public IContent Clone()
|
|
|
|
|
|
{
|
|
|
|
|
|
var clone = (Content)this.MemberwiseClone();
|
|
|
|
|
|
clone.Key = Guid.Empty;
|
|
|
|
|
|
clone.Version = Guid.NewGuid();
|
|
|
|
|
|
clone.ResetIdentity();
|
|
|
|
|
|
|
|
|
|
|
|
return clone;
|
|
|
|
|
|
}
|
2012-11-02 09:11:23 -01:00
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Indicates whether a specific property on the current <see cref="IContent"/> entity is dirty.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="propertyName">Name of the property to check</param>
|
|
|
|
|
|
/// <returns>True if Property is dirty, otherwise False</returns>
|
|
|
|
|
|
public override bool IsPropertyDirty(string propertyName)
|
|
|
|
|
|
{
|
|
|
|
|
|
bool existsInEntity = base.IsPropertyDirty(propertyName);
|
|
|
|
|
|
|
|
|
|
|
|
bool anyDirtyProperties = Properties.Any(x => x.IsPropertyDirty(propertyName));
|
|
|
|
|
|
|
|
|
|
|
|
return existsInEntity || anyDirtyProperties;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Indicates whether the current entity is dirty.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <returns>True if entity is dirty, otherwise False</returns>
|
|
|
|
|
|
public override bool IsDirty()
|
|
|
|
|
|
{
|
|
|
|
|
|
bool dirtyEntity = base.IsDirty();
|
|
|
|
|
|
|
|
|
|
|
|
bool dirtyProperties = Properties.Any(x => x.IsDirty());
|
|
|
|
|
|
|
|
|
|
|
|
return dirtyEntity || dirtyProperties;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Resets dirty properties by clearing the dictionary used to track changes.
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <remarks>
|
|
|
|
|
|
/// Please note that resetting the dirty properties could potentially
|
|
|
|
|
|
/// obstruct the saving of a new or updated entity.
|
|
|
|
|
|
/// </remarks>
|
|
|
|
|
|
public override void ResetDirtyProperties()
|
|
|
|
|
|
{
|
|
|
|
|
|
base.ResetDirtyProperties();
|
|
|
|
|
|
|
|
|
|
|
|
foreach (var property in Properties)
|
|
|
|
|
|
{
|
|
|
|
|
|
property.ResetDirtyProperties();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Method to call when Entity is being saved
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <remarks>Created date is set and a Unique key is assigned</remarks>
|
|
|
|
|
|
internal override void AddingEntity()
|
|
|
|
|
|
{
|
|
|
|
|
|
base.AddingEntity();
|
|
|
|
|
|
Key = Guid.NewGuid();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <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();
|
|
|
|
|
|
}
|
2012-10-03 10:26:50 -02:00
|
|
|
|
}
|
|
|
|
|
|
}
|