aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/PPC/Tango.PPC.UI/Views/LayoutView.xaml.cs
blob: 883d3f893cbeeb2cf38ec5a667980c5512934b38 (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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;
using Tango.Core.EventArguments;
using Tango.Logging;
using Tango.PPC.Common;
using Tango.PPC.UI.ViewModels;
using Tango.PPC.UI.ViewsContracts;

namespace Tango.PPC.UI.Views
{
    /// <summary>
    /// Interaction logic for LayoutView.xaml
    /// </summary>
    public partial class LayoutView : UserControl, ILayoutView
    {
        public static LayoutView Instance { get; private set; }
        private LayoutViewVM _vm;
        private DispatcherTimer _timer;

        public LayoutView()
        {
            InitializeComponent();
            Instance = this;
            Loaded += (_, __) => _vm = DataContext as LayoutViewVM;
            techPressElement.RegisterForPreviewMouseOrTouchDown(OnMouseOrTouchDown);
            techPressElement.RegisterForPreviewMouseOrTouchUp(OnMouseOrTouchUp);
            _timer = new DispatcherTimer();
            _timer.Interval = TimeSpan.FromSeconds(10);
            _timer.Tick += _timer_Tick;

            this.PreviewMouseUp += LayoutView_PreviewMouseUp;
        }

        private void LayoutView_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            _vm.ApplicationManager.ResetScreenLockTimer();
        }

        private void _timer_Tick(object sender, EventArgs e)
        {
            _timer.Stop();
            _vm.ToggleTechnicianMode();
        }

        private void OnMouseOrTouchDown(object sender, MouseOrTouchEventArgs e)
        {
            //if (e.TouchDevice != null)
            //{
            //    techPressElement.CaptureTouch(e.TouchDevice);
            //}
            //else
            //{
            //    techPressElement.CaptureMouse();
            //}

            _timer.Start();
        }

        private void OnMouseOrTouchUp(object sender, MouseOrTouchEventArgs e)
        {
            //if (e.TouchDevice != null)
            //{
            //    techPressElement.ReleaseTouchCapture(e.TouchDevice);
            //}
            //else
            //{
            //    techPressElement.ReleaseMouseCapture();
            //}

            _timer.Stop();
        }

        private void Grid_PreviewMouseUp(object sender, MouseButtonEventArgs e)
        {
            _vm.ApplicationManager.ReleaseScreenLock();
        }
    }
}
span class="p">.GetFileName(MasterSQL.Source) + " <=> " + Path.GetFileName(SlaveSQL.Source))); OnProgress(LogManager.Log("Loading master tables...")); MasterSQL.LoadTables(); OnProgress(LogManager.Log("Loading slave tables...")); SlaveSQL.LoadTables(); List<Diff> diffs = new List<Diff>(); DataTable sync_table = MasterSQL.Tables.Single(x => x.TableName == Constants.SYNC_CONFIGURATIONS_TABLE_NAME); List<DataTable> sync_tables = new List<DataTable>(); List<DataTable> overwrite_tables = new List<DataTable>(); foreach (var row in sync_table.AsEnumerable()) { SyncConfiguration config = (SyncConfiguration)row.Field<Int64>(Constants.SYNC_CONFIGURATION_SYNC_TYPE_COLUMN); if (config == SyncConfiguration.Synchronize || config == SyncConfiguration.SynchronizeToRemote) { sync_tables.Add(MasterSQL.Tables.Single(x => x.TableName == row.Field<String>(Constants.SYNC_CONFIGURATION_TABLE_NAME_COLUMN))); } else { overwrite_tables.Add(MasterSQL.Tables.Single(x => x.TableName == row.Field<String>(Constants.SYNC_CONFIGURATION_TABLE_NAME_COLUMN))); } } foreach (var masterTable in overwrite_tables) { OnProgress(LogManager.Log("Generating table overwrite difference for table " + masterTable.TableName)); var slaveTable = SlaveSQL.Tables.SingleOrDefault(x => x.TableName == masterTable.TableName); //Get matching slave table on slave db. if (slaveTable == null) //Table not found on slave. { OnProgress(LogManager.Log("Table not found on slave, adding difference.")); //Clone table from slave db to master db including records! diffs.Add(new Diff(DiffAction.AddTableToSlave, String.Format("Add table {0} to slave", masterTable.TableName), () => SlaveSQL.CloneTableFrom(MasterSQL, masterTable), SlaveSQL.GetCloneTableFromCommand(MasterSQL, masterTable))); continue; } foreach (DataColumn masterColumn in masterTable.Columns) { OnProgress(LogManager.Log("Searching for column " + masterColumn.ColumnName + " on slave...")); var slaveColumn = slaveTable.Columns[masterColumn.ColumnName]; //Get matching slave column on slave table. if (slaveColumn == null) //Slave column not found. { OnProgress(LogManager.Log("Column not found on slave, adding difference.")); //Add column to slave table. diffs.Add(new Diff(DiffAction.AddColumnToSlave, String.Format("Add column {0} to slave table", masterColumn.ColumnName), () => SlaveSQL.AddColumn(masterTable, masterColumn), SlaveSQL.GetAddColumnCommand(masterTable, masterColumn))); } OnProgress(LogManager.Log("Column found.")); } diffs.Add(new Diff(DiffAction.ReplaceTableDataInSlave, "Replace all rows on slave table " + masterTable.TableName, () => { SlaveSQL.ReplaceTableData(MasterSQL, masterTable); }, SlaveSQL.GetReplaceTableDataCommand(MasterSQL, masterTable))); } foreach (var masterTable in sync_tables) { OnProgress(LogManager.Log("Comparing table " + masterTable.TableName)); OnProgress(LogManager.Log("Searching table " + masterTable.TableName + " on slave...")); var slaveTable = SlaveSQL.Tables.SingleOrDefault(x => x.TableName == masterTable.TableName); //Get matching slave table on slave db. if (slaveTable == null) //Table not found on slave. { OnProgress(LogManager.Log("Table not found on slave, adding difference.")); //Clone table from slave db to master db including records! diffs.Add(new Diff(DiffAction.AddTableToSlave, String.Format("Add table {0} to slave", masterTable.TableName), () => SlaveSQL.CloneTableFrom(MasterSQL, masterTable), SlaveSQL.GetCloneTableFromCommand(MasterSQL, masterTable))); continue; } OnProgress(LogManager.Log("Table found, comparing columns...")); foreach (DataColumn masterColumn in masterTable.Columns) { OnProgress(LogManager.Log("Searching for column " + masterColumn.ColumnName + " on slave...")); var slaveColumn = slaveTable.Columns[masterColumn.ColumnName]; //Get matching slave column on slave table. if (slaveColumn == null) //Slave column not found. { OnProgress(LogManager.Log("Column not found on slave, adding difference.")); //Add column to slave table. diffs.Add(new Diff(DiffAction.AddColumnToSlave, String.Format("Add column {0} to slave table", masterColumn.ColumnName), () => SlaveSQL.AddColumn(masterTable, masterColumn), SlaveSQL.GetAddColumnCommand(masterTable, masterColumn))); } OnProgress(LogManager.Log("Column found.")); } List<DataRow> addToSlave = new List<DataRow>(); List<DataRow> updateSlave = new List<DataRow>(); List<DataRow> addToMaster = new List<DataRow>(); List<DataRow> updateMaster = new List<DataRow>(); OnProgress(LogManager.Log("Comparing rows...")); int count = 0; foreach (DataRow masterRow in masterTable.Rows) { OnProgress(LogManager.Log("Comparing row " + count++)); String guid = masterRow.Field<String>(Constants.GUID); OnProgress(LogManager.Log("Searching for row with GUID " + guid + " on slave table...")); //Get Matching slave row. DataRow slaveRow = slaveTable.AsEnumerable().SingleOrDefault(x => x.Field<String>(Constants.GUID) == guid); if (slaveRow != null) { OnProgress(LogManager.Log("Slave row found, comparing dates...")); DateTime masterDate = masterRow.Field<DateTime>(Constants.LAST_UPDATED); DateTime slaveDate = slaveRow.Field<DateTime>(Constants.LAST_UPDATED); if (masterDate > slaveDate) { OnProgress(LogManager.Log("Master => Slave Update " + masterDate.ToSQLiteDateString() + ", adding difference.")); updateSlave.Add(masterRow); } else if (slaveDate > masterDate) { OnProgress(LogManager.Log("Master <= Slave Update " + masterDate.ToSQLiteDateString() + ", adding difference.")); updateMaster.Add(slaveRow); } else { OnProgress(LogManager.Log("Master <=> Slave No Update.")); } } else { OnProgress(LogManager.Log("Slave row not found, adding difference.")); addToSlave.Add(masterRow); } } OnProgress(LogManager.Log("Done comparing rows...")); OnProgress(LogManager.Log("Searching for missing rows on master...")); foreach (DataRow slaveRow in slaveTable.Rows) { String guid = slaveRow.Field<String>(Constants.GUID); OnProgress(LogManager.Log("Searching for row with GUID " + guid + " on master table...")); //Get Matching slave row. DataRow masterRow = masterTable.AsEnumerable().SingleOrDefault(x => x.Field<String>(Constants.GUID) == guid); if (masterRow == null) { OnProgress(LogManager.Log("Master row not found, adding difference.")); addToMaster.Add(slaveRow); } else { OnProgress(LogManager.Log("Master row found.")); } } OnProgress(LogManager.Log("Done searching for missing rows on master...")); foreach (var row in addToSlave) { diffs.Add(new Diff(DiffAction.AddRowToSlave, String.Format("Add row to slave table {0}", slaveTable.TableName), () => SlaveSQL.AddRow(slaveTable, row), SlaveSQL.GetAddRowCommand(slaveTable, row))); } foreach (var row in addToMaster) { diffs.Add(new Diff(DiffAction.AddRowToMaster, String.Format("Add row to master table {0}", masterTable.TableName), () => MasterSQL.AddRow(masterTable, row), MasterSQL.GetAddRowCommand(masterTable, row))); } foreach (var row in updateSlave) { diffs.Add(new Diff(DiffAction.UpdateRowInSlave, String.Format("Update row in slave table {0}", slaveTable.TableName), () => SlaveSQL.UpdateRow(slaveTable, row), SlaveSQL.GetUpdateRowCommand(slaveTable, row))); } foreach (var row in updateMaster) { diffs.Add(new Diff(DiffAction.UpdateRowInMaster, String.Format("Update row in master table {0}", masterTable.TableName), () => MasterSQL.UpdateRow(masterTable, row), MasterSQL.GetUpdateRowCommand(masterTable, row))); } OnProgress(LogManager.Log("Done comparing table " + masterTable.TableName)); } OnProgress(LogManager.Log("Databases comparison completed.")); return diffs; } protected virtual void OnProgress(String message) { Progress?.Invoke(this, message); } /// <summary> /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. /// </summary> public void Dispose() { MasterSQL.Dispose(); SlaveSQL.Dispose(); } } }