Add project ConvertFromStrategy
This commit is contained in:
@@ -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<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();
|
||||
var 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
|
||||
// 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user