Skip to content

Commit

Permalink
feat: child parser only mappes a tranaction (#1393)
Browse files Browse the repository at this point in the history
  • Loading branch information
MadsDue authored Nov 27, 2024
1 parent a40ecdb commit 3135187
Show file tree
Hide file tree
Showing 8 changed files with 182 additions and 234 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -42,19 +41,29 @@ public class AggregatedMeasureDataJsonMessageParser(JsonSchemaProvider schemaPro

protected override string DocumentName => "RequestAggregatedMeasureData";

protected override IReadOnlyCollection<IIncomingMessageSeries> 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<RequestAggregatedMeasureDataMessageSeries>();

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<IIncomingMessageSeries> transactions)
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -38,36 +37,28 @@ public class AggregatedMeasureDataXmlMessageParser(CimXmlSchemaProvider schemaPr

protected override string RootPayloadElementName => "RequestAggregatedMeasureData_MarketDocument";

protected override IReadOnlyCollection<IIncomingMessageSeries> 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<RequestAggregatedMeasureDataMessageSeries>();
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<IIncomingMessageSeries> transactions)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ namespace Energinet.DataHub.EDI.IncomingMessages.Infrastructure.MessageParsers;

public abstract class JsonMessageParserBase(JsonSchemaProvider schemaProvider) : MessageParserBase<JsonSchema>()
{
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";
Expand Down Expand Up @@ -65,11 +66,18 @@ protected override async Task<IncomingMarketMessageParserResult> ParseMessageAsy
}

var header = ParseHeader(document);
var transactions = ParseTransactions(document, header.SenderId);
var transactionElements = document.RootElement.GetProperty(HeaderElementName).GetProperty(SeriesElementName);
var transactions = new List<IIncomingMessageSeries>();

foreach (var transactionElement in transactionElements.EnumerateArray())
{
transactions.Add(ParseTransaction(transactionElement, header.SenderId));
}

return CreateResult(header, transactions);
}

protected abstract IReadOnlyCollection<IIncomingMessageSeries> 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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand All @@ -47,41 +48,9 @@ public class MeteredDateForMeasurementPointJsonMessageParser(JsonSchemaProvider

protected override string DocumentName => "NotifyValidatedMeasureData";

protected override IReadOnlyCollection<IIncomingMessageSeries> 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<MeteredDataForMeasurementPointSeries>();

foreach (var transactionElement in transactionElements.EnumerateArray())
{
var transaction = ParseTransaction(senderNumber, transactionElement);

transactions.Add(transaction);
}

return transactions.AsReadOnly();
}

protected override IncomingMarketMessageParserResult CreateResult(MessageHeader header, IReadOnlyCollection<IIncomingMessageSeries> 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();
Expand Down Expand Up @@ -126,4 +95,19 @@ private static MeteredDataForMeasurementPointSeries ParseTransaction(
senderNumber,
energyObservations);
}

protected override IncomingMarketMessageParserResult CreateResult(MessageHeader header, IReadOnlyCollection<IIncomingMessageSeries> 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));
}
}
Loading

0 comments on commit 3135187

Please sign in to comment.