Change open and save file logic
This commit is contained in:
75
DataAccess/Infrastructures/CheckRootObjectLogic.cs
Normal file
75
DataAccess/Infrastructures/CheckRootObjectLogic.cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using DataAccess.DTOs;
|
||||
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.Infrastructures
|
||||
{
|
||||
public class CheckRootObjectLogic : ICheckEntityLogic<IRootObjectDTO>
|
||||
{
|
||||
public IRootObjectDTO Entity { get; set; }
|
||||
|
||||
public string CheckResult { get; private set; }
|
||||
|
||||
public IShiftTraceLogger? TraceLogger { get; set; }
|
||||
|
||||
public CheckRootObjectLogic(IShiftTraceLogger? traceLogger)
|
||||
{
|
||||
TraceLogger = traceLogger;
|
||||
}
|
||||
|
||||
public bool Check()
|
||||
{
|
||||
bool result = true;
|
||||
string checkResult = string.Empty;
|
||||
var checkRootResult = CheckRootObject();
|
||||
if (checkRootResult.checkResult = false)
|
||||
{
|
||||
checkResult += checkRootResult.errorString;
|
||||
result = false;
|
||||
}
|
||||
CheckResult = checkResult;
|
||||
return result;
|
||||
}
|
||||
|
||||
private (bool checkResult, string errorString) CheckRootObject()
|
||||
{
|
||||
bool checkRootResult = true;
|
||||
string errorString = string.Empty;
|
||||
|
||||
if (Entity.FileVersion is null)
|
||||
{
|
||||
checkRootResult = false;
|
||||
errorString += "Section of file version is damaged";
|
||||
TraceLogger?.AddMessage(errorString, TraceLogStatuses.Error);
|
||||
return (checkRootResult, errorString);
|
||||
}
|
||||
var fileVersion = Entity.FileVersion;
|
||||
var checkLogic = new CheckFileVersionLogic()
|
||||
{
|
||||
FileVersion = fileVersion,
|
||||
TraceLogger = TraceLogger
|
||||
};
|
||||
var checkResult = checkLogic.Check();
|
||||
if (checkResult == false)
|
||||
{
|
||||
checkRootResult = false;
|
||||
errorString += checkLogic.CheckResult;
|
||||
return (checkRootResult, errorString);
|
||||
}
|
||||
if (Entity.Project is null)
|
||||
{
|
||||
checkRootResult = false;
|
||||
errorString += "Section of project is damaged";
|
||||
TraceLogger?.AddMessage(errorString, TraceLogStatuses.Error);
|
||||
return (checkRootResult, errorString);
|
||||
}
|
||||
return (checkRootResult, errorString);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -16,10 +16,41 @@ namespace DataAccess.Infrastructures
|
||||
public class FileOpenLogic : IFileOpenLogic
|
||||
{
|
||||
private string fileName;
|
||||
private Dictionary<(Guid id, Type type), ISaveable> refDictinary;
|
||||
|
||||
private IGetProjectLogic getProjectLogic;
|
||||
public IShiftTraceLogger? TraceLogger { get; set; }
|
||||
|
||||
public OpenProjectResult OpenFile()
|
||||
{
|
||||
var result = new OpenProjectResult()
|
||||
{
|
||||
IsValid = true
|
||||
};
|
||||
if (GetFilePath() == false)
|
||||
{
|
||||
result.IsValid = false;
|
||||
return result;
|
||||
}
|
||||
return OpenFile(fileName);
|
||||
}
|
||||
|
||||
public OpenProjectResult OpenFile(string fileName)
|
||||
{
|
||||
var result = new OpenProjectResult()
|
||||
{
|
||||
IsValid = true
|
||||
};
|
||||
if (!File.Exists(fileName))
|
||||
{
|
||||
result.IsValid = false;
|
||||
TraceLogger?.AddMessage($"File {fileName} does not exists", TraceLogStatuses.Error);
|
||||
return result;
|
||||
}
|
||||
getProjectLogic = new GetProjectLogic(TraceLogger);
|
||||
getProjectLogic.TraceLogger = TraceLogger;
|
||||
getProjectLogic.FileName = fileName;
|
||||
return getProjectLogic.GetProject();
|
||||
}
|
||||
|
||||
private bool GetFilePath()
|
||||
{
|
||||
var inputData = new OpenFileInputData()
|
||||
@@ -37,106 +68,6 @@ namespace DataAccess.Infrastructures
|
||||
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;
|
||||
}
|
||||
string jsonData = File.ReadAllText(fileName);
|
||||
RootObjectDTO? rootObject = GetRootObject(jsonData);
|
||||
if (rootObject.FileVersion is null)
|
||||
{
|
||||
var errorString = "Section of file version is damaged";
|
||||
result.Description += errorString;
|
||||
result.IsValid = false;
|
||||
return result;
|
||||
}
|
||||
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
|
||||
{
|
||||
if (rootObject.Project is null)
|
||||
{
|
||||
var errorString = "Section of project is damaged";
|
||||
result.Description += errorString;
|
||||
result.IsValid = false;
|
||||
return result;
|
||||
}
|
||||
Project project = GetProject(rootObject);
|
||||
project.FullFileName = fileName;
|
||||
result.Project = project;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private Project GetProject(RootObjectDTO? rootObject)
|
||||
{
|
||||
if (rootObject.Project is null)
|
||||
{
|
||||
|
||||
}
|
||||
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<Project, ProjectDTO> convertStrategy = new ProjectFromDTOConvertStrategy()
|
||||
{
|
||||
ReferenceDictionary = refDictinary,
|
||||
TraceLogger = TraceLogger
|
||||
};
|
||||
Project project = convertStrategy.Convert(rootObject.Project);
|
||||
return project;
|
||||
}
|
||||
|
||||
private RootObjectDTO? GetRootObject(string jsonData)
|
||||
{
|
||||
JsonSerializerSettings settings = GetSettings();
|
||||
RootObjectDTO rootObject = JsonConvert.DeserializeObject<RootObjectDTO>(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<JsonConverter>
|
||||
{
|
||||
new FileVersionDTOJsonConverter(TraceLogger), // Add the specific converter
|
||||
new ProjectDTOJsonConverter(TraceLogger)
|
||||
},
|
||||
SerializationBinder = typeBinder,
|
||||
Formatting = Formatting.Indented,
|
||||
PreserveReferencesHandling = PreserveReferencesHandling.All,
|
||||
MissingMemberHandling = MissingMemberHandling.Ignore,
|
||||
TypeNameHandling = TypeNameHandling.All,
|
||||
NullValueHandling = NullValueHandling.Include
|
||||
};
|
||||
return settings;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,24 +12,33 @@ 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)
|
||||
public SaveFileResult SaveFile(IProject project)
|
||||
{
|
||||
SaveFileResult result = new();
|
||||
if (project.FullFileName == string.Empty || project.FullFileName == "")
|
||||
{
|
||||
var result = SelectFileName(project);
|
||||
if (result.IsValid == false)
|
||||
var selectResult = SelectFileName(project);
|
||||
if (selectResult.IsValid == false)
|
||||
{
|
||||
TraceLogger?.AddMessage(result.Description);
|
||||
return;
|
||||
TraceLogger?.AddMessage(selectResult.Description);
|
||||
result.IsValid = false;
|
||||
return result;
|
||||
}
|
||||
project.FullFileName = result.FileName;
|
||||
project.FullFileName = selectResult.FileName;
|
||||
}
|
||||
SaveToFile(project);
|
||||
result.FileName = project.FullFileName;
|
||||
try
|
||||
{
|
||||
SaveToFile(project);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
result.IsValid = false;
|
||||
result.Description += ex.Message;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private SaveFileResult SelectFileName(IProject project)
|
||||
@@ -47,115 +56,24 @@ namespace DataAccess.Infrastructures
|
||||
|
||||
private void SaveToFile(IProject project)
|
||||
{
|
||||
try
|
||||
ISaveProjectToFileLogic saveProjectLogic = new SaveProjectToFileLogic(TraceLogger)
|
||||
{
|
||||
version = ProgramSetting.GetCurrentFileVersion();
|
||||
refDictinary = new Dictionary<(Guid id, Type type), ISaveable>();
|
||||
FileVersionDTO versionDTO = GetVersionDTO();
|
||||
var versionString = Serialize(versionDTO, TraceLogger);
|
||||
File.Delete(project.FullFileName);
|
||||
refDictinary = new Dictionary<(Guid id, Type type), ISaveable>();
|
||||
ProjectDTO projectDTO = GetProjectDTO(project);
|
||||
RootObjectDTO rootObject = new()
|
||||
{
|
||||
FileVersion = versionDTO,
|
||||
Project = projectDTO
|
||||
};
|
||||
var rootString = Serialize(rootObject, TraceLogger);
|
||||
SaveStringToFile(project, rootString);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
TraceLogger?.AddMessage(ex.Message, TraceLogStatuses.Error);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void SaveStringToFile(IProject project, string versionString)
|
||||
{
|
||||
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 ProjectDTO GetProjectDTO(IProject project)
|
||||
{
|
||||
ProjectToDTOConvertStrategy convertStrategy = new()
|
||||
{
|
||||
ReferenceDictionary = refDictinary,
|
||||
TraceLogger = TraceLogger
|
||||
Project = project
|
||||
};
|
||||
DictionaryConvertStrategy<ProjectDTO, IProject> dictionaryConvertStrategy = new()
|
||||
{
|
||||
ConvertStrategy = convertStrategy,
|
||||
ReferenceDictionary = refDictinary,
|
||||
TraceLogger = TraceLogger
|
||||
};
|
||||
ProjectDTO projectDTO = dictionaryConvertStrategy.Convert(project);
|
||||
return projectDTO;
|
||||
saveProjectLogic.SaveProject();
|
||||
}
|
||||
|
||||
private FileVersionDTO GetVersionDTO()
|
||||
{
|
||||
FileVersionToDTOConvertStrategy fileVersionDTOConvertStrategy = new()
|
||||
{
|
||||
ReferenceDictionary = refDictinary,
|
||||
TraceLogger = TraceLogger
|
||||
};
|
||||
DictionaryConvertStrategy<FileVersionDTO,IFileVersion> dictionaryConvertStrategy = new()
|
||||
{
|
||||
ConvertStrategy = fileVersionDTOConvertStrategy,
|
||||
ReferenceDictionary = refDictinary,
|
||||
TraceLogger = TraceLogger
|
||||
};
|
||||
var versionDTO = dictionaryConvertStrategy.Convert(version);
|
||||
return versionDTO;
|
||||
}
|
||||
|
||||
private static string Serialize(object obj, IShiftTraceLogger logger)
|
||||
{
|
||||
JsonSerializerSettings settings = GetSerializingSettings(logger);
|
||||
string serializedObject = JsonConvert.SerializeObject(obj, settings);
|
||||
return serializedObject;
|
||||
}
|
||||
|
||||
private static JsonSerializerSettings GetSerializingSettings(IShiftTraceLogger logger)
|
||||
{
|
||||
List<(Type type, string name)> typesNames = TypeBinderListFactory.GetTypeNameList(TypeFileVersion.version_v1);
|
||||
TypeBinder typeBinder = new(typesNames);
|
||||
List<JsonConverter> jsonConverters = new List<JsonConverter>
|
||||
{
|
||||
new FileVersionDTOJsonConverter(logger),
|
||||
new ProjectDTOJsonConverter(logger)
|
||||
};
|
||||
|
||||
var settings = new JsonSerializerSettings
|
||||
{
|
||||
Converters = jsonConverters,
|
||||
SerializationBinder = typeBinder,
|
||||
Formatting = Formatting.Indented,
|
||||
PreserveReferencesHandling = PreserveReferencesHandling.All,
|
||||
MissingMemberHandling = MissingMemberHandling.Ignore,
|
||||
TypeNameHandling = TypeNameHandling.All,
|
||||
NullValueHandling = NullValueHandling.Include
|
||||
};
|
||||
return settings;
|
||||
}
|
||||
|
||||
public void SaveFileAs(IProject project)
|
||||
public SaveFileResult SaveFileAs(IProject project)
|
||||
{
|
||||
var tmpFullFileName = project.FullFileName;
|
||||
SaveFile(project);
|
||||
if (project.FullFileName == string.Empty)
|
||||
project.FullFileName = string.Empty;
|
||||
var result = SaveFile(project);
|
||||
if (result.IsValid == false)
|
||||
{
|
||||
project.FullFileName = tmpFullFileName;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
42
DataAccess/Infrastructures/GetJsonDataByRootObjectLogic.cs
Normal file
42
DataAccess/Infrastructures/GetJsonDataByRootObjectLogic.cs
Normal file
@@ -0,0 +1,42 @@
|
||||
using DataAccess.DTOs;
|
||||
using Newtonsoft.Json;
|
||||
using StructureHelperCommon.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DataAccess.Infrastructures
|
||||
{
|
||||
public class GetJsonDataByRootObjectLogic : IGetJsonDataByRootObjectLogic
|
||||
{
|
||||
private IGetJsonSettingsLogic getJsonSettingsLogic;
|
||||
|
||||
public GetJsonDataByRootObjectLogic(IGetJsonSettingsLogic getJsonSettingsLogic)
|
||||
{
|
||||
this.getJsonSettingsLogic = getJsonSettingsLogic;
|
||||
}
|
||||
|
||||
public GetJsonDataByRootObjectLogic(IShiftTraceLogger traceLogger) : this (new GetJsonSettingsLogic(traceLogger))
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public IRootObjectDTO RootObject { get; set; }
|
||||
public IShiftTraceLogger? TraceLogger { get; set; }
|
||||
|
||||
public string GetJsonData()
|
||||
{
|
||||
var rootString = Serialize(RootObject);
|
||||
return rootString;
|
||||
}
|
||||
|
||||
private string Serialize(object obj)
|
||||
{
|
||||
JsonSerializerSettings settings = getJsonSettingsLogic.GetSettings();
|
||||
string serializedObject = JsonConvert.SerializeObject(obj, settings);
|
||||
return serializedObject;
|
||||
}
|
||||
}
|
||||
}
|
||||
51
DataAccess/Infrastructures/GetJsonSettingsLogic.cs
Normal file
51
DataAccess/Infrastructures/GetJsonSettingsLogic.cs
Normal file
@@ -0,0 +1,51 @@
|
||||
using DataAccess.DTOs;
|
||||
using DataAccess.JsonConverters;
|
||||
using Newtonsoft.Json;
|
||||
using NLog;
|
||||
using StructureHelperCommon.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DataAccess.Infrastructures
|
||||
{
|
||||
public class GetJsonSettingsLogic : IGetJsonSettingsLogic
|
||||
{
|
||||
public IShiftTraceLogger? TraceLogger { get; set; }
|
||||
|
||||
public GetJsonSettingsLogic(IShiftTraceLogger? traceLogger)
|
||||
{
|
||||
TraceLogger = traceLogger;
|
||||
}
|
||||
|
||||
public JsonSerializerSettings GetSettings()
|
||||
{
|
||||
List<(Type type, string name)> typesNames = TypeBinderListFactory.GetTypeNameList(TypeFileVersion.version_v1);
|
||||
TypeBinder typeBinder = new(typesNames);
|
||||
List<JsonConverter> jsonConverters = GetConverters();
|
||||
|
||||
var settings = new JsonSerializerSettings
|
||||
{
|
||||
Converters = jsonConverters,
|
||||
SerializationBinder = typeBinder,
|
||||
Formatting = Formatting.Indented,
|
||||
PreserveReferencesHandling = PreserveReferencesHandling.All,
|
||||
MissingMemberHandling = MissingMemberHandling.Ignore,
|
||||
TypeNameHandling = TypeNameHandling.All,
|
||||
NullValueHandling = NullValueHandling.Include
|
||||
};
|
||||
return settings;
|
||||
}
|
||||
|
||||
private List<JsonConverter> GetConverters()
|
||||
{
|
||||
return new List<JsonConverter>
|
||||
{
|
||||
new FileVersionDTOJsonConverter(TraceLogger),
|
||||
new ProjectDTOJsonConverter(TraceLogger)
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
79
DataAccess/Infrastructures/GetProjectLogic.cs
Normal file
79
DataAccess/Infrastructures/GetProjectLogic.cs
Normal file
@@ -0,0 +1,79 @@
|
||||
using DataAccess.DTOs;
|
||||
using StructureHelperCommon.Infrastructures.Interfaces;
|
||||
using StructureHelperCommon.Infrastructures.Settings;
|
||||
using StructureHelperCommon.Models;
|
||||
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 GetProjectLogic : IGetProjectLogic
|
||||
{
|
||||
private IGetRootObjectByJsonDataLogic getRootObjectLogic;
|
||||
private RootObjectDTO? rootObject;
|
||||
private ICheckEntityLogic<IRootObjectDTO> checkEntityLogic;
|
||||
private OpenProjectResult result;
|
||||
|
||||
public GetProjectLogic(IGetRootObjectByJsonDataLogic getRootObjectLogic,
|
||||
ICheckEntityLogic<IRootObjectDTO> checkEntityLogic,
|
||||
IShiftTraceLogger traceLogger)
|
||||
{
|
||||
this.getRootObjectLogic = getRootObjectLogic;
|
||||
this.checkEntityLogic = checkEntityLogic;
|
||||
TraceLogger = traceLogger;
|
||||
}
|
||||
|
||||
public GetProjectLogic(IShiftTraceLogger traceLogger) : this (
|
||||
new GetRootObjectLogic(traceLogger),
|
||||
new CheckRootObjectLogic(traceLogger),
|
||||
traceLogger)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public string FileName { get; set; }
|
||||
public IShiftTraceLogger TraceLogger { get; set; }
|
||||
|
||||
public OpenProjectResult GetProject()
|
||||
{
|
||||
result = new OpenProjectResult()
|
||||
{
|
||||
IsValid = true
|
||||
};
|
||||
string jsonData = File.ReadAllText(FileName);
|
||||
getRootObjectLogic.JsonData = jsonData;
|
||||
rootObject = getRootObjectLogic.GetRootObject();
|
||||
checkEntityLogic.Entity = rootObject;
|
||||
var checkResult = checkEntityLogic.Check();
|
||||
if (checkResult == false)
|
||||
{
|
||||
TraceLogger?.AddMessage(checkEntityLogic.CheckResult, TraceLogStatuses.Error);
|
||||
result.Description += checkEntityLogic.CheckResult;
|
||||
result.IsValid = false;
|
||||
return result;
|
||||
}
|
||||
Project project = GetProjectByRootObject(rootObject);
|
||||
project.FullFileName = FileName;
|
||||
result.Project = project;
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private Project GetProjectByRootObject(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}");
|
||||
Dictionary<(Guid id, Type type), ISaveable> refDictinary = new Dictionary<(Guid id, Type type), ISaveable>();
|
||||
IConvertStrategy<Project, ProjectDTO> convertStrategy = new ProjectFromDTOConvertStrategy(refDictinary, TraceLogger);
|
||||
Project project = convertStrategy.Convert(rootObject.Project);
|
||||
return project;
|
||||
}
|
||||
}
|
||||
}
|
||||
69
DataAccess/Infrastructures/GetRootObjectByProjectLogic.cs
Normal file
69
DataAccess/Infrastructures/GetRootObjectByProjectLogic.cs
Normal file
@@ -0,0 +1,69 @@
|
||||
using DataAccess.DTOs;
|
||||
using StructureHelperCommon.Infrastructures.Interfaces;
|
||||
using StructureHelperCommon.Infrastructures.Settings;
|
||||
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.Infrastructures
|
||||
{
|
||||
public class GetRootObjectByProjectLogic : IGetRootObjectByProjectLogic
|
||||
{
|
||||
private readonly IFileVersion version = ProgramSetting.GetCurrentFileVersion();
|
||||
private Dictionary<(Guid id, Type type), ISaveable> refDictinary = new Dictionary<(Guid id, Type type), ISaveable>();
|
||||
|
||||
public IShiftTraceLogger? TraceLogger { get; set; }
|
||||
|
||||
public GetRootObjectByProjectLogic(IShiftTraceLogger? traceLogger)
|
||||
{
|
||||
TraceLogger = traceLogger;
|
||||
}
|
||||
|
||||
public IProject Project { get; set; }
|
||||
|
||||
public RootObjectDTO? GetRootObject()
|
||||
{
|
||||
FileVersionDTO fileVersion = GetVersionDTO();
|
||||
ProjectDTO project = GetProjectDTO();
|
||||
return new RootObjectDTO() { FileVersion = fileVersion, Project = project };
|
||||
}
|
||||
|
||||
private FileVersionDTO GetVersionDTO()
|
||||
{
|
||||
FileVersionToDTOConvertStrategy fileVersionDTOConvertStrategy = new()
|
||||
{
|
||||
ReferenceDictionary = refDictinary,
|
||||
TraceLogger = TraceLogger
|
||||
};
|
||||
DictionaryConvertStrategy<FileVersionDTO, IFileVersion> dictionaryConvertStrategy = new()
|
||||
{
|
||||
ConvertStrategy = fileVersionDTOConvertStrategy,
|
||||
ReferenceDictionary = refDictinary,
|
||||
TraceLogger = TraceLogger
|
||||
};
|
||||
var versionDTO = dictionaryConvertStrategy.Convert(version);
|
||||
return versionDTO;
|
||||
}
|
||||
|
||||
private ProjectDTO GetProjectDTO()
|
||||
{
|
||||
ProjectToDTOConvertStrategy convertStrategy = new()
|
||||
{
|
||||
ReferenceDictionary = refDictinary,
|
||||
TraceLogger = TraceLogger
|
||||
};
|
||||
DictionaryConvertStrategy<ProjectDTO, IProject> dictionaryConvertStrategy = new()
|
||||
{
|
||||
ConvertStrategy = convertStrategy,
|
||||
ReferenceDictionary = refDictinary,
|
||||
TraceLogger = TraceLogger
|
||||
};
|
||||
ProjectDTO projectDTO = dictionaryConvertStrategy.Convert(Project);
|
||||
return projectDTO;
|
||||
}
|
||||
}
|
||||
}
|
||||
53
DataAccess/Infrastructures/GetRootObjectLogic.cs
Normal file
53
DataAccess/Infrastructures/GetRootObjectLogic.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using DataAccess.DTOs;
|
||||
using Newtonsoft.Json;
|
||||
using StructureHelperCommon.Infrastructures.Exceptions;
|
||||
using StructureHelperCommon.Models;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DataAccess.Infrastructures
|
||||
{
|
||||
public class GetRootObjectLogic : IGetRootObjectByJsonDataLogic
|
||||
{
|
||||
private IGetJsonSettingsLogic jsonSettingsLogic;
|
||||
|
||||
public GetRootObjectLogic(
|
||||
IGetJsonSettingsLogic jsonSettingsLogic,
|
||||
IShiftTraceLogger traceLogger)
|
||||
{
|
||||
this.jsonSettingsLogic = jsonSettingsLogic;
|
||||
TraceLogger = traceLogger;
|
||||
}
|
||||
|
||||
public GetRootObjectLogic(IShiftTraceLogger traceLogger) : this (new GetJsonSettingsLogic(traceLogger), traceLogger)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public string JsonData { get; set; }
|
||||
public IShiftTraceLogger TraceLogger { get; set; }
|
||||
|
||||
public RootObjectDTO? GetRootObject()
|
||||
{
|
||||
jsonSettingsLogic.TraceLogger = TraceLogger;
|
||||
JsonSerializerSettings settings = jsonSettingsLogic.GetSettings();
|
||||
TraceLogger?.AddMessage("Deserialization of data is started");
|
||||
RootObjectDTO rootObject;
|
||||
try
|
||||
{
|
||||
rootObject = JsonConvert.DeserializeObject<RootObjectDTO>(JsonData, settings);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
string errorString = "Error of deserialization of json data" + ex.Message;
|
||||
TraceLogger?.AddMessage(errorString, TraceLogStatuses.Error);
|
||||
throw new StructureHelperException(errorString);
|
||||
}
|
||||
TraceLogger?.AddMessage("Deserialization of data has been finished successfully");
|
||||
return rootObject;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,5 +6,6 @@ namespace DataAccess.Infrastructures
|
||||
public interface IFileOpenLogic : ILogic
|
||||
{
|
||||
OpenProjectResult OpenFile();
|
||||
OpenProjectResult OpenFile(string fileName);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
using StructureHelperCommon.Infrastructures.Interfaces;
|
||||
using StructureHelperCommon.Models.Projects;
|
||||
using StructureHelperCommon.Services.FileServices;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
@@ -10,7 +11,7 @@ namespace DataAccess.Infrastructures
|
||||
{
|
||||
public interface IFileSaveLogic : ILogic
|
||||
{
|
||||
void SaveFile(IProject project);
|
||||
void SaveFileAs(IProject project);
|
||||
SaveFileResult SaveFile(IProject project);
|
||||
SaveFileResult SaveFileAs(IProject project);
|
||||
}
|
||||
}
|
||||
|
||||
12
DataAccess/Infrastructures/IGetJsonDataByRootObjectLogic.cs
Normal file
12
DataAccess/Infrastructures/IGetJsonDataByRootObjectLogic.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using DataAccess.DTOs;
|
||||
using StructureHelperCommon.Infrastructures.Interfaces;
|
||||
|
||||
namespace DataAccess.Infrastructures
|
||||
{
|
||||
public interface IGetJsonDataByRootObjectLogic : ILogic
|
||||
{
|
||||
IRootObjectDTO RootObject { get; set; }
|
||||
|
||||
string GetJsonData();
|
||||
}
|
||||
}
|
||||
15
DataAccess/Infrastructures/IGetJsonSettingsLogic.cs
Normal file
15
DataAccess/Infrastructures/IGetJsonSettingsLogic.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using Newtonsoft.Json;
|
||||
using StructureHelperCommon.Infrastructures.Interfaces;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace DataAccess.Infrastructures
|
||||
{
|
||||
public interface IGetJsonSettingsLogic : ILogic
|
||||
{
|
||||
JsonSerializerSettings GetSettings();
|
||||
}
|
||||
}
|
||||
18
DataAccess/Infrastructures/IGetProjectLogic.cs
Normal file
18
DataAccess/Infrastructures/IGetProjectLogic.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
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.Infrastructures
|
||||
{
|
||||
public interface IGetProjectLogic : ILogic
|
||||
{
|
||||
string FileName { get; set; }
|
||||
IShiftTraceLogger TraceLogger { get; set; }
|
||||
OpenProjectResult GetProject();
|
||||
}
|
||||
}
|
||||
16
DataAccess/Infrastructures/IGetRootObjectByJsonDataLogic.cs
Normal file
16
DataAccess/Infrastructures/IGetRootObjectByJsonDataLogic.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using DataAccess.DTOs;
|
||||
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.Infrastructures
|
||||
{
|
||||
public interface IGetRootObjectByJsonDataLogic : IGetRootObjectLogic
|
||||
{
|
||||
string JsonData { get; set; }
|
||||
}
|
||||
}
|
||||
14
DataAccess/Infrastructures/IGetRootObjectByProjectLogic.cs
Normal file
14
DataAccess/Infrastructures/IGetRootObjectByProjectLogic.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
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 IGetRootObjectByProjectLogic : IGetRootObjectLogic
|
||||
{
|
||||
IProject Project { get; set; }
|
||||
}
|
||||
}
|
||||
10
DataAccess/Infrastructures/IGetRootObjectLogic.cs
Normal file
10
DataAccess/Infrastructures/IGetRootObjectLogic.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using DataAccess.DTOs;
|
||||
using StructureHelperCommon.Infrastructures.Interfaces;
|
||||
|
||||
namespace DataAccess.Infrastructures
|
||||
{
|
||||
public interface IGetRootObjectLogic : ILogic
|
||||
{
|
||||
RootObjectDTO? GetRootObject();
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,8 @@ namespace DataAccess.Infrastructures
|
||||
public interface IProjectAccessLogic : ILogic
|
||||
{
|
||||
OpenProjectResult OpenProject();
|
||||
void SaveProject(IProject project);
|
||||
void SaveProjectAs(IProject project);
|
||||
OpenProjectResult OpenProject(string fileName);
|
||||
SaveFileResult SaveProject(IProject project);
|
||||
SaveFileResult SaveProjectAs(IProject project);
|
||||
}
|
||||
}
|
||||
|
||||
12
DataAccess/Infrastructures/ISaveProjectToFileLogic.cs
Normal file
12
DataAccess/Infrastructures/ISaveProjectToFileLogic.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using StructureHelperCommon.Infrastructures.Interfaces;
|
||||
using StructureHelperCommon.Models.Projects;
|
||||
|
||||
namespace DataAccess.Infrastructures
|
||||
{
|
||||
public interface ISaveProjectToFileLogic : ILogic
|
||||
{
|
||||
IProject Project { get; set; }
|
||||
|
||||
void SaveProject();
|
||||
}
|
||||
}
|
||||
@@ -32,16 +32,24 @@ namespace DataAccess.Infrastructures
|
||||
return openLogic.OpenFile();
|
||||
}
|
||||
|
||||
public void SaveProject(IProject project)
|
||||
public OpenProjectResult OpenProject(string fileName)
|
||||
{
|
||||
saveLogic.TraceLogger = TraceLogger;
|
||||
saveLogic.SaveFile(project);
|
||||
openLogic.TraceLogger = TraceLogger;
|
||||
return openLogic.OpenFile(fileName);
|
||||
}
|
||||
|
||||
public void SaveProjectAs(IProject project)
|
||||
public SaveFileResult SaveProject(IProject project)
|
||||
{
|
||||
saveLogic.TraceLogger = TraceLogger;
|
||||
saveLogic.SaveFileAs(project);
|
||||
var result = saveLogic.SaveFile(project);
|
||||
return result;
|
||||
}
|
||||
|
||||
public SaveFileResult SaveProjectAs(IProject project)
|
||||
{
|
||||
saveLogic.TraceLogger = TraceLogger;
|
||||
var result = saveLogic.SaveFileAs(project);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
61
DataAccess/Infrastructures/SaveProjectToFileLogic.cs
Normal file
61
DataAccess/Infrastructures/SaveProjectToFileLogic.cs
Normal file
@@ -0,0 +1,61 @@
|
||||
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.Infrastructures
|
||||
{
|
||||
public class SaveProjectToFileLogic : ISaveProjectToFileLogic
|
||||
{
|
||||
private IGetRootObjectByProjectLogic getRootObjectLogic;
|
||||
private IGetJsonDataByRootObjectLogic getJsonDataLogic;
|
||||
|
||||
public SaveProjectToFileLogic(
|
||||
IGetRootObjectByProjectLogic getRootObjectLogic,
|
||||
IGetJsonDataByRootObjectLogic getJsonDataLogic,
|
||||
IShiftTraceLogger traceLogger)
|
||||
{
|
||||
this.getRootObjectLogic = getRootObjectLogic;
|
||||
this.getJsonDataLogic = getJsonDataLogic;
|
||||
TraceLogger = traceLogger;
|
||||
}
|
||||
|
||||
public SaveProjectToFileLogic(IShiftTraceLogger traceLogger) : this (
|
||||
new GetRootObjectByProjectLogic(traceLogger),
|
||||
new GetJsonDataByRootObjectLogic(traceLogger),
|
||||
traceLogger
|
||||
)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public IProject Project { get; set; }
|
||||
public IShiftTraceLogger? TraceLogger { get; set; }
|
||||
|
||||
public void SaveProject()
|
||||
{
|
||||
getRootObjectLogic.Project = Project;
|
||||
var rootObject = getRootObjectLogic.GetRootObject();
|
||||
getJsonDataLogic.RootObject = rootObject;
|
||||
var jsonData = getJsonDataLogic.GetJsonData();
|
||||
SaveStringToFile(jsonData);
|
||||
}
|
||||
|
||||
private void SaveStringToFile(string jsonData)
|
||||
{
|
||||
try
|
||||
{
|
||||
File.Delete(Project.FullFileName);
|
||||
File.WriteAllText(Project.FullFileName, jsonData);
|
||||
TraceLogger?.AddMessage($"File {Project.FullFileName} was saved successfully", TraceLogStatuses.Service);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
TraceLogger?.AddMessage(ex.Message, TraceLogStatuses.Error);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user