Add converting primitives and calculators
This commit is contained in:
@@ -3,6 +3,7 @@ using DataAccess.JsonConverters;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using NLog;
|
||||
using StructureHelperCommon.Infrastructures.Exceptions;
|
||||
using StructureHelperCommon.Infrastructures.Interfaces;
|
||||
using StructureHelperCommon.Infrastructures.Settings;
|
||||
using StructureHelperCommon.Models;
|
||||
@@ -55,6 +56,13 @@ namespace DataAccess.Infrastructures
|
||||
}
|
||||
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()
|
||||
{
|
||||
@@ -69,6 +77,13 @@ namespace DataAccess.Infrastructures
|
||||
}
|
||||
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;
|
||||
@@ -78,6 +93,10 @@ namespace DataAccess.Infrastructures
|
||||
|
||||
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}");
|
||||
@@ -94,7 +113,7 @@ namespace DataAccess.Infrastructures
|
||||
private RootObjectDTO? GetRootObject(string jsonData)
|
||||
{
|
||||
JsonSerializerSettings settings = GetSettings();
|
||||
var rootObject = JsonConvert.DeserializeObject<RootObjectDTO>(jsonData, settings);
|
||||
RootObjectDTO rootObject = JsonConvert.DeserializeObject<RootObjectDTO>(jsonData, settings);
|
||||
return rootObject;
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@ namespace DataAccess.Infrastructures
|
||||
|
||||
public void SaveFile(IProject project)
|
||||
{
|
||||
if (project.IsNewFile == true)
|
||||
if (project.FullFileName == string.Empty || project.FullFileName == "")
|
||||
{
|
||||
var result = SelectFileName(project);
|
||||
if (result.IsValid == false)
|
||||
@@ -28,7 +28,6 @@ namespace DataAccess.Infrastructures
|
||||
return;
|
||||
}
|
||||
project.FullFileName = result.FileName;
|
||||
project.IsNewFile = false;
|
||||
}
|
||||
SaveToFile(project);
|
||||
}
|
||||
@@ -40,7 +39,6 @@ namespace DataAccess.Infrastructures
|
||||
{
|
||||
FilterString = "StructureHelper project file (*.shpj)|*.shpj|All Files (*.*)|*.*",
|
||||
InitialDirectory = project.FullFileName,
|
||||
|
||||
};
|
||||
saver.TraceLogger = TraceLogger;
|
||||
var saveResult = saver.SaveFile();
|
||||
@@ -58,7 +56,11 @@ namespace DataAccess.Infrastructures
|
||||
File.Delete(project.FullFileName);
|
||||
refDictinary = new Dictionary<(Guid id, Type type), ISaveable>();
|
||||
ProjectDTO projectDTO = GetProjectDTO(project);
|
||||
RootObjectDTO rootObject = new() { FileVersion = versionDTO, Project = projectDTO };
|
||||
RootObjectDTO rootObject = new()
|
||||
{
|
||||
FileVersion = versionDTO,
|
||||
Project = projectDTO
|
||||
};
|
||||
var rootString = Serialize(rootObject, TraceLogger);
|
||||
SaveStringToFile(project, rootString);
|
||||
}
|
||||
@@ -95,7 +97,8 @@ namespace DataAccess.Infrastructures
|
||||
ReferenceDictionary = refDictinary,
|
||||
TraceLogger = TraceLogger
|
||||
};
|
||||
return dictionaryConvertStrategy.Convert(project);
|
||||
ProjectDTO projectDTO = dictionaryConvertStrategy.Convert(project);
|
||||
return projectDTO;
|
||||
}
|
||||
|
||||
private FileVersionDTO GetVersionDTO()
|
||||
@@ -116,17 +119,25 @@ namespace DataAccess.Infrastructures
|
||||
}
|
||||
|
||||
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 = new List<JsonConverter>
|
||||
{
|
||||
// Add other converters if needed
|
||||
new FileVersionDTOJsonConverter(logger), // Add the specific converter
|
||||
new ProjectDTOJsonConverter(logger)
|
||||
},
|
||||
Converters = jsonConverters,
|
||||
SerializationBinder = typeBinder,
|
||||
Formatting = Formatting.Indented,
|
||||
PreserveReferencesHandling = PreserveReferencesHandling.All,
|
||||
@@ -134,19 +145,15 @@ namespace DataAccess.Infrastructures
|
||||
TypeNameHandling = TypeNameHandling.All,
|
||||
NullValueHandling = NullValueHandling.Include
|
||||
};
|
||||
|
||||
return JsonConvert.SerializeObject(obj, settings);
|
||||
return settings;
|
||||
}
|
||||
|
||||
public void SaveFileAs(IProject project)
|
||||
{
|
||||
var tmpIsNew = project.IsNewFile;
|
||||
var tmpFullFileName = project.FullFileName;
|
||||
project.IsNewFile = true;
|
||||
SaveFile(project);
|
||||
if (project.IsNewFile == true)
|
||||
if (project.FullFileName == string.Empty)
|
||||
{
|
||||
project.IsNewFile = tmpIsNew;
|
||||
project.FullFileName = tmpFullFileName;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user