From 4046d356047c981f5c446f614e8e2723dba1b6fc Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Fri, 20 Sep 2013 23:39:26 -0700 Subject: [PATCH] can now read/write provider config to db. --- .../ThingiProvider/ProviderBaseFixture.cs | 24 ++++--- .../Converters/EmbeddedDocumentConverter.cs | 5 +- ...2_move_notification_to_generic_provider.cs | 68 +++++++++++++++++++ NzbDrone.Core/Datastore/TableMapping.cs | 18 ++++- .../Notifications/Email/EmailSettings.cs | 3 +- .../Notifications/Growl/GrowlSettings.cs | 3 +- .../Notifications/INotifcationSettings.cs | 7 -- NzbDrone.Core/Notifications/Notification.cs | 6 +- .../Notifications/NotificationBase.cs | 3 +- .../Notifications/NotificationDefinition.cs | 8 +++ .../Notifications/NotificationService.cs | 3 +- .../NotificationSettingsProvider.cs | 5 +- .../NotifyMyAndroidSettings.cs | 3 +- .../Notifications/Plex/PlexClientSettings.cs | 3 +- .../Notifications/Plex/PlexServerSettings.cs | 3 +- .../Notifications/Prowl/ProwlSettings.cs | 3 +- .../PushBullet/PushBulletSettings.cs | 3 +- .../Pushover/PushoverSettings.cs | 3 +- .../Notifications/Xbmc/XbmcSettings.cs | 3 +- NzbDrone.Core/NzbDrone.Core.csproj | 2 +- NzbDrone.Core/ThingiProvider/ProviderBase.cs | 41 ++++++----- 21 files changed, 167 insertions(+), 50 deletions(-) create mode 100644 NzbDrone.Core/Datastore/Migration/022_move_notification_to_generic_provider.cs delete mode 100644 NzbDrone.Core/Notifications/INotifcationSettings.cs diff --git a/NzbDrone.Core.Test/ThingiProvider/ProviderBaseFixture.cs b/NzbDrone.Core.Test/ThingiProvider/ProviderBaseFixture.cs index 11006aa42..2d9cb0f9e 100644 --- a/NzbDrone.Core.Test/ThingiProvider/ProviderBaseFixture.cs +++ b/NzbDrone.Core.Test/ThingiProvider/ProviderBaseFixture.cs @@ -1,23 +1,31 @@ -using NUnit.Framework; -using NzbDrone.Core.Test.Datastore; +using FizzWare.NBuilder; +using FluentAssertions; +using NUnit.Framework; +using NzbDrone.Core.Notifications; +using NzbDrone.Core.Notifications.Email; using NzbDrone.Core.Test.Framework; using NzbDrone.Core.ThingiProvider; namespace NzbDrone.Core.Test.ThingiProvider { - public class ProviderRepositoryFixture : DbTest + public class ProviderRepositoryFixture : DbTest { [Test] public void should_read_write_download_provider() { - var model = new DownloadProviderModel(); + var model = Builder.CreateNew().BuildNew(); + var emailSettings = Builder.CreateNew().Build(); + model.Settings = emailSettings; + Subject.Insert(model); - model.Config = new DownloadProviderConfig(); + var storedProvider = Subject.Single(); + + storedProvider.Settings.Should().BeOfType(); - //Subject.Insert(new ) + var storedSetting = (EmailSettings) storedProvider.Settings; + + storedSetting.ShouldHave().AllProperties().EqualTo(emailSettings); } - - } } \ No newline at end of file diff --git a/NzbDrone.Core/Datastore/Converters/EmbeddedDocumentConverter.cs b/NzbDrone.Core/Datastore/Converters/EmbeddedDocumentConverter.cs index cde1c116d..89b2ecea1 100644 --- a/NzbDrone.Core/Datastore/Converters/EmbeddedDocumentConverter.cs +++ b/NzbDrone.Core/Datastore/Converters/EmbeddedDocumentConverter.cs @@ -1,7 +1,9 @@ using System; +using System.Linq; using Marr.Data.Converters; using Marr.Data.Mapping; using NzbDrone.Common.Serializer; +using NzbDrone.Core.ThingiProvider; namespace NzbDrone.Core.Datastore.Converters { @@ -26,7 +28,8 @@ namespace NzbDrone.Core.Datastore.Converters var implementation = context.DataRecord.GetString(ordinal); - var impType = Type.GetType(implementation, true, true); + + var impType = typeof(IProviderConfig).Assembly.GetTypes().Single(c => c.Name == implementation); return Json.Deserialize(stringValue, impType); } diff --git a/NzbDrone.Core/Datastore/Migration/022_move_notification_to_generic_provider.cs b/NzbDrone.Core/Datastore/Migration/022_move_notification_to_generic_provider.cs new file mode 100644 index 000000000..d0210438f --- /dev/null +++ b/NzbDrone.Core/Datastore/Migration/022_move_notification_to_generic_provider.cs @@ -0,0 +1,68 @@ +using System; +using System.Collections.Generic; +using System.Data; +using FluentMigrator; +using NzbDrone.Common.Serializer; +using NzbDrone.Core.Datastore.Migration.Framework; + +namespace NzbDrone.Core.Datastore.Migration +{ + [Migration(22)] + public class move_notification_to_generic_provider : NzbDroneMigrationBase + { + protected override void MainDbUpgrade() + { + Alter.Table("Notifications").AddColumn("ConfigContract").AsString().Nullable(); + + //Execute.WithConnection(ConvertSeasons); + } + + private void ConvertSeasons(IDbConnection conn, IDbTransaction tran) + { + using (IDbCommand allSeriesCmd = conn.CreateCommand()) + { + allSeriesCmd.Transaction = tran; + allSeriesCmd.CommandText = @"SELECT Id FROM Series"; + using (IDataReader allSeriesReader = allSeriesCmd.ExecuteReader()) + { + while (allSeriesReader.Read()) + { + int seriesId = allSeriesReader.GetInt32(0); + var seasons = new List(); + + using (IDbCommand seasonsCmd = conn.CreateCommand()) + { + seasonsCmd.Transaction = tran; + seasonsCmd.CommandText = String.Format(@"SELECT SeasonNumber, Monitored FROM Seasons WHERE SeriesId = {0}", seriesId); + + using (IDataReader seasonReader = seasonsCmd.ExecuteReader()) + { + while (seasonReader.Read()) + { + int seasonNumber = seasonReader.GetInt32(0); + bool monitored = seasonReader.GetBoolean(1); + + if (seasonNumber == 0) + { + monitored = false; + } + + seasons.Add(new { seasonNumber, monitored }); + } + } + } + + using (IDbCommand updateCmd = conn.CreateCommand()) + { + var text = String.Format("UPDATE Series SET Seasons = '{0}' WHERE Id = {1}", seasons.ToJson() , seriesId); + + updateCmd.Transaction = tran; + updateCmd.CommandText = text; + updateCmd.ExecuteNonQuery(); + } + } + } + } + } + } +} diff --git a/NzbDrone.Core/Datastore/TableMapping.cs b/NzbDrone.Core/Datastore/TableMapping.cs index 533b3ab70..3feda3e13 100644 --- a/NzbDrone.Core/Datastore/TableMapping.cs +++ b/NzbDrone.Core/Datastore/TableMapping.cs @@ -3,6 +3,7 @@ using System.Collections.Generic; using System.Linq; using Marr.Data; using Marr.Data.Mapping; +using NzbDrone.Common.Reflection; using NzbDrone.Core.Configuration; using NzbDrone.Core.DataAugmentation.Scene; using NzbDrone.Core.Datastore.Converters; @@ -15,6 +16,7 @@ using NzbDrone.Core.Organizer; using NzbDrone.Core.Qualities; using NzbDrone.Core.RootFolders; using NzbDrone.Core.SeriesStats; +using NzbDrone.Core.ThingiProvider; using NzbDrone.Core.Tv; namespace NzbDrone.Core.Datastore @@ -34,6 +36,7 @@ namespace NzbDrone.Core.Datastore Mapper.Entity().RegisterModel("Indexers"); Mapper.Entity().RegisterModel("ScheduledTasks"); Mapper.Entity().RegisterModel("Notifications"); + Mapper.Entity().RegisterModel("Notifications"); Mapper.Entity().RegisterModel("SceneMappings"); @@ -69,6 +72,7 @@ namespace NzbDrone.Core.Datastore private static void RegisterMappers() { RegisterEmbeddedConverter(); + RegisterProviderSettingConverter(); MapRepository.Instance.RegisterTypeConverter(typeof(Int32), new Int32Converter()); MapRepository.Instance.RegisterTypeConverter(typeof(DateTime), new UtcConverter()); @@ -78,10 +82,20 @@ namespace NzbDrone.Core.Datastore MapRepository.Instance.RegisterTypeConverter(typeof(Dictionary), new EmbeddedDocumentConverter()); } + private static void RegisterProviderSettingConverter() + { + var settingTypes = typeof(IProviderConfig).Assembly.ImplementationsOf(); + + var providerSettingConverter = new ProviderSettingConverter(); + foreach (var embeddedType in settingTypes) + { + MapRepository.Instance.RegisterTypeConverter(embeddedType, providerSettingConverter); + } + } + private static void RegisterEmbeddedConverter() { - var embeddedTypes = typeof(IEmbeddedDocument).Assembly.GetTypes() - .Where(c => c.GetInterfaces().Any(i => i == typeof(IEmbeddedDocument))); + var embeddedTypes = typeof(IEmbeddedDocument).Assembly.ImplementationsOf(); var embeddedConvertor = new EmbeddedDocumentConverter(); diff --git a/NzbDrone.Core/Notifications/Email/EmailSettings.cs b/NzbDrone.Core/Notifications/Email/EmailSettings.cs index 6ee61b6a2..1d678dae9 100644 --- a/NzbDrone.Core/Notifications/Email/EmailSettings.cs +++ b/NzbDrone.Core/Notifications/Email/EmailSettings.cs @@ -1,9 +1,10 @@ using System; using NzbDrone.Core.Annotations; +using NzbDrone.Core.ThingiProvider; namespace NzbDrone.Core.Notifications.Email { - public class EmailSettings : INotifcationSettings + public class EmailSettings : IProviderConfig { public EmailSettings() { diff --git a/NzbDrone.Core/Notifications/Growl/GrowlSettings.cs b/NzbDrone.Core/Notifications/Growl/GrowlSettings.cs index c66060a94..75c3de74a 100644 --- a/NzbDrone.Core/Notifications/Growl/GrowlSettings.cs +++ b/NzbDrone.Core/Notifications/Growl/GrowlSettings.cs @@ -1,9 +1,10 @@ using System; using NzbDrone.Core.Annotations; +using NzbDrone.Core.ThingiProvider; namespace NzbDrone.Core.Notifications.Growl { - public class GrowlSettings : INotifcationSettings + public class GrowlSettings : IProviderConfig { public GrowlSettings() { diff --git a/NzbDrone.Core/Notifications/INotifcationSettings.cs b/NzbDrone.Core/Notifications/INotifcationSettings.cs deleted file mode 100644 index 49e113de4..000000000 --- a/NzbDrone.Core/Notifications/INotifcationSettings.cs +++ /dev/null @@ -1,7 +0,0 @@ -namespace NzbDrone.Core.Notifications -{ - public interface INotifcationSettings - { - bool IsValid { get; } - } -} diff --git a/NzbDrone.Core/Notifications/Notification.cs b/NzbDrone.Core/Notifications/Notification.cs index 09919c958..40275ecf9 100644 --- a/NzbDrone.Core/Notifications/Notification.cs +++ b/NzbDrone.Core/Notifications/Notification.cs @@ -1,4 +1,6 @@ -namespace NzbDrone.Core.Notifications +using NzbDrone.Core.ThingiProvider; + +namespace NzbDrone.Core.Notifications { public class Notification { @@ -8,7 +10,7 @@ public string Link { get; set; } public bool OnGrab { get; set; } public bool OnDownload { get; set; } - public INotifcationSettings Settings { get; set; } + public IProviderConfig Settings { get; set; } public INotification Instance { get; set; } public string Implementation { get; set; } } diff --git a/NzbDrone.Core/Notifications/NotificationBase.cs b/NzbDrone.Core/Notifications/NotificationBase.cs index e8577c978..d121bd44b 100644 --- a/NzbDrone.Core/Notifications/NotificationBase.cs +++ b/NzbDrone.Core/Notifications/NotificationBase.cs @@ -1,9 +1,10 @@ using NzbDrone.Common.Serializer; +using NzbDrone.Core.ThingiProvider; using NzbDrone.Core.Tv; namespace NzbDrone.Core.Notifications { - public abstract class NotificationBase : INotification where TSetting : class, INotifcationSettings, new() + public abstract class NotificationBase : INotification where TSetting : class, IProviderConfig, new() { public abstract string Name { get; } public abstract string ImplementationName { get; } diff --git a/NzbDrone.Core/Notifications/NotificationDefinition.cs b/NzbDrone.Core/Notifications/NotificationDefinition.cs index 7456c7783..15f274dfc 100644 --- a/NzbDrone.Core/Notifications/NotificationDefinition.cs +++ b/NzbDrone.Core/Notifications/NotificationDefinition.cs @@ -1,5 +1,6 @@ using System; using NzbDrone.Core.Datastore; +using NzbDrone.Core.ThingiProvider; namespace NzbDrone.Core.Notifications { @@ -11,4 +12,11 @@ namespace NzbDrone.Core.Notifications public String Settings { get; set; } public String Implementation { get; set; } } + + + public class NotificationProviderModel : Provider + { + public Boolean OnGrab { get; set; } + public Boolean OnDownload { get; set; } + } } \ No newline at end of file diff --git a/NzbDrone.Core/Notifications/NotificationService.cs b/NzbDrone.Core/Notifications/NotificationService.cs index 0553f4669..3febb84e0 100644 --- a/NzbDrone.Core/Notifications/NotificationService.cs +++ b/NzbDrone.Core/Notifications/NotificationService.cs @@ -7,6 +7,7 @@ using NzbDrone.Common.Serializer; using NzbDrone.Core.Download; using NzbDrone.Core.MediaFiles.Events; using NzbDrone.Core.Messaging.Events; +using NzbDrone.Core.ThingiProvider; using NzbDrone.Core.Tv; using Omu.ValueInjecter; @@ -71,7 +72,7 @@ namespace NzbDrone.Core.Notifications var instanceType = newNotification.Instance.GetType(); var baseGenArgs = instanceType.BaseType.GetGenericArguments(); - newNotification.Settings = (INotifcationSettings)Activator.CreateInstance(baseGenArgs[0]); + newNotification.Settings = (IProviderConfig)Activator.CreateInstance(baseGenArgs[0]); newNotification.Implementation = type.Name; notifications.Add(newNotification); diff --git a/NzbDrone.Core/Notifications/NotificationSettingsProvider.cs b/NzbDrone.Core/Notifications/NotificationSettingsProvider.cs index 53f4d8a58..b26afa1fd 100644 --- a/NzbDrone.Core/Notifications/NotificationSettingsProvider.cs +++ b/NzbDrone.Core/Notifications/NotificationSettingsProvider.cs @@ -1,10 +1,11 @@ using NzbDrone.Common.Serializer; +using NzbDrone.Core.ThingiProvider; namespace NzbDrone.Core.Notifications { public interface INotificationSettingsProvider { - TSetting Get(INotification indexer) where TSetting : INotifcationSettings, new(); + TSetting Get(INotification indexer) where TSetting : IProviderConfig, new(); } public class NotificationSettingsProvider : INotificationSettingsProvider @@ -16,7 +17,7 @@ namespace NzbDrone.Core.Notifications _notificationRepository = notificationRepository; } - public TSetting Get(INotification indexer) where TSetting : INotifcationSettings, new() + public TSetting Get(INotification indexer) where TSetting : IProviderConfig, new() { var indexerDef = _notificationRepository.Find(indexer.Name); diff --git a/NzbDrone.Core/Notifications/NotifyMyAndroid/NotifyMyAndroidSettings.cs b/NzbDrone.Core/Notifications/NotifyMyAndroid/NotifyMyAndroidSettings.cs index c8c941239..f9a4da283 100644 --- a/NzbDrone.Core/Notifications/NotifyMyAndroid/NotifyMyAndroidSettings.cs +++ b/NzbDrone.Core/Notifications/NotifyMyAndroid/NotifyMyAndroidSettings.cs @@ -1,9 +1,10 @@ using System; using NzbDrone.Core.Annotations; +using NzbDrone.Core.ThingiProvider; namespace NzbDrone.Core.Notifications.NotifyMyAndroid { - public class NotifyMyAndroidSettings : INotifcationSettings + public class NotifyMyAndroidSettings : IProviderConfig { [FieldDefinition(0, Label = "API Key", HelpLink = "http://www.notifymyandroid.com/")] public String ApiKey { get; set; } diff --git a/NzbDrone.Core/Notifications/Plex/PlexClientSettings.cs b/NzbDrone.Core/Notifications/Plex/PlexClientSettings.cs index 130552e52..95c9a3291 100644 --- a/NzbDrone.Core/Notifications/Plex/PlexClientSettings.cs +++ b/NzbDrone.Core/Notifications/Plex/PlexClientSettings.cs @@ -1,9 +1,10 @@ using System; using NzbDrone.Core.Annotations; +using NzbDrone.Core.ThingiProvider; namespace NzbDrone.Core.Notifications.Plex { - public class PlexClientSettings : INotifcationSettings + public class PlexClientSettings : IProviderConfig { public PlexClientSettings() { diff --git a/NzbDrone.Core/Notifications/Plex/PlexServerSettings.cs b/NzbDrone.Core/Notifications/Plex/PlexServerSettings.cs index 40b05a2c0..9f4e3cc5f 100644 --- a/NzbDrone.Core/Notifications/Plex/PlexServerSettings.cs +++ b/NzbDrone.Core/Notifications/Plex/PlexServerSettings.cs @@ -1,9 +1,10 @@ using System; using NzbDrone.Core.Annotations; +using NzbDrone.Core.ThingiProvider; namespace NzbDrone.Core.Notifications.Plex { - public class PlexServerSettings : INotifcationSettings + public class PlexServerSettings : IProviderConfig { public PlexServerSettings() { diff --git a/NzbDrone.Core/Notifications/Prowl/ProwlSettings.cs b/NzbDrone.Core/Notifications/Prowl/ProwlSettings.cs index f6b9a7603..1b6db74cc 100644 --- a/NzbDrone.Core/Notifications/Prowl/ProwlSettings.cs +++ b/NzbDrone.Core/Notifications/Prowl/ProwlSettings.cs @@ -1,9 +1,10 @@ using System; using NzbDrone.Core.Annotations; +using NzbDrone.Core.ThingiProvider; namespace NzbDrone.Core.Notifications.Prowl { - public class ProwlSettings : INotifcationSettings + public class ProwlSettings : IProviderConfig { [FieldDefinition(0, Label = "API Key", HelpLink = "https://www.prowlapp.com/api_settings.php")] public String ApiKey { get; set; } diff --git a/NzbDrone.Core/Notifications/PushBullet/PushBulletSettings.cs b/NzbDrone.Core/Notifications/PushBullet/PushBulletSettings.cs index f7a492622..cf34b3718 100644 --- a/NzbDrone.Core/Notifications/PushBullet/PushBulletSettings.cs +++ b/NzbDrone.Core/Notifications/PushBullet/PushBulletSettings.cs @@ -1,9 +1,10 @@ using System; using NzbDrone.Core.Annotations; +using NzbDrone.Core.ThingiProvider; namespace NzbDrone.Core.Notifications.PushBullet { - public class PushBulletSettings : INotifcationSettings + public class PushBulletSettings : IProviderConfig { [FieldDefinition(0, Label = "API Key", HelpLink = "https://www.pushbullet.com/")] public String ApiKey { get; set; } diff --git a/NzbDrone.Core/Notifications/Pushover/PushoverSettings.cs b/NzbDrone.Core/Notifications/Pushover/PushoverSettings.cs index 43c7937b6..4a42baac9 100644 --- a/NzbDrone.Core/Notifications/Pushover/PushoverSettings.cs +++ b/NzbDrone.Core/Notifications/Pushover/PushoverSettings.cs @@ -1,9 +1,10 @@ using System; using NzbDrone.Core.Annotations; +using NzbDrone.Core.ThingiProvider; namespace NzbDrone.Core.Notifications.Pushover { - public class PushoverSettings : INotifcationSettings + public class PushoverSettings : IProviderConfig { [FieldDefinition(0, Label = "User Key", HelpLink = "https://pushover.net/")] public String UserKey { get; set; } diff --git a/NzbDrone.Core/Notifications/Xbmc/XbmcSettings.cs b/NzbDrone.Core/Notifications/Xbmc/XbmcSettings.cs index 0a09d9d57..26e114d30 100644 --- a/NzbDrone.Core/Notifications/Xbmc/XbmcSettings.cs +++ b/NzbDrone.Core/Notifications/Xbmc/XbmcSettings.cs @@ -2,10 +2,11 @@ using System.ComponentModel; using Newtonsoft.Json; using NzbDrone.Core.Annotations; +using NzbDrone.Core.ThingiProvider; namespace NzbDrone.Core.Notifications.Xbmc { - public class XbmcSettings : INotifcationSettings + public class XbmcSettings : IProviderConfig { public XbmcSettings() { diff --git a/NzbDrone.Core/NzbDrone.Core.csproj b/NzbDrone.Core/NzbDrone.Core.csproj index 2ab111e2a..049d2cb4c 100644 --- a/NzbDrone.Core/NzbDrone.Core.csproj +++ b/NzbDrone.Core/NzbDrone.Core.csproj @@ -171,6 +171,7 @@ + @@ -370,7 +371,6 @@ - diff --git a/NzbDrone.Core/ThingiProvider/ProviderBase.cs b/NzbDrone.Core/ThingiProvider/ProviderBase.cs index 61035d7de..bf24434d2 100644 --- a/NzbDrone.Core/ThingiProvider/ProviderBase.cs +++ b/NzbDrone.Core/ThingiProvider/ProviderBase.cs @@ -1,6 +1,7 @@  using NzbDrone.Core.Datastore; using NzbDrone.Core.Messaging.Events; +using NzbDrone.Core.Notifications; namespace NzbDrone.Core.ThingiProvider { @@ -14,36 +15,44 @@ namespace NzbDrone.Core.ThingiProvider } } - public class DownloadProviderModel : Provider - { + public class NotificationProviderRepository : BasicRepository + { + public NotificationProviderRepository(IDatabase database, IEventAggregator eventAggregator) + : base(database, eventAggregator) + { + } } - public class DownloadProviderConfig : ProviderSetting + public class DownloadProviderModel : Provider { } - public abstract class Provider : ModelBase - where TSettings : ProviderSetting + public abstract class Provider : ModelBase { + public string Name { get; set; } public string Implementation { get; set; } - public TSettings Config { get; set; } - } - public abstract class ProviderSetting : IEmbeddedDocument - { + public string ConfigContract + { + get + { + if (Settings == null) return null; + return Settings.GetType().Name; + } + set + { + + } + } + public IProviderConfig Settings { get; set; } } - public abstract class ProviderBase where TSettings : ProviderSetting + public interface IProviderConfig { - public TSettings Settings { get; private set; } - - public void LoadSettings(TSettings setting) - { - Settings = setting; - } + bool IsValid { get; } } } \ No newline at end of file