From 2952a9de1372d3e748d869e1dfd642f23ac6339b Mon Sep 17 00:00:00 2001
From: "harald.martini@student.tugraz.at" <harald.martini@student.tugraz.at>
Date: Tue, 13 Jul 2021 20:06:19 +0200
Subject: [PATCH] Added support for Json Jobs in JobListViewModel

---
 VECTO3GUI2020/VECTO3GUI2020.csproj            |  1 -
 .../Implementation/JobListViewModel.cs        | 38 ++++++++++++++++---
 2 files changed, 33 insertions(+), 6 deletions(-)

diff --git a/VECTO3GUI2020/VECTO3GUI2020.csproj b/VECTO3GUI2020/VECTO3GUI2020.csproj
index 55a7e7d849..84002528f9 100644
--- a/VECTO3GUI2020/VECTO3GUI2020.csproj
+++ b/VECTO3GUI2020/VECTO3GUI2020.csproj
@@ -273,7 +273,6 @@
     <Compile Include="ViewModel\Implementation\JobEdit\Vehicle\Components\EngineFuelViewModel.cs" />
     <Compile Include="ViewModel\Implementation\JobEdit\Vehicle\Components\EngineModeViewModel.cs" />
     <Compile Include="ViewModel\Implementation\JobEdit\Vehicle\Components\TorqueConverterViewModel.cs" />
-    <Compile Include="ViewModel\Interfaces\Document\IMultistageDocumentViewModelFactory.cs" />
     <Compile Include="ViewModel\Interfaces\JobEdit\Vehicle\Components\IEngineFuelViewModel.cs" />
     <Compile Include="ViewModel\Interfaces\JobEdit\Vehicle\Components\IEngineModeViewModel.cs" />
     <Compile Include="ViewModel\Interfaces\Common\IViewModelBase.cs" />
diff --git a/VECTO3GUI2020/ViewModel/Implementation/JobListViewModel.cs b/VECTO3GUI2020/ViewModel/Implementation/JobListViewModel.cs
index ed9c9b9f6c..8bd222b8a9 100644
--- a/VECTO3GUI2020/ViewModel/Implementation/JobListViewModel.cs
+++ b/VECTO3GUI2020/ViewModel/Implementation/JobListViewModel.cs
@@ -67,7 +67,6 @@ namespace VECTO3GUI2020.ViewModel.Implementation
 					_openSourceFileCommand?.NotifyCanExecuteChanged();
 					_showSourceFileInExplorerCommand?.NotifyCanExecuteChanged();
 				};
-
 			}
 		}
 
@@ -249,6 +248,36 @@ namespace VECTO3GUI2020.ViewModel.Implementation
 		}
 
 		private Task<IDocumentViewModel> LoadFileAsync([NotNull] string fileName)
+		{
+			var extension = Path.GetExtension(fileName);
+			switch (extension) {
+				case ".xml":
+					return LoadXMLFile(fileName);
+				case ".json":
+					return LoadJsonFile(fileName);
+				default: 
+					throw new VectoException($"{extension} not supported!");
+			}
+			
+		}
+
+		private Task<IDocumentViewModel> LoadJsonFile([NotNull] string fileName)
+		{
+			IDocumentViewModel result = null;
+			try {
+				var inputData = JSONInputDataFactory.ReadJsonJob(fileName, true);
+				return Task.FromResult(_multiStageViewModelFactory.CreateDocumentViewModel(inputData));
+
+			} catch (Exception ex) {
+				throw new VectoException($"File {fileName} not supported", ex);
+			}
+
+
+
+			return null;
+		}
+
+		private Task<IDocumentViewModel> LoadXMLFile([NotNull] string fileName)
 		{
 			var xElement = new System.Xml.XmlDocument();
 			xElement.Load(fileName);
@@ -273,11 +302,7 @@ namespace VECTO3GUI2020.ViewModel.Implementation
 					Debug.WriteLine(ex.GetInnerExceptionMessages());
 					result = new SimulationOnlyDeclarationJob(inputDataProvider.DataSource, inputDataProvider.JobInputData.JobName, XmlDocumentType.DeclarationJobData) as IDocumentViewModel;
 				}
-
-
 				return Task.FromResult(result);
-
-
 			}
 			else
 			{
@@ -285,6 +310,9 @@ namespace VECTO3GUI2020.ViewModel.Implementation
 			}
 
 			return null;
+
+
+
 		}
 
 		#endregion
-- 
GitLab