diff options
| author | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-08-07 19:26:57 +0300 |
|---|---|---|
| committer | Roy Ben-Shabat <Roy@Twine-s.com> | 2018-08-07 19:26:57 +0300 |
| commit | 6511527e4b576d29fc97aa80479b61d7d1abc079 (patch) | |
| tree | 9734dc9bc5b6c347c7fc651702419afa37d0d3d9 /Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/MachineSetupViewVM.cs | |
| parent | 3bcad30c7eacb41c134631f79bb50957751f11c7 (diff) | |
| download | Tango-6511527e4b576d29fc97aa80479b61d7d1abc079.tar.gz Tango-6511527e4b576d29fc97aa80479b61d7d1abc079.zip | |
Working on PPC Machine Setup !
Diffstat (limited to 'Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/MachineSetupViewVM.cs')
| -rw-r--r-- | Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/MachineSetupViewVM.cs | 186 |
1 files changed, 186 insertions, 0 deletions
diff --git a/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/MachineSetupViewVM.cs b/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/MachineSetupViewVM.cs new file mode 100644 index 000000000..6be3d8ca0 --- /dev/null +++ b/Software/Visual_Studio/PPC/Tango.PPC.UI/ViewModels/MachineSetupViewVM.cs @@ -0,0 +1,186 @@ +using System; +using System.Collections.Generic; +using System.Data.SqlClient; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Tango.Core; +using Tango.Core.Commands; +using Tango.PPC.Common; +using Tango.PPC.Common.Application; +using Tango.Settings; +using Tango.SQLExaminer; + +namespace Tango.PPC.UI.ViewModels +{ + public class MachineSetupViewVM : PPCViewModel + { + private bool _postSetp; + + private SetupRequiredEventArgs _setupRequiredEventArgs; + + private String _serialNumber; + public String SerialNumber + { + get { return _serialNumber; } + set { _serialNumber = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); } + } + + private String _hostAddress; + public String HostAddress + { + get { return _hostAddress; } + set { _hostAddress = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); } + } + + private String _log; + public String Log + { + get { return _log; } + set { _log = value; RaisePropertyChangedAuto(); } + } + + private bool _isWorking; + public bool IsWorking + { + get { return _isWorking; } + set { _isWorking = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); } + } + + public RelayCommand StartCommand { get; set; } + + public MachineSetupViewVM(IPPCApplicationManager applicationManager) + { + HostAddress = "localhost\\SQLEXPRESS"; + SerialNumber = "1111"; + + AppendLog("Ready to start..."); + StartCommand = new RelayCommand(StartSetup, () => !String.IsNullOrWhiteSpace(HostAddress) && !String.IsNullOrWhiteSpace(SerialNumber) && !IsWorking); + + applicationManager.SetupRequired += ApplicationManager_SetupRequired; + } + + private void ApplicationManager_SetupRequired(object sender, SetupRequiredEventArgs e) + { + _setupRequiredEventArgs = e; + } + + public override void OnApplicationStarted() + { + if (_postSetp) + { + NavigationManager.NavigateTo(Common.Navigation.NavigationView.LoginView); + } + } + + public override void OnNavigatedTo() + { + base.OnNavigatedTo(); + } + + private void AppendLog(String msg) + { + Log += msg + Environment.NewLine; + } + + private void StartSetup() + { + IsWorking = true; + + Task.Factory.StartNew(() => + { + try + { + String db_name = "Tango"; + + String localAddress = SettingsManager.Default.GetOrCreate<CoreSettings>().DataBaseSource; + + var tempFolder = TemporaryManager.CreateFolder("Machine Setup"); + String report_file = tempFolder.CreateImaginaryFile(".xml"); + String config_file = tempFolder.CreateImaginaryFile(".xml"); + + DbManager db = new DbManager(new SqlConnection(String.Format("Server={0};Integrated security=SSPI", localAddress))); + + if (!db.Exists("Tango")) + { + throw new InvalidProgramException("Database tango does not exists."); + } + + //Create schema configuration + ExaminerConfigurationBuilder builder = new ExaminerConfigurationBuilder(ExaminerConfigurationType.Schema); + + builder. + SetSourceServer(HostAddress, db_name). + SetTargetServer(localAddress, db_name). + Synchronize(); + + //Synchronize Source schema with Target schema + + var process = new ExaminerProcess(builder.Build(), ExaminerProcessType.Schema); + process.Progress += (x, msg) => + { + AppendLog(msg); + }; + var result = process.Execute().Result; + + //Synchronization was successful + if (result.ExitCode != ExaminerProcessExitCode.Success) + { + throw new InvalidProgramException("Error while trying to synchronize database schema."); + } + + //Create override data configuration + builder = new ExaminerConfigurationBuilder(ExaminerConfigurationType.OverrideData); + + builder. + SetSourceServer(HostAddress, db_name). + SetTargetServer(localAddress, db_name). + Synchronize(); + + process = new ExaminerProcess(builder.Build(), ExaminerProcessType.Data); + process.Progress += (x, msg) => + { + AppendLog(msg); + }; + result = process.Execute().Result; + + //Synchronization was successful + if (result.ExitCode != ExaminerProcessExitCode.Success) + { + throw new InvalidProgramException("Error while trying to synchronize database schema."); + } + + //Provision Target + builder = new ExaminerConfigurationBuilder(ExaminerConfigurationType.ProvisionMachine); + + builder. + SetSourceServer(HostAddress, db_name). + SetTargetServer(localAddress, db_name). + SetMachineSerialNumber(SerialNumber). + Synchronize(); + + result = new ExaminerProcess(builder.Build(), ExaminerProcessType.Data).Execute().Result; + + //Synchronization was successful + if (result.ExitCode != ExaminerProcessExitCode.Success) + { + throw new InvalidProgramException("Error while trying to synchronize database schema."); + } + + Settings.HasSetup = true; + _postSetp = true; + Settings.Save(); + _setupRequiredEventArgs.Continue(); + } + catch (Exception ex) + { + NotificationProvider.ShowError(ex.Message); + } + finally + { + IsWorking = false; + } + }); + } + } +} |
