Change open and save file logic

This commit is contained in:
Evgeny Redikultsev
2024-11-09 21:52:05 +05:00
parent a8d570713d
commit 0a2934a1ea
68 changed files with 1049 additions and 373 deletions

View File

@@ -1,82 +1,69 @@
using StructureHelperCommon.Infrastructures.Interfaces;
using StructureHelperCommon.Models;
using StructureHelperCommon.Models.Analyses;
using StructureHelperCommon.Models.Loggers;
using StructureHelperCommon.Models.Projects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataAccess.DTOs
{
public class ProjectFromDTOConvertStrategy : IConvertStrategy<Project, ProjectDTO>
public class ProjectFromDTOConvertStrategy : ConvertStrategy<Project, ProjectDTO>
{
private IUpdateStrategy<IProject> updateStrategy;
private IConvertStrategy<IVisualAnalysis, IVisualAnalysis> visualAnalysisConvertStrategy = new VisualAnaysisFromDTOConvertStrategy();
private IUpdateStrategy<IProject> _updateStrategy;
private IConvertStrategy<IVisualAnalysis, IVisualAnalysis> _convertLogic;
public ProjectFromDTOConvertStrategy(IUpdateStrategy<IProject> updateStrategy)
public ProjectFromDTOConvertStrategy(IUpdateStrategy<IProject> updateStrategy,
IConvertStrategy<IVisualAnalysis, IVisualAnalysis> convertLogic,
Dictionary<(Guid id, Type type), ISaveable> refDictinary,
IShiftTraceLogger? traceLogger)
{
this.updateStrategy = updateStrategy;
_updateStrategy = updateStrategy;
_convertLogic = convertLogic;
ReferenceDictionary = refDictinary;
TraceLogger = traceLogger;
}
public ProjectFromDTOConvertStrategy() : this (new ProjectUpdateStrategy())
{
}
public ProjectFromDTOConvertStrategy(
Dictionary<(Guid id, Type type), ISaveable> refDictinary,
IShiftTraceLogger? traceLogger)
: this(
new ProjectUpdateStrategy(),
new DictionaryConvertStrategy<IVisualAnalysis, IVisualAnalysis>(
refDictinary,
new VisualAnaysisFromDTOConvertStrategy(refDictinary, traceLogger),
traceLogger),
refDictinary,
traceLogger)
{ }
public Dictionary<(Guid id, Type type), ISaveable> ReferenceDictionary { get; set; }
public IShiftTraceLogger TraceLogger { get; set; }
public Project Convert(ProjectDTO source)
{
Check();
TraceLogger?.AddMessage("Converting project is started", TraceLogStatuses.Info);
try
{
Project newItem = GetProject(source);
return newItem;
}
catch (Exception ex)
{
TraceLogger?.AddMessage(LoggerStrings.LogicType(this), TraceLogStatuses.Error);
TraceLogger?.AddMessage(ex.Message, TraceLogStatuses.Error);
throw;
}
}
private Project GetProject(ProjectDTO source)
{
TraceLogger?.AddMessage("Converting of project is started", TraceLogStatuses.Service);
Project newItem = new();
updateStrategy.Update(newItem, source);
visualAnalysisConvertStrategy.ReferenceDictionary = ReferenceDictionary;
visualAnalysisConvertStrategy.TraceLogger = TraceLogger;
var convertLogic = new DictionaryConvertStrategy<IVisualAnalysis, IVisualAnalysis>(this, visualAnalysisConvertStrategy);
public override Project GetNewItem(ProjectDTO source)
{
TraceLogger?.AddMessage("Converting of project is started");
Project newItem = new(source.Id);
List<IVisualAnalysis> analyses = GetAnalyses(source, newItem);
newItem.VisualAnalyses.Clear();
TraceLogger?.AddMessage($"Source project has {source.VisualAnalyses.Count()} analyses", TraceLogStatuses.Service);
foreach (var item in source.VisualAnalyses)
{
var visualAnalysis = convertLogic.Convert(item);
newItem.VisualAnalyses.Add(visualAnalysis);
}
newItem.VisualAnalyses.AddRange(analyses);
if (newItem.VisualAnalyses.Any())
{
TraceLogger?.AddMessage($"Totaly {newItem.VisualAnalyses.Count()} were(was) obtained", TraceLogStatuses.Service);
TraceLogger?.AddMessage($"Totaly {newItem.VisualAnalyses.Count} were(was) obtained");
}
else
{
TraceLogger?.AddMessage($"Project does not have any analyses", TraceLogStatuses.Warning);
TraceLogger?.AddMessage($"Project does not have any analyses, it is possible to work with project", TraceLogStatuses.Warning);
}
TraceLogger?.AddMessage("Converting of project has completed succesfully", TraceLogStatuses.Service);
TraceLogger?.AddMessage("Converting of project has been finished successfully");
return newItem;
}
private void Check()
public List<IVisualAnalysis> GetAnalyses(ProjectDTO source, Project newItem)
{
var checkLogic = new CheckConvertLogic<Project, ProjectDTO>(this);
checkLogic.Check();
_updateStrategy.Update(newItem, source);
TraceLogger?.AddMessage($"Source project has {source.VisualAnalyses.Count} analyses");
List<IVisualAnalysis> analyses = new();
foreach (var item in source.VisualAnalyses)
{
var visualAnalysis = _convertLogic.Convert(item);
analyses.Add(visualAnalysis);
}
return analyses;
}
}
}