using System;
using System.Runtime.Serialization;
namespace Umbraco.Core.Models
{
///
/// Represents a Media object
///
[Serializable]
[DataContract(IsReference = true)]
public class Media : ContentBase, IMedia
{
private IMediaType _contentType;
///
/// Constructor for creating a Media object
///
///
/// MediaType for the current Media object
public Media(int parentId, IMediaType contentType)
: this(parentId, contentType, new PropertyCollection())
{
}
///
/// Constructor for creating a Media object
///
///
/// MediaType for the current Media object
/// Collection of properties
public Media(int parentId, IMediaType contentType, PropertyCollection properties) : base(parentId, contentType, properties)
{
_contentType = contentType;
}
///
/// Gets the ContentType used by this Media object
///
[IgnoreDataMember]
public IMediaType ContentType
{
get { return _contentType; }
}
///
/// Changes the for the current Media object
///
/// New MediaType for this Media
/// Leaves PropertyTypes intact after change
public void ChangeContentType(IMediaType contentType)
{
ContentTypeId = contentType.Id;
_contentType = contentType;
ContentTypeBase = contentType;
Properties.EnsurePropertyTypes(PropertyTypes);
Properties.CollectionChanged += PropertiesChanged;
}
///
/// Changes the for the current Media object and removes PropertyTypes,
/// which are not part of the new MediaType.
///
/// New MediaType for this Media
/// Boolean indicating whether to clear PropertyTypes upon change
public void ChangeContentType(IMediaType contentType, bool clearProperties)
{
if (clearProperties)
{
ContentTypeId = contentType.Id;
_contentType = contentType;
ContentTypeBase = contentType;
Properties.EnsureCleanPropertyTypes(PropertyTypes);
Properties.CollectionChanged += PropertiesChanged;
return;
}
ChangeContentType(contentType);
}
///
/// Changes the Trashed state of the content object
///
/// Boolean indicating whether content is trashed (true) or not trashed (false)
///
internal void ChangeTrashedState(bool isTrashed, int parentId = -1)
{
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;
}
}
///
/// Method to call when Entity is being saved
///
/// Created date is set and a Unique key is assigned
internal override void AddingEntity()
{
base.AddingEntity();
if (Key == Guid.Empty)
Key = Guid.NewGuid();
}
///
/// Method to call when Entity is being updated
///
/// Modified Date is set and a new Version guid is set
internal override void UpdatingEntity()
{
base.UpdatingEntity();
Version = Guid.NewGuid();
}
}
}