Serialize converters were added
This commit is contained in:
90
DataAccess/Infrastructures/FileOpenLogic.cs
Normal file
90
DataAccess/Infrastructures/FileOpenLogic.cs
Normal file
@@ -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<JsonConverter>
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
114
DataAccess/Infrastructures/FileSaveLogic.cs
Normal file
114
DataAccess/Infrastructures/FileSaveLogic.cs
Normal file
@@ -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<FileVersion, FileVersionDTO> 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<JsonConverter>
|
||||
{
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
10
DataAccess/Infrastructures/IFileOpenLogic.cs
Normal file
10
DataAccess/Infrastructures/IFileOpenLogic.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
using StructureHelperCommon.Infrastructures.Interfaces;
|
||||
using StructureHelperCommon.Models.Projects;
|
||||
|
||||
namespace DataAccess.Infrastructures
|
||||
{
|
||||
public interface IFileOpenLogic : ILogic
|
||||
{
|
||||
OpenProjectResult OpenFile();
|
||||
}
|
||||
}
|
||||
16
DataAccess/Infrastructures/IFileSaveLogic.cs
Normal file
16
DataAccess/Infrastructures/IFileSaveLogic.cs
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
18
DataAccess/Infrastructures/IProjectAccessLogic.cs
Normal file
18
DataAccess/Infrastructures/IProjectAccessLogic.cs
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
17
DataAccess/Infrastructures/OpenProjectResult.cs
Normal file
17
DataAccess/Infrastructures/OpenProjectResult.cs
Normal file
@@ -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; }
|
||||
}
|
||||
}
|
||||
47
DataAccess/Infrastructures/ProjectAccessLogic.cs
Normal file
47
DataAccess/Infrastructures/ProjectAccessLogic.cs
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user