aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.DataSynchronizer.CLI/Program.cs
blob: 091ef8f13251bf0b043ffa1fb1c85b1616ae0d7c (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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.BL;
using Tango.SQLExaminer;

namespace Tango.PPC.DataSynchronizer.CLI
{
    class Program
    {
        static void Main(string[] args)
        {
            DataSynchronizer synchronizer = new DataSynchronizer();
            synchronizer.Synchronize();
            Console.ReadLine();
        }
    }

    public class DataSynchronizer
    {
        private Core.DataSource source;
        private Core.DataSource target;
        private String machineGuid;

        public void Synchronize()
        {
            try
            {
                Console.WriteLine("Starting PPC data synchronization...");

                source = new Core.DataSource();
                source.IntegratedSecurity = false;
                source.Address = "twine.database.windows.net";
                source.Catalog = "Tango_DEV";
                source.UserName = "Roy";
                source.Password = "Aa123456";

                using (ObservablesContext db = ObservablesContext.CreateDefault(source))
                {
                    machineGuid = db.Machines.Where(x => x.SerialNumber == "LENA_TABLET").Take(1).Select(x => x.Guid).First();
                }

                target = new Core.DataSource();
                target.Address = "localhost\\SQLPPC";
                target.Catalog = "Tango";

                OverrideData();
                UpdateMachine();

                Console.ForegroundColor = ConsoleColor.Green;
                Console.WriteLine("Synchronization Completed Successfully.");
            }
            catch (Exception ex)
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine(ex.ToString());
            }
        }

        private void OverrideData()
        {
            Console.WriteLine("Executing override data script...");

            ExaminerConfigurationBuilder builder = new ExaminerConfigurationBuilder(ExaminerConfigurationType.OverrideData);
            builder.SetSource(source).SetTarget(target).Synchronize();
            var process = new ExaminerProcess(builder.Build(), ExaminerProcessType.Data);
            process.Progress += (x, msg) =>
            {
                if (msg != null && !msg.Contains("SQL Examiner"))
                {
                    Console.WriteLine(msg);
                }
            };
            var result = process.Execute().Result;

            if (result.ExitCode != ExaminerProcessExitCode.Success)
            {
                throw new InvalidOperationException(result.Output);
            }
        }

        private void UpdateMachine()
        {
            Console.WriteLine("Executing update machine script...");

            ExaminerConfigurationBuilder builder = new ExaminerConfigurationBuilder(ExaminerConfigurationType.UpdateMachine);
            builder.SetSource(source).SetTarget(target).SetMachineSerialNumber(machineGuid).Synchronize();
            var process = new ExaminerProcess(builder.Build(), ExaminerProcessType.Data);
            process.Progress += (x, msg) =>
            {
                if (msg != null && !msg.Contains("SQL Examiner"))
                {
                    Console.WriteLine(msg);
                }
            };
            var result = process.Execute().Result;

            if (result.ExitCode != ExaminerProcessExitCode.Success)
            {
                throw new InvalidOperationException(result.Output);
            }
        }
    }
}