aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.RML/Views/HeadCleaningParametersView.xaml.cs
blob: 8d036efa5cb76670738cafd7a3a445772d623b65 (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
using System;
using System.Collections.Generic;
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.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace Tango.MachineStudio.RML.Views
{
    /// <summary>
    /// Interaction logic for SpoolsView.xaml
    /// </summary>
    public partial class HeadCleaningParametersView : UserControl
    {
        public HeadCleaningParametersView()
        {
            InitializeComponent();
        }
    }
}
ric.Error */ .highlight .gh { color: #333333 } /* Generic.Heading */ .highlight .gi { color: #000000; background-color: #ddffdd } /* Generic.Inserted */ .highlight .go { color: #888888 } /* Generic.Output */ .highlight .gp { color: #555555 } /* Generic.Prompt */ .highlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Logging;

namespace Tango.Synchronization.Local
{
    /// <summary>
    /// Represents an <see cref="ILocalDataBase"/> synchronization engine.
    /// </summary>
    /// <seealso cref="System.IDisposable" />
    public class LocalDBComparer : IDBComparer
    {
        private LogManager LogManager = LogManager.Default;

        /// <summary>
        /// Used to notify about the comparer progress using text messages.
        /// </summary>
        public event EventHandler<string> Progress;

        /// <summary>
        /// Gets the master SQL.
        /// </summary>
        public ILocalDataBase MasterSQL { get; private set; }

        /// <summary>
        /// Gets the slave SQL.
        /// </summary>
        public ILocalDataBase SlaveSQL { get; private set; }

        /// <summary>
        /// Initializes a new instance of the <see cref="LocalDBComparer"/> class.
        /// </summary>
        /// <param name="masterSQL">The master SQL.</param>
        /// <param name="slaveSQL">The slave SQL.</param>
        public LocalDBComparer(ILocalDataBase masterSQL, ILocalDataBase slaveSQL)
        {
            MasterSQL = masterSQL;
            SlaveSQL = slaveSQL;
        }

        /// <summary>
        /// Compares the master and slave SQL data base and returns a collection of <see cref="Diff"/>.
        /// </summary>
        /// <returns></returns>
        public List<Diff> Compare()
        {
            OnProgress(LogManager.Log("Comparing databases " + Path.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();
        }
    }
}