using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.FSE.BL.CacheEntities;
using Tango.FSE.BL.WindowsRegistry;
using Tango.FSE.Web.Messages;
namespace Tango.FSE.BL.Services
{
public class BugReportingService : FSEServiceBase
{
private const string BUG_INFO_RESPONSES_COLLECTION = "BugInfoResponses";
///
/// Gets the necessary information required in order to initialize a TFS API client.
///
///
public Task GetBugReportingInfo()
{
LogManager.Log($"Retrieving bug reporting information for environment '{Authentication.CurrentEnvironment.Name}'...");
return DataResolver.Builder.New()
.ConfigureCascade(DataResolverNode.Online, DataResolverNode.DiskCache)
.Online((context) =>
{
var response = WebClient.GetBugReportInfo(new BugReportingInfoRequest() { Build = BuildProvider.Build }).Result;
LogManager.Log("Bug reporting information retrieved successfully. Caching bug reporting information on disk...");
try
{
using (var cache = DiskCache.CreateContext())
{
cache.Database.GetCollection(BUG_INFO_RESPONSES_COLLECTION).Upsert(new CachedBugInfoResponse()
{
Response = response,
EnvironmentID = Authentication.CurrentEnvironment.ID
});
}
}
catch (Exception ex)
{
LogManager.Log(ex, "Error caching online bug reporting information response.");
}
try
{
LogManager.Log("Storing bug reporting information (encrypted) on registry...");
RegistryService.Default.SetValue("TFS", CryptographyProvider.Encrypt(response));
}
catch (Exception ex)
{
LogManager.Log(ex, "Error settings bug reporting information on registry.");
}
return response;
})
.DiskCache((context) =>
{
using (var cache = DiskCache.CreateContext())
{
var cachedResponse = cache.Database.GetCollection(BUG_INFO_RESPONSES_COLLECTION).FindOne(x => x.EnvironmentID == Authentication.CurrentEnvironment.ID);
if (cachedResponse == null)
{
throw new KeyNotFoundException($"Could not locate cached bug reporting information for the current environment {Authentication.CurrentEnvironment.Name}.");
}
return cachedResponse.Response;
}
})
.BuildExecuteAsync();
}
}
}