2020-01-03 12:49:24 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
2017-11-26 03:51:37 +00:00
|
|
|
using NLog;
|
2020-01-03 12:49:24 +00:00
|
|
|
using NzbDrone.Core.ImportLists;
|
2017-11-26 03:51:37 +00:00
|
|
|
using NzbDrone.Core.Lifecycle;
|
|
|
|
using NzbDrone.Core.Messaging.Events;
|
|
|
|
using NzbDrone.Core.Music;
|
2020-02-09 19:15:43 +00:00
|
|
|
using NzbDrone.Core.RootFolders;
|
2017-11-26 03:51:37 +00:00
|
|
|
|
|
|
|
namespace NzbDrone.Core.Profiles.Metadata
|
|
|
|
{
|
|
|
|
public interface IMetadataProfileService
|
|
|
|
{
|
|
|
|
MetadataProfile Add(MetadataProfile profile);
|
|
|
|
void Update(MetadataProfile profile);
|
|
|
|
void Delete(int id);
|
|
|
|
List<MetadataProfile> All();
|
|
|
|
MetadataProfile Get(int id);
|
|
|
|
bool Exists(int id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public class MetadataProfileService : IMetadataProfileService, IHandle<ApplicationStartedEvent>
|
|
|
|
{
|
2019-12-16 21:21:32 +00:00
|
|
|
public const string NONE_PROFILE_NAME = "None";
|
2017-11-26 03:51:37 +00:00
|
|
|
private readonly IMetadataProfileRepository _profileRepository;
|
|
|
|
private readonly IArtistService _artistService;
|
2018-04-08 02:28:16 +00:00
|
|
|
private readonly IImportListFactory _importListFactory;
|
2020-02-09 19:15:43 +00:00
|
|
|
private readonly IRootFolderService _rootFolderService;
|
2017-11-26 03:51:37 +00:00
|
|
|
private readonly Logger _logger;
|
|
|
|
|
2018-04-08 02:28:16 +00:00
|
|
|
public MetadataProfileService(IMetadataProfileRepository profileRepository,
|
|
|
|
IArtistService artistService,
|
|
|
|
IImportListFactory importListFactory,
|
2020-02-09 19:15:43 +00:00
|
|
|
IRootFolderService rootFolderService,
|
2018-04-08 02:28:16 +00:00
|
|
|
Logger logger)
|
2017-11-26 03:51:37 +00:00
|
|
|
{
|
|
|
|
_profileRepository = profileRepository;
|
|
|
|
_artistService = artistService;
|
2018-04-08 02:28:16 +00:00
|
|
|
_importListFactory = importListFactory;
|
2020-02-09 19:15:43 +00:00
|
|
|
_rootFolderService = rootFolderService;
|
2017-11-26 03:51:37 +00:00
|
|
|
_logger = logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
public MetadataProfile Add(MetadataProfile profile)
|
|
|
|
{
|
|
|
|
return _profileRepository.Insert(profile);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Update(MetadataProfile profile)
|
|
|
|
{
|
2019-12-16 21:21:32 +00:00
|
|
|
if (profile.Name == NONE_PROFILE_NAME)
|
|
|
|
{
|
|
|
|
throw new InvalidOperationException("Not permitted to alter None metadata profile");
|
|
|
|
}
|
|
|
|
|
2017-11-26 03:51:37 +00:00
|
|
|
_profileRepository.Update(profile);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Delete(int id)
|
|
|
|
{
|
2019-12-16 21:21:32 +00:00
|
|
|
var profile = _profileRepository.Get(id);
|
|
|
|
|
|
|
|
if (profile.Name == NONE_PROFILE_NAME ||
|
|
|
|
_artistService.GetAllArtists().Any(c => c.MetadataProfileId == id) ||
|
2020-02-09 19:15:43 +00:00
|
|
|
_importListFactory.All().Any(c => c.MetadataProfileId == id) ||
|
|
|
|
_rootFolderService.All().Any(c => c.DefaultMetadataProfileId == id))
|
2017-11-26 03:51:37 +00:00
|
|
|
{
|
2018-02-16 03:03:24 +00:00
|
|
|
throw new MetadataProfileInUseException(profile.Name);
|
2017-11-26 03:51:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
_profileRepository.Delete(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public List<MetadataProfile> All()
|
|
|
|
{
|
|
|
|
return _profileRepository.All().ToList();
|
|
|
|
}
|
|
|
|
|
|
|
|
public MetadataProfile Get(int id)
|
|
|
|
{
|
|
|
|
return _profileRepository.Get(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Exists(int id)
|
|
|
|
{
|
|
|
|
return _profileRepository.Exists(id);
|
|
|
|
}
|
|
|
|
|
2018-03-22 00:43:10 +00:00
|
|
|
private void AddDefaultProfile(string name, List<PrimaryAlbumType> primAllowed, List<SecondaryAlbumType> secAllowed, List<ReleaseStatus> relAllowed)
|
2017-11-26 03:51:37 +00:00
|
|
|
{
|
|
|
|
var primaryTypes = PrimaryAlbumType.All
|
2018-03-22 00:43:10 +00:00
|
|
|
.OrderByDescending(l => l.Name)
|
2020-01-03 12:49:24 +00:00
|
|
|
.Select(v => new ProfilePrimaryAlbumTypeItem { PrimaryAlbumType = v, Allowed = primAllowed.Contains(v) })
|
2018-03-22 00:43:10 +00:00
|
|
|
.ToList();
|
2017-11-26 03:51:37 +00:00
|
|
|
|
|
|
|
var secondaryTypes = SecondaryAlbumType.All
|
2018-03-22 00:43:10 +00:00
|
|
|
.OrderByDescending(l => l.Name)
|
2020-01-03 12:49:24 +00:00
|
|
|
.Select(v => new ProfileSecondaryAlbumTypeItem { SecondaryAlbumType = v, Allowed = secAllowed.Contains(v) })
|
2018-03-22 00:43:10 +00:00
|
|
|
.ToList();
|
|
|
|
|
|
|
|
var releaseStatues = ReleaseStatus.All
|
|
|
|
.OrderByDescending(l => l.Name)
|
2020-01-03 12:49:24 +00:00
|
|
|
.Select(v => new ProfileReleaseStatusItem { ReleaseStatus = v, Allowed = relAllowed.Contains(v) })
|
2018-03-22 00:43:10 +00:00
|
|
|
.ToList();
|
2017-11-26 03:51:37 +00:00
|
|
|
|
|
|
|
var profile = new MetadataProfile
|
|
|
|
{
|
|
|
|
Name = name,
|
|
|
|
PrimaryAlbumTypes = primaryTypes,
|
2018-03-22 00:43:10 +00:00
|
|
|
SecondaryAlbumTypes = secondaryTypes,
|
|
|
|
ReleaseStatuses = releaseStatues
|
2017-11-26 03:51:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Add(profile);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Handle(ApplicationStartedEvent message)
|
|
|
|
{
|
2019-12-16 21:21:32 +00:00
|
|
|
var profiles = All();
|
|
|
|
|
|
|
|
// Name is a unique property
|
|
|
|
var emptyProfile = profiles.FirstOrDefault(x => x.Name == NONE_PROFILE_NAME);
|
|
|
|
|
|
|
|
// make sure empty profile exists and is actually empty
|
|
|
|
if (emptyProfile != null &&
|
|
|
|
!emptyProfile.PrimaryAlbumTypes.Any(x => x.Allowed) &&
|
|
|
|
!emptyProfile.SecondaryAlbumTypes.Any(x => x.Allowed) &&
|
|
|
|
!emptyProfile.ReleaseStatuses.Any(x => x.Allowed))
|
2017-11-26 03:51:37 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-12-16 21:21:32 +00:00
|
|
|
if (!profiles.Any())
|
|
|
|
{
|
|
|
|
_logger.Info("Setting up standard metadata profile");
|
|
|
|
|
2020-01-03 12:49:24 +00:00
|
|
|
AddDefaultProfile("Standard", new List<PrimaryAlbumType> { PrimaryAlbumType.Album }, new List<SecondaryAlbumType> { SecondaryAlbumType.Studio }, new List<ReleaseStatus> { ReleaseStatus.Official });
|
2019-12-16 21:21:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (emptyProfile != null)
|
|
|
|
{
|
|
|
|
// emptyProfile is not the correct empty profile - move it out of the way
|
|
|
|
_logger.Info($"Renaming non-empty metadata profile {emptyProfile.Name}");
|
|
|
|
|
|
|
|
var names = profiles.Select(x => x.Name).ToList();
|
|
|
|
|
|
|
|
int i = 1;
|
|
|
|
emptyProfile.Name = $"{NONE_PROFILE_NAME}.{i}";
|
|
|
|
|
|
|
|
while (names.Contains(emptyProfile.Name))
|
|
|
|
{
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
_profileRepository.Update(emptyProfile);
|
|
|
|
}
|
|
|
|
|
|
|
|
_logger.Info("Setting up empty metadata profile");
|
2017-11-26 03:51:37 +00:00
|
|
|
|
2019-12-16 21:21:32 +00:00
|
|
|
AddDefaultProfile(NONE_PROFILE_NAME, new List<PrimaryAlbumType>(), new List<SecondaryAlbumType>(), new List<ReleaseStatus>());
|
2017-11-26 03:51:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|