using System;
using System.Runtime.Serialization;
using Umbraco.Core.Persistence.Mappers;
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
///
/// ame of the Media object
/// Parent object
/// MediaType for the current Media object
public Media(string name, IMedia parent, IMediaType contentType)
: this(name, parent, contentType, new PropertyCollection())
{
}
///
/// Constructor for creating a Media object
///
/// ame of the Media object
/// Parent object
/// MediaType for the current Media object
/// Collection of properties
public Media(string name, IMedia parent, IMediaType contentType, PropertyCollection properties)
: base(name, parent, contentType, properties)
{
_contentType = contentType ?? throw new ArgumentNullException(nameof(contentType));
}
///
/// Constructor for creating a Media object
///
/// ame of the Media object
/// Id of the Parent IMedia
/// MediaType for the current Media object
public Media(string name, int parentId, IMediaType contentType)
: this(name, parentId, contentType, new PropertyCollection())
{
}
///
/// Constructor for creating a Media object
///
/// Name of the Media object
/// Id of the Parent IMedia
/// MediaType for the current Media object
/// Collection of properties
public Media(string name, int parentId, IMediaType contentType, PropertyCollection properties)
: base(name, parentId, contentType, properties)
{
_contentType = contentType ?? throw new ArgumentNullException(nameof(contentType));
}
///
/// Gets the ContentType used by this Media object
///
[IgnoreDataMember]
public IMediaType ContentType => _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)
///
public void ChangeTrashedState(bool isTrashed, int parentId = -20)
{
Trashed = isTrashed;
//The Media Recycle Bin Id is -21 so we correct that here
ParentId = parentId == -20 ? -21 : parentId;
}
}
}