diff options
Diffstat (limited to 'Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Synchronization/ViewModels/RemoteSynchronizationViewVM.cs')
| -rw-r--r-- | Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Synchronization/ViewModels/RemoteSynchronizationViewVM.cs | 175 |
1 files changed, 139 insertions, 36 deletions
diff --git a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Synchronization/ViewModels/RemoteSynchronizationViewVM.cs b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Synchronization/ViewModels/RemoteSynchronizationViewVM.cs index 5709c30b6..e14b0ffb9 100644 --- a/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Synchronization/ViewModels/RemoteSynchronizationViewVM.cs +++ b/Software/Visual_Studio/MachineStudio/Modules/Tango.MachineStudio.Synchronization/ViewModels/RemoteSynchronizationViewVM.cs @@ -2,29 +2,37 @@ using System; using System.Collections.Generic; using System.Collections.ObjectModel; +using System.Data.Entity.Validation; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using Tango.Core.Commands; +using Tango.DAL.Local.DB; +using Tango.DAL.Observables; using Tango.DAL.Remote.DB; +using Tango.Logging; using Tango.MachineStudio.Common.Notifications; +using Tango.MachineStudio.Common.StudioApplication; using Tango.MachineStudio.Synchronization.Navigation; using Tango.Settings; using Tango.SharedUI; using Tango.Synchronization; +using Tango.Synchronization.Local; using Tango.Synchronization.Remote; namespace Tango.MachineStudio.Synchronization.ViewModels { - public class RemoteSynchronizationViewVM : ViewModel + public class RemoteSynchronizationViewVM : ViewModel, IShutdownRequestBlocker { private SyncNavigationManager _navigation; private String _slaveDBFile; - private RemoteDBComparer _comparer; private INotificationProvider _notification; private bool _isWorking; + private RemoteDBComparer _comparer; + private RemoteDB _remoteDB; + private LocalDB _localDB; #region Constructors @@ -43,14 +51,15 @@ namespace Tango.MachineStudio.Synchronization.ViewModels Differences = new ObservableCollection<Diff>(); BrowseSlaveDBCommand = new RelayCommand(BrowseSlaveDB, (x) => !_isWorking); - CompareCommand = new RelayCommand(Compare, (x) => _slaveDBFile != null && !_isWorking); - CommitCommand = new RelayCommand(Commit, (x) => SelectedDifference != null && !_isWorking); - CommitAllCommand = new RelayCommand(CommitAll, (x) => Differences.Count > 0 && !_isWorking); + CompareCommand = new RelayCommand(Compare, (x) => SlaveDBFile != null && !_isWorking && SelectedMachine != null); + CommitCommand = new RelayCommand(Commit, (x) => SelectedDifference != null && !_isWorking && SelectedMachine != null); + CommitAllCommand = new RelayCommand(CommitAll, (x) => Differences.Count > 0 && !_isWorking && SelectedMachine != null); + CleanCommand = new RelayCommand(CleanSlave, (x) => !_isWorking && SlaveDBFile != null); if (File.Exists(SettingsManager.Default.MachineStudio.SynchronizationModule.RemoteSQLiteFile)) { - _slaveDBFile = SettingsManager.Default.MachineStudio.SynchronizationModule.RemoteSQLiteFile; - SlaveDBName = Path.GetFileName(_slaveDBFile); + SlaveDBFile = SettingsManager.Default.MachineStudio.SynchronizationModule.RemoteSQLiteFile; + SlaveDBName = Path.GetFileName(SlaveDBFile); } } @@ -64,6 +73,11 @@ namespace Tango.MachineStudio.Synchronization.ViewModels public RelayCommand BackCommand { get; set; } /// <summary> + /// Gets or sets the browse master database command. + /// </summary> + public RelayCommand BrowseMasterDBCommand { get; set; } + + /// <summary> /// Gets or sets the browse slave database command. /// </summary> public RelayCommand BrowseSlaveDBCommand { get; set; } @@ -82,6 +96,12 @@ namespace Tango.MachineStudio.Synchronization.ViewModels /// Gets or sets the commit all command. /// </summary> public RelayCommand CommitAllCommand { get; set; } + + /// <summary> + /// Gets or sets the clean command. + /// </summary> + public RelayCommand CleanCommand { get; set; } + #endregion #region Properties @@ -116,51 +136,109 @@ namespace Tango.MachineStudio.Synchronization.ViewModels set { _slaveDBName = value; RaisePropertyChanged(nameof(SlaveDBName)); } } + private Machine _selectedMachine; + /// <summary> + /// Gets or sets the selected machine. + /// </summary> + public Machine SelectedMachine + { + get { return _selectedMachine; } + set { _selectedMachine = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); } + } + + /// <summary> + /// Gets or sets the slave database file. + /// </summary> + public String SlaveDBFile + { + get { return _slaveDBFile; } + set { _slaveDBFile = value; RaisePropertyChangedAuto(); } + } #endregion #region Private Methods - private void Compare() + private async void CleanSlave() { - Task.Factory.StartNew(() => + if (_notification.ShowQuestion("Are you sure you want to erase all data on slave database?")) { - using (RemoteDB remoteDB = new RemoteDB(SettingsManager.Default.DataBase.SQLServerAddress, false)) + using (_notification.PushTaskItem("Clearing database...")) { - _comparer = new RemoteDBComparer(remoteDB, new DAL.Local.DB.LocalDB(_slaveDBFile), "1111"); - - using (_notification.PushTaskItem("Comparing Databases...")) + try { - try + _isWorking = true; + await Task.Factory.StartNew(() => { - _isWorking = true; - InvalidateRelayCommands(); - Thread.Sleep(1500); - var diffs = _comparer.Compare(); - Differences = new ObservableCollection<Diff>(diffs); - - if (diffs.Where(x => x.Action != DiffAction.ReplaceTableDataInSlave).Count() > 0) - { - ShowInfo("Found " + Differences.Where(x => x.Action != DiffAction.ReplaceTableDataInSlave).Count() + " differences."); - } - else + SQLiteDataBase localDB = new SQLiteDataBase(SlaveDBFile); + localDB.LoadTables(); + localDB.ClearDataBase(); + try { - ShowInfo("The master and slave databases are synchronized."); + localDB.Dispose(); } + catch{ } + }); + + Differences.Clear(); + } + catch (Exception ex) + { + ShowError(LogManager.Log(ex).Message); + } + finally + { + _isWorking = false; + InvalidateRelayCommands(); + SelectedDifference = null; + } + } + } + } + + private void Compare() + { + Task.Factory.StartNew(() => + { + using (_notification.PushTaskItem("Comparing Databases...")) + { + try + { + if (_comparer != null) + { + _comparer.Dispose(); } - catch (Exception ex) + + _remoteDB = RemoteDB.CreateDefault(); + _localDB = new LocalDB(SlaveDBFile); + _comparer = new RemoteDBComparer(_remoteDB, _localDB, SelectedMachine.SerialNumber); + _isWorking = true; + InvalidateRelayCommands(); + Thread.Sleep(1500); + var diffs = _comparer.Compare(); + Differences = new ObservableCollection<Diff>(diffs); + + if (diffs.Where(x => x.Action != DiffAction.ReplaceTableDataInSlave).Count() > 0) { - ShowError(ex.Message); + ShowInfo("Found " + Differences.Where(x => x.Action != DiffAction.ReplaceTableDataInSlave).Count() + " differences."); } - finally + else { - _isWorking = false; - SelectedDifference = null; - InvalidateRelayCommands(); - - SettingsManager.Default.MachineStudio.SynchronizationModule.RemoteSQLiteFile = _slaveDBFile; - SettingsManager.SaveDefaultSettings(); + ShowInfo("The master and slave databases are synchronized."); } } + catch (Exception ex) + { + ShowError(ex.Message); + } + finally + { + _isWorking = false; + SelectedDifference = null; + InvalidateRelayCommands(); + + SettingsManager.Default.MachineStudio.SynchronizationModule.RemoteSQLiteFile = SlaveDBFile; + SettingsManager.SaveDefaultSettings(); + } } }); } @@ -177,6 +255,8 @@ namespace Tango.MachineStudio.Synchronization.ViewModels InvalidateRelayCommands(); Thread.Sleep(1500); SelectedDifference.Commit(); + _remoteDB.SaveChanges(); + _localDB.SaveChanges(); InvokeUINow(() => Differences.Remove(SelectedDifference)); } @@ -216,6 +296,20 @@ namespace Tango.MachineStudio.Synchronization.ViewModels i--; } } + + _remoteDB.SaveChanges(); + _localDB.SaveChanges(); + } + catch (DbEntityValidationException ex) + { + String message = "The following validation errors occurred while trying to update the database." + Environment.NewLine + Environment.NewLine; + + foreach (var error in ex.EntityValidationErrors.SelectMany(x => x.ValidationErrors).ToList()) + { + message += error.ErrorMessage + Environment.NewLine; + } + + ShowError(message); } catch (Exception ex) { @@ -236,7 +330,7 @@ namespace Tango.MachineStudio.Synchronization.ViewModels String file = BrowseForFilePath(); if (file != null) { - _slaveDBFile = file; + SlaveDBFile = file; SlaveDBName = Path.GetFileName(file); InvalidateRelayCommands(); } @@ -264,6 +358,15 @@ namespace Tango.MachineStudio.Synchronization.ViewModels InvokeUINow(() => _notification.ShowInfo(message)); } + public Task<bool> OnShutdownRequest() + { + if (_comparer != null) + { + _comparer.Dispose(); + } + return Task.FromResult(true); + } + #endregion } } |
