using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace GreenTree.Nachtragsmanagement.Core.Plugins
{
///
/// Plugin finder
///
public class PluginFinder : IPluginFinder
{
#region Fields
private IList _plugins;
private bool _arePluginsLoaded;
#endregion
#region Utilities
///
/// Ensure plugins are loaded
///
protected virtual void EnsurePluginsAreLoaded()
{
if (!_arePluginsLoaded)
{
var foundPlugins = PluginManager.ReferencedPlugins.ToList();
foundPlugins.Sort();
_plugins = foundPlugins.ToList();
_arePluginsLoaded = true;
}
}
///
/// Check whether the plugin is available in a certain store
///
/// Plugin descriptor to check
/// Load plugins mode
/// true - available; false - no
protected virtual bool CheckLoadMode(PluginDescriptor pluginDescriptor, LoadPluginsMode loadMode)
{
if (pluginDescriptor == null)
throw new ArgumentNullException("pluginDescriptor");
switch (loadMode)
{
case LoadPluginsMode.All:
//no filering
return true;
case LoadPluginsMode.InstalledOnly:
return pluginDescriptor.Installed;
case LoadPluginsMode.NotInstalledOnly:
return !pluginDescriptor.Installed;
default:
throw new Exception("Not supported LoadPluginsMode");
}
}
///
/// Check whether the plugin is in a certain group
///
/// Plugin descriptor to check
/// Group
/// true - available; false - no
protected virtual bool CheckGroup(PluginDescriptor pluginDescriptor, string group)
{
if (pluginDescriptor == null)
throw new ArgumentNullException("pluginDescriptor");
if (String.IsNullOrEmpty(group))
return true;
return group.Equals(pluginDescriptor.Group, StringComparison.InvariantCultureIgnoreCase);
}
#endregion
#region Methods
///
/// Gets plugin groups
///
/// Plugins groups
public virtual IEnumerable GetPluginGroups()
{
return GetPluginDescriptors(LoadPluginsMode.All).Select(x => x.Group).Distinct().OrderBy(x => x);
}
///
/// Gets plugins
///
/// The type of plugins to get.
/// Load plugins mode
/// Load records allowed only in a specified store; pass 0 to load all records
/// Filter by plugin group; pass null to load all records
/// Plugins
public virtual IEnumerable GetPlugins(LoadPluginsMode loadMode = LoadPluginsMode.InstalledOnly,
int storeId = 0, string group = null) where T : class, IPlugin
{
return GetPluginDescriptors(loadMode, storeId, group).Select(p => p.Instance());
}
///
/// Get plugin descriptors
///
/// Load plugins mode
/// Load records allowed only in a specified store; pass 0 to load all records
/// Filter by plugin group; pass null to load all records
/// Plugin descriptors
public virtual IEnumerable GetPluginDescriptors(LoadPluginsMode loadMode = LoadPluginsMode.InstalledOnly,
int storeId = 0, string group = null)
{
//ensure plugins are loaded
EnsurePluginsAreLoaded();
return _plugins.Where(p => CheckLoadMode(p, loadMode) && CheckGroup(p, group));
}
///
/// Get plugin descriptors
///
/// The type of plugin to get.
/// Load plugins mode
/// Load records allowed only in a specified store; pass 0 to load all records
/// Filter by plugin group; pass null to load all records
/// Plugin descriptors
public virtual IEnumerable GetPluginDescriptors(LoadPluginsMode loadMode = LoadPluginsMode.InstalledOnly,
int storeId = 0, string group = null)
where T : class, IPlugin
{
return GetPluginDescriptors(loadMode, storeId, group)
.Where(p => typeof(T).IsAssignableFrom(p.PluginType));
}
///
/// Get a plugin descriptor by its system name
///
/// Plugin system name
/// Load plugins mode
/// >Plugin descriptor
public virtual PluginDescriptor GetPluginDescriptorBySystemName(string systemName, LoadPluginsMode loadMode = LoadPluginsMode.InstalledOnly)
{
return GetPluginDescriptors(loadMode)
.SingleOrDefault(p => p.SystemName.Equals(systemName, StringComparison.InvariantCultureIgnoreCase));
}
///
/// Get a plugin descriptor by its system name
///
/// The type of plugin to get.
/// Plugin system name
/// Load plugins mode
/// >Plugin descriptor
public virtual PluginDescriptor GetPluginDescriptorBySystemName(string systemName, LoadPluginsMode loadMode = LoadPluginsMode.InstalledOnly)
where T : class, IPlugin
{
return GetPluginDescriptors(loadMode)
.SingleOrDefault(p => p.SystemName.Equals(systemName, StringComparison.InvariantCultureIgnoreCase));
}
///
/// Reload plugins
///
public virtual void ReloadPlugins()
{
_arePluginsLoaded = false;
EnsurePluginsAreLoaded();
}
#endregion
}
}