From 6366298ed2c9fcee14c025a4bb4811dabcfad6d0 Mon Sep 17 00:00:00 2001 From: "VKMTHD\\franzjosefkober" <franz.josef.kober@ivt.tugraz.at> Date: Tue, 5 May 2020 22:39:50 +0200 Subject: [PATCH] vectoJob serialization changed to make it compatible with the existing --- VECTO3GUI/Helper/SerializeHelper.cs | 23 ++- VECTO3GUI/Model/JobListModel.cs | 16 ++- .../ViewModel/Impl/AbstractBusJobViewModel.cs | 33 +++-- .../Impl/CompletedBusJobViewModel.cs | 2 +- VECTO3GUI/ViewModel/Impl/JobEntry.cs | 133 +++++++++++++----- VECTO3GUI/ViewModel/Impl/JoblistViewModel.cs | 60 +++++--- .../ViewModel/Interfaces/IBusJobViewModel.cs | 1 - 7 files changed, 195 insertions(+), 73 deletions(-) diff --git a/VECTO3GUI/Helper/SerializeHelper.cs b/VECTO3GUI/Helper/SerializeHelper.cs index fea12d4803..8b1696f7b6 100644 --- a/VECTO3GUI/Helper/SerializeHelper.cs +++ b/VECTO3GUI/Helper/SerializeHelper.cs @@ -3,27 +3,44 @@ using System.Collections.Generic; using System.IO; using Castle.Core.Internal; using Newtonsoft.Json; +using Newtonsoft.Json.Serialization; + namespace VECTO3GUI.Helper { public static class SerializeHelper { - public static void SerializeToFile<T>(string filePath, T file) + public const string DATE_FORMAT = "yyyy-MM-dd'T'HH:mm:ss'Z'"; + + public static void SerializeToFile<T>(string filePath, T file, IContractResolver resolver = null) { if (filePath.IsNullOrEmpty() || file == null) return; - var serializedObject = JsonConvert.SerializeObject(file, Formatting.Indented); + var settings = new JsonSerializerSettings { + Formatting = Formatting.Indented, + DateFormatString = DATE_FORMAT + }; + + if (resolver != null) + settings.ContractResolver = resolver; + + var serializedObject = JsonConvert.SerializeObject(file, settings); File.WriteAllText(filePath, serializedObject); } - public static T DeserializeToObject<T>(string filePath) + public static T DeserializeToObject<T>(string filePath, IContractResolver resolver = null) { if (filePath.IsNullOrEmpty()) return default(T); using (var file = File.OpenText(filePath)) { var serializer = new JsonSerializer(); + serializer.DateFormatString = DATE_FORMAT; + + if (resolver != null) + serializer.ContractResolver = resolver; + var result = serializer.Deserialize(file,typeof(T)); if (result == null) return default(T); diff --git a/VECTO3GUI/Model/JobListModel.cs b/VECTO3GUI/Model/JobListModel.cs index 99a2fab301..511572795c 100644 --- a/VECTO3GUI/Model/JobListModel.cs +++ b/VECTO3GUI/Model/JobListModel.cs @@ -7,7 +7,6 @@ using VECTO3GUI.ViewModel.Impl; namespace VECTO3GUI.Model { - public class JobListEntry { public bool IsSelected { get; set; } @@ -43,8 +42,7 @@ namespace VECTO3GUI.Model jobList = SerializeHelper.DeserializeToObject<List<JobListEntry>>(_jobListFilePath); JobList = jobList; } - - + public void SaveJobList(IList<JobEntry> jobEntries) { SetJobList(jobEntries); @@ -58,7 +56,8 @@ namespace VECTO3GUI.Model JobList = new List<JobListEntry>(); - for (int i = 0; i < jobEntries.Count; i++) { + for (int i = 0; i < jobEntries.Count; i++) + { JobList.Add ( new JobListEntry @@ -69,7 +68,7 @@ namespace VECTO3GUI.Model ); } } - + public IList<JobEntry> GetJobEntries() { var jobEntries = new List<JobEntry>(); @@ -78,9 +77,12 @@ namespace VECTO3GUI.Model return jobEntries; for (int i = 0; i < JobList.Count; i++) { - var jobEntry = SerializeHelper.DeserializeToObject<JobEntry>(JobList[i].JobFilePath); - if(jobEntry != null) + var jobEntry = SerializeHelper.DeserializeToObject<JobEntry>(JobList[i].JobFilePath); + if (jobEntry != null) { + jobEntry.JobEntryFilePath = JobList[i].JobFilePath; + jobEntry.Selected = JobList[i].IsSelected; jobEntries.Add(jobEntry); + } } return jobEntries; diff --git a/VECTO3GUI/ViewModel/Impl/AbstractBusJobViewModel.cs b/VECTO3GUI/ViewModel/Impl/AbstractBusJobViewModel.cs index e1e0968684..ead3ff00bf 100644 --- a/VECTO3GUI/ViewModel/Impl/AbstractBusJobViewModel.cs +++ b/VECTO3GUI/ViewModel/Impl/AbstractBusJobViewModel.cs @@ -120,7 +120,7 @@ namespace VECTO3GUI.ViewModel.Impl protected AbstractBusJobViewModel(IKernel kernel, JobEntry jobEntry) { - Init(kernel, jobEntry.JobType); + Init(kernel, jobEntry.Header.JobType); SetJobEntryData(jobEntry); SavedJobEntry = jobEntry; _editJob = true; @@ -147,8 +147,10 @@ namespace VECTO3GUI.ViewModel.Impl private void SetJobEntryData(JobEntry jobEntry) { - FirstFilePath = jobEntry.FirstFilePath; - SecondFilePath = jobEntry.SecondFilePath; + FirstFilePath = jobEntry.Header.JobType == JobType.SingleBusJob + ? jobEntry.Body.PrimaryVehicle + : jobEntry.Body.PrimaryVehicleResults; + SecondFilePath = jobEntry.Body.CompletedVehicle; } protected abstract void SetFirstFileLabel(); @@ -209,6 +211,7 @@ namespace VECTO3GUI.ViewModel.Impl #endregion + private void SaveJob(Window window) { var jobFilePath = FileDialogHelper.SaveJobFileToDialog(Settings.XmlFilePathFolder); @@ -218,11 +221,25 @@ namespace VECTO3GUI.ViewModel.Impl var job = new JobEntry { JobEntryFilePath = jobFilePath, - FirstFilePath = FirstFilePath, - SecondFilePath = SecondFilePath, - JobType = JobType + + Header = new JobHeader { + JobType = JobType, + FileVersion = JobType.GetJobTypeNumberByJobType(), + AppVersion = "unknown", + CreatedBy = "unknown", + Date = DateTime.UtcNow + } }; + var jobBody = new JobBody { + CompletedVehicle = SecondFilePath + }; + jobBody.PrimaryVehicle = JobType.SingleBusJob == JobType ? FirstFilePath : null; + jobBody.PrimaryVehicleResults = JobType.CompletedBusJob == JobType ? FirstFilePath : null; + + job.Body = jobBody; + + SerializeHelper.SerializeToFile(jobFilePath, job); SavedJobEntry = job; DoCancelCommand(window); @@ -231,8 +248,8 @@ namespace VECTO3GUI.ViewModel.Impl private void UpdateJobData() { - SavedJobEntry.FirstFilePath = FirstFilePath; - SavedJobEntry.SecondFilePath = SecondFilePath; + SavedJobEntry.Body.PrimaryVehicle = FirstFilePath; + SavedJobEntry.Body.CompletedVehicle = SecondFilePath; } diff --git a/VECTO3GUI/ViewModel/Impl/CompletedBusJobViewModel.cs b/VECTO3GUI/ViewModel/Impl/CompletedBusJobViewModel.cs index eaae17058b..89f06c0efb 100644 --- a/VECTO3GUI/ViewModel/Impl/CompletedBusJobViewModel.cs +++ b/VECTO3GUI/ViewModel/Impl/CompletedBusJobViewModel.cs @@ -18,7 +18,7 @@ namespace VECTO3GUI.ViewModel.Impl { SetFirstFileLabel(); } - + protected sealed override void SetFirstFileLabel() { FirstLabelText = $"Select {JobFileType.PIFBusFile.GetLable()}"; diff --git a/VECTO3GUI/ViewModel/Impl/JobEntry.cs b/VECTO3GUI/ViewModel/Impl/JobEntry.cs index 936da2c900..cc789f3525 100644 --- a/VECTO3GUI/ViewModel/Impl/JobEntry.cs +++ b/VECTO3GUI/ViewModel/Impl/JobEntry.cs @@ -1,6 +1,7 @@ using System; using Newtonsoft.Json; + namespace VECTO3GUI.ViewModel.Impl { public enum JobType @@ -15,7 +16,6 @@ namespace VECTO3GUI.ViewModel.Impl private const string SingleBusJobLabel = "Single Bus Job"; private const string CompletedBusJobLabel = "Completed Bus Job"; - public static string GetLabel(this JobType jobType) { switch (jobType) @@ -37,62 +37,131 @@ namespace VECTO3GUI.ViewModel.Impl return JobType.CompletedBusJob; return JobType.Unknown; } + + public static JobType GetJobTypeByFileVersion(int fileVersion) + { + if (fileVersion == JobHeader.SingleBusFileVersion) + return JobType.SingleBusJob; + if (fileVersion == JobHeader.CompletedBusFileVersion) + return JobType.CompletedBusJob; + return JobType.Unknown; + } + + public static int GetJobTypeNumberByJobType(this JobType jobType) + { + if (jobType == JobType.CompletedBusJob) + return JobHeader.CompletedBusFileVersion; + if (jobType == JobType.SingleBusJob) + return JobHeader.SingleBusFileVersion; + return 0; + } } - [JsonObject(ItemNullValueHandling = NullValueHandling.Ignore)] public class JobEntry : ObservableObject { - private JobType _jobType; - private string _jobTypeName; + private JobHeader _header; + private JobBody _body; private bool _selected; private string _jobEntryFilePath; - private string _firstFilePath; - private string _secondFilePath; [JsonIgnore] - public int Sorting; + public bool Selected + { + get { return _selected; } + set { SetProperty(ref _selected, value); } + } + + [JsonIgnore] + public string JobEntryFilePath + { + get { return _jobEntryFilePath; } + set { SetProperty(ref _jobEntryFilePath, value); } + } + + public JobHeader Header + { + get { return _header; } + set { SetProperty(ref _header, value); } + } + public JobBody Body + { + get { return _body; } + set { SetProperty(ref _body, value); } + } + } + + public class JobHeader : ObservableObject + { + public const int SingleBusFileVersion = 6; + public const int CompletedBusFileVersion = 7; + + private JobType _jobType; + private string _createdBy; + private DateTime _dateTime; + private string _appVersion; + private int _fileVersion; + [JsonIgnore] public JobType JobType { - get { return _jobType;} - set - { - _jobType = value; - _jobTypeName = _jobType.GetLabel(); - } + get { return _jobType; } + set { SetProperty(ref _jobType, value); } + } + + public string CreatedBy + { + get { return _createdBy; } + set { SetProperty(ref _createdBy, value); } + } + + public DateTime Date + { + get { return _dateTime; } + set { SetProperty(ref _dateTime, value); } + } + + public string AppVersion + { + get { return _appVersion; } + set { SetProperty(ref _appVersion, value); } } - public string JobTypeName + public int FileVersion { - get { return _jobTypeName; } + get { return _fileVersion; } set { - _jobTypeName = value; - _jobType = _jobTypeName.Parse(); + SetProperty(ref _fileVersion, value); + JobType = JobTypeHelper.GetJobTypeByFileVersion(_fileVersion); } } - - public bool Selected - { - get { return _selected; } - set { SetProperty(ref _selected, value); } - } + } + - public string JobEntryFilePath + [JsonObject(ItemNullValueHandling = NullValueHandling.Ignore)] + public class JobBody : ObservableObject + { + private string _completedVehicle; + private string _primaryVehicle; + private string _primaryVehicleResults; + + public string CompletedVehicle { - get { return _jobEntryFilePath; } - set { SetProperty(ref _jobEntryFilePath, value); } + get { return _completedVehicle; } + set { SetProperty(ref _completedVehicle, value); } } - public string FirstFilePath + + public string PrimaryVehicle { - get { return _firstFilePath; } - set { SetProperty(ref _firstFilePath, value); } + get { return _primaryVehicle; } + set { SetProperty(ref _primaryVehicle, value); } } - public string SecondFilePath + + public string PrimaryVehicleResults { - get { return _secondFilePath; } - set { SetProperty(ref _secondFilePath, value); } + get { return _primaryVehicleResults; } + set { SetProperty(ref _primaryVehicleResults, value); } } } } \ No newline at end of file diff --git a/VECTO3GUI/ViewModel/Impl/JoblistViewModel.cs b/VECTO3GUI/ViewModel/Impl/JoblistViewModel.cs index f0646e800f..b88563191e 100644 --- a/VECTO3GUI/ViewModel/Impl/JoblistViewModel.cs +++ b/VECTO3GUI/ViewModel/Impl/JoblistViewModel.cs @@ -59,13 +59,14 @@ namespace VECTO3GUI.ViewModel.Impl set { SetProperty(ref _selectedJobEntry, value); - if (_selectedJobEntry != null) { - var firstJobTyp = _selectedJobEntry.JobType == JobType.SingleBusJob + if (_selectedJobEntry != null) + { + var firstJobTyp = _selectedJobEntry.Header.JobType == JobType.SingleBusJob ? JobFileType.PrimaryBusFile : JobFileType.PIFBusFile; FirstContextMenu = $"View {firstJobTyp.GetLable()}"; - } - } + } + } } public ObservableCollectionEx<JobEntry> Jobs @@ -74,6 +75,8 @@ namespace VECTO3GUI.ViewModel.Impl set { SetProperty(ref _jobs, value); } } + + public ObservableCollection<MessageEntry> Messages { get { return _messages; } @@ -104,7 +107,7 @@ namespace VECTO3GUI.ViewModel.Impl private void JobItemChanged(object sender, PropertyChangedEventArgs e) { if (e.PropertyName == "Selected" && sender is JobEntry) - UpdateJobEntry((JobEntry)sender); + UpdateJobListEntry((JobEntry)sender); } private void JobsCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) @@ -138,7 +141,7 @@ namespace VECTO3GUI.ViewModel.Impl return jobEntry != null; } - + public ICommand RemoveAllJobs { get @@ -166,15 +169,14 @@ namespace VECTO3GUI.ViewModel.Impl } private void DoEditJob(JobEntry jobEntry) { - var viewModel = GetBusJobViewModel(jobEntry.JobType, jobEntry); - var window = CreateBusJobOutputWindow(viewModel, jobEntry.JobType); + var viewModel = GetBusJobViewModel(jobEntry.Header.JobType, jobEntry); + var window = CreateBusJobOutputWindow(viewModel, jobEntry.Header.JobType); if (window.ShowDialog() != true) ResetBusJobEntries(jobEntry); else UpdateJobEntry(((IBusJobViewModel)viewModel).SavedJobEntry); } - public ICommand EditCompletedFile { get @@ -199,6 +201,7 @@ namespace VECTO3GUI.ViewModel.Impl } + public ICommand CreateNewJob { get @@ -231,10 +234,10 @@ namespace VECTO3GUI.ViewModel.Impl switch (jobFileType) { case JobFileType.PIFBusFile: case JobFileType.PrimaryBusFile: - xmlViewModel = new XMLViewModel(SelectedJobEntry.FirstFilePath); + xmlViewModel = new XMLViewModel(SelectedJobEntry.Body.PrimaryVehicle); break; case JobFileType.CompletedBusFile: - xmlViewModel = new XMLViewModel(SelectedJobEntry.SecondFilePath); + xmlViewModel = new XMLViewModel(SelectedJobEntry.Body.CompletedVehicle); break; } var window = OutputWindowHelper.CreateOutputWindow(Kernel, xmlViewModel, xmlViewModel.FileName); @@ -256,6 +259,7 @@ namespace VECTO3GUI.ViewModel.Impl return; var jobEntry = SerializeHelper.DeserializeToObject<JobEntry>(filePath.First()); + jobEntry.JobEntryFilePath = filePath.First(); _jobs.Add(jobEntry); } @@ -317,8 +321,7 @@ namespace VECTO3GUI.ViewModel.Impl if (index - 1 >= 0) _jobs.Move(index, index - 1); } - - + public ICommand MoveJobDown { get { return _moveJobDownCommand ?? (_moveJobDownCommand = new RelayCommand<JobEntry>(DoMoveJobDownCommand)); } @@ -331,37 +334,40 @@ namespace VECTO3GUI.ViewModel.Impl if (index + 1 < _jobs.Count) _jobs.Move(index, index + 1); } + public ICommand StartSimulation { get { return _startSimulationCommand ?? (_startSimulationCommand = new RelayCommand(DoStartSimulationCommand)); } } - private void DoStartSimulationCommand() { } + public ICommand OpenInFolder { get { return _openInFolderCommand ?? (_openInFolderCommand = new RelayCommand<JobEntry>(DoOpenInFolderCommand)); } } - private void DoOpenInFolderCommand(JobEntry jobEntry) { - if (jobEntry != null) { + if (jobEntry != null) + { var dirPath = Path.GetDirectoryName(jobEntry.JobEntryFilePath); - if (Directory.Exists(dirPath)) { + if (Directory.Exists(dirPath)) + { Process.Start("explorer.exe", dirPath); } } } + #endregion private object GetBusJobViewModel(JobType jobType, JobEntry jobEntry = null) { - var currentJobType = jobEntry?.JobType ?? jobType; + var currentJobType = jobEntry?.Header.JobType ?? jobType; object viewModel = null; @@ -387,7 +393,7 @@ namespace VECTO3GUI.ViewModel.Impl return OutputWindowHelper.CreateOutputWindow(Kernel, viewModel, $"Create {jobType.GetLabel()}", 460, 200, ResizeMode.NoResize); } - + private void AddBusJobEntry(JobEntry jobEntry) { if (jobEntry == null) @@ -405,16 +411,28 @@ namespace VECTO3GUI.ViewModel.Impl SerializeHelper.SerializeToFile(jobEntry.JobEntryFilePath, jobEntry); } + private void UpdateJobListEntry(JobEntry jobEntry) + { + for (int i = 0; i < Jobs.Count; i++) + { + if (Jobs[i].JobEntryFilePath == jobEntry.JobEntryFilePath) + { + Jobs[i].Selected = jobEntry.Selected; + } + } + _jobListModel.SaveJobList(_jobs); + } + private IJobEditViewModel ReadCompletedXmlFile(JobEntry jobEntry) { var xmlInputReader = Kernel.Get<IXMLInputDataReader>(); - using (var reader = XmlReader.Create(jobEntry.SecondFilePath)) + using (var reader = XmlReader.Create(jobEntry.Body.CompletedVehicle)) { var readerResult = xmlInputReader.Create(reader) as IDeclarationInputDataProvider; return CreateCompleteBusVehicleViewModel(readerResult); } } - + private IJobEditViewModel CreateCompleteBusVehicleViewModel(IDeclarationInputDataProvider dataProvider) { _messages.Add(new MessageEntry diff --git a/VECTO3GUI/ViewModel/Interfaces/IBusJobViewModel.cs b/VECTO3GUI/ViewModel/Interfaces/IBusJobViewModel.cs index 2af1764f52..e90db0170d 100644 --- a/VECTO3GUI/ViewModel/Interfaces/IBusJobViewModel.cs +++ b/VECTO3GUI/ViewModel/Interfaces/IBusJobViewModel.cs @@ -17,7 +17,6 @@ namespace VECTO3GUI.ViewModel.Interfaces string FirstLabelText { get; } string SecondLabelText { get; } JobEntry SavedJobEntry { get; } - ICommand SelectFirstFileCommand { get; } ICommand SelectSecondFileCommand { get; } ICommand CancelCommand { get; } -- GitLab