diff --git a/DataAccess/DTOs/FileVersionDTO.cs b/DataAccess/DTOs/FileVersionDTO.cs new file mode 100644 index 0000000..88e91e9 --- /dev/null +++ b/DataAccess/DTOs/FileVersionDTO.cs @@ -0,0 +1,27 @@ +using Newtonsoft.Json; +using StructureHelperCommon.Models.Projects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DataAccess.DTOs +{ + + [JsonObject(IsReference = true)] + public class FileVersionDTO : IFileVersion + { + + [JsonProperty("Id")] + public Guid Id { get; set; } + [JsonProperty("VersionNumber")] + public int VersionNumber { get; set; } + [JsonProperty("SubVersionNumber")] + public int SubVersionNumber { get; set; } + public FileVersionDTO(Guid id) + { + Id = id; + } + } +} diff --git a/DataAccess/DTOs/FileVersionDTOConvertStrategy.cs b/DataAccess/DTOs/FileVersionDTOConvertStrategy.cs new file mode 100644 index 0000000..78e934c --- /dev/null +++ b/DataAccess/DTOs/FileVersionDTOConvertStrategy.cs @@ -0,0 +1,41 @@ +using StructureHelperCommon.Infrastructures.Interfaces; +using StructureHelperCommon.Models; +using StructureHelperCommon.Models.Projects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DataAccess.DTOs +{ + public class FileVersionDTOConvertStrategy : IConvertStrategy + { + private IUpdateStrategy updateStrategy; + + public IShiftTraceLogger TraceLogger { get; set; } + + public FileVersionDTOConvertStrategy(IUpdateStrategy updateStrategy) + { + this.updateStrategy = updateStrategy; + } + public FileVersionDTOConvertStrategy() : this(new FileVersionUpdateStrategy()) + { + + } + + public FileVersionDTO ConvertTo(FileVersion source) + { + FileVersionDTO fileVersion = new(source.Id); + updateStrategy.Update(fileVersion, source); + return fileVersion; + } + + public FileVersion ConvertFrom(FileVersionDTO source) + { + FileVersion fileVersion = new(source.Id); + updateStrategy.Update(fileVersion, source); + return fileVersion; + } + } +} diff --git a/DataAccess/DTOs/ProjectDTO.cs b/DataAccess/DTOs/ProjectDTO.cs new file mode 100644 index 0000000..8014ba9 --- /dev/null +++ b/DataAccess/DTOs/ProjectDTO.cs @@ -0,0 +1,35 @@ +using Newtonsoft.Json; +using StructureHelperCommon.Models.Analyses; +using StructureHelperCommon.Models.Projects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DataAccess.DTOs +{ + public class ProjectDTO : IProject + { + [JsonProperty("Id")] + public Guid Id { get; set; } + [JsonIgnore] + public string FullFileName { get; set; } + [JsonIgnore] + public bool IsNewFile { get; set; } + [JsonIgnore] + public bool IsActual { get; set; } + + [JsonProperty("VisualAnalyses")] + public List VisualAnalyses { get; private set; } + + [JsonIgnore] + public string FileName { get; set; } + + public ProjectDTO(Guid id) + { + Id = id; + } + + } +} diff --git a/DataAccess/DataAccess.csproj b/DataAccess/DataAccess.csproj index d8687c3..36deaff 100644 --- a/DataAccess/DataAccess.csproj +++ b/DataAccess/DataAccess.csproj @@ -15,4 +15,8 @@ + + + + diff --git a/DataAccess/Example.cs b/DataAccess/Example.cs index 92f0f52..64a44c1 100644 --- a/DataAccess/Example.cs +++ b/DataAccess/Example.cs @@ -1,124 +1,113 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +using Newtonsoft.Json; +using StructureHelperCommon.Models; namespace DataAccess { - using Newtonsoft.Json; - using StructureHelperCommon.Models; - using System; - using System.Collections.Generic; - using static System.Windows.Forms.VisualStyles.VisualStyleElement.ListView; - using static System.Windows.Forms.VisualStyles.VisualStyleElement.TextBox; - class Program - { - static void Main(string[] args) - { - var logger = new TraceLogger(); +// class Program +// { +// static void Main(string[] args) +// { +// var logger = new TraceLogger(); - // Create objects with complex relationships - var parent1 = new Parent { Name = "Parent_1" }; - var parent2 = new Parent { Name = "Parent_2" }; +// // Create objects with complex relationships +// var parent1 = new Parent { Name = "Parent_1" }; +// var parent2 = new Parent { Name = "Parent_2" }; - var detail1 = new Detail { Description = "Detail_1", InternalNote = "Secret Note 1" }; - var detail2 = new Detail { Description = "Detail_2", InternalNote = "Secret Note 2" }; - var detail3 = new Detail { Description = "Detail_3", InternalNote = "Secret Note 3" }; +// var detail1 = new Detail { Description = "Detail_1", InternalNote = "Secret Note 1" }; +// var detail2 = new Detail { Description = "Detail_2", InternalNote = "Secret Note 2" }; +// var detail3 = new Detail { Description = "Detail_3", InternalNote = "Secret Note 3" }; - var subDetail1 = new SubDetail { Info = "SubDetail_1" }; +// var subDetail1 = new SubDetail { Info = "SubDetail_1" }; - // Set up relationships - parent1.Details.Add(detail1); - parent1.Details.Add(detail2); +// // Set up relationships +// parent1.Details.Add(detail1); +// parent1.Details.Add(detail2); - parent2.Details.Add(detail2); // Shared detail - parent2.Details.Add(detail3); +// parent2.Details.Add(detail2); // Shared detail +// parent2.Details.Add(detail3); - detail3.SubDetails.Add(subDetail1); +// detail3.SubDetails.Add(subDetail1); - // Serialize with custom converters and trace logging - string json = Serialize(new List { parent1, parent2 }, logger); - Console.WriteLine("Serialized JSON:"); - Console.WriteLine(json); +// // Serialize with custom converters and trace logging +// string json = Serialize(new List { parent1, parent2 }, logger); +// Console.WriteLine("Serialized JSON:"); +// Console.WriteLine(json); - // Deserialize with custom converters and trace logging - var deserializedParents = Deserialize>(json, logger); +// // Deserialize with custom converters and trace logging +// var deserializedParents = Deserialize>(json, logger); - Console.WriteLine("\nDeserialized Objects:"); - foreach (var parent in deserializedParents) - { - Console.WriteLine($"Parent: {parent.Name}, Id: {parent.Id}"); - foreach (var detail in parent.Details) - { - Console.WriteLine($" Detail: {detail.Description}, Id: {detail.Id}"); - } - } - } +// Console.WriteLine("\nDeserialized Objects:"); +// foreach (var parent in deserializedParents) +// { +// Console.WriteLine($"Parent: {parent.Name}, Id: {parent.Id}"); +// foreach (var detail in parent.Details) +// { +// Console.WriteLine($" Detail: {detail.Description}, Id: {detail.Id}"); +// } +// } +// } - static string Serialize(object obj, TraceLogger logger) - { - var settings = new JsonSerializerSettings - { - Converters = new List - { - new ParentConverter(logger), // Add the specific converter - // Add other converters if needed - }, - Formatting = Formatting.Indented - }; +// static string Serialize(object obj, TraceLogger logger) +// { +// var settings = new JsonSerializerSettings +// { +// Converters = new List +// { +// new ParentConverter(logger), // Add the specific converter +// // Add other converters if needed +// }, +// Formatting = Formatting.Indented +// }; - return JsonConvert.SerializeObject(obj, settings); - } +// return JsonConvert.SerializeObject(obj, settings); +// } - static T Deserialize(string json, TraceLogger logger) - { - var settings = new JsonSerializerSettings - { - Converters = new List - { - new ParentConverter(logger), // Add the specific converter - // Add other converters if needed - } - }; +// static T Deserialize(string json, TraceLogger logger) +// { +// var settings = new JsonSerializerSettings +// { +// Converters = new List +// { +// new ParentConverter(logger), // Add the specific converter +// // Add other converters if needed +// } +// }; - return JsonConvert.DeserializeObject(json, settings); - } - } +// return JsonConvert.DeserializeObject(json, settings); +// } +// } - using Newtonsoft.Json; -using System; -using System.Collections.Generic; -public class Parent - { - public Guid Id { get; set; } = Guid.NewGuid(); - [JsonProperty("parent_name")] - public string Name { get; set; } +//public class Parent +// { +// public Guid Id { get; set; } = Guid.NewGuid(); - public List Details { get; set; } = new List(); - } +// [JsonProperty("parent_name")] +// public string Name { get; set; } - public class Detail - { - public Guid Id { get; set; } = Guid.NewGuid(); +// public List Details { get; set; } = new List(); +// } - [JsonPropertyName("detail_description")] // Compatible with System.Text.Json - public string Description { get; set; } +// public class Detail +// { +// public Guid Id { get; set; } = Guid.NewGuid(); - [JsonIgnore] // This property will be ignored during serialization - public string InternalNote { get; set; } +// [JsonPropertyName("detail_description")] // Compatible with System.Text.Json +// public string Description { get; set; } - public List SubDetails { get; set; } = new List(); - } +// [JsonIgnore] // This property will be ignored during serialization +// public string InternalNote { get; set; } - public class SubDetail - { - public Guid Id { get; set; } = Guid.NewGuid(); - public string Info { get; set; } - } +// public List SubDetails { get; set; } = new List(); +// } + +// public class SubDetail +// { +// public Guid Id { get; set; } = Guid.NewGuid(); +// public string Info { get; set; } +// } } diff --git a/DataAccess/FileDialogs/FileDialogOpener.cs b/DataAccess/FileDialogs/FileDialogOpener.cs deleted file mode 100644 index 55df864..0000000 --- a/DataAccess/FileDialogs/FileDialogOpener.cs +++ /dev/null @@ -1,49 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Windows.Forms; - -namespace DataAccess.FileDialogs -{ - public class FileDialogOpener - { - public void OpenFileAndRead() - { - // Create an instance of OpenFileDialog - using (OpenFileDialog openFileDialog = new OpenFileDialog()) - { - // Set filter options and filter index - openFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*"; - openFileDialog.FilterIndex = 1; - openFileDialog.Multiselect = false; // Set to true if you want to allow multiple file selection - openFileDialog.Title = "Select a File"; - - // Show the dialog and get result - if (openFileDialog.ShowDialog() == DialogResult.OK) - { - // Get the path of the selected file - string selectedFilePath = openFileDialog.FileName; - - // Read the content of the file - try - { - string fileContent = File.ReadAllText(selectedFilePath); - Console.WriteLine($"File Content of '{selectedFilePath}':"); - Console.WriteLine(fileContent); - } - catch (IOException ex) - { - Console.WriteLine($"An error occurred while reading the file: {ex.Message}"); - } - } - else - { - Console.WriteLine("File selection was cancelled."); - } - } - } - } - -} diff --git a/DataAccess/FileDialogs/FileStorage.cs b/DataAccess/FileDialogs/FileStorage.cs index 68aac4a..028fb8a 100644 --- a/DataAccess/FileDialogs/FileStorage.cs +++ b/DataAccess/FileDialogs/FileStorage.cs @@ -10,10 +10,6 @@ namespace DataAccess.FileDialogs { internal class FileStorage { - using System; -using System.Collections.Generic; -using System.IO; -using System.Windows.Forms; public class FileStorageManager { @@ -109,30 +105,30 @@ class Program [STAThread] // Required for OpenFileDialog static void Main() { - var fileStorageManager = new FileStorageManager(); + //var fileStorageManager = new FileStorageManager(); - // Open files and add them to the storage - fileStorageManager.OpenFile(); + //// Open files and add them to the storage + //fileStorageManager.OpenFile(); - // List all opened files - Console.WriteLine("\nOpened Files:"); - fileStorageManager.ListOpenedFiles(); + //// List all opened files + //Console.WriteLine("\nOpened Files:"); + //fileStorageManager.ListOpenedFiles(); - // Example: Read content of the first opened file (if any) - var openedFiles = new List(fileStorageManager._openedFiles.Keys); - if (openedFiles.Count > 0) - { - var firstFileId = openedFiles[0]; - Console.WriteLine($"\nReading content of the first opened file (ID: {firstFileId}):"); - string content = fileStorageManager.ReadFileContent(firstFileId); - Console.WriteLine(content); - } + //// Example: Read content of the first opened file (if any) + //var openedFiles = new List(fileStorageManager._openedFiles.Keys); + //if (openedFiles.Count > 0) + //{ + // var firstFileId = openedFiles[0]; + // Console.WriteLine($"\nReading content of the first opened file (ID: {firstFileId}):"); + // string content = fileStorageManager.ReadFileContent(firstFileId); + // Console.WriteLine(content); + //} - // Close all files - foreach (var fileId in openedFiles) - { - fileStorageManager.CloseFile(fileId); - } + //// Close all files + //foreach (var fileId in openedFiles) + //{ + // fileStorageManager.CloseFile(fileId); + //} } } diff --git a/DataAccess/Infrastructures/FileOpenLogic.cs b/DataAccess/Infrastructures/FileOpenLogic.cs new file mode 100644 index 0000000..ce60666 --- /dev/null +++ b/DataAccess/Infrastructures/FileOpenLogic.cs @@ -0,0 +1,90 @@ +using DataAccess.DTOs; +using DataAccess.JsonConverters; +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using NLog; +using StructureHelperCommon.Infrastructures.Settings; +using StructureHelperCommon.Models; +using StructureHelperCommon.Models.Projects; +using StructureHelperCommon.Services.FileServices; + +namespace DataAccess.Infrastructures +{ + public class FileOpenLogic : IFileOpenLogic + { + private string fileName; + + public IShiftTraceLogger? TraceLogger { get; set; } + + private bool GetFilePath() + { + var inputData = new OpenFileInputData() + { + FilterString = "StructureHelper project file (*.shpj)|*.shpj|All Files (*.*)|*.*", + TraceLogger = TraceLogger + }; + var fileDialog = new FileOpener(inputData); + var fileDialogResult = fileDialog.OpenFile(); + if (fileDialogResult.IsValid != true) + { + return false; + } + fileName = fileDialogResult.FilePath; + return true; + } + + public OpenProjectResult OpenFile() + { + var result = new OpenProjectResult() + { + IsValid = true + }; + if (GetFilePath() == false) + { + result.IsValid = false; + return result; + } + if (! File.Exists(fileName)) + { + result.IsValid = false; + TraceLogger?.AddMessage($"File {fileName} does not exists", TraceLogStatuses.Error); + return result; + } + var settings = new JsonSerializerSettings + { + Converters = new List + { + new FileVersionDTOJsonConverter(TraceLogger), // Add the specific converter + // Add other converters if needed + }, + Formatting = Formatting.Indented, + PreserveReferencesHandling = PreserveReferencesHandling.All, + MissingMemberHandling = MissingMemberHandling.Ignore, + TypeNameHandling = TypeNameHandling.All, + NullValueHandling = NullValueHandling.Include + }; + using (StreamReader file = File.OpenText(fileName)) + { + JsonSerializer serializer = new JsonSerializer(); + var fileVersion = (FileVersionDTO)serializer.Deserialize(file, typeof(FileVersionDTO)); + var checkLogic = new CheckFileVersionLogic() + { + FileVersion = fileVersion, + TraceLogger = TraceLogger + }; + var checkResult = checkLogic.Check(); + if (checkResult == false) + { + result.IsValid = false; + result.Description += checkLogic.CheckResult; + } + else + { + var currentVersion = ProgramSetting.GetCurrentFileVersion(); + TraceLogger.AddMessage($"File version is {fileVersion.VersionNumber}.{fileVersion.SubVersionNumber}, current version is {currentVersion.VersionNumber}.{currentVersion.SubVersionNumber}"); + } + } + return result; + } + } +} diff --git a/DataAccess/Infrastructures/FileSaveLogic.cs b/DataAccess/Infrastructures/FileSaveLogic.cs new file mode 100644 index 0000000..488c8be --- /dev/null +++ b/DataAccess/Infrastructures/FileSaveLogic.cs @@ -0,0 +1,114 @@ +using DataAccess.DTOs; +using DataAccess.JsonConverters; +using Newtonsoft.Json; +using StructureHelperCommon.Infrastructures.Interfaces; +using StructureHelperCommon.Infrastructures.Settings; +using StructureHelperCommon.Models; +using StructureHelperCommon.Models.Projects; +using StructureHelperCommon.Services.FileServices; + +namespace DataAccess.Infrastructures +{ + public class FileSaveLogic : IFileSaveLogic + { + private IFileVersion version; + private Dictionary<(Guid id, Type type), ISaveable> refDictinary; + + public IShiftTraceLogger? TraceLogger { get; set; } + + public void SaveFile(IProject project) + { + if (project.IsNewFile == true) + { + var result = SelectFileName(project); + if (result.IsValid == false) + { + TraceLogger?.AddMessage(result.Description); + return; + } + project.FullFileName = result.FileName; + project.IsNewFile = false; + } + SaveToFile(project); + } + + private SaveFileResult SelectFileName(IProject project) + { + FileDialogSaver saver = new(); + saver.InputData = new SaveDialogInputData() + { + FilterString = "StructureHelper project file (*.shpj)|*.shpj|All Files (*.*)|*.*", + InitialDirectory = project.FullFileName, + + }; + saver.TraceLogger = TraceLogger; + var saveResult = saver.SaveFile(); + return saveResult; + } + + private void SaveToFile(IProject project) + { + version = ProgramSetting.GetCurrentFileVersion(); + refDictinary = new Dictionary<(Guid id, Type type), ISaveable>(); + FileVersionDTO versionDTO = GetVersionDTO(); + var versionString = Serialize(versionDTO, TraceLogger); + try + { + File.WriteAllText(project.FullFileName, versionString); + TraceLogger?.AddMessage($"File {project.FullFileName} was saved successfully", TraceLogStatuses.Service); + } + catch (Exception ex) + { + TraceLogger?.AddMessage(ex.Message, TraceLogStatuses.Error); + } + } + + private FileVersionDTO GetVersionDTO() + { + FileVersionDTOConvertStrategy fileVersionDTOConvertStrategy = new() + { + TraceLogger = TraceLogger + }; + DictionaryConvertStrategy dictionaryConvertStrategy = new() + { + ConvertStrategy = fileVersionDTOConvertStrategy, + ReferenceDictionary = refDictinary, + TraceLogger = TraceLogger + }; + var versionDTO = dictionaryConvertStrategy.ConvertTo(version as FileVersion); + return versionDTO; + } + + private static string Serialize(object obj, IShiftTraceLogger logger) + { + var settings = new JsonSerializerSettings + { + Converters = new List + { + new FileVersionDTOJsonConverter(logger), // Add the specific converter + // Add other converters if needed + }, + Formatting = Formatting.Indented, + PreserveReferencesHandling = PreserveReferencesHandling.All, + MissingMemberHandling = MissingMemberHandling.Ignore, + TypeNameHandling = TypeNameHandling.All, + NullValueHandling = NullValueHandling.Include + }; + + return JsonConvert.SerializeObject(obj, settings); + } + + public void SaveFileAs(IProject project) + { + var tmpIsNew = project.IsNewFile; + var tmpFullFileName = project.FullFileName; + project.IsNewFile = true; + SaveFile(project); + if (project.IsNewFile == true) + { + project.IsNewFile = tmpIsNew; + project.FullFileName = tmpFullFileName; + } + } + } +} diff --git a/DataAccess/Infrastructures/IFileOpenLogic.cs b/DataAccess/Infrastructures/IFileOpenLogic.cs new file mode 100644 index 0000000..c9c45df --- /dev/null +++ b/DataAccess/Infrastructures/IFileOpenLogic.cs @@ -0,0 +1,10 @@ +using StructureHelperCommon.Infrastructures.Interfaces; +using StructureHelperCommon.Models.Projects; + +namespace DataAccess.Infrastructures +{ + public interface IFileOpenLogic : ILogic + { + OpenProjectResult OpenFile(); + } +} \ No newline at end of file diff --git a/DataAccess/Infrastructures/IFileSaveLogic.cs b/DataAccess/Infrastructures/IFileSaveLogic.cs new file mode 100644 index 0000000..4cd5f0a --- /dev/null +++ b/DataAccess/Infrastructures/IFileSaveLogic.cs @@ -0,0 +1,16 @@ +using StructureHelperCommon.Infrastructures.Interfaces; +using StructureHelperCommon.Models.Projects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DataAccess.Infrastructures +{ + public interface IFileSaveLogic : ILogic + { + void SaveFile(IProject project); + void SaveFileAs(IProject project); + } +} diff --git a/DataAccess/Infrastructures/IProjectAccessLogic.cs b/DataAccess/Infrastructures/IProjectAccessLogic.cs new file mode 100644 index 0000000..c5a59b8 --- /dev/null +++ b/DataAccess/Infrastructures/IProjectAccessLogic.cs @@ -0,0 +1,18 @@ +using StructureHelperCommon.Infrastructures.Interfaces; +using StructureHelperCommon.Models.Projects; +using StructureHelperCommon.Services.FileServices; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DataAccess.Infrastructures +{ + public interface IProjectAccessLogic : ILogic + { + OpenProjectResult OpenProject(); + void SaveProject(IProject project); + void SaveProjectAs(IProject project); + } +} diff --git a/DataAccess/Infrastructures/OpenProjectResult.cs b/DataAccess/Infrastructures/OpenProjectResult.cs new file mode 100644 index 0000000..614c452 --- /dev/null +++ b/DataAccess/Infrastructures/OpenProjectResult.cs @@ -0,0 +1,17 @@ +using StructureHelperCommon.Models.Calculators; +using StructureHelperCommon.Models.Projects; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DataAccess.Infrastructures +{ + public class OpenProjectResult : IResult + { + public bool IsValid { get; set; } = true; + public string? Description { get; set; } = string.Empty; + public IProject Project { get; set; } + } +} diff --git a/DataAccess/Infrastructures/ProjectAccessLogic.cs b/DataAccess/Infrastructures/ProjectAccessLogic.cs new file mode 100644 index 0000000..edeb7ec --- /dev/null +++ b/DataAccess/Infrastructures/ProjectAccessLogic.cs @@ -0,0 +1,47 @@ +using StructureHelperCommon.Models; +using StructureHelperCommon.Models.Projects; +using StructureHelperCommon.Services.FileServices; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DataAccess.Infrastructures +{ + public class ProjectAccessLogic : IProjectAccessLogic + { + private IFileOpenLogic openLogic; + private IFileSaveLogic saveLogic; + public IShiftTraceLogger? TraceLogger { get; set; } + + public ProjectAccessLogic(IFileOpenLogic openLogic, IFileSaveLogic saveLogic) + { + this.openLogic = openLogic; + this.saveLogic = saveLogic; + } + + public ProjectAccessLogic() : this(new FileOpenLogic(), new FileSaveLogic()) + { + + } + + public OpenProjectResult OpenProject() + { + openLogic.TraceLogger = TraceLogger; + return openLogic.OpenFile(); + } + + public void SaveProject(IProject project) + { + saveLogic.TraceLogger = TraceLogger; + saveLogic.SaveFile(project); + } + + public void SaveProjectAs(IProject project) + { + saveLogic.TraceLogger = TraceLogger; + saveLogic.SaveFileAs(project); + } + } +} diff --git a/DataAccess/JsonConverters/BaseConverter.cs b/DataAccess/JsonConverters/BaseConverter.cs deleted file mode 100644 index bb5c897..0000000 --- a/DataAccess/JsonConverters/BaseConverter.cs +++ /dev/null @@ -1,95 +0,0 @@ -using Newtonsoft.Json; -using Newtonsoft.Json.Linq; -using StructureHelperCommon.Models; -using System; -using System.Reflection; - -namespace DataAccess.JsonConverters -{ - - - public abstract class BaseConverter : JsonConverter - { - private IShiftTraceLogger traceLogger; - - protected BaseConverter(IShiftTraceLogger logger) - { - traceLogger = logger; - } - - public override void WriteJson(JsonWriter writer, T value, JsonSerializer serializer) - { - traceLogger.AddMessage($"Serializing {typeof(T).Name} (ID: {GetId(value)})"); - - // Use JsonSerializer's default behavior to handle attributes like [JsonIgnore] and [JsonProperty] - var jo = new JObject(); - foreach (var prop in typeof(T).GetProperties()) - { - if (!ShouldIgnoreProperty(prop)) - { - string propertyName = GetPropertyName(prop); - var propValue = prop.GetValue(value); - jo.Add(propertyName, JToken.FromObject(propValue, serializer)); - } - } - jo.WriteTo(writer); - } - - // Helper method to check if a property should be ignored - private bool ShouldIgnoreProperty(PropertyInfo prop) - { - // Check for [JsonIgnore] attribute - var jsonIgnoreAttribute = prop.GetCustomAttribute(); - return jsonIgnoreAttribute != null; - } - - // Helper method to get the property name, considering [JsonProperty] and [JsonPropertyName] attributes - private string GetPropertyName(PropertyInfo prop) - { - // Check for [JsonProperty] attribute (for Newtonsoft.Json) - var jsonPropertyAttribute = prop.GetCustomAttribute(); - if (jsonPropertyAttribute != null) - { - return jsonPropertyAttribute.PropertyName; - } - - // Check for [JsonPropertyName] attribute (for System.Text.Json compatibility) - var jsonPropertyNameAttribute = prop.GetCustomAttribute(); - if (jsonPropertyNameAttribute != null) - { - return jsonPropertyNameAttribute.Name; - } - - // Default to the property name if no attributes are found - return prop.Name; - } - - public override T ReadJson(JsonReader reader, Type objectType, T existingValue, bool hasExistingValue, JsonSerializer serializer) - { - traceLogger.AddMessage($"Deserializing {typeof(T).Name}"); - // Use JsonSerializer's default behavior to handle attributes during deserialization - JObject jo = JObject.Load(reader); - T obj = Activator.CreateInstance(); - - foreach (var prop in typeof(T).GetProperties()) - { - if (!ShouldIgnoreProperty(prop) && jo.TryGetValue(GetPropertyName(prop), out JToken value)) - { - var propValue = value.ToObject(prop.PropertyType, serializer); - prop.SetValue(obj, propValue); - } - } - - traceLogger.AddMessage($"Deserialized {typeof(T).Name} (ID: {GetId(obj)})"); - return obj; - } - - // Method to get the ID for logging purposes, assumes all classes have an 'Id' property of type Guid. - private Guid GetId(object obj) - { - var idProp = obj.GetType().GetProperty("Id"); - return idProp != null ? (Guid)idProp.GetValue(obj) : Guid.Empty; - } - } - -} diff --git a/DataAccess/JsonConverters/BaseJsonConverter.cs b/DataAccess/JsonConverters/BaseJsonConverter.cs new file mode 100644 index 0000000..13cae23 --- /dev/null +++ b/DataAccess/JsonConverters/BaseJsonConverter.cs @@ -0,0 +1,48 @@ +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using StructureHelperCommon.Models; +using System; +using System.Reflection; + +namespace DataAccess.JsonConverters +{ + + + public abstract class BaseJsonConverter : JsonConverter + { + private IWriteJsonLogic writeJsonLogic; + private IReadJsonLogic readJsonLogic; + + public IShiftTraceLogger TraceLogger { get; set; } + + + protected BaseJsonConverter(IShiftTraceLogger logger, IWriteJsonLogic writeJsonLogic, IReadJsonLogic readJsonLogic) + { + this.writeJsonLogic = writeJsonLogic; + this.readJsonLogic = readJsonLogic; + TraceLogger = logger; + } + + protected BaseJsonConverter(IShiftTraceLogger logger) + : this (logger, + new WriteJsonLogic() { TraceLogger = logger}, + new ReadJsonLogic() { TraceLogger = logger}) + { + } + + public override void WriteJson(JsonWriter writer, T? value, JsonSerializer serializer) + { + writeJsonLogic.TraceLogger = TraceLogger; + writeJsonLogic.WriteJson(writer, value, serializer); + } + + public override T ReadJson(JsonReader reader, Type objectType, T? existingValue, bool hasExistingValue, JsonSerializer serializer) + { + readJsonLogic.TraceLogger = TraceLogger; + return readJsonLogic.ReadJson(reader, objectType, existingValue, hasExistingValue, serializer); + } + + + } + +} diff --git a/DataAccess/JsonConverters/CrossSectionJsonConverter.cs b/DataAccess/JsonConverters/CrossSectionJsonConverter.cs index 33c3b81..9a361c5 100644 --- a/DataAccess/JsonConverters/CrossSectionJsonConverter.cs +++ b/DataAccess/JsonConverters/CrossSectionJsonConverter.cs @@ -8,7 +8,7 @@ using System.Threading.Tasks; namespace DataAccess.JsonConverters { - public class CrossSectionJsonConverter : BaseConverter + public class CrossSectionJsonConverter : BaseJsonConverter { public CrossSectionJsonConverter(IShiftTraceLogger logger) : base(logger) { diff --git a/DataAccess/JsonConverters/FileVersionDTOJsonConverter.cs b/DataAccess/JsonConverters/FileVersionDTOJsonConverter.cs new file mode 100644 index 0000000..48bf4d5 --- /dev/null +++ b/DataAccess/JsonConverters/FileVersionDTOJsonConverter.cs @@ -0,0 +1,17 @@ +using DataAccess.DTOs; +using StructureHelperCommon.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DataAccess.JsonConverters +{ + public class FileVersionDTOJsonConverter : BaseJsonConverter + { + public FileVersionDTOJsonConverter(IShiftTraceLogger logger) : base(logger) + { + } + } +} diff --git a/DataAccess/JsonConverters/Logics/GetIdFromObjectLogic.cs b/DataAccess/JsonConverters/Logics/GetIdFromObjectLogic.cs new file mode 100644 index 0000000..db07d56 --- /dev/null +++ b/DataAccess/JsonConverters/Logics/GetIdFromObjectLogic.cs @@ -0,0 +1,22 @@ +using StructureHelperCommon.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DataAccess.JsonConverters +{ + /// + public class GetIdFromObjectLogic : IGetIdFromObjectLogic + { + /// + public IShiftTraceLogger? TraceLogger { get; set; } + /// + public Guid GetId(object obj) + { + var idProp = obj.GetType().GetProperty("Id"); + return idProp != null ? (Guid)idProp.GetValue(obj) : Guid.Empty; + } + } +} diff --git a/DataAccess/JsonConverters/Logics/GetPropertyNameLogic.cs b/DataAccess/JsonConverters/Logics/GetPropertyNameLogic.cs new file mode 100644 index 0000000..09d85f3 --- /dev/null +++ b/DataAccess/JsonConverters/Logics/GetPropertyNameLogic.cs @@ -0,0 +1,38 @@ +using Newtonsoft.Json; +using StructureHelperCommon.Infrastructures.Interfaces; +using StructureHelperCommon.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace DataAccess.JsonConverters +{ + public class GetPropertyNameLogic : IGetPropertyNameLogic + { + public IShiftTraceLogger? TraceLogger { get; set; } + + // Helper method to get the property name, considering [JsonProperty] and [JsonPropertyName] attributes + public string GetPropertyName(PropertyInfo prop) + { + // Check for [JsonProperty] attribute (for Newtonsoft.Json) + var jsonPropertyAttribute = prop.GetCustomAttribute(); + if (jsonPropertyAttribute != null) + { + return jsonPropertyAttribute.PropertyName; + } + + // Check for [JsonPropertyName] attribute (for System.Text.Json compatibility) + var jsonPropertyNameAttribute = prop.GetCustomAttribute(); + if (jsonPropertyNameAttribute != null) + { + return jsonPropertyNameAttribute.Name; + } + + // Default to the property name if no attributes are found + return prop.Name; + } + } +} diff --git a/DataAccess/JsonConverters/Logics/IGetIdFromObjectLogic.cs b/DataAccess/JsonConverters/Logics/IGetIdFromObjectLogic.cs new file mode 100644 index 0000000..8b76c82 --- /dev/null +++ b/DataAccess/JsonConverters/Logics/IGetIdFromObjectLogic.cs @@ -0,0 +1,12 @@ +using StructureHelperCommon.Infrastructures.Interfaces; + +namespace DataAccess.JsonConverters +{ + /// + /// Logic to get the ID for logging purposes, assumes all classes have an 'Id' property of type Guid. + /// + public interface IGetIdFromObjectLogic : ILogic + { + Guid GetId(object obj); + } +} \ No newline at end of file diff --git a/DataAccess/JsonConverters/Logics/IGetPropertyNameLogic.cs b/DataAccess/JsonConverters/Logics/IGetPropertyNameLogic.cs new file mode 100644 index 0000000..3ea6e4f --- /dev/null +++ b/DataAccess/JsonConverters/Logics/IGetPropertyNameLogic.cs @@ -0,0 +1,13 @@ +using StructureHelperCommon.Infrastructures.Interfaces; +using System.Reflection; + +namespace DataAccess.JsonConverters +{ + /// + /// Helper logic to get the property name, considering [JsonProperty] and [JsonPropertyName] attributes + /// + public interface IGetPropertyNameLogic : ILogic + { + string GetPropertyName(PropertyInfo prop); + } +} \ No newline at end of file diff --git a/DataAccess/JsonConverters/Logics/IReadJsonLogic.cs b/DataAccess/JsonConverters/Logics/IReadJsonLogic.cs new file mode 100644 index 0000000..9e11f22 --- /dev/null +++ b/DataAccess/JsonConverters/Logics/IReadJsonLogic.cs @@ -0,0 +1,23 @@ +using Newtonsoft.Json; +using StructureHelperCommon.Infrastructures.Interfaces; + +namespace DataAccess.JsonConverters +{ + /// + /// Helper logic for JSON converter + /// + /// + public interface IReadJsonLogic : ILogic + { + /// + /// + /// + /// + /// + /// + /// + /// + /// + T ReadJson(JsonReader reader, Type objectType, T existingValue, bool hasExistingValue, JsonSerializer serializer); + } +} \ No newline at end of file diff --git a/DataAccess/JsonConverters/Logics/IShouldIgnorePropertyLogic.cs b/DataAccess/JsonConverters/Logics/IShouldIgnorePropertyLogic.cs new file mode 100644 index 0000000..4b79061 --- /dev/null +++ b/DataAccess/JsonConverters/Logics/IShouldIgnorePropertyLogic.cs @@ -0,0 +1,13 @@ +using StructureHelperCommon.Infrastructures.Interfaces; +using System.Reflection; + +namespace DataAccess.JsonConverters +{ + /// + /// Helper logic to check if a property should be ignored + /// + public interface IShouldIgnorePropertyLogic : ILogic + { + bool ShouldIgnoreProperty(PropertyInfo prop); + } +} \ No newline at end of file diff --git a/DataAccess/JsonConverters/Logics/IWriteJsonLogic.cs b/DataAccess/JsonConverters/Logics/IWriteJsonLogic.cs new file mode 100644 index 0000000..ffff7f6 --- /dev/null +++ b/DataAccess/JsonConverters/Logics/IWriteJsonLogic.cs @@ -0,0 +1,10 @@ +using Newtonsoft.Json; +using StructureHelperCommon.Infrastructures.Interfaces; + +namespace DataAccess.JsonConverters +{ + public interface IWriteJsonLogic : ILogic + { + void WriteJson(JsonWriter writer, T value, JsonSerializer serializer); + } +} \ No newline at end of file diff --git a/DataAccess/JsonConverters/Logics/ReadJsonLogic.cs b/DataAccess/JsonConverters/Logics/ReadJsonLogic.cs new file mode 100644 index 0000000..3ed8436 --- /dev/null +++ b/DataAccess/JsonConverters/Logics/ReadJsonLogic.cs @@ -0,0 +1,56 @@ +using Newtonsoft.Json.Linq; +using Newtonsoft.Json; +using StructureHelperCommon.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DataAccess.JsonConverters +{ + public class ReadJsonLogic : IReadJsonLogic + { + private IShouldIgnorePropertyLogic shouldIgnorePropertyLogic; + private IGetPropertyNameLogic getPropertyNameLogic; + private IGetIdFromObjectLogic getIdFromObjectLogic; + public IShiftTraceLogger? TraceLogger { get; set; } + + public ReadJsonLogic(IShouldIgnorePropertyLogic shouldIgnorePropertyLogic, + IGetPropertyNameLogic getPropertyNameLogic, + IGetIdFromObjectLogic getIdFromObjectLogic) + { + this.shouldIgnorePropertyLogic = shouldIgnorePropertyLogic; + this.getPropertyNameLogic = getPropertyNameLogic; + this.getIdFromObjectLogic = getIdFromObjectLogic; + } + public ReadJsonLogic() + : this(new ShouldIgnorePropertyLogic(), + new GetPropertyNameLogic(), + new GetIdFromObjectLogic()) + { + + } + + public T ReadJson(JsonReader reader, Type objectType, T existingValue, bool hasExistingValue, JsonSerializer serializer) + { + TraceLogger?.AddMessage($"Deserializing {typeof(T).Name}"); + shouldIgnorePropertyLogic.TraceLogger = getPropertyNameLogic.TraceLogger = getIdFromObjectLogic.TraceLogger = TraceLogger; + // Use JsonSerializer's default behavior to handle attributes during deserialization + JObject jo = JObject.Load(reader); + T obj = Activator.CreateInstance(); + + foreach (var prop in typeof(T).GetProperties()) + { + if (! shouldIgnorePropertyLogic.ShouldIgnoreProperty(prop) && jo.TryGetValue(getPropertyNameLogic.GetPropertyName(prop), out JToken value)) + { + var propValue = value.ToObject(prop.PropertyType, serializer); + prop.SetValue(obj, propValue); + } + } + + TraceLogger?.AddMessage($"Deserialized {typeof(T).Name} (ID: {getIdFromObjectLogic.GetId(obj)})"); + return obj; + } + } +} diff --git a/DataAccess/JsonConverters/Logics/ShouldIgnorePropertyLogic.cs b/DataAccess/JsonConverters/Logics/ShouldIgnorePropertyLogic.cs new file mode 100644 index 0000000..1e6d912 --- /dev/null +++ b/DataAccess/JsonConverters/Logics/ShouldIgnorePropertyLogic.cs @@ -0,0 +1,22 @@ +using Newtonsoft.Json; +using StructureHelperCommon.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Reflection; +using System.Text; +using System.Threading.Tasks; + +namespace DataAccess.JsonConverters +{ + public class ShouldIgnorePropertyLogic : IShouldIgnorePropertyLogic + { + public IShiftTraceLogger? TraceLogger { get; set; } + public bool ShouldIgnoreProperty(PropertyInfo prop) + { + // Check for [JsonIgnore] attribute + var jsonIgnoreAttribute = prop.GetCustomAttribute(); + return jsonIgnoreAttribute != null; + } + } +} diff --git a/DataAccess/JsonConverters/Logics/WriteJsonLogic.cs b/DataAccess/JsonConverters/Logics/WriteJsonLogic.cs new file mode 100644 index 0000000..59fee0e --- /dev/null +++ b/DataAccess/JsonConverters/Logics/WriteJsonLogic.cs @@ -0,0 +1,54 @@ +using Newtonsoft.Json.Linq; +using Newtonsoft.Json; +using StructureHelperCommon.Infrastructures.Interfaces; +using StructureHelperCommon.Models; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DataAccess.JsonConverters +{ + public class WriteJsonLogic : IWriteJsonLogic + { + private IShouldIgnorePropertyLogic shouldIgnorePropertyLogic; + private IGetPropertyNameLogic getPropertyNameLogic; + private IGetIdFromObjectLogic getIdFromObjectLogic; + + public IShiftTraceLogger? TraceLogger { get; set; } + public WriteJsonLogic(IShouldIgnorePropertyLogic shouldIgnorePropertyLogic, + IGetPropertyNameLogic getPropertyNameLogic, + IGetIdFromObjectLogic getIdFromObjectLogic) + { + this.shouldIgnorePropertyLogic = shouldIgnorePropertyLogic; + this.getPropertyNameLogic = getPropertyNameLogic; + this.getIdFromObjectLogic = getIdFromObjectLogic; + } + public WriteJsonLogic() + : this(new ShouldIgnorePropertyLogic(), + new GetPropertyNameLogic(), + new GetIdFromObjectLogic()) + { + + } + public void WriteJson(JsonWriter writer, T value, JsonSerializer serializer) + { + TraceLogger?.AddMessage($"Serializing {typeof(T).Name} (ID: {getIdFromObjectLogic.GetId(value)})"); + shouldIgnorePropertyLogic.TraceLogger = getPropertyNameLogic.TraceLogger = getIdFromObjectLogic.TraceLogger = TraceLogger; + + // Use JsonSerializer's default behavior to handle attributes like [JsonIgnore] and [JsonProperty] + var jo = new JObject(); + foreach (var prop in typeof(T).GetProperties()) + { + if (!shouldIgnorePropertyLogic.ShouldIgnoreProperty(prop)) + { + string propertyName = getPropertyNameLogic.GetPropertyName(prop); + var propValue = prop.GetValue(value); + jo.Add(propertyName, JToken.FromObject(propValue, serializer)); + } + } + jo.WriteTo(writer); + } + } +} diff --git a/StructureHelper/StructureHelper.csproj b/StructureHelper/StructureHelper.csproj index a5adeb8..1a0483e 100644 --- a/StructureHelper/StructureHelper.csproj +++ b/StructureHelper/StructureHelper.csproj @@ -54,16 +54,17 @@ - + - - - + + + + diff --git a/StructureHelper/Windows/CalculationWindows/ProgressViews/TraceDocumentView.xaml b/StructureHelper/Windows/CalculationWindows/ProgressViews/TraceDocumentView.xaml index 0e8b5c0..5d8f958 100644 --- a/StructureHelper/Windows/CalculationWindows/ProgressViews/TraceDocumentView.xaml +++ b/StructureHelper/Windows/CalculationWindows/ProgressViews/TraceDocumentView.xaml @@ -6,7 +6,7 @@ xmlns:local="clr-namespace:StructureHelper.Windows.CalculationWindows.ProgressViews" d:DataContext="{d:DesignInstance local:TraceDocumentVM}" mc:Ignorable="d" - Title="Trace Document Viewer" Height="450" Width="800" MinHeight="400" MinWidth="600" WindowStartupLocation="CenterScreen" ShowInTaskbar="False"> + Title="Trace Document Viewer" Height="450" Width="800" MinHeight="400" MinWidth="600" WindowStartupLocation="CenterScreen"> diff --git a/StructureHelper/Windows/CalculationWindows/ProgressViews/TraceDocumentView.xaml.cs b/StructureHelper/Windows/CalculationWindows/ProgressViews/TraceDocumentView.xaml.cs index 655eb25..ec27e0e 100644 --- a/StructureHelper/Windows/CalculationWindows/ProgressViews/TraceDocumentView.xaml.cs +++ b/StructureHelper/Windows/CalculationWindows/ProgressViews/TraceDocumentView.xaml.cs @@ -18,6 +18,8 @@ namespace StructureHelper.Windows.CalculationWindows.ProgressViews this.viewModel.DocumentReader = this.DocumentReader; this.viewModel.Show(); } + public TraceDocumentView(ITraceLogger traceLogger) : this (traceLogger.TraceLoggerEntries) { } + public TraceDocumentView(IEnumerable loggerEntries) : this(new TraceDocumentVM(loggerEntries)) { } } } diff --git a/StructureHelper/Windows/MainWindow/Analyses/AnalysesLogic.cs b/StructureHelper/Windows/MainWindow/Analyses/AnalysesLogic.cs index 39ecff6..60526ac 100644 --- a/StructureHelper/Windows/MainWindow/Analyses/AnalysesLogic.cs +++ b/StructureHelper/Windows/MainWindow/Analyses/AnalysesLogic.cs @@ -1,5 +1,7 @@ using StructureHelper.Infrastructure; using StructureHelper.Windows.MainWindow.Analyses; +using StructureHelperCommon.Infrastructures.Settings; +using StructureHelperCommon.Models.Analyses; using StructureHelperLogic.Models.Analyses; using System; using System.Collections.Generic; @@ -21,7 +23,6 @@ namespace StructureHelper.Windows.MainWindow public IVisualAnalysis? SelectedAnalysis { get; set; } - public List AnalysesList { get; } public ObservableCollection FilteredAnalyses { get; } public RelayCommand AddAnalysisCommand { @@ -74,13 +75,12 @@ namespace StructureHelper.Windows.MainWindow public AnalysesLogic() { - AnalysesList = new(); FilteredAnalyses = new(); } public void Refresh() { FilteredAnalyses.Clear(); - var analysesList = AnalysesList.ToList(); + var analysesList = ProgramSetting.CurrentProject.VisualAnalyses.ToList(); foreach (var analysis in analysesList) { FilteredAnalyses.Add(analysis); @@ -108,7 +108,7 @@ namespace StructureHelper.Windows.MainWindow var dialogResult = MessageBox.Show("Delete analysis?", "Please, confirm deleting", MessageBoxButtons.YesNo, MessageBoxIcon.Warning); if (dialogResult == DialogResult.Yes) { - AnalysesList.Remove(SelectedAnalysis); + ProgramSetting.CurrentProject.VisualAnalyses.Remove(SelectedAnalysis); } } } @@ -122,7 +122,7 @@ namespace StructureHelper.Windows.MainWindow analysis.Name = "New NDM Analysis"; analysis.Tags = "#New group"; var visualAnalysis = new VisualAnalysis(analysis); - AnalysesList.Add(visualAnalysis); + ProgramSetting.CurrentProject.VisualAnalyses.Add(visualAnalysis); } } } diff --git a/StructureHelper/Windows/MainWindow/Analyses/FileLogic.cs b/StructureHelper/Windows/MainWindow/Analyses/FileLogic.cs new file mode 100644 index 0000000..d7d4dcd --- /dev/null +++ b/StructureHelper/Windows/MainWindow/Analyses/FileLogic.cs @@ -0,0 +1,147 @@ +using DataAccess.Infrastructures; +using StructureHelper.Infrastructure; +using StructureHelper.Windows.CalculationWindows.ProgressViews; +using StructureHelperCommon.Infrastructures.Settings; +using StructureHelperCommon.Models; +using StructureHelperCommon.Models.Projects; +using System; +using System.Linq; +using System.Windows.Forms; +using System.Windows.Input; + +namespace StructureHelper.Windows.MainWindow +{ + public class FileLogic : ViewModelBase + { + private ICommand fileOpen; + private ICommand fileSave; + private IShiftTraceLogger traceLogger; + + private RelayCommand fileNew; + private RelayCommand fileClose; + private IProjectAccessLogic projectAccessLogic; + private RelayCommand fileSaveAs; + + public FileLogic(IProjectAccessLogic projectAccessLogic) + { + this.projectAccessLogic = projectAccessLogic; + } + + public FileLogic() : this(new ProjectAccessLogic()) + { + + } + + public ICommand FileOpen => fileOpen ??= new RelayCommand(obj => OpenFile()); + public ICommand FileNew => fileNew ??= new RelayCommand(obj => NewFile()); + public ICommand ProgramExit => fileNew ??= new RelayCommand(obj => ExitProgram()); + public ICommand FileClose => fileClose ??= new RelayCommand(obj => CloseFile()); + public ICommand FileSave => fileSave ??= new RelayCommand(obj => SaveFile()); + public ICommand FileSaveAs => fileSaveAs ??= new RelayCommand(obj => SaveAsFile()); + + public void NewFile() + { + var closeResult = CloseFile(); + if (closeResult == false) + { + return; + } + var newProject = CreateNewFile(); + ProgramSetting.Projects.Add(newProject); + } + public IProject CreateNewFile() + { + var newProject = new Project() + { + IsNewFile = true, + IsActual = true + }; + ProgramSetting.Projects.Add(newProject); + return newProject; + } + private void SaveAsFile() + { + var project = ProgramSetting.CurrentProject; + traceLogger = new ShiftTraceLogger(); + projectAccessLogic.TraceLogger = traceLogger; + projectAccessLogic.SaveProjectAs(project); + ShowEntries(); + } + private void ExitProgram() + { + foreach (var project in ProgramSetting.Projects) + { + CloseFile(project); + } + } + private bool CloseFile() + { + var project = ProgramSetting.CurrentProject; + if (project is null) { return false; } + return CloseFile(project); + } + private bool CloseFile(IProject project) + { + if (project.IsActual == true & project.IsNewFile == false) + { + ProgramSetting.Projects.Remove(project); + return true; + } + var dialogResult = MessageBox.Show($"Save file?", $"File {project.FullFileName} is not saved", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning); + if (dialogResult == DialogResult.Yes) + { + SaveFile(project); + } + if (dialogResult == DialogResult.Cancel) + { + return false; + } + project.IsActual = true; + project.IsNewFile = false; + CloseFile(project); + return true; + } + private void SaveFile() + { + var project = ProgramSetting.CurrentProject; + SaveFile(project); + } + private void SaveFile(IProject project) + { + traceLogger = new ShiftTraceLogger(); + projectAccessLogic.TraceLogger = traceLogger; + projectAccessLogic.SaveProject(project); + ShowEntries(); + } + private void OpenFile() + { + var currentProject = ProgramSetting.CurrentProject; + var closeResult = CloseFile(); + if (closeResult == false) + { + return; + } + traceLogger = new ShiftTraceLogger(); + projectAccessLogic.TraceLogger = traceLogger; + var result = projectAccessLogic.OpenProject(); + if (result.IsValid == true) + { + ProgramSetting.Projects.Add(result.Project); + } + else + { + ProgramSetting.Projects.Add(currentProject); + } + ShowEntries(); + } + private void ShowEntries() + { + var filteredEntries = traceLogger.TraceLoggerEntries.Where(x => x.Priority <= 300); + if (filteredEntries.Any()) + { + var wnd = new TraceDocumentView(traceLogger); + wnd.ShowDialog(); + } + } + } +} diff --git a/StructureHelper/Windows/MainWindow/Analyses/IVisualAnalysis.cs b/StructureHelper/Windows/MainWindow/Analyses/IVisualAnalysis.cs deleted file mode 100644 index ccb522b..0000000 --- a/StructureHelper/Windows/MainWindow/Analyses/IVisualAnalysis.cs +++ /dev/null @@ -1,15 +0,0 @@ -using StructureHelperCommon.Models.Analyses; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace StructureHelper.Windows.MainWindow.Analyses -{ - public interface IVisualAnalysis - { - IAnalysis Analysis {get;set;} - void Run(); - } -} diff --git a/StructureHelper/Windows/MainWindow/Analyses/VisualAnalysis.cs b/StructureHelper/Windows/MainWindow/Analyses/VisualAnalysis.cs index f3d3d70..c90b5ed 100644 --- a/StructureHelper/Windows/MainWindow/Analyses/VisualAnalysis.cs +++ b/StructureHelper/Windows/MainWindow/Analyses/VisualAnalysis.cs @@ -2,22 +2,26 @@ using StructureHelperCommon.Models.Analyses; using StructureHelperLogics.Models.CrossSections; using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; namespace StructureHelper.Windows.MainWindow.Analyses { public class VisualAnalysis : IVisualAnalysis { + public Guid Id { get; } public IAnalysis Analysis { get; set; } - public VisualAnalysis(IAnalysis analysis) + + public VisualAnalysis(Guid id, IAnalysis analysis) { + Id = id; Analysis = analysis; } + public VisualAnalysis(IAnalysis analysis) : this (Guid.NewGuid(), analysis) + { + + } + public void Run() { var version = Analysis.VersionProcessor.GetCurrentVersion(); diff --git a/StructureHelper/Windows/MainWindow/AnalysesManagerView.xaml b/StructureHelper/Windows/MainWindow/AnalysesManagerView.xaml index 2cda902..3ca9f66 100644 --- a/StructureHelper/Windows/MainWindow/AnalysesManagerView.xaml +++ b/StructureHelper/Windows/MainWindow/AnalysesManagerView.xaml @@ -9,16 +9,24 @@ Title="Analyses Manager" Height="450" Width="800" MinHeight="400" MinWidth="600" WindowStartupLocation="CenterScreen"> + + + + + + + +