2012-10-10 08:42:54 -02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using System.Collections.Specialized;
|
2013-10-09 13:32:58 +11:00
|
|
|
|
using System.ComponentModel;
|
2013-02-09 04:05:01 +06:00
|
|
|
|
using System.Diagnostics;
|
2012-10-10 08:42:54 -02:00
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
using System.Runtime.Serialization;
|
2013-01-14 11:02:12 -01:00
|
|
|
|
using System.Web;
|
2018-01-15 11:32:30 +01:00
|
|
|
|
using Umbraco.Core.Models.Entities;
|
2012-10-10 08:42:54 -02:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Core.Models
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Represents an abstract class for base Content properties and methods
|
|
|
|
|
|
/// </summary>
|
2014-02-20 22:34:54 +11:00
|
|
|
|
[Serializable]
|
|
|
|
|
|
[DataContract(IsReference = true)]
|
2013-02-09 04:05:01 +06:00
|
|
|
|
[DebuggerDisplay("Id: {Id}, Name: {Name}, ContentType: {ContentTypeBase.Alias}")]
|
2018-01-15 11:32:30 +01:00
|
|
|
|
public abstract class ContentBase : TreeEntityBase, IContentBase
|
2012-10-10 08:42:54 -02:00
|
|
|
|
{
|
2017-11-07 19:49:14 +01:00
|
|
|
|
private static readonly Lazy<PropertySelectors> Ps = new Lazy<PropertySelectors>();
|
|
|
|
|
|
|
|
|
|
|
|
private int _contentTypeId;
|
2012-10-10 08:42:54 -02:00
|
|
|
|
protected IContentTypeComposition ContentTypeBase;
|
2017-11-10 11:27:12 +01:00
|
|
|
|
private int _writerId;
|
2017-11-07 19:49:14 +01:00
|
|
|
|
|
|
|
|
|
|
// fixme need to deal with localized names, how?
|
|
|
|
|
|
// for the time being, this is the node text = unique
|
|
|
|
|
|
private string _name;
|
|
|
|
|
|
|
2012-10-10 08:42:54 -02:00
|
|
|
|
private PropertyCollection _properties;
|
2017-11-07 19:49:14 +01:00
|
|
|
|
|
2013-01-15 10:12:23 -01:00
|
|
|
|
/// <summary>
|
2017-11-07 19:49:14 +01:00
|
|
|
|
/// Initializes a new instance of the <see cref="ContentBase"/> class.
|
2013-01-15 10:12:23 -01:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
protected ContentBase(string name, int parentId, IContentTypeComposition contentType, PropertyCollection properties)
|
2017-11-07 19:49:14 +01:00
|
|
|
|
: this(name, contentType, properties)
|
2012-10-10 08:42:54 -02:00
|
|
|
|
{
|
2017-05-31 09:18:09 +02:00
|
|
|
|
if (parentId == 0) throw new ArgumentOutOfRangeException(nameof(parentId));
|
2018-01-15 11:32:30 +01:00
|
|
|
|
ParentId = parentId;
|
2012-10-10 08:42:54 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-01-15 10:12:23 -01:00
|
|
|
|
/// <summary>
|
2017-11-07 19:49:14 +01:00
|
|
|
|
/// Initializes a new instance of the <see cref="ContentBase"/> class.
|
2013-01-15 10:12:23 -01:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
protected ContentBase(string name, IContentBase parent, IContentTypeComposition contentType, PropertyCollection properties)
|
2017-11-07 19:49:14 +01:00
|
|
|
|
: this(name, contentType, properties)
|
2017-07-20 11:21:28 +02:00
|
|
|
|
{
|
|
|
|
|
|
if (parent == null) throw new ArgumentNullException(nameof(parent));
|
2018-01-15 11:32:30 +01:00
|
|
|
|
SetParent(parent);
|
2017-11-07 19:49:14 +01:00
|
|
|
|
}
|
2012-12-16 04:02:29 +05:00
|
|
|
|
|
2017-11-07 19:49:14 +01:00
|
|
|
|
private ContentBase(string name, IContentTypeComposition contentType, PropertyCollection properties)
|
|
|
|
|
|
{
|
2017-07-20 11:21:28 +02:00
|
|
|
|
ContentTypeBase = contentType ?? throw new ArgumentNullException(nameof(contentType));
|
2013-01-15 10:21:24 -01:00
|
|
|
|
|
2017-11-07 19:49:14 +01:00
|
|
|
|
// initially, all new instances have
|
|
|
|
|
|
Id = 0; // no identity
|
2017-12-01 19:29:54 +01:00
|
|
|
|
VersionId = 0; // no versions
|
2017-11-07 19:49:14 +01:00
|
|
|
|
|
2018-01-15 11:32:30 +01:00
|
|
|
|
Name = _name = name;
|
2017-07-20 11:21:28 +02:00
|
|
|
|
_contentTypeId = contentType.Id;
|
|
|
|
|
|
_properties = properties ?? throw new ArgumentNullException(nameof(properties));
|
|
|
|
|
|
_properties.EnsurePropertyTypes(PropertyTypes);
|
|
|
|
|
|
}
|
2012-12-16 04:02:29 +05:00
|
|
|
|
|
2017-11-07 19:49:14 +01:00
|
|
|
|
// ReSharper disable once ClassNeverInstantiated.Local
|
2016-06-21 18:11:03 +02:00
|
|
|
|
private class PropertySelectors
|
|
|
|
|
|
{
|
|
|
|
|
|
public readonly PropertyInfo DefaultContentTypeIdSelector = ExpressionHelper.GetPropertyInfo<ContentBase, int>(x => x.ContentTypeId);
|
|
|
|
|
|
public readonly PropertyInfo PropertyCollectionSelector = ExpressionHelper.GetPropertyInfo<ContentBase, PropertyCollection>(x => x.Properties);
|
2017-11-10 11:27:12 +01:00
|
|
|
|
public readonly PropertyInfo WriterSelector = ExpressionHelper.GetPropertyInfo<ContentBase, int>(x => x.WriterId);
|
2016-11-03 10:31:44 +01:00
|
|
|
|
}
|
2012-10-10 08:42:54 -02:00
|
|
|
|
|
2018-01-15 11:32:30 +01:00
|
|
|
|
protected void PropertiesChanged(object sender, NotifyCollectionChangedEventArgs e)
|
2012-10-10 08:42:54 -02:00
|
|
|
|
{
|
2018-01-15 11:32:30 +01:00
|
|
|
|
OnPropertyChanged(Ps.Value.PropertyCollectionSelector);
|
2012-10-10 08:42:54 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-11-10 11:27:12 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Id of the user who wrote/updated this entity
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[DataMember]
|
|
|
|
|
|
public virtual int WriterId
|
|
|
|
|
|
{
|
|
|
|
|
|
get => _writerId;
|
|
|
|
|
|
set => SetPropertyValueAndDetectChanges(value, ref _writerId, Ps.Value.WriterSelector);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-11-15 19:44:32 +01:00
|
|
|
|
[IgnoreDataMember]
|
2017-12-01 19:29:54 +01:00
|
|
|
|
public int VersionId { get; internal set; }
|
2017-11-15 19:44:32 +01:00
|
|
|
|
|
2012-10-10 08:42:54 -02:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Integer Id of the default ContentType
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[DataMember]
|
|
|
|
|
|
public virtual int ContentTypeId
|
|
|
|
|
|
{
|
2014-10-01 10:18:51 +10:00
|
|
|
|
get
|
|
|
|
|
|
{
|
|
|
|
|
|
//There will be cases where this has not been updated to reflect the true content type ID.
|
|
|
|
|
|
//This will occur when inserting new content.
|
|
|
|
|
|
if (_contentTypeId == 0 && ContentTypeBase != null && ContentTypeBase.HasIdentity)
|
|
|
|
|
|
{
|
|
|
|
|
|
_contentTypeId = ContentTypeBase.Id;
|
|
|
|
|
|
}
|
|
|
|
|
|
return _contentTypeId;
|
|
|
|
|
|
}
|
2018-01-15 11:32:30 +01:00
|
|
|
|
protected set => SetPropertyValueAndDetectChanges(value, ref _contentTypeId, Ps.Value.DefaultContentTypeIdSelector);
|
2012-10-10 08:42:54 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-11-07 19:49:14 +01:00
|
|
|
|
/// Gets or sets the collection of properties for the entity.
|
2012-10-10 08:42:54 -02:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
[DataMember]
|
|
|
|
|
|
public virtual PropertyCollection Properties
|
|
|
|
|
|
{
|
2017-11-07 19:49:14 +01:00
|
|
|
|
get => _properties;
|
2012-10-10 08:42:54 -02:00
|
|
|
|
set
|
|
|
|
|
|
{
|
|
|
|
|
|
_properties = value;
|
|
|
|
|
|
_properties.CollectionChanged += PropertiesChanged;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-11-07 19:49:14 +01:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Gets the enumeration of property groups for the entity.
|
|
|
|
|
|
/// fixme is a proxy, kill this
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
[IgnoreDataMember]
|
|
|
|
|
|
public IEnumerable<PropertyGroup> PropertyGroups => ContentTypeBase.CompositionPropertyGroups;
|
2014-02-20 22:34:54 +11:00
|
|
|
|
|
2013-10-09 13:32:58 +11:00
|
|
|
|
/// <summary>
|
2017-11-07 19:49:14 +01:00
|
|
|
|
/// Gets the numeration of property types for the entity.
|
|
|
|
|
|
/// fixme is a proxy, kill this
|
2013-10-09 13:32:58 +11:00
|
|
|
|
/// </summary>
|
2017-11-07 19:49:14 +01:00
|
|
|
|
[IgnoreDataMember]
|
|
|
|
|
|
public IEnumerable<PropertyType> PropertyTypes => ContentTypeBase.CompositionPropertyTypes;
|
|
|
|
|
|
|
2017-11-10 11:27:12 +01:00
|
|
|
|
#region Has, Get, Set, Publish Property Value
|
2017-11-07 19:49:14 +01:00
|
|
|
|
|
2017-11-30 13:56:29 +01:00
|
|
|
|
/// <inheritdoc />
|
2017-11-07 19:49:14 +01:00
|
|
|
|
public virtual bool HasProperty(string propertyTypeAlias)
|
|
|
|
|
|
=> Properties.Contains(propertyTypeAlias);
|
|
|
|
|
|
|
2017-11-30 13:56:29 +01:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public virtual object GetValue(string propertyTypeAlias, int? languageId = null, string segment = null, bool published = false)
|
2017-11-23 16:30:04 +01:00
|
|
|
|
{
|
|
|
|
|
|
return Properties.TryGetValue(propertyTypeAlias, out var property)
|
|
|
|
|
|
? property.GetValue(languageId, segment, published)
|
|
|
|
|
|
: null;
|
2017-11-07 19:49:14 +01:00
|
|
|
|
}
|
2012-10-10 08:42:54 -02:00
|
|
|
|
|
2017-11-30 13:56:29 +01:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public virtual TValue GetValue<TValue>(string propertyTypeAlias, int? languageId = null, string segment = null, bool published = false)
|
2012-10-10 08:42:54 -02:00
|
|
|
|
{
|
2017-11-23 16:30:04 +01:00
|
|
|
|
if (!Properties.TryGetValue(propertyTypeAlias, out var property))
|
2017-11-07 19:49:14 +01:00
|
|
|
|
return default;
|
|
|
|
|
|
|
2017-11-30 13:56:29 +01:00
|
|
|
|
var convertAttempt = property.GetValue(languageId, segment, published).TryConvertTo<TValue>();
|
2017-11-07 19:49:14 +01:00
|
|
|
|
return convertAttempt.Success ? convertAttempt.Result : default;
|
2012-10-10 08:42:54 -02:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-11-30 13:56:29 +01:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public virtual void SetValue(string propertyTypeAlias, object value, int? languageId = null, string segment = null)
|
2017-11-23 16:30:04 +01:00
|
|
|
|
{
|
2017-11-30 13:56:29 +01:00
|
|
|
|
if (Properties.Contains(propertyTypeAlias))
|
|
|
|
|
|
{
|
|
|
|
|
|
Properties[propertyTypeAlias].SetValue(value, languageId, segment);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
2013-01-14 11:02:12 -01:00
|
|
|
|
|
2017-11-30 13:56:29 +01:00
|
|
|
|
var propertyType = PropertyTypes.FirstOrDefault(x => x.Alias.InvariantEquals(propertyTypeAlias));
|
|
|
|
|
|
if (propertyType == null)
|
|
|
|
|
|
throw new InvalidOperationException($"No PropertyType exists with the supplied alias \"{propertyTypeAlias}\".");
|
2017-11-23 16:30:04 +01:00
|
|
|
|
|
2017-11-30 13:56:29 +01:00
|
|
|
|
var property = propertyType.CreateProperty();
|
|
|
|
|
|
property.SetValue(value, languageId, segment);
|
|
|
|
|
|
Properties.Add(property);
|
2017-11-07 19:49:14 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-11-27 19:17:50 +01:00
|
|
|
|
// HttpPostedFileBase is the base class that can be mocked
|
|
|
|
|
|
// HttpPostedFile is what we get in ASP.NET
|
|
|
|
|
|
// HttpPostedFileWrapper wraps sealed HttpPostedFile as HttpPostedFileBase
|
|
|
|
|
|
|
2017-11-07 19:49:14 +01:00
|
|
|
|
/// <summary>
|
2017-12-06 10:20:15 +01:00
|
|
|
|
/// Sets the posted file value of a property.
|
2013-01-14 11:02:12 -01:00
|
|
|
|
/// </summary>
|
2017-12-06 10:20:15 +01:00
|
|
|
|
public virtual void SetValue(string propertyTypeAlias, HttpPostedFile value, int? languageId = null, string segment = null)
|
2013-01-14 11:02:12 -01:00
|
|
|
|
{
|
2017-12-06 10:20:15 +01:00
|
|
|
|
ContentExtensions.SetValue(this, propertyTypeAlias, new HttpPostedFileWrapper(value), languageId, segment);
|
2013-01-14 11:02:12 -01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-12-06 10:20:15 +01:00
|
|
|
|
/// Sets the posted file value of a property.
|
2013-01-14 11:02:12 -01:00
|
|
|
|
/// </summary>
|
2017-12-06 10:20:15 +01:00
|
|
|
|
public virtual void SetValue(string propertyTypeAlias, HttpPostedFileBase value, int? languageId = null, string segment = null)
|
2013-01-14 11:02:12 -01:00
|
|
|
|
{
|
2017-12-06 10:20:15 +01:00
|
|
|
|
ContentExtensions.SetValue(this, propertyTypeAlias, value, languageId, segment);
|
2013-01-14 11:02:12 -01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-11-07 19:49:14 +01:00
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Validation
|
|
|
|
|
|
|
2017-11-30 13:56:29 +01:00
|
|
|
|
public virtual Property[] ValidateAll()
|
2017-11-23 16:30:04 +01:00
|
|
|
|
{
|
2017-11-30 13:56:29 +01:00
|
|
|
|
return Properties.Where(x => !x.IsAllValid()).ToArray();
|
2017-11-23 16:30:04 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-11-30 13:56:29 +01:00
|
|
|
|
public virtual Property[] Validate(int? languageId = null, string segment = null)
|
2017-11-27 19:17:50 +01:00
|
|
|
|
{
|
2017-11-30 13:56:29 +01:00
|
|
|
|
return Properties.Where(x => !x.IsValid(languageId, segment)).ToArray();
|
2017-11-27 19:17:50 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-11-30 13:56:29 +01:00
|
|
|
|
public virtual Property[] ValidateCulture(int? languageId = null)
|
2017-11-27 19:17:50 +01:00
|
|
|
|
{
|
2017-11-30 13:56:29 +01:00
|
|
|
|
return Properties.Where(x => !x.IsCultureValid(languageId)).ToArray();
|
2017-11-27 19:17:50 +01:00
|
|
|
|
}
|
2017-11-07 19:49:14 +01:00
|
|
|
|
|
|
|
|
|
|
#endregion
|
|
|
|
|
|
|
|
|
|
|
|
#region Dirty
|
2015-05-07 11:43:53 +10:00
|
|
|
|
|
2018-03-21 14:40:59 +01:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
/// <remarks>Overriden to include user properties.</remarks>
|
2017-11-07 19:49:14 +01:00
|
|
|
|
public override void ResetDirtyProperties(bool rememberDirty)
|
2013-08-07 11:39:25 +10:00
|
|
|
|
{
|
2017-11-07 19:49:14 +01:00
|
|
|
|
base.ResetDirtyProperties(rememberDirty);
|
2013-08-07 11:39:25 +10:00
|
|
|
|
|
2017-11-07 19:49:14 +01:00
|
|
|
|
// also reset dirty changes made to user's properties
|
2013-08-07 11:39:25 +10:00
|
|
|
|
foreach (var prop in Properties)
|
2017-11-07 19:49:14 +01:00
|
|
|
|
prop.ResetDirtyProperties(rememberDirty);
|
2013-08-07 11:39:25 +10:00
|
|
|
|
}
|
2015-05-07 11:43:53 +10:00
|
|
|
|
|
2018-03-21 14:40:59 +01:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
/// <remarks>Overriden to include user properties.</remarks>
|
2015-05-07 11:43:53 +10:00
|
|
|
|
public override bool IsDirty()
|
|
|
|
|
|
{
|
|
|
|
|
|
return IsEntityDirty() || this.IsAnyUserPropertyDirty();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-03-21 14:40:59 +01:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
/// <remarks>Overriden to include user properties.</remarks>
|
2015-05-07 11:43:53 +10:00
|
|
|
|
public override bool WasDirty()
|
|
|
|
|
|
{
|
|
|
|
|
|
return WasEntityDirty() || this.WasAnyUserPropertyDirty();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-11-07 19:49:14 +01:00
|
|
|
|
/// Gets a value indicating whether the current entity's own properties (not user) are dirty.
|
2015-05-07 11:43:53 +10:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public bool IsEntityDirty()
|
|
|
|
|
|
{
|
|
|
|
|
|
return base.IsDirty();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2017-11-07 19:49:14 +01:00
|
|
|
|
/// Gets a value indicating whether the current entity's own properties (not user) were dirty.
|
2015-05-07 11:43:53 +10:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
public bool WasEntityDirty()
|
|
|
|
|
|
{
|
|
|
|
|
|
return base.WasDirty();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-03-21 14:40:59 +01:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
/// <remarks>Overriden to include user properties.</remarks>
|
2015-05-07 11:43:53 +10:00
|
|
|
|
public override bool IsPropertyDirty(string propertyName)
|
|
|
|
|
|
{
|
2017-11-07 19:49:14 +01:00
|
|
|
|
if (base.IsPropertyDirty(propertyName))
|
2015-05-07 11:43:53 +10:00
|
|
|
|
return true;
|
|
|
|
|
|
|
2017-11-07 19:49:14 +01:00
|
|
|
|
return Properties.Contains(propertyName) && Properties[propertyName].IsDirty();
|
2015-05-07 11:43:53 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-03-21 14:40:59 +01:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
/// <remarks>Overriden to include user properties.</remarks>
|
2015-05-07 11:43:53 +10:00
|
|
|
|
public override bool WasPropertyDirty(string propertyName)
|
|
|
|
|
|
{
|
2017-11-07 19:49:14 +01:00
|
|
|
|
if (base.WasPropertyDirty(propertyName))
|
2015-05-07 11:43:53 +10:00
|
|
|
|
return true;
|
|
|
|
|
|
|
2017-11-07 19:49:14 +01:00
|
|
|
|
return Properties.Contains(propertyName) && Properties[propertyName].WasDirty();
|
2015-05-07 11:43:53 +10:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-03-21 14:40:59 +01:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
/// <remarks>Overriden to include user properties.</remarks>
|
|
|
|
|
|
public override IEnumerable<string> GetDirtyProperties()
|
|
|
|
|
|
{
|
|
|
|
|
|
var instanceProperties = base.GetDirtyProperties();
|
|
|
|
|
|
var propertyTypes = Properties.Where(x => x.IsDirty()).Select(x => x.Alias);
|
|
|
|
|
|
return instanceProperties.Concat(propertyTypes);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
/// <remarks>Overriden to include user properties.</remarks>
|
|
|
|
|
|
public override IEnumerable<string> GetWereDirtyProperties()
|
|
|
|
|
|
{
|
|
|
|
|
|
var instanceProperties = base.GetWereDirtyProperties();
|
|
|
|
|
|
var propertyTypes = Properties.Where(x => x.WasDirty()).Select(x => x.Alias);
|
|
|
|
|
|
return instanceProperties.Concat(propertyTypes);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2015-05-07 11:43:53 +10:00
|
|
|
|
#endregion
|
2012-10-10 08:42:54 -02:00
|
|
|
|
}
|
2017-07-20 11:21:28 +02:00
|
|
|
|
}
|