Change open and save file logic
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user