using Umbraco.Cms.Core.Models.Entities;
namespace Umbraco.Cms.Core.Models;
///
/// Represents a folder for organizing entities such as content types and data types.
///
public sealed class EntityContainer : TreeEntityBase, IUmbracoEntity
{
private static readonly Dictionary ObjectTypeMap = new()
{
{ Constants.ObjectTypes.DataType, Constants.ObjectTypes.DataTypeContainer },
{ Constants.ObjectTypes.DocumentType, Constants.ObjectTypes.DocumentTypeContainer },
{ Constants.ObjectTypes.MediaType, Constants.ObjectTypes.MediaTypeContainer },
};
///
/// Initializes a new instance of an class.
///
public EntityContainer(Guid containedObjectType)
{
if (ObjectTypeMap.ContainsKey(containedObjectType) == false)
{
throw new ArgumentException("Not a contained object type.", nameof(containedObjectType));
}
ContainedObjectType = containedObjectType;
ParentId = -1;
Path = "-1";
Level = 0;
SortOrder = 0;
}
///
/// Initializes a new instance of an class.
///
public EntityContainer(int id, Guid uniqueId, int parentId, string path, int level, int sortOrder, Guid containedObjectType, string? name, int userId)
: this(containedObjectType)
{
Id = id;
Key = uniqueId;
ParentId = parentId;
Name = name;
Path = path;
Level = level;
SortOrder = sortOrder;
CreatorId = userId;
}
///
/// Gets or sets the node object type of the contained objects.
///
public Guid ContainedObjectType { get; }
///
/// Gets the node object type of the container objects.
///
public Guid ContainerObjectType => ObjectTypeMap[ContainedObjectType];
///
/// Gets the container object type corresponding to a contained object type.
///
/// The contained object type.
/// The object type of containers containing objects of the contained object type.
public static Guid GetContainerObjectType(Guid containedObjectType)
{
if (ObjectTypeMap.ContainsKey(containedObjectType) == false)
{
throw new ArgumentException("Not a contained object type.", nameof(containedObjectType));
}
return ObjectTypeMap[containedObjectType];
}
///
/// Gets the contained object type corresponding to a container object type.
///
/// The container object type.
/// The object type of objects that containers of the container object type can contain.
public static Guid GetContainedObjectType(Guid containerObjectType)
{
Guid contained = ObjectTypeMap.FirstOrDefault(x => x.Value == containerObjectType).Key;
if (contained == null)
{
throw new ArgumentException("Not a container object type.", nameof(containerObjectType));
}
return contained;
}
}