using System;
using System.Collections.Concurrent;
using Umbraco.Cms.Core;
using Umbraco.Cms.Core.Events;
using Umbraco.Cms.Core.Services;
using Umbraco.Cms.Core.Trees;
using Umbraco.Extensions;
namespace Umbraco.Cms.Web.BackOffice.Trees
{
///
/// The base controller for all tree requests
///
public abstract class TreeController : TreeControllerBase
{
private static readonly ConcurrentDictionary _treeAttributeCache = new ConcurrentDictionary();
private readonly TreeAttribute _treeAttribute;
protected ILocalizedTextService LocalizedTextService { get; }
protected TreeController(ILocalizedTextService localizedTextService, UmbracoApiControllerTypeCollection umbracoApiControllerTypeCollection, IEventAggregator eventAggregator)
: base(umbracoApiControllerTypeCollection, eventAggregator)
{
LocalizedTextService = localizedTextService ?? throw new ArgumentNullException(nameof(localizedTextService));
_treeAttribute = GetTreeAttribute();
}
///
public override string RootNodeDisplayName => Tree.GetRootNodeDisplayName(this, LocalizedTextService);
///
public override string TreeGroup => _treeAttribute.TreeGroup;
///
public override string TreeAlias => _treeAttribute.TreeAlias;
///
public override string TreeTitle => _treeAttribute.TreeTitle;
///
public override TreeUse TreeUse => _treeAttribute.TreeUse;
///
public override string SectionAlias => _treeAttribute.SectionAlias;
///
public override int SortOrder => _treeAttribute.SortOrder;
///
public override bool IsSingleNodeTree => _treeAttribute.IsSingleNodeTree;
private TreeAttribute GetTreeAttribute()
{
return _treeAttributeCache.GetOrAdd(GetType(), type =>
{
var treeAttribute = type.GetCustomAttribute(false);
if (treeAttribute == null)
throw new InvalidOperationException("The Tree controller is missing the " + typeof(TreeAttribute).FullName + " attribute");
return treeAttribute;
});
}
}
}