aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.Synchronization/Remote/RemoteDBSynchronizer.cs
blob: 778a24b865b023e12b48ff668be9938f708c88cb (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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using remote = Tango.DAL.Remote.DB;
using local = Tango.DAL.Local.DB;
using Tango.Settings;
using Tango.Synchronization.Local;
using Tango.Core;

namespace Tango.Synchronization.Remote
{
    /// <summary>
    /// Represents a remote database to SQLite file synchronizer.
    /// </summary>
    public class RemoteDBSynchronizer
    {
        /// <summary>
        /// Synchronizes the specified SQLite file by a machine serial number.
        /// </summary>
        /// <param name="sqliteDbFile">The SQLite database file.</param>
        /// <param name="serialNumber">The machine serial number.</param>
        /// <param name="overrideLocal">if set to <c>true</c> will erase the SQLite file before performing the synchronization.</param>
        /// <returns></returns>
        public static List<Diff> Synchronize(String sqliteDbFile, String serialNumber, bool overrideLocal = false)
        {
            if (overrideLocal)
            {
                SQLiteDataBase sqlite = new SQLiteDataBase(sqliteDbFile);
                sqlite.LoadTables();
                sqlite.ClearDataBase();
            }

            using (var remoteDB = new remote.RemoteDB(SettingsManager.Default.GetOrCreate<CoreSettings>().DataSource))
            {
                using (var localDB = new local.LocalDB(sqliteDbFile))
                {
                    RemoteDBComparer comparer = new RemoteDBComparer(remoteDB, localDB, serialNumber);
                    var diffs = comparer.Compare();

                    foreach (var diff in diffs)
                    {
                        diff.Commit();
                    }

                    remoteDB.SaveChanges();
                    localDB.SaveChanges();

                    return diffs;
                }
            }
        }

        /// <summary>
        /// Synchronizes the specified SQLite file by a machine serial number.
        /// </summary>
        /// <param name="sqliteDbFile">The SQLite database file.</param>
        /// <param name="serialNumber">The machine serial number.</param>
        /// <param name="overrideLocal">if set to <c>true</c> will erase the SQLite file before performing the synchronization.</param>
        /// <returns></returns>
        public static Task<List<Diff>> SynchronizeAsync(String sqliteDBFile, String serialNumber, bool overrideLocal = false)
        {
            return Task.Factory.StartNew<List<Diff>>(() =>
            {
                return Synchronize(sqliteDBFile, serialNumber, overrideLocal);
            });
        }
    }
}