diff --git a/VECTO3GUI2020/ViewModel/MultiStage/Implementation/InterimStageBusVehicleViewModel_v2_8.cs b/VECTO3GUI2020/ViewModel/MultiStage/Implementation/InterimStageBusVehicleViewModel_v2_8.cs index 09ccb4d641ca647caacbdb180410c89f27176a3a..6c144e24269b2627dc5ed23d632df3c08f259ed6 100644 --- a/VECTO3GUI2020/ViewModel/MultiStage/Implementation/InterimStageBusVehicleViewModel_v2_8.cs +++ b/VECTO3GUI2020/ViewModel/MultiStage/Implementation/InterimStageBusVehicleViewModel_v2_8.cs @@ -70,7 +70,7 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation } } - public interface IMultistageVehicleViewModel : IVehicleViewModel + public interface IMultistageVehicleViewModel : IVehicleViewModel, INotifyPropertyChanged { bool HasErrors { get; } Dictionary<string, string> Errors { get; } diff --git a/VECTO3GUI2020/ViewModel/MultiStage/Implementation/MultistageJobViewModel_v0_1.cs b/VECTO3GUI2020/ViewModel/MultiStage/Implementation/MultistageJobViewModel_v0_1.cs index ba3b4bce842bf14e80a4e4f01fb6935328344a0b..8222936fa7e91eeaeef19b6b4ea9123bedccbfcb 100644 --- a/VECTO3GUI2020/ViewModel/MultiStage/Implementation/MultistageJobViewModel_v0_1.cs +++ b/VECTO3GUI2020/ViewModel/MultiStage/Implementation/MultistageJobViewModel_v0_1.cs @@ -247,10 +247,10 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation public string VehicleInputDataFilePath { - get => ManufacturingStageViewModel.VehicleInputDataFilePath; + get => ManufacturingStageViewModel.InputDataFilePath; set { - ManufacturingStageViewModel.VehicleInputDataFilePath = value; + ManufacturingStageViewModel.InputDataFilePath = value; OnPropertyChanged(); } } diff --git a/VECTO3GUI2020/ViewModel/MultiStage/Implementation/StageInputViewModel.cs b/VECTO3GUI2020/ViewModel/MultiStage/Implementation/StageInputViewModel.cs index 098b23d75d3e29c3c9c7949cb7afe2a0627cd399..303a0cddd86e961ab4f5173ad7e1babb7d0c6496 100644 --- a/VECTO3GUI2020/ViewModel/MultiStage/Implementation/StageInputViewModel.cs +++ b/VECTO3GUI2020/ViewModel/MultiStage/Implementation/StageInputViewModel.cs @@ -1,5 +1,8 @@ -using System.Diagnostics; +using System; +using System.ComponentModel; +using System.Diagnostics; using System.IO; +using Castle.Core.Smtp; using TUGraz.VectoCommon.InputData; using TUGraz.VectoCore.Models.Declaration; using TUGraz.VectoCore.Utils; @@ -17,7 +20,7 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation public class StageInputViewModel : StageViewModelBase, IDocumentViewModel, IJobEditViewModel { private bool _canBeEdited; - private readonly DataSource _dataSource; + private DataSource _dataSource; private readonly XmlDocumentType _documentType; private readonly string _documentName; private bool _selected; @@ -30,10 +33,10 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation : InterimStageBusVehicleViewModel_v2_8.VERSION) as IMultistageVehicleViewModel; Debug.Assert(_vehicleViewModel != null); + Title = "Edit Stage Input - New File"; Init(); } - public StageInputViewModel(IDeclarationInputDataProvider inputData, IMultiStageViewModelFactory multiStageViewModelFactory) : base(multiStageViewModelFactory) { _documentName = inputData.JobInputData.JobName; @@ -42,13 +45,32 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation (_vehicleViewModel as InterimStageBusVehicleViewModel_v2_8).ShowConsolidatedData = false; _dataSource = inputData.DataSource; _documentType = XmlDocumentType.DeclarationJobData; - Title = $"Edit Stage Input - {Path.GetFileNameWithoutExtension(_dataSource.SourceFile)}"; + Title = $"Edit Stage Input - {Path.GetFileName(_dataSource.SourceFile)}"; Init(); } + #region Overrides of StageViewModelBase + + protected override bool LoadStageInputData(IDeclarationInputDataProvider inputData) + { + base.LoadStageInputData(inputData); + DataSource = inputData.DataSource; + return true; + } + + #endregion + + private void SetTitle() + { + Title = "Edit Stage Input - " + ((_dataSource?.SourceFile != null) + ? Path.GetFileName(_dataSource.SourceFile) + : "New File"); + } + private void Init() { + SetTitle(); Components.Add("vehicle", VehicleViewModel as IViewModelBase); Components.Add("auxiliaries", VehicleViewModel.MultistageAuxiliariesViewModel as IViewModelBase); Components.Add("airdrag", VehicleViewModel.MultistageAirdragViewModel as IViewModelBase); @@ -63,7 +85,15 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation public XmlDocumentType DocumentType => _documentType; - public DataSource DataSource => _dataSource; + public DataSource DataSource + { + get => _dataSource; + set + { + SetProperty(ref _dataSource, value); + SetTitle(); + } + } public IEditViewModel EditViewModel => this; diff --git a/VECTO3GUI2020/ViewModel/MultiStage/Implementation/StageViewModelBase.cs b/VECTO3GUI2020/ViewModel/MultiStage/Implementation/StageViewModelBase.cs index 29bcae28083a12553dc6009590613f0f696822ab..62ba44058a54c7ae02f7bbb323161e909e60ec58 100644 --- a/VECTO3GUI2020/ViewModel/MultiStage/Implementation/StageViewModelBase.cs +++ b/VECTO3GUI2020/ViewModel/MultiStage/Implementation/StageViewModelBase.cs @@ -24,7 +24,7 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation { public interface IStageViewModelBase { - string VehicleInputDataFilePath { get; set; } + string InputDataFilePath { get; set; } IMultistageVehicleViewModel VehicleViewModel { get; set; } ICommand SwitchComponentViewCommand { get; } ICommand SaveInputDataCommand { get; } @@ -36,7 +36,9 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation public class StageViewModelBase : ViewModelBase, IStageViewModelBase { - protected Dictionary<string, IViewModelBase> Components = new Dictionary<string, IViewModelBase>(StringComparer.CurrentCultureIgnoreCase); + protected Dictionary<string, IViewModelBase> Components = + new Dictionary<string, IViewModelBase>(StringComparer.CurrentCultureIgnoreCase); + protected IMultistageVehicleViewModel _vehicleViewModel; protected IMultiStageViewModelFactory _viewModelFactory; private IViewModelBase _currentview; @@ -72,7 +74,9 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation get => _vehicleViewModel; set => SetProperty(ref _vehicleViewModel, value); } + private bool _showSaveAndCloseButtons = false; + public bool ShowSaveAndCloseButtons { get => _showSaveAndCloseButtons; @@ -80,17 +84,20 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation } #region Commands + public ICommand SwitchComponentViewCommand { - get { - return _switchComponentViewCommand ?? new RelayCommand<string>(SwitchViewExecute, (string s) => SwitchViewCanExecute(s)); + get + { + return _switchComponentViewCommand ?? + new RelayCommand<string>(SwitchViewExecute, (string s) => SwitchViewCanExecute(s)); } } private void SwitchViewExecute(string viewToShow) { IViewModelBase newView; - var success= Components.TryGetValue(viewToShow, out newView); + var success = Components.TryGetValue(viewToShow, out newView); if (success) { CurrentView = newView; } @@ -107,14 +114,12 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation private IXMLInputDataReader _inputDataReader; public ICommand SaveInputDataCommand => - _saveInputDataCommand ?? new RelayCommand(() => { - SaveInputDataExecute(filename: _vehicleInputDataFilePath); - }, () => _vehicleInputDataFilePath != null); + _saveInputDataCommand ?? + new RelayCommand(() => { SaveInputDataExecute(filename: _vehicleInputDataFilePath); }, + () => _vehicleInputDataFilePath != null); public ICommand SaveInputDataAsCommand => - _saveInputDataAsCommand ?? new RelayCommand(() => { - SaveInputDataExecute(filename: null); - }, () => true); + _saveInputDataAsCommand ?? new RelayCommand(() => { SaveInputDataExecute(filename: null); }, () => true); private ICommand _loadVehicleDataCommand; @@ -123,23 +128,21 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation public ICommand LoadVehicleDataCommand { - get - { - return _loadVehicleDataCommand ?? new RelayCommand(LoadVehicleDataExecute, () => true); - } + get { return _loadVehicleDataCommand ?? new RelayCommand(LoadVehicleDataExecute, () => true); } } + #endregion Commands #region File I/O + private void LoadVehicleDataExecute() { var fileName = _multistageDependencies.DialogHelper.OpenXMLFileDialog(); - if (fileName == null) - { + if (fileName == null) { return; } - LoadVehicleData(fileName); + LoadStageInputData(fileName); return; } @@ -168,11 +171,9 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation } - if (filename == null) - { + if (filename == null) { filename = dialogHelper.SaveToXMLDialog(Settings.Default.DefaultFilePath); - if (filename == null) - { + if (filename == null) { return; } } @@ -189,65 +190,64 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation var valid = false; var validationError = ""; - try - { + try { var validator = new XMLValidator(xDoc.ToXmlDocument()); valid = validator.ValidateXML(XmlDocumentType.DeclarationJobData); validationError = validator.ValidationError; + } catch (Exception e) { + dialogHelper.ShowMessageBox(messageBoxText: (e.Message + "\n" + e.InnerException), + caption: "Error saving File"); } - catch (Exception e) - { - dialogHelper.ShowMessageBox(messageBoxText: (e.Message + "\n" + e.InnerException), caption: "Error saving File"); - } - if (!valid) - { + + if (!valid) { dialogHelper.ShowMessageBox($"Invalid Document: {validationError}", "Error"); var tempFile = Path.GetTempFileName(); - try - { + try { xDoc.Save(tempFile, SaveOptions.OmitDuplicateNamespaces); - LoadVehicleData(tempFile); - File.Delete(tempFile); - } - catch (Exception e) - { + LoadStageInputData(tempFile); + + } catch (Exception e) { dialogHelper.ShowMessageBox(e.Message, "Error"); throw; + } finally { + if (File.Exists(tempFile)) { + File.Delete(tempFile); + } + + ; } - } - else - { + } else { xDoc.Save(filename, SaveOptions.OmitDuplicateNamespaces); - LoadVehicleData(filename); + LoadStageInputData(filename); } } + protected virtual bool LoadStageInputData(IDeclarationInputDataProvider inputData) + { - - - private bool LoadVehicleData(string fileName) + var vehicleInputData = inputData.JobInputData.Vehicle; + VehicleViewModel.SetVehicleInputData(vehicleInputData); + InputDataFilePath = inputData.DataSource.SourceFile; + return true; + } + protected bool LoadStageInputData(string fileName) { - try { var inputData = (IDeclarationInputDataProvider)_inputDataReader.Create(fileName); - var vehicleInputData = inputData.JobInputData.Vehicle; - VehicleViewModel.SetVehicleInputData(vehicleInputData); - - VehicleInputDataFilePath = fileName; + return LoadStageInputData(inputData); } catch (Exception e) { _multistageDependencies.DialogHelper.ShowMessageBox(e.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error); return false; } - return true; } - public string VehicleInputDataFilePath + public string InputDataFilePath { get => _vehicleInputDataFilePath; set => SetProperty(ref _vehicleInputDataFilePath, value); diff --git a/Vecto3GUI2020Test/TestHelper.cs b/Vecto3GUI2020Test/TestHelper.cs index 0e77911f6efc8c5aab9834450b96316de5be2472..0f800f625c404c350eeaf8e664fe342b627193d3 100644 --- a/Vecto3GUI2020Test/TestHelper.cs +++ b/Vecto3GUI2020Test/TestHelper.cs @@ -1,4 +1,6 @@ -using System.CodeDom; +using System; +using System.CodeDom; +using System.Runtime.CompilerServices; using TUGraz.VectoCommon.InputData; using TUGraz.VectoCore.InputData.FileIO.XML; @@ -20,5 +22,13 @@ namespace Vecto3GUI2020Test { return _inputDataReader.Create(fileName); } + + public static string GetMethodName([CallerMemberName] string name = null) + { + if (name == null) { + throw new ArgumentException(); + } + return name; + } } } \ No newline at end of file diff --git a/Vecto3GUI2020Test/ViewModelTests/StageViewModelTests.cs b/Vecto3GUI2020Test/ViewModelTests/StageViewModelTests.cs new file mode 100644 index 0000000000000000000000000000000000000000..143149ed07d1f8716b68a98d854fc2a6ecff83c8 --- /dev/null +++ b/Vecto3GUI2020Test/ViewModelTests/StageViewModelTests.cs @@ -0,0 +1,44 @@ +using System.Runtime.InteropServices; +using Ninject; +using NUnit.Framework; +using TUGraz.VectoCore.Configuration; +using VECTO3GUI2020.ViewModel.Interfaces.Document; +using VECTO3GUI2020.ViewModel.Interfaces.JobEdit; +using VECTO3GUI2020.ViewModel.MultiStage.Implementation; +using VECTO3GUI2020.ViewModel.MultiStage.Interfaces; + +namespace Vecto3GUI2020Test.ViewModelTests +{ + [TestFixture] + public class StageViewModelTests : ViewModelTestBase + { + + + [TestCase(true, TestName="Exempted")] + [TestCase(false, TestName="NotExempted")] + public void updateFilePathsWhenSavedAs_non_exempted(bool exempted) + { + IMultiStageViewModelFactory vmFactory = _kernel.Get<IMultiStageViewModelFactory>(); + + var StageInput = vmFactory.GetStageInputViewModel(exempted) as StageInputViewModel; + var vehicleVm = StageInput.VehicleViewModel as InterimStageBusVehicleViewModel_v2_8; + vehicleVm.Manufacturer = "adsf"; + vehicleVm.ManufacturerAddress = "asdf 123"; + vehicleVm.VIN = "1234567890"; + + var fileName = TestHelper.GetMethodName() + ".xml"; + StageInput.SaveInputDataExecute(GetFullPath(fileName)); + Assert.True(checkFileNameExists(fileName)); + + Assert.AreEqual(GetFullPath(fileName), StageInput.InputDataFilePath); + + //Check if title is updated + StringAssert.Contains(fileName, StageInput.Title); + + //Check datasource + Assert.NotNull(StageInput.DataSource); + + + } + } +} \ No newline at end of file