aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/FSE/Tango.FSE.BL/Services/BugReportingService.cs
blob: b6f9ffff8eb3896c8a7bf377200ca2daed74edce (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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";

        /// <summary>
        /// Gets the necessary information required in order to initialize a TFS API client.
        /// </summary>
        /// <returns></returns>
        public Task<BugReportingInfoResponse> GetBugReportingInfo()
        {
            LogManager.Log($"Retrieving bug reporting information for environment '{Authentication.CurrentEnvironment.Name}'...");

            return DataResolver<BugReportingInfoResponse>.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<CachedBugInfoResponse>(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<CachedBugInfoResponse>(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();
        }
    }
}