diff --git a/VECTO3GUI2020/ViewModel/MultiStage/Implementation/CreateVifViewModel.cs b/VECTO3GUI2020/ViewModel/MultiStage/Implementation/CreateVifViewModel.cs index bf0513a2672558780c8af4421c6871c3cd55329b..af393deb13ef309fb82fb0aa9a54eb40a70c1654 100644 --- a/VECTO3GUI2020/ViewModel/MultiStage/Implementation/CreateVifViewModel.cs +++ b/VECTO3GUI2020/ViewModel/MultiStage/Implementation/CreateVifViewModel.cs @@ -1,11 +1,15 @@ using System; +using System.Diagnostics; +using System.IO; using System.Windows; using System.Windows.Input; using InteractiveDataDisplay.WPF; using Microsoft.Toolkit.Mvvm.Input; using Microsoft.WindowsAPICodePack.Shell.Interop; +using Newtonsoft.Json; using TUGraz.VectoCommon.InputData; using TUGraz.VectoCommon.Models; +using TUGraz.VectoCore.InputData.FileIO.JSON; using TUGraz.VectoCore.InputData.FileIO.XML; using TUGraz.VectoCore.InputData.FileIO.XML.Declaration; using TUGraz.VectoCore.InputData.FileIO.XML.Declaration.DataProvider; @@ -26,6 +30,7 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation bool? StageInputExempted { get; set; } string PrimaryInputPath { get; set; } string StageInputPath { get; set; } + void SaveJob(string path); } public class CreateVifViewModel : ViewModelBase, ICreateVifViewModel { @@ -34,7 +39,6 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation private readonly IDialogHelper _dialogHelper; private readonly IXMLInputDataReader _inputDataReader; private static uint _newVifCounter = 0; - private readonly JSONJob _jsonJob; private bool? _exemptedPrimary; @@ -52,9 +56,10 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation set => SetProperty(ref _stageInputExempted, value); } - public CreateVifViewModel(IDialogHelper dialogHelper, IXMLInputDataReader inputDataReader, IAdditionalJobInfoViewModel additionalJobInfo, JSONJob jsonJob) + public CreateVifViewModel(IDialogHelper dialogHelper, + IXMLInputDataReader inputDataReader, + IAdditionalJobInfoViewModel additionalJobInfo) { - _jsonJob = jsonJob; _dialogHelper = dialogHelper; _inputDataReader = inputDataReader; Title = "Create VIF"; @@ -62,6 +67,25 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation _documentName = $"New Vif {++_newVifCounter}"; } + public CreateVifViewModel(IDeclarationInputDataProvider inputData, + IDialogHelper dialogHelper, + IXMLInputDataReader inputDataReader, + IAdditionalJobInfoViewModel additionalJobInfo) : this(dialogHelper, inputDataReader, additionalJobInfo) + { + SetInputData(inputData); + } + + + private void SetInputData(IInputDataProvider inputData) + { + var inputDataProvider = inputData as JSONInputDataV10_PrimaryAndInterimBus; + Debug.Assert(inputDataProvider != null); + DataSource = inputData.DataSource; + Title += $"- {Path.GetFileName(_dataSource.SourceFile)}"; + DocumentName = Path.GetFileNameWithoutExtension(_dataSource.SourceFile); + + } + public string PrimaryInputPath { @@ -109,18 +133,67 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation })); } - private ICommand _saveJobCommand; + private IRelayCommand _saveJobCommand; public ICommand SaveJobCommand { - get => _saveJobCommand ?? (_saveJobCommand = new RelayCommand(() => { return; })); + get => _saveJobCommand ?? (_saveJobCommand = new RelayCommand(() => { + if (_dataSource.SourceFile != null) { + if (CanBeSaved()) { + SaveJob(_dataSource.SourceFile); + } + } + }, () => DataSource != null)); + } + + private bool CanBeSaved() + { + if (_primaryInputPath == null) { + _dialogHelper.ShowMessageBox("At least Primary Vehicle has to be provided", "Info", MessageBoxButton.OK, + MessageBoxImage.Information); + return false; + } + + return true; + } + + public void SaveJob(string path) + { + if (path == null) { + return; + } + + var jsonJob = new JSONJob() { + Header = new JSONJobHeader() { + AppVersion = "Vecto3GUI2020", + CreatedBy = Environment.UserName, + Date = DateTime.Today, + FileVersion = JSONJobHeader.PrimaryAndInterimVersion + }, + Body = new JSONJobBody() { + PrimaryVehicle = PrimaryInputPath, + InterimStage = StageInputPath + } + }; + + string jsonString = JsonConvert.SerializeObject(jsonJob, Formatting.Indented); + + + Debug.WriteLine(jsonString); + File.WriteAllText(path, jsonString); + SetInputData(JSONInputDataFactory.ReadJsonJob(path)); } private ICommand _saveJobAsCommand; public ICommand SaveJobAsCommand { - get => _saveJobAsCommand ?? (_saveJobAsCommand = new RelayCommand(() => { return; })); + get => _saveJobAsCommand ?? (_saveJobAsCommand = new RelayCommand(() => { + if (CanBeSaved()) { + var path = _dialogHelper.SaveToJsonDialog(); + SaveJob(path); + } + })); } public bool LoadStageInput(string fileName) @@ -251,6 +324,8 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation #region Implementation of IDocumentViewModel private bool _selected; private string _documentName; + private DataSource _dataSource; + public string DocumentName { get => _documentName; @@ -260,7 +335,16 @@ namespace VECTO3GUI2020.ViewModel.MultiStage.Implementation public XmlDocumentType DocumentType => throw new NotImplementedException(); - public DataSource DataSource => null; + public DataSource DataSource + { + get => _dataSource; + set + { + if (SetProperty(ref _dataSource, value)) { + _saveJobCommand.NotifyCanExecuteChanged(); + } + } + } public IEditViewModel EditViewModel => this; diff --git a/VECTO3GUI2020/Views/Multistage/CreateVifView.xaml b/VECTO3GUI2020/Views/Multistage/CreateVifView.xaml index c189cd023690bf1e535c13bef8407df83351bf1b..a4712154d70c8cec42fdf81bcedeab91c3cefa85 100644 --- a/VECTO3GUI2020/Views/Multistage/CreateVifView.xaml +++ b/VECTO3GUI2020/Views/Multistage/CreateVifView.xaml @@ -39,8 +39,8 @@ <DockPanel DockPanel.Dock="Bottom" LastChildFill="False"> <UniformGrid DockPanel.Dock="Right" Rows="1" Width="500" HorizontalAlignment="Right"> <Button Style="{StaticResource MultiStageButtonStyle1}">Create new VIF</Button> - <Button Style="{StaticResource MultiStageButtonStyle1}">Save Job as ...</Button> - <Button Style="{StaticResource MultiStageButtonStyle1}">Save Job</Button> + <Button Style="{StaticResource MultiStageButtonStyle1}" Command="{Binding SaveJobAsCommand}">Save Job as ...</Button> + <Button Style="{StaticResource MultiStageButtonStyle1}" Command="{Binding SaveJobCommand}">Save Job</Button> <Button Style="{StaticResource MultiStageButtonStyle1}" Command="{Binding CloseWindowCommand}" CommandParameter="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type Window}}}">Close</Button>