2013-04-11 02:50:56 +03:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2013-02-21 10:07:34 +03:00
|
|
|
using System.Linq;
|
|
|
|
using NLog;
|
2013-04-24 04:56:00 +03:00
|
|
|
using NzbDrone.Common.Messaging;
|
2013-02-21 19:38:31 +03:00
|
|
|
using NzbDrone.Core.Lifecycle;
|
2013-03-02 21:25:39 +03:00
|
|
|
|
2013-02-21 10:07:34 +03:00
|
|
|
namespace NzbDrone.Core.Indexers
|
|
|
|
{
|
2013-05-02 04:59:09 +03:00
|
|
|
public class Indexer
|
|
|
|
{
|
2013-05-03 02:06:08 +03:00
|
|
|
public int Id { get; set; }
|
2013-05-02 04:59:09 +03:00
|
|
|
public string Name { get; set; }
|
|
|
|
public bool Enable { get; set; }
|
|
|
|
public IIndexerSetting Settings { get; set; }
|
|
|
|
public IIndexer Instance { get; set; }
|
|
|
|
}
|
|
|
|
|
2013-02-21 10:07:34 +03:00
|
|
|
public interface IIndexerService
|
|
|
|
{
|
2013-05-02 04:59:09 +03:00
|
|
|
List<Indexer> All();
|
|
|
|
List<IIndexer> GetAvailableIndexers();
|
|
|
|
Indexer Get(string name);
|
2013-02-21 10:07:34 +03:00
|
|
|
}
|
|
|
|
|
2013-04-10 03:47:04 +03:00
|
|
|
public class IndexerService : IIndexerService, IHandle<ApplicationStartedEvent>
|
2013-02-21 10:07:34 +03:00
|
|
|
{
|
|
|
|
private readonly IIndexerRepository _indexerRepository;
|
2013-02-21 19:38:31 +03:00
|
|
|
private readonly Logger _logger;
|
2013-02-21 10:07:34 +03:00
|
|
|
|
2013-05-03 07:37:08 +03:00
|
|
|
private readonly List<IIndexer> _indexers;
|
2013-02-21 10:07:34 +03:00
|
|
|
|
2013-05-03 07:37:08 +03:00
|
|
|
public IndexerService(IIndexerRepository indexerRepository, IEnumerable<IIndexer> indexers, Logger logger)
|
2013-02-21 10:07:34 +03:00
|
|
|
{
|
|
|
|
_indexerRepository = indexerRepository;
|
2013-02-21 19:38:31 +03:00
|
|
|
_logger = logger;
|
2013-02-21 10:07:34 +03:00
|
|
|
_indexers = indexers.ToList();
|
2013-02-21 19:38:31 +03:00
|
|
|
}
|
|
|
|
|
2013-05-02 04:59:09 +03:00
|
|
|
public List<Indexer> All()
|
2013-02-21 10:07:34 +03:00
|
|
|
{
|
2013-05-02 04:59:09 +03:00
|
|
|
return _indexerRepository.All().Select(ToIndexer).ToList();
|
2013-02-21 10:07:34 +03:00
|
|
|
}
|
|
|
|
|
2013-05-02 04:59:09 +03:00
|
|
|
public List<IIndexer> GetAvailableIndexers()
|
2013-02-21 10:07:34 +03:00
|
|
|
{
|
2013-05-03 02:06:08 +03:00
|
|
|
return All().Where(c => c.Enable && c.Settings.IsValid).Select(c => c.Instance).ToList();
|
2013-05-02 04:59:09 +03:00
|
|
|
}
|
2013-04-07 10:30:37 +03:00
|
|
|
|
2013-05-02 04:59:09 +03:00
|
|
|
public Indexer Get(string name)
|
|
|
|
{
|
|
|
|
return ToIndexer(_indexerRepository.Get(name));
|
2013-02-21 10:07:34 +03:00
|
|
|
}
|
|
|
|
|
2013-05-02 04:59:09 +03:00
|
|
|
|
|
|
|
private Indexer ToIndexer(IndexerDefinition definition)
|
2013-02-21 10:07:34 +03:00
|
|
|
{
|
2013-05-02 04:59:09 +03:00
|
|
|
var indexer = new Indexer();
|
2013-05-03 02:06:08 +03:00
|
|
|
indexer.Id = definition.Id;
|
2013-05-02 04:59:09 +03:00
|
|
|
indexer.Enable = definition.Enable;
|
|
|
|
indexer.Instance = GetInstance(definition);
|
|
|
|
indexer.Name = definition.Name;
|
|
|
|
|
|
|
|
if (indexer.Instance.GetType().GetMethod("ImportSettingsFromJson") != null)
|
|
|
|
{
|
|
|
|
indexer.Settings = ((dynamic)indexer.Instance).ImportSettingsFromJson(definition.Settings);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
indexer.Settings = NullSetting.Instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
return indexer;
|
2013-02-21 10:07:34 +03:00
|
|
|
}
|
|
|
|
|
2013-05-02 04:59:09 +03:00
|
|
|
private IIndexer GetInstance(IndexerDefinition indexerDefinition)
|
2013-02-21 10:07:34 +03:00
|
|
|
{
|
2013-05-03 07:37:08 +03:00
|
|
|
var type = _indexers.Single(c => c.GetType().Name.Equals(indexerDefinition.Implementation, StringComparison.InvariantCultureIgnoreCase)).GetType();
|
|
|
|
|
|
|
|
var instance = (IIndexer)Activator.CreateInstance(type);
|
|
|
|
|
2013-05-02 04:59:09 +03:00
|
|
|
instance.InstanceDefinition = indexerDefinition;
|
|
|
|
return instance;
|
2013-02-21 10:07:34 +03:00
|
|
|
}
|
2013-04-10 03:47:04 +03:00
|
|
|
|
|
|
|
public void Handle(ApplicationStartedEvent message)
|
|
|
|
{
|
|
|
|
_logger.Debug("Initializing indexers. Count {0}", _indexers.Count);
|
|
|
|
|
2013-05-02 04:59:09 +03:00
|
|
|
if (!All().Any())
|
2013-04-10 03:47:04 +03:00
|
|
|
{
|
2013-05-03 07:37:08 +03:00
|
|
|
var definitions = _indexers.SelectMany(indexer => indexer.DefaultDefinitions);
|
2013-05-02 04:59:09 +03:00
|
|
|
_indexerRepository.InsertMany(definitions.ToList());
|
2013-04-10 03:47:04 +03:00
|
|
|
}
|
|
|
|
}
|
2013-02-21 10:07:34 +03:00
|
|
|
}
|
|
|
|
}
|