Refactoring Content/Media and ContentTypes/MediaTypes so the two are as close as possible. By doing this Media and Content are practically the same witht the except of a few properties.

This implements U4-10
This commit is contained in:
Morten@Thinkpad-X220
2012-10-10 08:42:54 -02:00
parent 90108e344f
commit 74ecbac7d7
14 changed files with 885 additions and 1368 deletions

View File

@@ -0,0 +1,262 @@
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Globalization;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using Umbraco.Core.Models.EntityBase;
namespace Umbraco.Core.Models
{
/// <summary>
/// Represents an abstract class for base Content properties and methods
/// </summary>
public abstract class ContentBase : Entity, IContentBase
{
protected IContentTypeComposition ContentTypeBase;
private int _parentId;
private string _name;
private int _sortOrder;
private int _level;
private string _path;
private int _userId;
private bool _trashed;
private int _contentTypeId;
private PropertyCollection _properties;
protected ContentBase(int parentId, IContentTypeComposition contentType, PropertyCollection properties)
{
_parentId = parentId;
_contentTypeId = int.Parse(contentType.Id.ToString(CultureInfo.InvariantCulture));
ContentTypeBase = contentType;
_properties = properties;
_properties.EnsurePropertyTypes(PropertyTypes);
Version = Guid.NewGuid();
}
private static readonly PropertyInfo NameSelector = ExpressionHelper.GetPropertyInfo<ContentBase, string>(x => x.Name);
private static readonly PropertyInfo ParentIdSelector = ExpressionHelper.GetPropertyInfo<ContentBase, int>(x => x.ParentId);
private static readonly PropertyInfo SortOrderSelector = ExpressionHelper.GetPropertyInfo<ContentBase, int>(x => x.SortOrder);
private static readonly PropertyInfo LevelSelector = ExpressionHelper.GetPropertyInfo<ContentBase, int>(x => x.Level);
private static readonly PropertyInfo PathSelector = ExpressionHelper.GetPropertyInfo<ContentBase, string>(x => x.Path);
private static readonly PropertyInfo UserIdSelector = ExpressionHelper.GetPropertyInfo<ContentBase, int>(x => x.UserId);
private static readonly PropertyInfo TrashedSelector = ExpressionHelper.GetPropertyInfo<ContentBase, bool>(x => x.Trashed);
private static readonly PropertyInfo DefaultContentTypeIdSelector = ExpressionHelper.GetPropertyInfo<ContentBase, int>(x => x.ContentTypeId);
private readonly static PropertyInfo PropertyCollectionSelector = ExpressionHelper.GetPropertyInfo<ContentBase, PropertyCollection>(x => x.Properties);
protected void PropertiesChanged(object sender, NotifyCollectionChangedEventArgs e)
{
OnPropertyChanged(PropertyCollectionSelector);
}
/// <summary>
/// Gets or sets the Id of the Parent entity
/// </summary>
/// <remarks>Might not be necessary if handled as a relation?</remarks>
[DataMember]
public virtual int ParentId
{
get { return _parentId; }
set
{
_parentId = value;
OnPropertyChanged(ParentIdSelector);
}
}
/// <summary>
/// Gets or sets the name of the entity
/// </summary>
[DataMember]
public virtual string Name
{
get { return _name; }
set
{
_name = value;
OnPropertyChanged(NameSelector);
}
}
/// <summary>
/// Gets the Url name of the entity
/// </summary>
[IgnoreDataMember]
public virtual string UrlName
{
//TODO Needs to implement proper url casing/syntax
get { return Name.ToLower().Replace(" ", "-"); }
}
/// <summary>
/// Gets or sets the sort order of the content entity
/// </summary>
[DataMember]
public virtual int SortOrder
{
get { return _sortOrder; }
set
{
_sortOrder = value;
OnPropertyChanged(SortOrderSelector);
}
}
/// <summary>
/// Gets or sets the level of the content entity
/// </summary>
[DataMember]
public virtual int Level
{
get { return _level; }
set
{
_level = value;
OnPropertyChanged(LevelSelector);
}
}
/// <summary>
/// Gets or sets the path
/// </summary>
[DataMember]
public virtual string Path //Setting this value should be handled by the class not the user
{
get { return _path; }
set
{
_path = value;
OnPropertyChanged(PathSelector);
}
}
/// <summary>
/// Id of the user who created this Content
/// </summary>
[DataMember]
public virtual int UserId
{
get { return _userId; }
set
{
_userId = value;
OnPropertyChanged(UserIdSelector);
}
}
/// <summary>
/// Boolean indicating whether this Content is Trashed or not.
/// If Content is Trashed it will be located in the Recyclebin.
/// </summary>
/// <remarks>When content is trashed it should be unpublished</remarks>
[DataMember]
public virtual bool Trashed //Setting this value should be handled by the class not the user
{
get { return _trashed; }
internal set
{
_trashed = value;
OnPropertyChanged(TrashedSelector);
}
}
/// <summary>
/// Guid Id of the curent Version
/// </summary>
[DataMember]
public Guid Version { get; internal set; }
/// <summary>
/// Integer Id of the default ContentType
/// </summary>
[DataMember]
public virtual int ContentTypeId
{
get { return _contentTypeId; }
protected set
{
_contentTypeId = value;
OnPropertyChanged(DefaultContentTypeIdSelector);
}
}
/// <summary>
/// Collection of properties, which make up all the data available for this Content object
/// </summary>
[DataMember]
public virtual PropertyCollection Properties
{
get { return _properties; }
set
{
_properties = value;
_properties.CollectionChanged += PropertiesChanged;
}
}
/// <summary>
/// List of PropertyGroups available on this Content object
/// </summary>
[IgnoreDataMember]
public IEnumerable<PropertyGroup> PropertyGroups { get { return ContentTypeBase.CompositionPropertyGroups; } }
/// <summary>
/// List of PropertyTypes available on this Content object
/// </summary>
[IgnoreDataMember]
public IEnumerable<PropertyType> PropertyTypes { get { return ContentTypeBase.CompositionPropertyTypes; } }
/// <summary>
/// Indicates whether the content object has a property with the supplied alias
/// </summary>
/// <param name="propertyTypeAlias">Alias of the PropertyType</param>
/// <returns>True if Property with given alias exists, otherwise False</returns>
public virtual bool HasProperty(string propertyTypeAlias)
{
return Properties.Contains(propertyTypeAlias);
}
/// <summary>
/// Gets the value of a Property
/// </summary>
/// <param name="propertyTypeAlias">Alias of the PropertyType</param>
/// <returns><see cref="Property"/> Value as an <see cref="object"/></returns>
public virtual object GetValue(string propertyTypeAlias)
{
return Properties[propertyTypeAlias].Value;
}
/// <summary>
/// Gets the value of a Property
/// </summary>
/// <typeparam name="TPassType">Type of the value to return</typeparam>
/// <param name="propertyTypeAlias">Alias of the PropertyType</param>
/// <returns><see cref="Property"/> Value as a <see cref="TPassType"/></returns>
public virtual TPassType GetValue<TPassType>(string propertyTypeAlias)
{
return (TPassType)Properties[propertyTypeAlias].Value;
}
/// <summary>
/// Sets the value of a Property
/// </summary>
/// <param name="propertyTypeAlias">Alias of the PropertyType</param>
/// <param name="value">Value to set for the Property</param>
public virtual void SetValue(string propertyTypeAlias, object value)
{
if (Properties.Contains(propertyTypeAlias))
{
Properties[propertyTypeAlias].Value = value;
return;
}
var propertyType = PropertyTypes.FirstOrDefault(x => x.Alias == propertyTypeAlias);
if (propertyType == null)
{
throw new Exception(String.Format("No PropertyType exists with the supplied alias: {0}", propertyTypeAlias));
}
Properties.Add(propertyType.CreatePropertyFromValue(value));
}
}
}