forked from Lidarr/Lidarr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ProviderFactory.cs
190 lines (158 loc) · 6.66 KB
/
ProviderFactory.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
using System;
using System.Collections.Generic;
using System.Linq;
using FluentValidation.Results;
using Microsoft.Extensions.DependencyInjection;
using NLog;
using NzbDrone.Common.Composition;
using NzbDrone.Core.Lifecycle;
using NzbDrone.Core.Messaging.Events;
using NzbDrone.Core.ThingiProvider.Events;
namespace NzbDrone.Core.ThingiProvider
{
public abstract class ProviderFactory<TProvider, TProviderDefinition> : IProviderFactory<TProvider, TProviderDefinition>, IHandle<ApplicationStartedEvent>
where TProviderDefinition : ProviderDefinition, new()
where TProvider : IProvider
{
private readonly IProviderRepository<TProviderDefinition> _providerRepository;
private readonly IServiceProvider _container;
private readonly IEventAggregator _eventAggregator;
private readonly Logger _logger;
protected readonly List<TProvider> _providers;
protected ProviderFactory(IProviderRepository<TProviderDefinition> providerRepository,
IEnumerable<TProvider> providers,
IServiceProvider container,
IEventAggregator eventAggregator,
Logger logger)
{
_providerRepository = providerRepository;
_container = container;
_eventAggregator = eventAggregator;
_providers = providers.ToList();
_logger = logger;
}
public List<TProviderDefinition> All()
{
return _providerRepository.All().ToList();
}
public IEnumerable<TProviderDefinition> GetDefaultDefinitions()
{
foreach (var provider in _providers)
{
var definition = provider.DefaultDefinitions
.OfType<TProviderDefinition>()
.FirstOrDefault(v => v.Name == null || v.Name == provider.GetType().Name);
if (definition == null)
{
definition = new TProviderDefinition()
{
Name = string.Empty,
ConfigContract = provider.ConfigContract.Name,
Implementation = provider.GetType().Name,
Settings = (IProviderConfig)Activator.CreateInstance(provider.ConfigContract)
};
}
SetProviderCharacteristics(provider, definition);
yield return definition;
}
}
public IEnumerable<TProviderDefinition> GetPresetDefinitions(TProviderDefinition providerDefinition)
{
var provider = _providers.First(v => v.GetType().Name == providerDefinition.Implementation);
var definitions = provider.DefaultDefinitions
.OfType<TProviderDefinition>()
.Where(v => v.Name != null && v.Name != provider.GetType().Name)
.ToList();
return definitions;
}
public virtual ValidationResult Test(TProviderDefinition definition)
{
return GetInstance(definition).Test();
}
public object RequestAction(TProviderDefinition definition, string action, IDictionary<string, string> query)
{
return GetInstance(definition).RequestAction(action, query);
}
public List<TProvider> GetAvailableProviders()
{
return Active().Select(GetInstance).ToList();
}
public bool Exists(int id)
{
return _providerRepository.Find(id) != null;
}
public TProviderDefinition Get(int id)
{
return _providerRepository.Get(id);
}
public TProviderDefinition Find(int id)
{
return _providerRepository.Find(id);
}
public virtual TProviderDefinition Create(TProviderDefinition definition)
{
var addedDefinition = _providerRepository.Insert(definition);
_eventAggregator.PublishEvent(new ProviderAddedEvent<TProvider>(definition));
return addedDefinition;
}
public virtual void Update(TProviderDefinition definition)
{
_providerRepository.Update(definition);
_eventAggregator.PublishEvent(new ProviderUpdatedEvent<TProvider>(definition));
}
public void Delete(int id)
{
_providerRepository.Delete(id);
_eventAggregator.PublishEvent(new ProviderDeletedEvent<TProvider>(id));
}
public TProvider GetInstance(TProviderDefinition definition)
{
var type = GetImplementation(definition);
var instance = (TProvider)_container.GetRequiredService(type);
instance.Definition = definition;
SetProviderCharacteristics(instance, definition);
return instance;
}
private Type GetImplementation(TProviderDefinition definition)
{
return _providers.Select(c => c.GetType()).SingleOrDefault(c => c.Name.Equals(definition.Implementation, StringComparison.InvariantCultureIgnoreCase));
}
public void Handle(ApplicationStartedEvent message)
{
_logger.Debug("Initializing Providers. Count {0}", _providers.Count);
RemoveMissingImplementations();
InitializeProviders();
}
protected virtual void InitializeProviders()
{
}
protected virtual List<TProviderDefinition> Active()
{
return All().Where(c => c.Settings.Validate().IsValid).ToList();
}
public void SetProviderCharacteristics(TProviderDefinition definition)
{
GetInstance(definition);
}
public virtual void SetProviderCharacteristics(TProvider provider, TProviderDefinition definition)
{
definition.ImplementationName = provider.Name;
definition.Message = provider.Message;
}
//TODO: Remove providers even if the ConfigContract can't be deserialized (this will fail to remove providers if the settings can't be deserialized).
private void RemoveMissingImplementations()
{
var storedProvider = _providerRepository.All();
foreach (var invalidDefinition in storedProvider.Where(def => GetImplementation(def) == null))
{
_logger.Debug("Removing {0} ", invalidDefinition.Name);
_providerRepository.Delete(invalidDefinition);
}
}
public List<TProviderDefinition> AllForTag(int tagId)
{
return All().Where(p => p.Tags.Contains(tagId))
.ToList();
}
}
}