From d4b6d0465464db53b08048d98a5e1ed6c523ebe1 Mon Sep 17 00:00:00 2001 From: schnulller Date: Tue, 7 Jun 2022 22:35:49 +0200 Subject: [PATCH] Added a workaround for a bug in the parser --- HaDocumentV6/Document.cs | 1 + HaWeb/Controllers/APIController.cs | 20 ++++++++++--------- HaWeb/FileHelpers/HaDocumentWrapper.cs | 3 +-- HaWeb/FileHelpers/IXMLProvider.cs | 1 + HaWeb/FileHelpers/XMLProvider.cs | 22 +++++++++++++++++---- HaWeb/Views/Shared/_FileList.cshtml | 2 +- HaWeb/Views/Shared/_PublishForm.cshtml | 2 +- HaWeb/XMLParser/IXMLService.cs | 1 + HaWeb/XMLParser/XMLService.cs | 27 ++++++++++++++++++++------ 9 files changed, 56 insertions(+), 23 deletions(-) diff --git a/HaDocumentV6/Document.cs b/HaDocumentV6/Document.cs index 89eadfc..5f2be81 100644 --- a/HaDocumentV6/Document.cs +++ b/HaDocumentV6/Document.cs @@ -21,6 +21,7 @@ namespace HaDocument private static ILibrary _library; public static ILibrary Create(IHaDocumentOptions Settings) { + _lib = new IntermediateLibrary(); SettingsValidator.Validate(Settings); _settings = Settings; _createReader(); diff --git a/HaWeb/Controllers/APIController.cs b/HaWeb/Controllers/APIController.cs index 3e61d3a..c1bef53 100644 --- a/HaWeb/Controllers/APIController.cs +++ b/HaWeb/Controllers/APIController.cs @@ -166,11 +166,18 @@ public class APIController : Controller { if (!ModelState.IsValid || element == null) return BadRequest(ModelState); var savedfile = await _xmlProvider.SaveHamannFile(element, _targetFilePath, ModelState); - if (!ModelState.IsValid || savedfile == null) + if (!ModelState.IsValid || savedfile == null) { + if (savedfile != null) + _xmlProvider.DeleteHamannFile(savedfile.Name); return BadRequest(ModelState); + } _ = _lib.SetLibrary(savedfile.PhysicalPath, ModelState); - if (!ModelState.IsValid) + if (!ModelState.IsValid) { + _xmlProvider.DeleteHamannFile(savedfile.Name); return BadRequest(ModelState); + } + _xmlProvider.SetInProduction(savedfile); + _xmlService.SetInProduction(); return Created("/", _xmlProvider.GetHamannFiles()); } @@ -329,13 +336,8 @@ public class APIController : Controller { return BadRequest(ModelState); } - try { - _ = _lib.SetLibrary(newFile.First().PhysicalPath, ModelState); - } - catch (Exception ex) { - ModelState.AddModelError("Error", "Error parsing the file: " + ex.Message); - return BadRequest(ModelState); - } + _ = _lib.SetLibrary(newFile.First().PhysicalPath, ModelState); + if (!ModelState.IsValid) return BadRequest(ModelState); _xmlProvider.SetInProduction(newFile.First()); _xmlService.UnUseProduction(); diff --git a/HaWeb/FileHelpers/HaDocumentWrapper.cs b/HaWeb/FileHelpers/HaDocumentWrapper.cs index e2f06d1..6c710e9 100644 --- a/HaWeb/FileHelpers/HaDocumentWrapper.cs +++ b/HaWeb/FileHelpers/HaDocumentWrapper.cs @@ -35,8 +35,7 @@ public class HaDocumentWrapper : IHaDocumentWrappper { Library = HaDocument.Document.Create(new HaWeb.Settings.HaDocumentOptions() { HamannXMLFilePath = filepath, AvailableYearRange = (_startYear, _endYear) }); } catch (Exception ex) { - if (ModelState != null) ModelState.AddModelError("Error:", "Das Dokument konnte nicht geparst werden: " + ex.Message); - Console.WriteLine(ex.Message); + if (ModelState != null) ModelState.AddModelError("Error", "Das Dokument konnte nicht geparst werden: " + ex.Message); return null; } return Library; diff --git a/HaWeb/FileHelpers/IXMLProvider.cs b/HaWeb/FileHelpers/IXMLProvider.cs index 5c0af13..e411d6c 100644 --- a/HaWeb/FileHelpers/IXMLProvider.cs +++ b/HaWeb/FileHelpers/IXMLProvider.cs @@ -12,4 +12,5 @@ public interface IXMLProvider { public List? GetHamannFiles(); public IFileInfo? GetInProduction(); public void SetInProduction(IFileInfo info); + public void DeleteHamannFile(string filename); } \ No newline at end of file diff --git a/HaWeb/FileHelpers/XMLProvider.cs b/HaWeb/FileHelpers/XMLProvider.cs index b6953a7..87b2485 100644 --- a/HaWeb/FileHelpers/XMLProvider.cs +++ b/HaWeb/FileHelpers/XMLProvider.cs @@ -10,7 +10,7 @@ public class XMLProvider : IXMLProvider { private Dictionary? _Files; private Dictionary? _Roots; private List? _HamannFiles; - private IFileInfo? _InProduction; + private Stack? _InProduction; public XMLProvider(IFileProvider provider, IXMLService xmlservice) { _fileProvider = provider; @@ -26,9 +26,24 @@ public class XMLProvider : IXMLProvider { public List? GetHamannFiles() => this._HamannFiles; - public IFileInfo? GetInProduction() => this._InProduction; + public IFileInfo? GetInProduction() { + if (_InProduction == null || !_InProduction.Any()) return null; + return this._InProduction.Peek(); + } - public void SetInProduction(IFileInfo info) => _InProduction = info; + public void DeleteHamannFile(string filename) { + if (_HamannFiles == null) return; + var files = _HamannFiles.Where(x => x.Name == filename); + foreach (var file in files) { + File.Delete(file.PhysicalPath); + } + _HamannFiles.RemoveAll(x => x.Name == filename); + } + + public void SetInProduction(IFileInfo info) { + if (_InProduction == null) _InProduction = new Stack(); + _InProduction.Push(info); + } public FileList? GetFiles(string prefix) => _Files != null && _Files.ContainsKey(prefix) ? _Files[prefix] : null; @@ -89,7 +104,6 @@ public class XMLProvider : IXMLProvider { if (_HamannFiles == null) _HamannFiles = new List(); _HamannFiles.RemoveAll(x => x.Name == info.Name); _HamannFiles.Add(info); - _InProduction = info; return info; } diff --git a/HaWeb/Views/Shared/_FileList.cshtml b/HaWeb/Views/Shared/_FileList.cshtml index 9969851..56e46ac 100644 --- a/HaWeb/Views/Shared/_FileList.cshtml +++ b/HaWeb/Views/Shared/_FileList.cshtml @@ -61,7 +61,7 @@ document.getElementById("ha-filelistoutput").textContent = json.Error; } else { - location.reload(); + location.reload(); } }) .catch ((e) => { diff --git a/HaWeb/Views/Shared/_PublishForm.cshtml b/HaWeb/Views/Shared/_PublishForm.cshtml index 5d559b6..d1dba96 100644 --- a/HaWeb/Views/Shared/_PublishForm.cshtml +++ b/HaWeb/Views/Shared/_PublishForm.cshtml @@ -57,7 +57,7 @@ document.getElementById("ha-lds-ellipsis-publish").style.display = "none"; document.getElementById("ha-publishmessage").style.opacity = "1"; document.getElementById("publish-result").value = "Erfolg!"; - window.location.replace("/Admin/Upload/"); + @* location.reload(); *@ } }) .catch ((e) => { diff --git a/HaWeb/XMLParser/IXMLService.cs b/HaWeb/XMLParser/IXMLService.cs index d687e5d..6b68cc8 100644 --- a/HaWeb/XMLParser/IXMLService.cs +++ b/HaWeb/XMLParser/IXMLService.cs @@ -16,4 +16,5 @@ public interface IXMLService { public Dictionary? GetInProduction(); public void UnUse(string prefix); public void UnUseProduction(); + public void SetInProduction(); } \ No newline at end of file diff --git a/HaWeb/XMLParser/XMLService.cs b/HaWeb/XMLParser/XMLService.cs index f2df2a9..8f8810b 100644 --- a/HaWeb/XMLParser/XMLService.cs +++ b/HaWeb/XMLParser/XMLService.cs @@ -7,7 +7,7 @@ public class XMLService : IXMLService { private Dictionary? _Used; private Dictionary? _Roots; - private Dictionary? _InProduction; + private Stack>? _InProduction; public XMLService() { // Getting all classes which implement IXMLRoot for possible document endpoints @@ -32,7 +32,23 @@ public class XMLService : IXMLService { public Dictionary? GetRootsDictionary() => this._Roots == null ? null : this._Roots; - public Dictionary? GetInProduction() => this._InProduction; + public Dictionary? GetInProduction() { + if (_InProduction == null) return null; + return this._InProduction.Peek(); + } + + public void SetInProduction() { + if (_Used == null) return; + var inProduction = new Dictionary(); + foreach (var category in _Used) { + if (category.Value == null || category.Value.GetFileList() == null || !category.Value.GetFileList()!.Any()) + return; + inProduction.Add(category.Key, category.Value); + } + + if(_InProduction == null) _InProduction = new Stack>(); + _InProduction.Push(inProduction); + } public void UnUseProduction() => this._InProduction = null; @@ -104,20 +120,19 @@ public class XMLService : IXMLService { } var opus = new XElement("opus"); - var inProduction = new Dictionary(); - foreach (var category in _Used) { + // TODO: Workaround for bug in HaDocument: roots have to be added in a specific order + var used = _Used.OrderByDescending(x => x.Key); + foreach (var category in used) { if (category.Value == null || category.Value.GetFileList() == null || !category.Value.GetFileList()!.Any()) { ModelState.AddModelError("Error", _Roots![category.Key].Type + " nicht vorhanden."); return null; } - inProduction.Add(category.Key, category.Value); var documents = category.Value.GetFileList(); foreach (var document in documents!) { document.XMLRoot.MergeIntoFile(opus, document); } } - _InProduction = inProduction; return opus; }