Skip to content

Commit

Permalink
feat: move old xml parsers into new pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
MadsDue committed Nov 20, 2024
1 parent e02ba0f commit 270bc97
Show file tree
Hide file tree
Showing 9 changed files with 186 additions and 251 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,11 @@ public static IServiceCollection AddIncomingMessagesModule(this IServiceCollecti
.AddScoped<DelegateIncomingMessage>()
.AddScoped<ITransactionIdRepository, TransactionIdRepository>()
.AddScoped<IMessageIdRepository, MessageIdRepository>()
.AddScoped<IMarketMessageParser, AggregatedMeasureDataXmlMessageParser>()
.AddScoped<IMarketMessageParser, OldAggregatedMeasureDataXmlMessageParser>()
.AddScoped<IMarketMessageParser, AggregatedMeasureDataJsonMessageParser>()
.AddScoped<IMarketMessageParser, AggregatedMeasureDataB2CJsonMessageParser>()
.AddScoped<IMarketMessageParser, WholesaleSettlementJsonMessageParser>()
.AddScoped<IMarketMessageParser, WholesaleSettlementXmlMessageParser>()
.AddScoped<IMarketMessageParser, OldWholesaleSettlementXmlMessageParser>()
.AddScoped<IMarketMessageParser, WholesaleSettlementB2CJsonMessageParser>()
.AddScoped<IMarketMessageParser, MeteredDataForMeasurementPointEbixMessageParser>()
.AddScoped<MarketMessageParser>()
Expand Down Expand Up @@ -137,11 +137,14 @@ public static IServiceCollection AddIncomingMessagesModule(this IServiceCollecti
.AddSingleton<JsonSchemaProvider>();

services
.AddTransient<IMessageParser, Energinet.DataHub.EDI.IncomingMessages.Infrastructure.MessageParsers.MeteredDateForMeasurementPointParsers.MeteredDateForMeasurementPointJsonMessageParser>();
.AddTransient<IMessageParser, MeteredDateForMeasurementPointJsonMessageParser>();
services
.AddTransient<IMessageParser, Energinet.DataHub.EDI.IncomingMessages.Infrastructure.MessageParsers.MeteredDateForMeasurementPointParsers.MeteredDateForMeasurementPointEbixMessageParser>();
.AddTransient<IMessageParser, MeteredDateForMeasurementPointEbixMessageParser>();
services
.AddTransient<IMessageParser, Energinet.DataHub.EDI.IncomingMessages.Infrastructure.MessageParsers.MeteredDateForMeasurementPointParsers.MeteredDateForMeasurementPointXmlMessageParser>();
.AddTransient<IMessageParser, MeteredDateForMeasurementPointXmlMessageParser>();

services
.AddTransient<IMessageParser, WholesaleSettlementXmlMessageParser>();

return services;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,131 +12,77 @@
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Xml;
using System.Xml.Schema;
using System.Xml.Linq;
using Energinet.DataHub.EDI.BuildingBlocks.Domain.Models;
using Energinet.DataHub.EDI.IncomingMessages.Domain;
using Energinet.DataHub.EDI.IncomingMessages.Domain.Abstractions;
using Energinet.DataHub.EDI.IncomingMessages.Infrastructure.MessageParsers.BaseParsers;
using Energinet.DataHub.EDI.IncomingMessages.Infrastructure.MessageParsers.BaseParsers.Xml;
using Energinet.DataHub.EDI.IncomingMessages.Infrastructure.Schemas.Cim.Xml;

namespace Energinet.DataHub.EDI.IncomingMessages.Infrastructure.MessageParsers.AggregatedMeasureDataRequestMessageParsers;

public class AggregatedMeasureDataXmlMessageParser : XmlBaseParser
public class AggregatedMeasureDataXmlMessageParser(CimXmlSchemaProvider schemaProvider) : XmlMessageParserBase(schemaProvider)
{
private const string SeriesRecordElementName = "Series";
private const string HeaderElementName = "RequestAggregatedMeasureData_MarketDocument";

public AggregatedMeasureDataXmlMessageParser(CimXmlSchemaProvider schemaProvider)
: base(schemaProvider)
{
}
private const string SeriesElementName = "Series";
private const string MridElementName = "mRID";
private const string MarketEvaluationPointTypeElementName = "marketEvaluationPoint.type";
private const string MarketEvaluationPointSettlementMethodElementName = "marketEvaluationPoint.settlementMethod";
private const string StartElementName = "start_DateAndOrTime.dateTime";
private const string EndElementName = "end_DateAndOrTime.dateTime";
private const string GridAreaElementName = "meteringGridArea_Domain.mRID";
private const string EnergySupplierNumberElementName = "energySupplier_MarketParticipant.mRID";
private const string BalanceResponsibleNumberElementName = "balanceResponsibleParty_MarketParticipant.mRID";
private const string SettlementVersionElementName = "settlement_Series.version";

public override IncomingDocumentType DocumentType => IncomingDocumentType.RequestAggregatedMeasureData;

protected override async Task<IncomingMarketMessageParserResult> ParseXmlDataAsync(
XmlReader reader)
protected override string RootPayloadElementName => "RequestAggregatedMeasureData_MarketDocument";

protected override IReadOnlyCollection<IIncomingMessageSeries> ParseTransactions(XDocument document, XNamespace ns, string senderNumber)
{
var root = await reader.ReadRootElementAsync().ConfigureAwait(false);
var messageHeader = await MessageHeaderExtractor
.ExtractAsync(reader, root, HeaderElementName, SeriesRecordElementName)
.ConfigureAwait(false);
var seriesElements = document.Descendants(ns + SeriesElementName);
var result = new List<RequestAggregatedMeasureDataMessageSeries>();

var series = new List<RequestAggregatedMeasureDataMessageSeries>();
await foreach (var serie in ParseSeriesAsync(reader, root))
foreach (var seriesElement in seriesElements)
{
series.Add(serie);
var id = seriesElement.Element(ns + MridElementName)?.Value ?? string.Empty;
var marketEvaluationPointType = seriesElement.Element(ns + MarketEvaluationPointTypeElementName)?.Value ?? string.Empty;
var marketEvaluationSettlementMethod = seriesElement.Element(ns + MarketEvaluationPointSettlementMethodElementName)?.Value ?? string.Empty;
var startDateAndOrTimeDateTime = seriesElement.Element(ns + StartElementName)?.Value ?? string.Empty;
var endDateAndOrTimeDateTime = seriesElement.Element(ns + EndElementName)?.Value ?? string.Empty;
var meteringGridAreaDomainId = seriesElement.Element(ns + GridAreaElementName)?.Value ?? string.Empty;
var energySupplierMarketParticipantId = seriesElement.Element(ns + EnergySupplierNumberElementName)?.Value ?? string.Empty;
var balanceResponsiblePartyMarketParticipantId = seriesElement.Element(ns + BalanceResponsibleNumberElementName)?.Value ?? string.Empty;
var settlementVersion = seriesElement.Element(ns + SettlementVersionElementName)?.Value ?? string.Empty;

result.Add(new RequestAggregatedMeasureDataMessageSeries(
id,
marketEvaluationPointType,
marketEvaluationSettlementMethod,
startDateAndOrTimeDateTime,
endDateAndOrTimeDateTime,
meteringGridAreaDomainId,
energySupplierMarketParticipantId,
balanceResponsiblePartyMarketParticipantId,
settlementVersion));
}

return new IncomingMarketMessageParserResult(
RequestAggregatedMeasureDataMessageFactory.Create(messageHeader, series.AsReadOnly()));
return result.AsReadOnly();
}

private static async IAsyncEnumerable<RequestAggregatedMeasureDataMessageSeries> ParseSeriesAsync(XmlReader reader, RootElement rootElement)
protected override IncomingMarketMessageParserResult CreateResult(MessageHeader header, IReadOnlyCollection<IIncomingMessageSeries> transactions)
{
var id = string.Empty;
var startDateAndOrTimeDateTime = string.Empty;
string? marketEvaluationPointType = null;
string? marketEvaluationSettlementMethod = null;
string? endDateAndOrTimeDateTime = null;
string? meteringGridAreaDomainId = null;
string? energySupplierMarketParticipantId = null;
string? balanceResponsiblePartyMarketParticipantId = null;
string? settlementVersion = null;
var ns = rootElement.DefaultNamespace;

await reader.AdvanceToAsync(SeriesRecordElementName, ns).ConfigureAwait(false);

while (!reader.EOF)
{
if (reader.Is(SeriesRecordElementName, ns, XmlNodeType.EndElement))
{
var series = new RequestAggregatedMeasureDataMessageSeries(
id,
marketEvaluationPointType,
marketEvaluationSettlementMethod,
startDateAndOrTimeDateTime,
endDateAndOrTimeDateTime,
meteringGridAreaDomainId,
energySupplierMarketParticipantId,
balanceResponsiblePartyMarketParticipantId,
settlementVersion);

id = string.Empty;
startDateAndOrTimeDateTime = string.Empty;
marketEvaluationPointType = null;
marketEvaluationSettlementMethod = null;
endDateAndOrTimeDateTime = null;
meteringGridAreaDomainId = null;
energySupplierMarketParticipantId = null;
balanceResponsiblePartyMarketParticipantId = null;

yield return series;
}

if (reader.NodeType == XmlNodeType.Element && reader.SchemaInfo?.Validity == XmlSchemaValidity.Invalid)
await reader.ReadToEndAsync().ConfigureAwait(false);

if (reader.Is("mRID", ns))
{
id = await reader.ReadElementContentAsStringAsync().ConfigureAwait(false);
}
else if (reader.Is("marketEvaluationPoint.type", ns))
{
marketEvaluationPointType = await reader.ReadElementContentAsStringAsync().ConfigureAwait(false);
}
else if (reader.Is("marketEvaluationPoint.settlementMethod", ns))
{
marketEvaluationSettlementMethod = await reader.ReadElementContentAsStringAsync().ConfigureAwait(false);
}
else if (reader.Is("start_DateAndOrTime.dateTime", ns))
{
startDateAndOrTimeDateTime = await reader.ReadElementContentAsStringAsync().ConfigureAwait(false);
}
else if (reader.Is("end_DateAndOrTime.dateTime", ns))
{
endDateAndOrTimeDateTime = await reader.ReadElementContentAsStringAsync().ConfigureAwait(false);
}
else if (reader.Is("meteringGridArea_Domain.mRID", ns))
{
meteringGridAreaDomainId = await reader.ReadElementContentAsStringAsync().ConfigureAwait(false);
}
else if (reader.Is("energySupplier_MarketParticipant.mRID", ns))
{
energySupplierMarketParticipantId = await reader.ReadElementContentAsStringAsync().ConfigureAwait(false);
}
else if (reader.Is("balanceResponsibleParty_MarketParticipant.mRID", ns))
{
balanceResponsiblePartyMarketParticipantId = await reader.ReadElementContentAsStringAsync().ConfigureAwait(false);
}
else if (reader.Is("settlement_Series.version", ns))
{
settlementVersion = await reader.ReadElementContentAsStringAsync().ConfigureAwait(false);
}
else
{
await reader.ReadAsync().ConfigureAwait(false);
}
}
return new IncomingMarketMessageParserResult(
new RequestAggregatedMeasureDataMessage(
header.SenderId,
header.SenderRole,
header.ReceiverId,
header.ReceiverRole,
header.BusinessReason,
header.MessageType,
header.MessageId,
header.CreatedAt,
header.BusinessType,
transactions));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Copyright 2020 Energinet DataHub A/S
//
// Licensed under the Apache License, Version 2.0 (the "License2");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using System.Xml;
using System.Xml.Schema;
using Energinet.DataHub.EDI.BuildingBlocks.Domain.Models;
using Energinet.DataHub.EDI.IncomingMessages.Domain;
using Energinet.DataHub.EDI.IncomingMessages.Infrastructure.MessageParsers.BaseParsers;
using Energinet.DataHub.EDI.IncomingMessages.Infrastructure.MessageParsers.BaseParsers.Xml;
using Energinet.DataHub.EDI.IncomingMessages.Infrastructure.Schemas.Cim.Xml;

namespace Energinet.DataHub.EDI.IncomingMessages.Infrastructure.MessageParsers.AggregatedMeasureDataRequestMessageParsers;

public class OldAggregatedMeasureDataXmlMessageParser(IEnumerable<IMessageParser> messageParsers) : IMarketMessageParser
{
private readonly IEnumerable<IMessageParser> _messageParsers = messageParsers;

public DocumentFormat HandledFormat => DocumentFormat.Xml;

public IncomingDocumentType DocumentType => IncomingDocumentType.RequestAggregatedMeasureData;

public async Task<IncomingMarketMessageParserResult> ParseAsync(IIncomingMarketMessageStream incomingMarketMessageStream, CancellationToken cancellationToken)
{
var messageParser = _messageParsers
.Single(x => x.DocumentFormat == HandledFormat && x.DocumentType == DocumentType);
return await messageParser.ParseAsync(incomingMarketMessageStream, cancellationToken).ConfigureAwait(false);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,6 @@ public class MeteredDateForMeasurementPointXmlMessageParser(CimXmlSchemaProvider

public override IncomingDocumentType DocumentType => IncomingDocumentType.MeteredDataForMeasurementPoint;

public override DocumentFormat DocumentFormat => DocumentFormat.Xml;

protected override string RootPayloadElementName => "NotifyValidatedMeasureData_MarketDocument";

protected override IReadOnlyCollection<IIncomingMessageSeries> ParseTransactions(XDocument document, XNamespace ns, string senderNumber)
Expand Down Expand Up @@ -73,7 +71,6 @@ protected override IReadOnlyCollection<IIncomingMessageSeries> ParseTransactions
?.Value;

var energyObservations = seriesElement
//.Element(ns + Period)?
.Descendants(ns + PointElementName)
.Select(
e => new EnergyObservation(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright 2020 Energinet DataHub A/S
//
// Licensed under the Apache License, Version 2.0 (the "License2");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

using Energinet.DataHub.EDI.BuildingBlocks.Domain.Models;

namespace Energinet.DataHub.EDI.IncomingMessages.Infrastructure.MessageParsers.WholesaleSettlementMessageParsers;

public class OldWholesaleSettlementXmlMessageParser(IEnumerable<IMessageParser> messageParsers) : IMarketMessageParser
{
private readonly IEnumerable<IMessageParser> _messageParsers = messageParsers;

public DocumentFormat HandledFormat => DocumentFormat.Xml;

public IncomingDocumentType DocumentType => IncomingDocumentType.RequestWholesaleSettlement;

public async Task<IncomingMarketMessageParserResult> ParseAsync(IIncomingMarketMessageStream incomingMarketMessageStream, CancellationToken cancellationToken)
{
var messageParser = _messageParsers
.Single(x => x.DocumentFormat == HandledFormat && x.DocumentType == DocumentType);
return await messageParser.ParseAsync(incomingMarketMessageStream, cancellationToken).ConfigureAwait(false);
}
}
Loading

0 comments on commit 270bc97

Please sign in to comment.