using System;
using System.Collections.Generic;
using System.Collections.Immutable;
using System.Linq;
using Abp.Collections.Extensions;
using Abp.Localization;
using Abp.UI.Inputs;
namespace Abp.Application.Features
{
///
/// Defines a feature of the application. A can be used in a multi-tenant application
/// to enable or disable some application features depending on editions and tenants.
///
public class Feature
{
///
/// Gets/sets the arbitrary objects related to this object.
/// Gets null if a given key does not exist.
/// This is a shortcut for the dictionary.
///
/// Key
public object this[string key]
{
get => Attributes.GetOrDefault(key);
set => Attributes[key] = value;
}
///
/// Arbitrary objects related to this object.
/// These objects must be serializable.
///
public IDictionary Attributes { get; private set; }
///
/// Parent of this feature, if one exists.
/// If set, this feature can be enabled only if the parent is enabled.
///
public Feature Parent { get; private set; }
///
/// Unique name of the feature.
///
public string Name { get; private set; }
///
/// Display name of this feature.
/// This can be used to show features on the UI.
///
public ILocalizableString DisplayName { get; set; }
///
/// A brief description for this feature.
/// This can be used to show this feature's description on the UI.
///
public ILocalizableString Description { get; set; }
///
/// Input type.
/// This can be used to prepare an input for changing this feature's value.
/// Default: .
///
public IInputType InputType { get; set; }
///
/// Default value of this feature.
/// This value is used if this feature's value is not defined for the current edition or tenant.
///
public string DefaultValue { get; set; }
///
/// Feature's scope.
///
public FeatureScopes Scope { get; set; }
///
/// List of child features.
///
public IReadOnlyList Children => _children.ToImmutableList();
private readonly List _children;
///
/// Creates a new feature.
///
/// Unique name of the feature
/// Default value
/// Display name of the feature
/// A brief description for the feature
/// Feature scope
/// Input type
public Feature(string name, string defaultValue, ILocalizableString displayName = null, ILocalizableString description = null, FeatureScopes scope = FeatureScopes.All, IInputType inputType = null)
{
Name = name ?? throw new ArgumentNullException("name");
DisplayName = displayName;
Description = description;
Scope = scope;
DefaultValue = defaultValue;
InputType = inputType ?? new CheckboxInputType();
_children = new List();
Attributes = new Dictionary();
}
///
/// Adds a child feature.
///
/// Returns a newly created child feature
public Feature CreateChildFeature(string name, string defaultValue, ILocalizableString displayName = null, ILocalizableString description = null, FeatureScopes scope = FeatureScopes.All, IInputType inputType = null)
{
var feature = new Feature(name, defaultValue, displayName, description, scope, inputType) { Parent = this };
_children.Add(feature);
return feature;
}
public void RemoveChildFeature(string name)
{
var featureToRemove = _children.FirstOrDefault(f => f.Name == name);
_children.Remove(featureToRemove);
}
public override string ToString()
{
return string.Format("[Feature: {0}]", Name);
}
}
}