using DataAccess.DTOs.Converters; using StructureHelperCommon.Infrastructures.Exceptions; using StructureHelperCommon.Infrastructures.Interfaces; using StructureHelperCommon.Models; using StructureHelperCommon.Models.Loggers; using StructureHelperCommon.Models.Materials; using StructureHelperLogics.Models.CrossSections; using StructureHelperLogics.Models.Materials; using StructureHelperLogics.Models.Materials.Logics; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace DataAccess.DTOs { internal class HelperMaterialToDTOConvertStrategy : IConvertStrategy { private LibMaterialToDTOConvertStrategy concreteConvertStrategy; private LibMaterialToDTOConvertStrategy reinforcementConvertStrategy; private IConvertStrategy elasticConvertStrategy; private IConvertStrategy frMaterialConvertStrategy; private IUpdateStrategy safetyFactorUpdateStrategy = new HelperMaterialDTOSafetyFactorUpdateStrategy(new MaterialSafetyFactorToDTOLogic()); public Dictionary<(Guid id, Type type), ISaveable> ReferenceDictionary { get; set; } public IShiftTraceLogger TraceLogger { get; set; } public HelperMaterialToDTOConvertStrategy( LibMaterialToDTOConvertStrategy concreteConvertStrategy, LibMaterialToDTOConvertStrategy reinforcementConvertStrategy, IConvertStrategy elasticConvertStrategy, IConvertStrategy frMaterialConvertStrategy) { this.concreteConvertStrategy = concreteConvertStrategy; this.reinforcementConvertStrategy = reinforcementConvertStrategy; this.elasticConvertStrategy = elasticConvertStrategy; this.frMaterialConvertStrategy = frMaterialConvertStrategy; } public HelperMaterialToDTOConvertStrategy() : this ( new ConcreteLibMaterialToDTOConvertStrategy(), new ReinforcementLibMaterialToDTOConvertStrategy(), new ElasticMaterialToDTOConvertStrategy(), new FRMaterialToDTOConvertStrategy() ) { } public IHelperMaterial Convert(IHelperMaterial source) { Check(); try { IHelperMaterial helperMaterial = GetMaterial(source); safetyFactorUpdateStrategy.Update(helperMaterial, source); return helperMaterial; } catch (Exception ex) { TraceLogger?.AddMessage(LoggerStrings.LogicType(this), TraceLogStatuses.Debug); TraceLogger?.AddMessage(ex.Message, TraceLogStatuses.Error); throw; } } private IHelperMaterial GetMaterial(IHelperMaterial source) { if (source is IConcreteLibMaterial concreteLibMaterial) { return ProcessConcrete(concreteLibMaterial); } else if (source is IReinforcementLibMaterial reinforcementMaterial) { return ProcessReinforcement(reinforcementMaterial); } else if (source is IFRMaterial frMaterial) { return ProcessFRMaterial(frMaterial); } else if (source is IElasticMaterial elasticMaterial) { return ProcessElastic(elasticMaterial); } else { throw new StructureHelperException(ErrorStrings.ObjectTypeIsUnknownObj(source)); } } private IHelperMaterial ProcessFRMaterial(IFRMaterial frMaterial) { frMaterialConvertStrategy.ReferenceDictionary = ReferenceDictionary; frMaterialConvertStrategy.TraceLogger = TraceLogger; var convertLogic = new DictionaryConvertStrategy(this, frMaterialConvertStrategy); return convertLogic.Convert(frMaterial); } private IHelperMaterial ProcessElastic(IElasticMaterial elasticMaterial) { elasticConvertStrategy.ReferenceDictionary = ReferenceDictionary; elasticConvertStrategy.TraceLogger = TraceLogger; var convertLogic = new DictionaryConvertStrategy(this, elasticConvertStrategy); return convertLogic.Convert(elasticMaterial); } private IHelperMaterial ProcessReinforcement(IReinforcementLibMaterial reinforcementMaterial) { reinforcementConvertStrategy.ReferenceDictionary = ReferenceDictionary; reinforcementConvertStrategy.TraceLogger = TraceLogger; var convertLogic = new DictionaryConvertStrategy(this, reinforcementConvertStrategy); return convertLogic.Convert(reinforcementMaterial); } private IHelperMaterial ProcessConcrete(IConcreteLibMaterial concreteLibMaterial) { concreteConvertStrategy.ReferenceDictionary = ReferenceDictionary; concreteConvertStrategy.TraceLogger = TraceLogger; var convertLogic = new DictionaryConvertStrategy(this, concreteConvertStrategy); return convertLogic.Convert(concreteLibMaterial); } private void Check() { var checkLogic = new CheckConvertLogic(this); checkLogic.Check(); } } }