From 5f2d790458fbb76efadcb3cc99dc71695074a146 Mon Sep 17 00:00:00 2001 From: Mads Magnus Due Date: Tue, 26 Nov 2024 14:44:14 +0100 Subject: [PATCH] feat: child parser only mappes a tranaction --- .../AggregatedMeasureDataJsonMessageParser.cs | 56 +++------ .../AggregatedMeasureDataXmlMessageParser.cs | 49 +++----- .../MessageParsers/JsonMessageParserBase.cs | 16 ++- ...ateForMeasurementPointJsonMessageParser.cs | 54 +++----- ...DateForMeasurementPointXmlMessageParser.cs | 116 +++++++++--------- .../WholesaleSettlementJsonMessageParser.cs | 58 ++++----- .../WholesaleSettlementXmlMessageParser.cs | 54 ++++---- .../MessageParsers/XmlMessageParserBase.cs | 13 +- 8 files changed, 182 insertions(+), 234 deletions(-) diff --git a/source/IncomingMessages.Infrastructure/MessageParsers/AggregatedMeasureDataRequestMessageParsers/AggregatedMeasureDataJsonMessageParser.cs b/source/IncomingMessages.Infrastructure/MessageParsers/AggregatedMeasureDataRequestMessageParsers/AggregatedMeasureDataJsonMessageParser.cs index 654d05f6ac..03d195d811 100644 --- a/source/IncomingMessages.Infrastructure/MessageParsers/AggregatedMeasureDataRequestMessageParsers/AggregatedMeasureDataJsonMessageParser.cs +++ b/source/IncomingMessages.Infrastructure/MessageParsers/AggregatedMeasureDataRequestMessageParsers/AggregatedMeasureDataJsonMessageParser.cs @@ -23,7 +23,6 @@ namespace Energinet.DataHub.EDI.IncomingMessages.Infrastructure.MessageParsers.A public class AggregatedMeasureDataJsonMessageParser(JsonSchemaProvider schemaProvider) : JsonMessageParserBase(schemaProvider) { - private const string SeriesElementName = "Series"; private const string MridElementName = "mRID"; private const string MarketEvaluationPointTypeElementName = "marketEvaluationPoint.type"; private const string MarketEvaluationPointSettlementMethodElementName = "marketEvaluationPoint.settlementMethod"; @@ -42,19 +41,29 @@ public class AggregatedMeasureDataJsonMessageParser(JsonSchemaProvider schemaPro protected override string DocumentName => "RequestAggregatedMeasureData"; - protected override IReadOnlyCollection ParseTransactions(JsonDocument document, string senderNumber) + protected override IIncomingMessageSeries ParseTransaction(JsonElement transactionElement, string senderNumber) { - var transactionElements = document.RootElement.GetProperty(HeaderElementName).GetProperty(SeriesElementName); - var transactions = new List(); - - foreach (var transactionElement in transactionElements.EnumerateArray()) - { - var transaction = ParseTransaction(transactionElement); + var id = transactionElement.GetProperty(MridElementName).ToString(); + var startDateTime = transactionElement.GetProperty(StartElementName).ToString(); + var endDateTime = transactionElement.TryGetProperty(EndElementName, out var endDateProperty) ? endDateProperty.ToString() : null; - transactions.Add(transaction); - } + var meteringPointType = GetPropertyWithValue(transactionElement, MarketEvaluationPointTypeElementName); + var settlementMethod = GetPropertyWithValue(transactionElement, MarketEvaluationPointSettlementMethodElementName); + var gridArea = GetPropertyWithValue(transactionElement, GridAreaElementName); + var energySupplierId = GetPropertyWithValue(transactionElement, EnergySupplierNumberElementName); + var balanceResponsibleId = GetPropertyWithValue(transactionElement, BalanceResponsibleNumberElementName); + var settlementVersion = GetPropertyWithValue(transactionElement, SettlementVersionElementName); - return transactions.AsReadOnly(); + return new RequestAggregatedMeasureDataMessageSeries( + id, + meteringPointType, + settlementMethod, + startDateTime, + endDateTime, + gridArea, + energySupplierId, + balanceResponsibleId, + settlementVersion); } protected override IncomingMarketMessageParserResult CreateResult(MessageHeader header, IReadOnlyCollection transactions) @@ -77,29 +86,4 @@ protected override IncomingMarketMessageParserResult CreateResult(MessageHeader { return element.TryGetProperty(propertyName, out var property) ? property.GetProperty("value").ToString() : null; } - - private RequestAggregatedMeasureDataMessageSeries ParseTransaction(JsonElement transactionElement) - { - var id = transactionElement.GetProperty(MridElementName).ToString(); - var startDateTime = transactionElement.GetProperty("start_DateAndOrTime.dateTime").ToString(); - var endDateTime = transactionElement.TryGetProperty("end_DateAndOrTime.dateTime", out var endDateProperty) ? endDateProperty.ToString() : null; - - var meteringPointType = GetPropertyWithValue(transactionElement, MarketEvaluationPointTypeElementName); - var settlementMethod = GetPropertyWithValue(transactionElement, MarketEvaluationPointSettlementMethodElementName); - var gridArea = GetPropertyWithValue(transactionElement, GridAreaElementName); - var energySupplierId = GetPropertyWithValue(transactionElement, EnergySupplierNumberElementName); - var balanceResponsibleId = GetPropertyWithValue(transactionElement, BalanceResponsibleNumberElementName); - var settlementVersion = GetPropertyWithValue(transactionElement, SettlementVersionElementName); - - return new RequestAggregatedMeasureDataMessageSeries( - id, - meteringPointType, - settlementMethod, - startDateTime, - endDateTime, - gridArea, - energySupplierId, - balanceResponsibleId, - settlementVersion); - } } diff --git a/source/IncomingMessages.Infrastructure/MessageParsers/AggregatedMeasureDataRequestMessageParsers/AggregatedMeasureDataXmlMessageParser.cs b/source/IncomingMessages.Infrastructure/MessageParsers/AggregatedMeasureDataRequestMessageParsers/AggregatedMeasureDataXmlMessageParser.cs index b79c7e52ff..e02e464454 100644 --- a/source/IncomingMessages.Infrastructure/MessageParsers/AggregatedMeasureDataRequestMessageParsers/AggregatedMeasureDataXmlMessageParser.cs +++ b/source/IncomingMessages.Infrastructure/MessageParsers/AggregatedMeasureDataRequestMessageParsers/AggregatedMeasureDataXmlMessageParser.cs @@ -23,7 +23,6 @@ namespace Energinet.DataHub.EDI.IncomingMessages.Infrastructure.MessageParsers.A public class AggregatedMeasureDataXmlMessageParser(CimXmlSchemaProvider schemaProvider) : XmlMessageParserBase(schemaProvider) { - private const string SeriesElementName = "Series"; private const string MridElementName = "mRID"; private const string MarketEvaluationPointTypeElementName = "marketEvaluationPoint.type"; private const string MarketEvaluationPointSettlementMethodElementName = "marketEvaluationPoint.settlementMethod"; @@ -38,36 +37,28 @@ public class AggregatedMeasureDataXmlMessageParser(CimXmlSchemaProvider schemaPr protected override string RootPayloadElementName => "RequestAggregatedMeasureData_MarketDocument"; - protected override IReadOnlyCollection ParseTransactions(XDocument document, XNamespace ns, string senderNumber) + protected override IIncomingMessageSeries ParseTransaction(XElement seriesElement, XNamespace ns, string senderNumber) { - var seriesElements = document.Descendants(ns + SeriesElementName); - var result = new List(); + var id = seriesElement.Element(ns + MridElementName)?.Value ?? string.Empty; + var marketEvaluationPointType = seriesElement.Element(ns + MarketEvaluationPointTypeElementName)?.Value; + var marketEvaluationSettlementMethod = seriesElement.Element(ns + MarketEvaluationPointSettlementMethodElementName)?.Value; + var startDateAndOrTimeDateTime = seriesElement.Element(ns + StartElementName)?.Value ?? string.Empty; + var endDateAndOrTimeDateTime = seriesElement.Element(ns + EndElementName)?.Value; + var meteringGridAreaDomainId = seriesElement.Element(ns + GridAreaElementName)?.Value; + var energySupplierMarketParticipantId = seriesElement.Element(ns + EnergySupplierNumberElementName)?.Value; + var balanceResponsiblePartyMarketParticipantId = seriesElement.Element(ns + BalanceResponsibleNumberElementName)?.Value; + var settlementVersion = seriesElement.Element(ns + SettlementVersionElementName)?.Value; - foreach (var seriesElement in seriesElements) - { - var id = seriesElement.Element(ns + MridElementName)?.Value ?? string.Empty; - var marketEvaluationPointType = seriesElement.Element(ns + MarketEvaluationPointTypeElementName)?.Value; - var marketEvaluationSettlementMethod = seriesElement.Element(ns + MarketEvaluationPointSettlementMethodElementName)?.Value; - var startDateAndOrTimeDateTime = seriesElement.Element(ns + StartElementName)?.Value ?? string.Empty; - var endDateAndOrTimeDateTime = seriesElement.Element(ns + EndElementName)?.Value; - var meteringGridAreaDomainId = seriesElement.Element(ns + GridAreaElementName)?.Value; - var energySupplierMarketParticipantId = seriesElement.Element(ns + EnergySupplierNumberElementName)?.Value; - var balanceResponsiblePartyMarketParticipantId = seriesElement.Element(ns + BalanceResponsibleNumberElementName)?.Value; - var settlementVersion = seriesElement.Element(ns + SettlementVersionElementName)?.Value; - - result.Add(new RequestAggregatedMeasureDataMessageSeries( - id, - marketEvaluationPointType, - marketEvaluationSettlementMethod, - startDateAndOrTimeDateTime, - endDateAndOrTimeDateTime, - meteringGridAreaDomainId, - energySupplierMarketParticipantId, - balanceResponsiblePartyMarketParticipantId, - settlementVersion)); - } - - return result.AsReadOnly(); + return new RequestAggregatedMeasureDataMessageSeries( + id, + marketEvaluationPointType, + marketEvaluationSettlementMethod, + startDateAndOrTimeDateTime, + endDateAndOrTimeDateTime, + meteringGridAreaDomainId, + energySupplierMarketParticipantId, + balanceResponsiblePartyMarketParticipantId, + settlementVersion); } protected override IncomingMarketMessageParserResult CreateResult(MessageHeader header, IReadOnlyCollection transactions) diff --git a/source/IncomingMessages.Infrastructure/MessageParsers/JsonMessageParserBase.cs b/source/IncomingMessages.Infrastructure/MessageParsers/JsonMessageParserBase.cs index 6bdb62f8c0..d99f982fe8 100644 --- a/source/IncomingMessages.Infrastructure/MessageParsers/JsonMessageParserBase.cs +++ b/source/IncomingMessages.Infrastructure/MessageParsers/JsonMessageParserBase.cs @@ -26,8 +26,9 @@ namespace Energinet.DataHub.EDI.IncomingMessages.Infrastructure.MessageParsers; public abstract class JsonMessageParserBase(JsonSchemaProvider schemaProvider) : MessageParserBase() { - protected const string ValueElementName = "value"; - protected const string IdentificationElementName = "mRID"; + private const string ValueElementName = "value"; + private const string IdentificationElementName = "mRID"; + private const string SeriesElementName = "Series"; private const string MessageTypeElementName = "type"; private const string ProcessTypeElementName = "process.processType"; private const string SenderIdentificationElementName = "sender_MarketParticipant.mRID"; @@ -65,11 +66,18 @@ protected override async Task ParseMessageAsy } var header = ParseHeader(document); - var transactions = ParseTransactions(document, header.SenderId); + var transactionElements = document.RootElement.GetProperty(HeaderElementName).GetProperty(SeriesElementName); + var transactions = new List(); + + foreach (var transactionElement in transactionElements.EnumerateArray()) + { + transactions.Add(ParseTransaction(transactionElement, header.SenderId)); + } + return CreateResult(header, transactions); } - protected abstract IReadOnlyCollection ParseTransactions(JsonDocument document, string senderNumber); + protected abstract IIncomingMessageSeries ParseTransaction(JsonElement transactionElement, string senderNumber); protected override async Task<(JsonSchema? Schema, ValidationError? ValidationError)> GetSchemaAsync(IIncomingMarketMessageStream marketMessage, CancellationToken cancellationToken) { diff --git a/source/IncomingMessages.Infrastructure/MessageParsers/MeteredDateForMeasurementPointParsers/MeteredDateForMeasurementPointJsonMessageParser.cs b/source/IncomingMessages.Infrastructure/MessageParsers/MeteredDateForMeasurementPointParsers/MeteredDateForMeasurementPointJsonMessageParser.cs index 576fa4acb0..5621fc87fb 100644 --- a/source/IncomingMessages.Infrastructure/MessageParsers/MeteredDateForMeasurementPointParsers/MeteredDateForMeasurementPointJsonMessageParser.cs +++ b/source/IncomingMessages.Infrastructure/MessageParsers/MeteredDateForMeasurementPointParsers/MeteredDateForMeasurementPointJsonMessageParser.cs @@ -23,7 +23,8 @@ namespace Energinet.DataHub.EDI.IncomingMessages.Infrastructure.MessageParsers.M public class MeteredDateForMeasurementPointJsonMessageParser(JsonSchemaProvider schemaProvider) : JsonMessageParserBase(schemaProvider) { - private const string SeriesElementName = "Series"; + private const string ValueElementName = "value"; + private const string MridElementName = "mRID"; private const string MeteringPointIdentificationElementName = "marketEvaluationPoint.mRID"; private const string MeteringPointTypeElementName = "marketEvaluationPoint.type"; private const string ProductNumberElementName = "product"; @@ -47,41 +48,9 @@ public class MeteredDateForMeasurementPointJsonMessageParser(JsonSchemaProvider protected override string DocumentName => "NotifyValidatedMeasureData"; - protected override IReadOnlyCollection ParseTransactions(JsonDocument document, string senderNumber) + protected override IIncomingMessageSeries ParseTransaction(JsonElement transactionElement, string senderNumber) { - var transactionElements = document.RootElement.GetProperty(HeaderElementName).GetProperty(SeriesElementName); - var transactions = new List(); - - foreach (var transactionElement in transactionElements.EnumerateArray()) - { - var transaction = ParseTransaction(senderNumber, transactionElement); - - transactions.Add(transaction); - } - - return transactions.AsReadOnly(); - } - - protected override IncomingMarketMessageParserResult CreateResult(MessageHeader header, IReadOnlyCollection transactions) - { - return new IncomingMarketMessageParserResult(new MeteredDataForMeasurementPointMessage( - header.MessageId, - header.MessageType, - header.CreatedAt, - header.SenderId, - header.ReceiverId, - header.SenderRole, - header.BusinessReason, - header.ReceiverRole, - header.BusinessType, - transactions)); - } - - private static MeteredDataForMeasurementPointSeries ParseTransaction( - string senderNumber, - JsonElement transactionElement) - { - var id = transactionElement.GetProperty(IdentificationElementName).ToString() ?? string.Empty; + var id = transactionElement.GetProperty(MridElementName).ToString() ?? string.Empty; var meteringPointLocationId = transactionElement.GetProperty(MeteringPointIdentificationElementName) .GetProperty(ValueElementName) .ToString(); @@ -126,4 +95,19 @@ private static MeteredDataForMeasurementPointSeries ParseTransaction( senderNumber, energyObservations); } + + protected override IncomingMarketMessageParserResult CreateResult(MessageHeader header, IReadOnlyCollection transactions) + { + return new IncomingMarketMessageParserResult(new MeteredDataForMeasurementPointMessage( + header.MessageId, + header.MessageType, + header.CreatedAt, + header.SenderId, + header.ReceiverId, + header.SenderRole, + header.BusinessReason, + header.ReceiverRole, + header.BusinessType, + transactions)); + } } diff --git a/source/IncomingMessages.Infrastructure/MessageParsers/MeteredDateForMeasurementPointParsers/MeteredDateForMeasurementPointXmlMessageParser.cs b/source/IncomingMessages.Infrastructure/MessageParsers/MeteredDateForMeasurementPointParsers/MeteredDateForMeasurementPointXmlMessageParser.cs index af6e953419..c8c46e128f 100644 --- a/source/IncomingMessages.Infrastructure/MessageParsers/MeteredDateForMeasurementPointParsers/MeteredDateForMeasurementPointXmlMessageParser.cs +++ b/source/IncomingMessages.Infrastructure/MessageParsers/MeteredDateForMeasurementPointParsers/MeteredDateForMeasurementPointXmlMessageParser.cs @@ -21,7 +21,8 @@ namespace Energinet.DataHub.EDI.IncomingMessages.Infrastructure.MessageParsers.MeteredDateForMeasurementPointParsers; -public class MeteredDateForMeasurementPointXmlMessageParser(CimXmlSchemaProvider schemaProvider) : XmlMessageParserBase(schemaProvider) +public class MeteredDateForMeasurementPointXmlMessageParser(CimXmlSchemaProvider schemaProvider) + : XmlMessageParserBase(schemaProvider) { private const string SeriesElementName = "Series"; private const string MridElementName = "mRID"; @@ -44,71 +45,68 @@ public class MeteredDateForMeasurementPointXmlMessageParser(CimXmlSchemaProvider protected override string RootPayloadElementName => "NotifyValidatedMeasureData_MarketDocument"; - protected override IReadOnlyCollection ParseTransactions(XDocument document, XNamespace ns, string senderNumber) + protected override IIncomingMessageSeries ParseTransaction( + XElement seriesElement, + XNamespace ns, + string senderNumber) { - var seriesElements = document.Descendants(ns + SeriesElementName); - var result = new List(); + var id = seriesElement.Element(ns + MridElementName)?.Value ?? string.Empty; + var meteringPointLocationId = seriesElement.Element(ns + MeteringPointDomainLocationElementName)?.Value; + var meteringPointType = seriesElement.Element(ns + TypeOfMeteringPointElementName)?.Value; + var productNumber = seriesElement.Element(ns + ProductElementName)?.Value; + var registrationDateAndOrTime = seriesElement.Element(ns + RegistrationDateAndOrTimeElementName)?.Value; + var productUnitType = seriesElement.Element(ns + UnitTypeElementName)?.Value; - foreach (var seriesElement in seriesElements) - { - var id = seriesElement.Element(ns + MridElementName)?.Value ?? string.Empty; - var meteringPointLocationId = seriesElement.Element(ns + MeteringPointDomainLocationElementName)?.Value; - var meteringPointType = seriesElement.Element(ns + TypeOfMeteringPointElementName)?.Value; - var productNumber = seriesElement.Element(ns + ProductElementName)?.Value; - var registrationDateAndOrTime = seriesElement.Element(ns + RegistrationDateAndOrTimeElementName)?.Value; - var productUnitType = seriesElement.Element(ns + UnitTypeElementName)?.Value; - - var periodElement = seriesElement.Element(ns + PeriodElementName); - var resolution = periodElement?.Element(ns + ResolutionElementName)?.Value; - var startDateAndOrTimeDateTime = - periodElement - ?.Element(ns + TimeIntervalElementName) - ?.Element(ns + StartElementName) - ?.Value ?? string.Empty; - var endDateAndOrTimeDateTime = periodElement + var periodElement = seriesElement.Element(ns + PeriodElementName); + var resolution = periodElement?.Element(ns + ResolutionElementName)?.Value; + var startDateAndOrTimeDateTime = + periodElement ?.Element(ns + TimeIntervalElementName) - ?.Element(ns + EndElementName) - ?.Value; - - var energyObservations = seriesElement - .Descendants(ns + PointElementName) - .Select( - e => new EnergyObservation( - e.Element(ns + PositionElementName)?.Value, - e.Element(ns + QuantityElementName)?.Value, - e.Element(ns + QualityElementName)?.Value)) - .ToList(); + ?.Element(ns + StartElementName) + ?.Value ?? string.Empty; + var endDateAndOrTimeDateTime = periodElement + ?.Element(ns + TimeIntervalElementName) + ?.Element(ns + EndElementName) + ?.Value; - result.Add( - new MeteredDataForMeasurementPointSeries( - id, - resolution, - startDateAndOrTimeDateTime, - endDateAndOrTimeDateTime, - productNumber, - registrationDateAndOrTime, - productUnitType, - meteringPointType, - meteringPointLocationId, - senderNumber, - energyObservations)); - } + var energyObservations = seriesElement + .Descendants(ns + PointElementName) + .Select( + e => new EnergyObservation( + e.Element(ns + PositionElementName)?.Value, + e.Element(ns + QuantityElementName)?.Value, + e.Element(ns + QualityElementName)?.Value)) + .ToList(); - return result.AsReadOnly(); + return new MeteredDataForMeasurementPointSeries( + id, + resolution, + startDateAndOrTimeDateTime, + endDateAndOrTimeDateTime, + productNumber, + registrationDateAndOrTime, + productUnitType, + meteringPointType, + meteringPointLocationId, + senderNumber, + energyObservations); } - protected override IncomingMarketMessageParserResult CreateResult(MessageHeader header, IReadOnlyCollection transactions) + protected override IncomingMarketMessageParserResult CreateResult( + MessageHeader header, + IReadOnlyCollection transactions) { - return new IncomingMarketMessageParserResult(new MeteredDataForMeasurementPointMessage( - header.MessageId, - header.MessageType, - header.CreatedAt, - header.SenderId, - header.ReceiverId, - header.SenderRole, - header.BusinessReason, - header.ReceiverRole, - header.BusinessType, - transactions)); + return new IncomingMarketMessageParserResult( + new MeteredDataForMeasurementPointMessage( + header.MessageId, + header.MessageType, + header.CreatedAt, + header.SenderId, + header.ReceiverId, + header.SenderRole, + header.BusinessReason, + header.ReceiverRole, + header.BusinessType, + transactions)); } } diff --git a/source/IncomingMessages.Infrastructure/MessageParsers/WholesaleSettlementMessageParsers/WholesaleSettlementJsonMessageParser.cs b/source/IncomingMessages.Infrastructure/MessageParsers/WholesaleSettlementMessageParsers/WholesaleSettlementJsonMessageParser.cs index 450ebd3963..36a50a49ed 100644 --- a/source/IncomingMessages.Infrastructure/MessageParsers/WholesaleSettlementMessageParsers/WholesaleSettlementJsonMessageParser.cs +++ b/source/IncomingMessages.Infrastructure/MessageParsers/WholesaleSettlementMessageParsers/WholesaleSettlementJsonMessageParser.cs @@ -23,7 +23,6 @@ namespace Energinet.DataHub.EDI.IncomingMessages.Infrastructure.MessageParsers.W public class WholesaleSettlementJsonMessageParser(JsonSchemaProvider schemaProvider) : JsonMessageParserBase(schemaProvider) { - private const string SeriesElementName = "Series"; private const string MridElementName = "mRID"; private const string StartElementName = "start_DateAndOrTime.dateTime"; private const string EndElementName = "end_DateAndOrTime.dateTime"; @@ -43,42 +42,7 @@ public class WholesaleSettlementJsonMessageParser(JsonSchemaProvider schemaProvi protected override string DocumentName => "RequestWholesaleSettlement"; - protected override IReadOnlyCollection ParseTransactions(JsonDocument document, string senderNumber) - { - var transactionElements = document.RootElement.GetProperty(HeaderElementName).GetProperty(SeriesElementName); - var transactions = new List(); - - foreach (var transactionElement in transactionElements.EnumerateArray()) - { - var transaction = ParseTransaction(transactionElement); - - transactions.Add(transaction); - } - - return transactions.AsReadOnly(); - } - - protected override IncomingMarketMessageParserResult CreateResult(MessageHeader header, IReadOnlyCollection transactions) - { - return new IncomingMarketMessageParserResult(new RequestWholesaleServicesMessage( - header.SenderId, - header.SenderRole, - header.ReceiverId, - header.ReceiverRole, - header.BusinessReason, - header.MessageType, - header.MessageId, - header.CreatedAt, - header.BusinessType, - transactions)); - } - - private static string? GetPropertyWithValue(JsonElement element, string propertyName) - { - return element.TryGetProperty(propertyName, out var property) ? property.GetProperty("value").ToString() : null; - } - - private RequestWholesaleServicesSeries ParseTransaction(JsonElement transactionElement) + protected override IIncomingMessageSeries ParseTransaction(JsonElement transactionElement, string senderNumber) { var id = transactionElement.GetProperty(MridElementName).ToString(); var startDateTime = transactionElement.GetProperty(StartElementName).ToString(); @@ -114,4 +78,24 @@ private RequestWholesaleServicesSeries ParseTransaction(JsonElement transactionE chargeOwner, chargeTypes); } + + protected override IncomingMarketMessageParserResult CreateResult(MessageHeader header, IReadOnlyCollection transactions) + { + return new IncomingMarketMessageParserResult(new RequestWholesaleServicesMessage( + header.SenderId, + header.SenderRole, + header.ReceiverId, + header.ReceiverRole, + header.BusinessReason, + header.MessageType, + header.MessageId, + header.CreatedAt, + header.BusinessType, + transactions)); + } + + private static string? GetPropertyWithValue(JsonElement element, string propertyName) + { + return element.TryGetProperty(propertyName, out var property) ? property.GetProperty("value").ToString() : null; + } } diff --git a/source/IncomingMessages.Infrastructure/MessageParsers/WholesaleSettlementMessageParsers/WholesaleSettlementXmlMessageParser.cs b/source/IncomingMessages.Infrastructure/MessageParsers/WholesaleSettlementMessageParsers/WholesaleSettlementXmlMessageParser.cs index 6fe03e83ab..f98dde5830 100644 --- a/source/IncomingMessages.Infrastructure/MessageParsers/WholesaleSettlementMessageParsers/WholesaleSettlementXmlMessageParser.cs +++ b/source/IncomingMessages.Infrastructure/MessageParsers/WholesaleSettlementMessageParsers/WholesaleSettlementXmlMessageParser.cs @@ -39,40 +39,32 @@ public class WholesaleSettlementXmlMessageParser(CimXmlSchemaProvider schemaProv protected override string RootPayloadElementName => "RequestWholesaleSettlement_MarketDocument"; - protected override IReadOnlyCollection ParseTransactions(XDocument document, XNamespace ns, string senderNumber) + protected override IIncomingMessageSeries ParseTransaction(XElement seriesElement, XNamespace ns, string senderNumber) { - var seriesElements = document.Descendants(ns + SeriesElementName); - var result = new List(); + var id = seriesElement.Element(ns + MridElementName)?.Value ?? string.Empty; + var startDateAndOrTimeDateTime = seriesElement.Element(ns + StartElementName)?.Value ?? string.Empty; + var endDateAndOrTimeDateTime = seriesElement.Element(ns + EndElementName)?.Value; + var gridArea = seriesElement.Element(ns + GridAreaElementName)?.Value; + var energySupplierId = seriesElement.Element(ns + EnergySupplierElementName)?.Value; + var chargeTypeOwnerId = seriesElement.Element(ns + ChargeOwnerElementName)?.Value; + var settlementVersion = seriesElement.Element(ns + SettlementVersionElementName)?.Value; + var resolution = seriesElement.Element(ns + ResolutionElementName)?.Value; - foreach (var seriesElement in seriesElements) - { - var id = seriesElement.Element(ns + MridElementName)?.Value ?? string.Empty; - var startDateAndOrTimeDateTime = seriesElement.Element(ns + StartElementName)?.Value ?? string.Empty; - var endDateAndOrTimeDateTime = seriesElement.Element(ns + EndElementName)?.Value; - var gridArea = seriesElement.Element(ns + GridAreaElementName)?.Value; - var energySupplierId = seriesElement.Element(ns + EnergySupplierElementName)?.Value; - var chargeTypeOwnerId = seriesElement.Element(ns + ChargeOwnerElementName)?.Value; - var settlementVersion = seriesElement.Element(ns + SettlementVersionElementName)?.Value; - var resolution = seriesElement.Element(ns + ResolutionElementName)?.Value; + var chargeTypes = seriesElement.Descendants(ns + ChargeElementName) + .Select(e => new RequestWholesaleServicesChargeType( + e.Element(ns + MridElementName)?.Value, + e.Element(ns + ChargeTypeElementName)?.Value)); - var chargeTypes = seriesElement.Descendants(ns + ChargeElementName) - .Select(e => new RequestWholesaleServicesChargeType( - e.Element(ns + MridElementName)?.Value, - e.Element(ns + ChargeTypeElementName)?.Value)); - - result.Add(new RequestWholesaleServicesSeries( - id, - startDateAndOrTimeDateTime, - endDateAndOrTimeDateTime, - gridArea, - energySupplierId, - settlementVersion, - resolution, - chargeTypeOwnerId, - chargeTypes.Select(a => a).ToList())); - } - - return result.AsReadOnly(); + return new RequestWholesaleServicesSeries( + id, + startDateAndOrTimeDateTime, + endDateAndOrTimeDateTime, + gridArea, + energySupplierId, + settlementVersion, + resolution, + chargeTypeOwnerId, + chargeTypes.Select(a => a).ToList()); } protected override IncomingMarketMessageParserResult CreateResult(MessageHeader header, IReadOnlyCollection transactions) diff --git a/source/IncomingMessages.Infrastructure/MessageParsers/XmlMessageParserBase.cs b/source/IncomingMessages.Infrastructure/MessageParsers/XmlMessageParserBase.cs index 1324d13b0d..0abe4cb32d 100644 --- a/source/IncomingMessages.Infrastructure/MessageParsers/XmlMessageParserBase.cs +++ b/source/IncomingMessages.Infrastructure/MessageParsers/XmlMessageParserBase.cs @@ -26,6 +26,7 @@ namespace Energinet.DataHub.EDI.IncomingMessages.Infrastructure.MessageParsers; public abstract class XmlMessageParserBase(CimXmlSchemaProvider schemaProvider) : MessageParserBase { + private const string SeriesElementName = "Series"; private const string MridElementName = "mRID"; private const string TypeElementName = "type"; private const string ProcessTypeElementName = "process.processType"; @@ -59,11 +60,19 @@ protected override async Task ParseMessageAsy var ns = XNamespace.Get(@namespace); var header = ParseHeader(document, ns); - var transactions = ParseTransactions(document, ns, header.SenderId); + var seriesElements = document.Descendants(ns + SeriesElementName); + var transactions = new List(); + + foreach (var seriesElement in seriesElements) + { + transactions.Add(ParseTransaction(seriesElement, ns, header.SenderId)); + } return CreateResult(header, transactions); } + protected abstract IIncomingMessageSeries ParseTransaction(XElement seriesElement, XNamespace ns, string senderNumber); + protected override async Task<(XmlSchema? Schema, ValidationError? ValidationError)> GetSchemaAsync(IIncomingMarketMessageStream marketMessage, CancellationToken cancellationToken) { XmlSchema? xmlSchema = default; @@ -96,8 +105,6 @@ protected override async Task ParseMessageAsy return (xmlSchema, null); } - protected abstract IReadOnlyCollection ParseTransactions(XDocument document, XNamespace ns, string senderNumber); - protected abstract IncomingMarketMessageParserResult CreateResult(MessageHeader header, IReadOnlyCollection transactions); private static string[] SplitNamespace(string @namespace)