using System; using System.Collections.Generic; using Marr.Data; using Marr.Data.Mapping; using NzbDrone.Common.Reflection; using NzbDrone.Core.Blacklisting; using NzbDrone.Core.Configuration; using NzbDrone.Core.DataAugmentation.Scene; using NzbDrone.Core.Datastore.Converters; using NzbDrone.Core.Datastore.Extensions; using NzbDrone.Core.Download; using NzbDrone.Core.Download.Pending; using NzbDrone.Core.Indexers; using NzbDrone.Core.Instrumentation; using NzbDrone.Core.Jobs; using NzbDrone.Core.MediaFiles; using NzbDrone.Core.Metadata; using NzbDrone.Core.Metadata.Files; using NzbDrone.Core.Profiles.Delay; using NzbDrone.Core.RemotePathMappings; using NzbDrone.Core.Notifications; using NzbDrone.Core.Organizer; using NzbDrone.Core.Parser.Model; using NzbDrone.Core.Profiles; using NzbDrone.Core.Qualities; using NzbDrone.Core.Restrictions; using NzbDrone.Core.RootFolders; using NzbDrone.Core.SeriesStats; using NzbDrone.Core.Tags; using NzbDrone.Core.ThingiProvider; using NzbDrone.Core.Tv; using NzbDrone.Common.Disk; using NzbDrone.Core.Authentication; using NzbDrone.Core.Messaging.Commands; namespace NzbDrone.Core.Datastore { public static class TableMapping { private static readonly FluentMappings Mapper = new FluentMappings(true); public static void Map() { RegisterMappers(); Mapper.Entity().RegisterModel("Config"); Mapper.Entity().RegisterModel("RootFolders").Ignore(r => r.FreeSpace); Mapper.Entity().RegisterModel("ScheduledTasks"); Mapper.Entity().RegisterDefinition("Indexers") .Ignore(i => i.Enable) .Ignore(i => i.Protocol) .Ignore(i => i.SupportsRss) .Ignore(i => i.SupportsSearch); Mapper.Entity().RegisterDefinition("Notifications") .Ignore(i => i.SupportsOnGrab) .Ignore(i => i.SupportsOnDownload) .Ignore(i => i.SupportsOnUpgrade) .Ignore(i => i.SupportsOnRename); Mapper.Entity().RegisterDefinition("Metadata"); Mapper.Entity().RegisterDefinition("DownloadClients") .Ignore(d => d.Protocol); Mapper.Entity().RegisterModel("SceneMappings"); Mapper.Entity().RegisterModel("History") .AutoMapChildModels(); Mapper.Entity().RegisterModel("Series") .Ignore(s => s.RootFolderPath) .Relationship() .HasOne(s => s.Profile, s => s.ProfileId); Mapper.Entity().RegisterModel("EpisodeFiles") .Ignore(f => f.Path) .Relationships.AutoMapICollectionOrComplexProperties() .For("Episodes") .LazyLoad(condition: parent => parent.Id > 0, query: (db, parent) => db.Query().Where(c => c.EpisodeFileId == parent.Id).ToList()) .HasOne(file => file.Series, file => file.SeriesId); Mapper.Entity().RegisterModel("Episodes") .Ignore(e => e.SeriesTitle) .Ignore(e => e.Series) .Ignore(e => e.HasFile) .Relationship() .HasOne(episode => episode.EpisodeFile, episode => episode.EpisodeFileId); Mapper.Entity().RegisterModel("QualityDefinitions") .Ignore(d => d.Weight); Mapper.Entity().RegisterModel("Profiles"); Mapper.Entity().RegisterModel("Logs"); Mapper.Entity().RegisterModel("NamingConfig"); Mapper.Entity().MapResultSet(); Mapper.Entity().RegisterModel("Blacklist"); Mapper.Entity().RegisterModel("MetadataFiles"); Mapper.Entity().RegisterModel("PendingReleases") .Ignore(e => e.RemoteEpisode); Mapper.Entity().RegisterModel("RemotePathMappings"); Mapper.Entity().RegisterModel("Tags"); Mapper.Entity().RegisterModel("Restrictions"); Mapper.Entity().RegisterModel("DelayProfiles"); Mapper.Entity().RegisterModel("Users"); Mapper.Entity().RegisterModel("Commands") .Ignore(c => c.Message); } private static void RegisterMappers() { RegisterEmbeddedConverter(); RegisterProviderSettingConverter(); MapRepository.Instance.RegisterTypeConverter(typeof(Int32), new Int32Converter()); MapRepository.Instance.RegisterTypeConverter(typeof(Double), new DoubleConverter()); MapRepository.Instance.RegisterTypeConverter(typeof(DateTime), new UtcConverter()); MapRepository.Instance.RegisterTypeConverter(typeof(Boolean), new BooleanIntConverter()); MapRepository.Instance.RegisterTypeConverter(typeof(Enum), new EnumIntConverter()); MapRepository.Instance.RegisterTypeConverter(typeof(Quality), new QualityIntConverter()); MapRepository.Instance.RegisterTypeConverter(typeof(List), new EmbeddedDocumentConverter(new QualityIntConverter())); MapRepository.Instance.RegisterTypeConverter(typeof(QualityModel), new EmbeddedDocumentConverter(new QualityIntConverter())); MapRepository.Instance.RegisterTypeConverter(typeof(Dictionary), new EmbeddedDocumentConverter()); MapRepository.Instance.RegisterTypeConverter(typeof(List), new EmbeddedDocumentConverter()); MapRepository.Instance.RegisterTypeConverter(typeof(List), new EmbeddedDocumentConverter()); MapRepository.Instance.RegisterTypeConverter(typeof(ParsedEpisodeInfo), new EmbeddedDocumentConverter()); MapRepository.Instance.RegisterTypeConverter(typeof(ReleaseInfo), new EmbeddedDocumentConverter()); MapRepository.Instance.RegisterTypeConverter(typeof(HashSet), new EmbeddedDocumentConverter()); MapRepository.Instance.RegisterTypeConverter(typeof(OsPath), new OsPathConverter()); MapRepository.Instance.RegisterTypeConverter(typeof(Guid), new GuidConverter()); MapRepository.Instance.RegisterTypeConverter(typeof(Command), new CommandConverter()); MapRepository.Instance.RegisterTypeConverter(typeof(TimeSpan), new TimeSpanConverter()); MapRepository.Instance.RegisterTypeConverter(typeof(TimeSpan?), new TimeSpanConverter()); } 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.ImplementationsOf(); var embeddedConvertor = new EmbeddedDocumentConverter(); var genericListDefinition = typeof(List<>).GetGenericTypeDefinition(); foreach (var embeddedType in embeddedTypes) { var embeddedListType = genericListDefinition.MakeGenericType(embeddedType); MapRepository.Instance.RegisterTypeConverter(embeddedType, embeddedConvertor); MapRepository.Instance.RegisterTypeConverter(embeddedListType, embeddedConvertor); } } } }