diff --git a/DataAccess/DTOs/Converters/AnalysisFromDTOConvertStrategy.cs b/DataAccess/DTOs/Converters/AnalysisFromDTOConvertStrategy.cs new file mode 100644 index 0000000..4f9e6ca --- /dev/null +++ b/DataAccess/DTOs/Converters/AnalysisFromDTOConvertStrategy.cs @@ -0,0 +1,79 @@ +using StructureHelperCommon.Infrastructures.Exceptions; +using StructureHelperCommon.Infrastructures.Interfaces; +using StructureHelperCommon.Models; +using StructureHelperCommon.Models.Analyses; +using StructureHelperCommon.Models.Loggers; +using StructureHelperLogic.Models.Analyses; +using StructureHelperLogics.NdmCalculations.Analyses.ByForces.LimitCurve; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace DataAccess.DTOs.Converters +{ + public class AnalysisFromDTOConvertStrategy : IConvertStrategy + { + private const string Message = "Analysis type is"; + private IConvertStrategy convertCrossSectionNdmAnalysisStrategy = new CrossSectionNdmAnalysisFromDTOConvertStrategy(); + + public Dictionary<(Guid id, Type type), ISaveable> ReferenceDictionary { get; set; } + public IShiftTraceLogger TraceLogger { get; set; } + + public IAnalysis Convert(IAnalysis source) + { + Check(); + try + { + IAnalysis analysis = GetAnalysis(source); + return analysis; + } + catch (Exception ex) + { + TraceLogger?.AddMessage(LoggerStrings.LogicType(this), TraceLogStatuses.Error); + TraceLogger?.AddMessage(ex.Message, TraceLogStatuses.Error); + throw; + } + + } + + private IAnalysis GetAnalysis(IAnalysis source) + { + IAnalysis analysis; + if (source is ICrossSectionNdmAnalysis crossSectionNdmAnalysis) + { + analysis = GetCrossSectionNdmAnalysis(crossSectionNdmAnalysis); + } + else + { + string errorString = ErrorStrings.ObjectTypeIsUnknownObj(source); + TraceLogger?.AddMessage(errorString, TraceLogStatuses.Error); + throw new StructureHelperException(errorString); + } + foreach (var item in source.VersionProcessor.Versions) + { + //to do + } + + return analysis; + } + + private ICrossSectionNdmAnalysis GetCrossSectionNdmAnalysis(ICrossSectionNdmAnalysis source) + { + TraceLogger?.AddMessage(Message + " Cross-Section Ndm Analysis", TraceLogStatuses.Debug); + convertCrossSectionNdmAnalysisStrategy.ReferenceDictionary = ReferenceDictionary; + convertCrossSectionNdmAnalysisStrategy.TraceLogger = TraceLogger; + var convertLogic = new DictionaryConvertStrategy(this, convertCrossSectionNdmAnalysisStrategy); + ICrossSectionNdmAnalysis crossSectionNdmAnalysis = convertLogic.Convert(source); + return crossSectionNdmAnalysis; + } + + private void Check() + { + var checkLogic = new CheckConvertLogic(this); + checkLogic.Check(); + } + } +} diff --git a/DataAccess/DTOs/Converters/AnalysisToDTOConvertStrategy.cs b/DataAccess/DTOs/Converters/AnalysisToDTOConvertStrategy.cs index 2b7ce1a..c7fdcff 100644 --- a/DataAccess/DTOs/Converters/AnalysisToDTOConvertStrategy.cs +++ b/DataAccess/DTOs/Converters/AnalysisToDTOConvertStrategy.cs @@ -37,7 +37,7 @@ namespace DataAccess.DTOs.Converters } foreach (var item in source.VersionProcessor.Versions) { - + //to do } return analysis; } diff --git a/DataAccess/DTOs/Converters/CrossSectionNdmAnalysisFromDTOConvertStrategy.cs b/DataAccess/DTOs/Converters/CrossSectionNdmAnalysisFromDTOConvertStrategy.cs new file mode 100644 index 0000000..c270d18 --- /dev/null +++ b/DataAccess/DTOs/Converters/CrossSectionNdmAnalysisFromDTOConvertStrategy.cs @@ -0,0 +1,43 @@ +using StructureHelperCommon.Infrastructures.Interfaces; +using StructureHelperCommon.Models; +using StructureHelperCommon.Models.Loggers; +using StructureHelperLogic.Models.Analyses; +using StructureHelperLogics.Models.Analyses; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DataAccess.DTOs.Converters +{ + public class CrossSectionNdmAnalysisFromDTOConvertStrategy : IConvertStrategy + { + private IUpdateStrategy updateStrategy = new CrossSectionNdmAnalysisUpdateStrategy(); + public Dictionary<(Guid id, Type type), ISaveable> ReferenceDictionary { get; set; } + public IShiftTraceLogger TraceLogger { get; set; } + + public ICrossSectionNdmAnalysis Convert(ICrossSectionNdmAnalysis source) + { + try + { + CrossSectionNdmAnalysis newItem = GetCrossSectinNDMAnalysis(source); + return newItem; + } + catch (Exception ex) + { + TraceLogger?.AddMessage(LoggerStrings.LogicType(this), TraceLogStatuses.Error); + TraceLogger?.AddMessage(ex.Message, TraceLogStatuses.Error); + throw; + } + + } + + private CrossSectionNdmAnalysis GetCrossSectinNDMAnalysis(ICrossSectionNdmAnalysis source) + { + CrossSectionNdmAnalysis newItem = new(source.Id); + updateStrategy.Update(newItem, source); + return newItem; + } + } +} diff --git a/DataAccess/DTOs/Converters/CrossSectionNdmAnalysisToDTOConvertStrategy.cs b/DataAccess/DTOs/Converters/CrossSectionNdmAnalysisToDTOConvertStrategy.cs index f7be3a2..8a8cb7d 100644 --- a/DataAccess/DTOs/Converters/CrossSectionNdmAnalysisToDTOConvertStrategy.cs +++ b/DataAccess/DTOs/Converters/CrossSectionNdmAnalysisToDTOConvertStrategy.cs @@ -28,7 +28,8 @@ namespace DataAccess.DTOs.Converters this.TraceLogger = traceLogger; } - public CrossSectionNdmAnalysisToDTOConvertStrategy() : this(new CrossSectionNdmAnalysisUpdateStrategy(), + public CrossSectionNdmAnalysisToDTOConvertStrategy() : this( + new CrossSectionNdmAnalysisUpdateStrategy(), new VersionProcessorToDTOConvertStrategy(), null) { @@ -55,9 +56,7 @@ namespace DataAccess.DTOs.Converters private void Check() { - var checkLogic = new CheckConvertLogic(); - checkLogic.ConvertStrategy = this; - checkLogic.TraceLogger = TraceLogger; + var checkLogic = new CheckConvertLogic(this); checkLogic.Check(); } } diff --git a/DataAccess/DTOs/Converters/LibMaterialToDTOConvertStrategy.cs b/DataAccess/DTOs/Converters/LibMaterialToDTOConvertStrategy.cs index d4a9b7d..2f66856 100644 --- a/DataAccess/DTOs/Converters/LibMaterialToDTOConvertStrategy.cs +++ b/DataAccess/DTOs/Converters/LibMaterialToDTOConvertStrategy.cs @@ -33,7 +33,7 @@ namespace DataAccess.DTOs } catch (Exception ex) { - TraceLogger?.AddMessage(LoggerStrings.LogicType(this), TraceLogStatuses.Debug); + TraceLogger?.AddMessage(LoggerStrings.LogicType(this), TraceLogStatuses.Error); TraceLogger?.AddMessage(ex.Message, TraceLogStatuses.Error); throw; } diff --git a/DataAccess/DTOs/Converters/ProjectFromDTOConvertStrategy.cs b/DataAccess/DTOs/Converters/ProjectFromDTOConvertStrategy.cs new file mode 100644 index 0000000..8e03426 --- /dev/null +++ b/DataAccess/DTOs/Converters/ProjectFromDTOConvertStrategy.cs @@ -0,0 +1,72 @@ +using StructureHelperCommon.Infrastructures.Interfaces; +using StructureHelperCommon.Models; +using StructureHelperCommon.Models.Analyses; +using StructureHelperCommon.Models.Loggers; +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 ProjectFromDTOConvertStrategy : IConvertStrategy + { + private IUpdateStrategy updateStrategy; + private IConvertStrategy visualAnalysisConvertStrategy = new VisualAnaysisFromDTOConvertStrategy(); + + public ProjectFromDTOConvertStrategy(IUpdateStrategy updateStrategy) + { + this.updateStrategy = updateStrategy; + } + + public ProjectFromDTOConvertStrategy() : this (new ProjectUpdateStrategy()) + { + + } + + public Dictionary<(Guid id, Type type), ISaveable> ReferenceDictionary { get; set; } + public IShiftTraceLogger TraceLogger { get; set; } + + public Project Convert(ProjectDTO source) + { + Check(); + TraceLogger?.AddMessage("Converting project is started", TraceLogStatuses.Info); + try + { + Project newItem = GetProject(source); + return newItem; + } + catch (Exception ex) + { + TraceLogger?.AddMessage(LoggerStrings.LogicType(this), TraceLogStatuses.Error); + TraceLogger?.AddMessage(ex.Message, TraceLogStatuses.Error); + throw; + } + } + + private Project GetProject(ProjectDTO source) + { + Project newItem = new(); + updateStrategy.Update(newItem, source); + visualAnalysisConvertStrategy.ReferenceDictionary = ReferenceDictionary; + visualAnalysisConvertStrategy.TraceLogger = TraceLogger; + var convertLogic = new DictionaryConvertStrategy(this, visualAnalysisConvertStrategy); + newItem.VisualAnalyses.Clear(); + foreach (var item in source.VisualAnalyses) + { + var visualAnalysis = convertLogic.Convert(item); + newItem.VisualAnalyses.Add(visualAnalysis); + } + TraceLogger?.AddMessage("Converting project has completed succesfully", TraceLogStatuses.Info); + return newItem; + } + + private void Check() + { + var checkLogic = new CheckConvertLogic(this); + checkLogic.Check(); + } + } +} diff --git a/DataAccess/DTOs/Converters/VisualAnalysisToDTOConvertStrategy.cs b/DataAccess/DTOs/Converters/VisualAnalysisToDTOConvertStrategy.cs index c1be1ad..d0cc426 100644 --- a/DataAccess/DTOs/Converters/VisualAnalysisToDTOConvertStrategy.cs +++ b/DataAccess/DTOs/Converters/VisualAnalysisToDTOConvertStrategy.cs @@ -2,6 +2,7 @@ using StructureHelperCommon.Infrastructures.Interfaces; using StructureHelperCommon.Models; using StructureHelperCommon.Models.Analyses; +using StructureHelperCommon.Models.Loggers; using StructureHelperCommon.Models.Projects; using StructureHelperLogic.Models.Analyses; using System; @@ -32,13 +33,28 @@ namespace DataAccess.DTOs public VisualAnalysisDTO Convert(IVisualAnalysis source) { Check(); + try + { + VisualAnalysisDTO visualAnalysisDTO = GetNewAnalysis(source); + return visualAnalysisDTO; + } + catch (Exception ex) + { + TraceLogger?.AddMessage(LoggerStrings.LogicType(this), TraceLogStatuses.Error); + TraceLogger?.AddMessage(ex.Message, TraceLogStatuses.Error); + throw; + } + } + + private VisualAnalysisDTO GetNewAnalysis(IVisualAnalysis source) + { VisualAnalysisDTO visualAnalysisDTO = new() { Id = source.Id }; convertStrategy.ReferenceDictionary = ReferenceDictionary; convertStrategy.TraceLogger = TraceLogger; - var convertLogic = new DictionaryConvertStrategy() + var convertLogic = new DictionaryConvertStrategy(this, convertStrategy) { ReferenceDictionary = ReferenceDictionary, ConvertStrategy = convertStrategy, diff --git a/DataAccess/DTOs/Converters/VisualAnaysisFromDTOConvertStrategy.cs b/DataAccess/DTOs/Converters/VisualAnaysisFromDTOConvertStrategy.cs new file mode 100644 index 0000000..e098651 --- /dev/null +++ b/DataAccess/DTOs/Converters/VisualAnaysisFromDTOConvertStrategy.cs @@ -0,0 +1,53 @@ +using DataAccess.DTOs.Converters; +using StructureHelperCommon.Infrastructures.Interfaces; +using StructureHelperCommon.Models; +using StructureHelperCommon.Models.Analyses; +using StructureHelperCommon.Models.Loggers; +using StructureHelperLogic.Models.Analyses; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace DataAccess.DTOs +{ + public class VisualAnaysisFromDTOConvertStrategy : IConvertStrategy + { + private IConvertStrategy analysisConvertStrategy = new AnalysisFromDTOConvertStrategy(); + public Dictionary<(Guid id, Type type), ISaveable> ReferenceDictionary { get; set; } + public IShiftTraceLogger TraceLogger { get; set; } + + public IVisualAnalysis Convert(IVisualAnalysis source) + { + Check(); + try + { + VisualAnalysis newItem = GetAnalysis(source); + return newItem; + } + catch (Exception ex) + { + TraceLogger?.AddMessage(LoggerStrings.LogicType(this), TraceLogStatuses.Error); + TraceLogger?.AddMessage(ex.Message, TraceLogStatuses.Error); + throw; + } + } + + private VisualAnalysis GetAnalysis(IVisualAnalysis source) + { + analysisConvertStrategy.ReferenceDictionary = ReferenceDictionary; + analysisConvertStrategy.TraceLogger = TraceLogger; + IAnalysis analysis = analysisConvertStrategy.Convert(source.Analysis); + VisualAnalysis newItem = new(source.Id, analysis); + TraceLogger?.AddMessage($"Visual Analysis was obtained succesfully", TraceLogStatuses.Debug); + return newItem; + } + + private void Check() + { + var checkLogic = new CheckConvertLogic(this); + checkLogic.Check(); + } + } +} diff --git a/DataAccess/DTOs/DTOEntities/AccuracyDTO.cs b/DataAccess/DTOs/DTOEntities/AccuracyDTO.cs index e3108a5..250bf18 100644 --- a/DataAccess/DTOs/DTOEntities/AccuracyDTO.cs +++ b/DataAccess/DTOs/DTOEntities/AccuracyDTO.cs @@ -11,7 +11,7 @@ namespace DataAccess.DTOs.DTOEntities public class AccuracyDTO : IAccuracy { [JsonProperty("Id")] - public Guid Id { get; set; } + public Guid Id { get; set; } = Guid.NewGuid(); [JsonProperty("IterationAccuracy")] public double IterationAccuracy { get; set; } [JsonProperty("MaxIterationCount")] diff --git a/DataAccess/DTOs/DTOEntities/ConcreteLibMaterialDTO.cs b/DataAccess/DTOs/DTOEntities/ConcreteLibMaterialDTO.cs index 778634d..9e6e77a 100644 --- a/DataAccess/DTOs/DTOEntities/ConcreteLibMaterialDTO.cs +++ b/DataAccess/DTOs/DTOEntities/ConcreteLibMaterialDTO.cs @@ -18,6 +18,7 @@ namespace DataAccess.DTOs { const MaterialTypes materialType = MaterialTypes.Concrete; + [JsonProperty("Id")] public Guid Id { get; set; } [JsonProperty("RelativeHumidity")] @@ -29,7 +30,11 @@ namespace DataAccess.DTOs [JsonProperty("MaterialEntityId")] public Guid MaterialEntityId { - get => MaterialEntity.Id; + get + { + return MaterialEntity.Id; + } + set { MaterialEntity = ProgramSetting.MaterialRepository.Repository.Single(x => x.Id == value); diff --git a/DataAccess/DTOs/DTOEntities/CrackCalculatorInputDataDTO.cs b/DataAccess/DTOs/DTOEntities/CrackCalculatorInputDataDTO.cs index 8ae3ad1..a9c2343 100644 --- a/DataAccess/DTOs/DTOEntities/CrackCalculatorInputDataDTO.cs +++ b/DataAccess/DTOs/DTOEntities/CrackCalculatorInputDataDTO.cs @@ -8,7 +8,7 @@ namespace DataAccess.DTOs public class CrackCalculatorInputDataDTO : ICrackCalculatorInputData { [JsonProperty("Id")] - public Guid Id { get; set; } + public Guid Id { get; set; } = Guid.NewGuid(); [JsonProperty("ForceActions")] public List ForceActions { get; set; } = new(); [JsonProperty("ForcePrimitives")] diff --git a/DataAccess/DTOs/DTOEntities/CrossSectionNdmAnalysisDTO.cs b/DataAccess/DTOs/DTOEntities/CrossSectionNdmAnalysisDTO.cs index 18a0f51..7656bb2 100644 --- a/DataAccess/DTOs/DTOEntities/CrossSectionNdmAnalysisDTO.cs +++ b/DataAccess/DTOs/DTOEntities/CrossSectionNdmAnalysisDTO.cs @@ -1,6 +1,8 @@ using Newtonsoft.Json; +using StructureHelperCommon.Infrastructures.Interfaces; using StructureHelperCommon.Models.Analyses; using StructureHelperLogic.Models.Analyses; +using StructureHelperLogics.Models.Analyses; using System; using System.Collections.Generic; using System.Linq; @@ -22,7 +24,7 @@ namespace DataAccess.DTOs public object Clone() { - throw new NotImplementedException(); + return this; } } } diff --git a/DataAccess/DTOs/DTOEntities/FileVersionDTO.cs b/DataAccess/DTOs/DTOEntities/FileVersionDTO.cs index 88e91e9..ee6eb45 100644 --- a/DataAccess/DTOs/DTOEntities/FileVersionDTO.cs +++ b/DataAccess/DTOs/DTOEntities/FileVersionDTO.cs @@ -23,5 +23,9 @@ namespace DataAccess.DTOs { Id = id; } + public FileVersionDTO() : this (Guid.NewGuid()) + { + + } } } diff --git a/DataAccess/DTOs/DTOEntities/IRootObjectDTO.cs b/DataAccess/DTOs/DTOEntities/IRootObjectDTO.cs new file mode 100644 index 0000000..3fc6e47 --- /dev/null +++ b/DataAccess/DTOs/DTOEntities/IRootObjectDTO.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.DTOs +{ + public interface IRootObjectDTO + { + FileVersionDTO FileVersion { get; set; } + ProjectDTO Project { get; set; } + } +} diff --git a/DataAccess/DTOs/DTOEntities/ProjectDTO.cs b/DataAccess/DTOs/DTOEntities/ProjectDTO.cs index de7a259..2c5d22b 100644 --- a/DataAccess/DTOs/DTOEntities/ProjectDTO.cs +++ b/DataAccess/DTOs/DTOEntities/ProjectDTO.cs @@ -1,4 +1,5 @@ using Newtonsoft.Json; +using StructureHelperCommon.Infrastructures.Interfaces; using StructureHelperCommon.Models.Analyses; using StructureHelperCommon.Models.Projects; using System; diff --git a/DataAccess/DTOs/DTOEntities/RootObjectDTO.cs b/DataAccess/DTOs/DTOEntities/RootObjectDTO.cs new file mode 100644 index 0000000..3a4ef8f --- /dev/null +++ b/DataAccess/DTOs/DTOEntities/RootObjectDTO.cs @@ -0,0 +1,19 @@ +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 +{ + public class RootObjectDTO : IRootObjectDTO + { + [JsonProperty("FileVersion")] + public FileVersionDTO FileVersion { get; set; } + [JsonProperty("Project")] + public ProjectDTO Project { get; set; } + + } +} diff --git a/DataAccess/DTOs/DTOEntities/TypeBinderListFactory.cs b/DataAccess/DTOs/DTOEntities/TypeBinderListFactory.cs index e4feeac..6b1e819 100644 --- a/DataAccess/DTOs/DTOEntities/TypeBinderListFactory.cs +++ b/DataAccess/DTOs/DTOEntities/TypeBinderListFactory.cs @@ -81,6 +81,7 @@ namespace DataAccess.DTOs { (typeof(RectangleNdmPrimitiveDTO), "RectangleNdmPrimitive") }, { (typeof(RectangleShapeDTO), "RectangleShape") }, { (typeof(ReinforcementLibMaterialDTO), "ReinforcementLibMaterial") }, + { (typeof(RootObjectDTO), "RootObject") }, { (typeof(MaterialPartialFactorDTO), "MaterialPartialFactor") }, { (typeof(VersionProcessorDTO), "VersionProcessor") }, { (typeof(VisualAnalysisDTO), "VisualAnalysis") }, diff --git a/DataAccess/DTOs/DTOEntities/UserCrackInputDataDTO.cs b/DataAccess/DTOs/DTOEntities/UserCrackInputDataDTO.cs index 19cff8a..b479af3 100644 --- a/DataAccess/DTOs/DTOEntities/UserCrackInputDataDTO.cs +++ b/DataAccess/DTOs/DTOEntities/UserCrackInputDataDTO.cs @@ -11,7 +11,7 @@ namespace DataAccess.DTOs public class UserCrackInputDataDTO : IUserCrackInputData { [JsonProperty("Id")] - public Guid Id { get; set; } + public Guid Id { get; set; } = Guid.NewGuid(); [JsonProperty("LengthBetweenCracks")] public double LengthBetweenCracks { get; set; } [JsonProperty("SetLengthBetweenCracks")] diff --git a/DataAccess/DTOs/DTOEntities/VisualAnalysisDTO.cs b/DataAccess/DTOs/DTOEntities/VisualAnalysisDTO.cs index 18914ac..fa49e73 100644 --- a/DataAccess/DTOs/DTOEntities/VisualAnalysisDTO.cs +++ b/DataAccess/DTOs/DTOEntities/VisualAnalysisDTO.cs @@ -1,4 +1,5 @@ using Newtonsoft.Json; +using StructureHelperCommon.Infrastructures.Interfaces; using StructureHelperCommon.Models.Analyses; using System; using System.Collections.Generic; @@ -10,15 +11,17 @@ namespace DataAccess.DTOs { public class VisualAnalysisDTO : IVisualAnalysis { - + [JsonProperty("Id")] public Guid Id { get; set; } [JsonProperty("Analysis")] public IAnalysis Analysis { get; set; } + [JsonIgnore] + public Action ActionToRun { get; set; } public object Clone() { - throw new NotImplementedException(); + return this; } public void Run() diff --git a/DataAccess/Infrastructures/FileOpenLogic.cs b/DataAccess/Infrastructures/FileOpenLogic.cs index ce60666..dedc2ba 100644 --- a/DataAccess/Infrastructures/FileOpenLogic.cs +++ b/DataAccess/Infrastructures/FileOpenLogic.cs @@ -3,16 +3,19 @@ using DataAccess.JsonConverters; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using NLog; +using StructureHelperCommon.Infrastructures.Interfaces; using StructureHelperCommon.Infrastructures.Settings; using StructureHelperCommon.Models; using StructureHelperCommon.Models.Projects; using StructureHelperCommon.Services.FileServices; +using System.Reflection.Emit; namespace DataAccess.Infrastructures { public class FileOpenLogic : IFileOpenLogic { private string fileName; + private Dictionary<(Guid id, Type type), ISaveable> refDictinary; public IShiftTraceLogger? TraceLogger { get; set; } @@ -44,47 +47,77 @@ namespace DataAccess.Infrastructures result.IsValid = false; return result; } - if (! File.Exists(fileName)) + if (!File.Exists(fileName)) { result.IsValid = false; TraceLogger?.AddMessage($"File {fileName} does not exists", TraceLogStatuses.Error); return result; } + string jsonData = File.ReadAllText(fileName); + RootObjectDTO? rootObject = GetRootObject(jsonData); + var fileVersion = rootObject.FileVersion; + var checkLogic = new CheckFileVersionLogic() + { + FileVersion = fileVersion, + TraceLogger = TraceLogger + }; + var checkResult = checkLogic.Check(); + if (checkResult == false) + { + result.IsValid = false; + result.Description += checkLogic.CheckResult; + } + else + { + Project project = GetProject(rootObject); + project.FullFileName = fileName; + result.Project = project; + } + return result; + } + + private Project GetProject(RootObjectDTO? rootObject) + { + var currentVersion = ProgramSetting.GetCurrentFileVersion(); + IFileVersion fileVersion = rootObject.FileVersion; + TraceLogger?.AddMessage($"File version is {fileVersion.VersionNumber}.{fileVersion.SubVersionNumber}, current version is {currentVersion.VersionNumber}.{currentVersion.SubVersionNumber}"); + refDictinary = new Dictionary<(Guid id, Type type), ISaveable>(); + IConvertStrategy convertStrategy = new ProjectFromDTOConvertStrategy() + { + ReferenceDictionary = refDictinary, + TraceLogger = TraceLogger + }; + Project project = convertStrategy.Convert(rootObject.Project); + return project; + } + + private RootObjectDTO? GetRootObject(string jsonData) + { + JsonSerializerSettings settings = GetSettings(); + var rootObject = JsonConvert.DeserializeObject(jsonData, settings); + return rootObject; + } + + private JsonSerializerSettings GetSettings() + { + List<(Type type, string name)> typesNames = TypeBinderListFactory.GetTypeNameList(TypeFileVersion.version_v1); + TypeBinder typeBinder = new(typesNames); var settings = new JsonSerializerSettings { + Converters = new List { new FileVersionDTOJsonConverter(TraceLogger), // Add the specific converter - // Add other converters if needed + new ProjectDTOJsonConverter(TraceLogger) }, + SerializationBinder = typeBinder, 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; + return settings; } } } diff --git a/DataAccess/Infrastructures/FileSaveLogic.cs b/DataAccess/Infrastructures/FileSaveLogic.cs index 60001a9..549a5a8 100644 --- a/DataAccess/Infrastructures/FileSaveLogic.cs +++ b/DataAccess/Infrastructures/FileSaveLogic.cs @@ -1,4 +1,5 @@ using DataAccess.DTOs; +using DataAccess.DTOs.DTOEntities; using DataAccess.JsonConverters; using Newtonsoft.Json; using StructureHelperCommon.Infrastructures.Interfaces; @@ -55,11 +56,11 @@ namespace DataAccess.Infrastructures FileVersionDTO versionDTO = GetVersionDTO(); var versionString = Serialize(versionDTO, TraceLogger); File.Delete(project.FullFileName); - SaveStringToFile(project, versionString); refDictinary = new Dictionary<(Guid id, Type type), ISaveable>(); ProjectDTO projectDTO = GetProjectDTO(project); - var projectString = Serialize(projectDTO, TraceLogger); - SaveStringToFile(project, projectString); + RootObjectDTO rootObject = new() { FileVersion = versionDTO, Project = projectDTO }; + var rootString = Serialize(rootObject, TraceLogger); + SaveStringToFile(project, rootString); } catch (Exception ex) { @@ -72,7 +73,7 @@ namespace DataAccess.Infrastructures { try { - File.AppendAllText(project.FullFileName, versionString); + File.WriteAllText(project.FullFileName, versionString); TraceLogger?.AddMessage($"File {project.FullFileName} was saved successfully", TraceLogStatuses.Service); } catch (Exception ex) diff --git a/StructureHelper/Windows/MainWindow/Analyses/AnalysesLogic.cs b/StructureHelper/Windows/MainWindow/Analyses/AnalysesLogic.cs index 60526ac..7336241 100644 --- a/StructureHelper/Windows/MainWindow/Analyses/AnalysesLogic.cs +++ b/StructureHelper/Windows/MainWindow/Analyses/AnalysesLogic.cs @@ -1,8 +1,10 @@ using StructureHelper.Infrastructure; using StructureHelper.Windows.MainWindow.Analyses; +using StructureHelperCommon.Infrastructures.Exceptions; using StructureHelperCommon.Infrastructures.Settings; using StructureHelperCommon.Models.Analyses; using StructureHelperLogic.Models.Analyses; +using StructureHelperLogics.Models.CrossSections; using System; using System.Collections.Generic; using System.Collections.ObjectModel; @@ -114,6 +116,8 @@ namespace StructureHelper.Windows.MainWindow } private void RunAnalysis() { + if (SelectedAnalysis is null) { return; } + SelectedAnalysis.ActionToRun = ActionToRun; SelectedAnalysis?.Run(); } private void AddCrossSectionNdmAnalysis() @@ -124,5 +128,29 @@ namespace StructureHelper.Windows.MainWindow var visualAnalysis = new VisualAnalysis(analysis); ProgramSetting.CurrentProject.VisualAnalyses.Add(visualAnalysis); } + + private void ActionToRun() + { + if (SelectedAnalysis is null) { return; } + var version = SelectedAnalysis.Analysis.VersionProcessor.GetCurrentVersion(); + if (version is null) + { + throw new StructureHelperException(ErrorStrings.NullReference); + } + if (version.AnalysisVersion is ICrossSection crossSection) + { + ProcessCrossSection(crossSection); + } + else + { + throw new StructureHelperException(ErrorStrings.ObjectTypeIsUnknownObj(version)); + } + } + + private void ProcessCrossSection(ICrossSection crossSection) + { + var window = new CrossSectionView(crossSection); + window.ShowDialog(); + } } } diff --git a/StructureHelper/Windows/MainWindow/Analyses/FileLogic.cs b/StructureHelper/Windows/MainWindow/Analyses/FileLogic.cs index b096d5c..2a5eebd 100644 --- a/StructureHelper/Windows/MainWindow/Analyses/FileLogic.cs +++ b/StructureHelper/Windows/MainWindow/Analyses/FileLogic.cs @@ -1,9 +1,12 @@ using DataAccess.Infrastructures; using StructureHelper.Infrastructure; using StructureHelper.Windows.CalculationWindows.ProgressViews; +using StructureHelperCommon.Infrastructures.Exceptions; using StructureHelperCommon.Infrastructures.Settings; using StructureHelperCommon.Models; +using StructureHelperCommon.Models.Analyses; using StructureHelperCommon.Models.Projects; +using StructureHelperLogics.Models.CrossSections; using System; using System.Linq; using System.Windows.Forms; @@ -22,6 +25,8 @@ namespace StructureHelper.Windows.MainWindow private IProjectAccessLogic projectAccessLogic; private RelayCommand fileSaveAs; + public AnalysesManagerViewModel ParentVM { get; set; } + public FileLogic(IProjectAccessLogic projectAccessLogic) { this.projectAccessLogic = projectAccessLogic; @@ -123,16 +128,27 @@ namespace StructureHelper.Windows.MainWindow } traceLogger = new ShiftTraceLogger(); projectAccessLogic.TraceLogger = traceLogger; - var result = projectAccessLogic.OpenProject(); - if (result.IsValid == true) + try { - ProgramSetting.Projects.Add(result.Project); + var result = projectAccessLogic.OpenProject(); + if (result.IsValid == true) + { + result.Project.IsActual = true; + result.Project.IsNewFile = false; + ProgramSetting.Projects.Clear(); + ProgramSetting.Projects.Add(result.Project); + } + else + { + ProgramSetting.Projects.Add(currentProject); + } } - else + catch (Exception ex) { - ProgramSetting.Projects.Add(currentProject); + traceLogger?.AddMessage(ex.Message, TraceLogStatuses.Error); } ShowEntries(); + ParentVM.AnalysesLogic.Refresh(); } private void ShowEntries() { diff --git a/StructureHelper/Windows/MainWindow/AnalysesManagerViewModel.cs b/StructureHelper/Windows/MainWindow/AnalysesManagerViewModel.cs index 3258ecf..00f08e4 100644 --- a/StructureHelper/Windows/MainWindow/AnalysesManagerViewModel.cs +++ b/StructureHelper/Windows/MainWindow/AnalysesManagerViewModel.cs @@ -15,7 +15,7 @@ namespace StructureHelper.Windows.MainWindow public AnalysesManagerViewModel() { - FileLogic = new(); + FileLogic = new() { ParentVM = this }; FileLogic.CreateNewFile(); DiagramLogic = new(); AnalysesLogic = new(); diff --git a/StructureHelperCommon/Infrastructures/Settings/ProgramSetting.cs b/StructureHelperCommon/Infrastructures/Settings/ProgramSetting.cs index 8726a91..70dad36 100644 --- a/StructureHelperCommon/Infrastructures/Settings/ProgramSetting.cs +++ b/StructureHelperCommon/Infrastructures/Settings/ProgramSetting.cs @@ -28,11 +28,11 @@ namespace StructureHelperCommon.Infrastructures.Settings set { natSystem = value; - codesList = CodeFactory + codesList ??= CodeFactory .GetCodeEntities() .Where(x => x.NatSystem == natSystem) .ToList(); - materialRepository = new MaterialRepository(codesList); + materialRepository ??= new MaterialRepository(codesList); } } public static GeometryNames GeometryNames => geometryNames ??= new GeometryNames(); @@ -53,6 +53,10 @@ namespace StructureHelperCommon.Infrastructures.Settings { get { + codesList ??= CodeFactory + .GetCodeEntities() + .Where(x => x.NatSystem == NatSystem) + .ToList(); materialRepository ??= new MaterialRepository(codesList); return materialRepository; } diff --git a/StructureHelperCommon/Models/Analyses/IVisualAnalysis.cs b/StructureHelperCommon/Models/Analyses/IVisualAnalysis.cs index d11928b..c50296c 100644 --- a/StructureHelperCommon/Models/Analyses/IVisualAnalysis.cs +++ b/StructureHelperCommon/Models/Analyses/IVisualAnalysis.cs @@ -10,7 +10,8 @@ namespace StructureHelperCommon.Models.Analyses { public interface IVisualAnalysis : ISaveable, ICloneable { - IAnalysis Analysis {get;set;} + IAnalysis Analysis { get; set; } + Action ActionToRun { get; set; } void Run(); } } diff --git a/StructureHelper/Windows/MainWindow/Analyses/VisualAnalysis.cs b/StructureHelperCommon/Models/Analyses/VisualAnalysis.cs similarity index 53% rename from StructureHelper/Windows/MainWindow/Analyses/VisualAnalysis.cs rename to StructureHelperCommon/Models/Analyses/VisualAnalysis.cs index 67e32be..5cf8ec9 100644 --- a/StructureHelper/Windows/MainWindow/Analyses/VisualAnalysis.cs +++ b/StructureHelperCommon/Models/Analyses/VisualAnalysis.cs @@ -1,17 +1,16 @@ using StructureHelperCommon.Infrastructures.Exceptions; using StructureHelperCommon.Infrastructures.Interfaces; using StructureHelperCommon.Models.Analyses; -using StructureHelperLogics.Models.CrossSections; using System; -namespace StructureHelper.Windows.MainWindow.Analyses +namespace StructureHelperCommon.Models.Analyses { public class VisualAnalysis : IVisualAnalysis { private IUpdateStrategy updateStrategy = new VisualAnalysisUpdateStrategy(); public Guid Id { get; } public IAnalysis Analysis { get; set; } - + public Action ActionToRun { get; set; } public VisualAnalysis(Guid id, IAnalysis analysis) { @@ -26,25 +25,7 @@ namespace StructureHelper.Windows.MainWindow.Analyses public void Run() { - var version = Analysis.VersionProcessor.GetCurrentVersion(); - if (version is null) - { - throw new StructureHelperException(ErrorStrings.NullReference); - } - if (version.AnalysisVersion is ICrossSection crossSection) - { - ProcessCrossSection(crossSection); - } - else - { - throw new StructureHelperException(ErrorStrings.ObjectTypeIsUnknownObj(version)); - } - } - - private void ProcessCrossSection(ICrossSection crossSection) - { - var window = new CrossSectionView(crossSection); - window.ShowDialog(); + ActionToRun?.Invoke(); } public object Clone() diff --git a/StructureHelperLogics/Models/Analyses/CrossSectionNdmAnalysis.cs b/StructureHelperLogics/Models/Analyses/CrossSectionNdmAnalysis.cs index bba9a0c..23bb058 100644 --- a/StructureHelperLogics/Models/Analyses/CrossSectionNdmAnalysis.cs +++ b/StructureHelperLogics/Models/Analyses/CrossSectionNdmAnalysis.cs @@ -12,12 +12,17 @@ namespace StructureHelperLogic.Models.Analyses public string Tags { get; set; } public IVersionProcessor VersionProcessor { get; private set; } - public CrossSectionNdmAnalysis(Guid Id, IVersionProcessor versionProcessor) + public CrossSectionNdmAnalysis(Guid id, IVersionProcessor versionProcessor) { - this.Id = Id; + Id = id; VersionProcessor = versionProcessor; } + public CrossSectionNdmAnalysis(Guid id) : this (id, new VersionProcessor()) + { + + } + public CrossSectionNdmAnalysis() : this(Guid.NewGuid(), new VersionProcessor()) { CrossSection crossSection = new CrossSection();