From 896195cc9b287a3f41008cc85997b9c2209120b8 Mon Sep 17 00:00:00 2001 From: Christof Rabensteiner Date: Tue, 16 Jul 2019 16:53:41 +0200 Subject: DeliveryNotification: Change Mzs Schema, Convert From Msg to Mzs Mzs Schema Changes: - Formerly: The mzs:DeliveryNotificationType is a choice between Success And Error; Change: Wrap choice in a sequence. Reason: msg 2.0.0 adds fields that fit best in this sequence. Adding new fields to Success or Error duplicates code, whereas adding it to the base type (which is MessageType) also affects mzs:DeliveryRequestStatus. - Add msg:SenderDetails, ReceiverDetails, user, AdditionalFormat, NotificationsPerformed; Reason: It was added to zusemsg 2.0.0. --- .../at/gv/egiz/moazs/scheme/Msg2MzsConverter.java | 102 +++++++++++++++++++-- src/main/resources/mzs/app2mzs.xsd | 23 ++++- 2 files changed, 113 insertions(+), 12 deletions(-) diff --git a/src/main/java/at/gv/egiz/moazs/scheme/Msg2MzsConverter.java b/src/main/java/at/gv/egiz/moazs/scheme/Msg2MzsConverter.java index 271cf67..65b6425 100644 --- a/src/main/java/at/gv/egiz/moazs/scheme/Msg2MzsConverter.java +++ b/src/main/java/at/gv/egiz/moazs/scheme/Msg2MzsConverter.java @@ -1,21 +1,38 @@ package at.gv.egiz.moazs.scheme; +import at.gv.zustellung.app2mzs.xsd.DeliveryNotificationType; import at.gv.zustellung.app2mzs.xsd.*; -import at.gv.zustellung.msg.xsd.DeliveryAnswerType; -import at.gv.zustellung.msg.xsd.DeliveryRequestStatusType; +import at.gv.zustellung.msg.xsd.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; +import javax.xml.bind.JAXBElement; import java.math.BigInteger; +import java.util.List; import java.util.Optional; +import static at.gv.egiz.moazs.MoaZSException.moaZSException; +import static at.gv.zustellung.app2mzs.xsd.DeliveryNotificationType.AdditionalFormat.additionalFormatBuilder; +import static at.gv.zustellung.app2mzs.xsd.DeliveryNotificationType.deliveryNotificationTypeBuilder; import static at.gv.zustellung.app2mzs.xsd.DeliveryResponseType.deliveryResponseTypeBuilder; import static at.gv.zustellung.app2mzs.xsd.ErrorType.errorTypeBuilder; import static at.gv.zustellung.app2mzs.xsd.PartialSuccessType.partialSuccessTypeBuilder; import static at.gv.zustellung.app2mzs.xsd.SuccessType.successTypeBuilder; +import static java.util.stream.Collectors.toList; @Component public class Msg2MzsConverter { + private static final Logger log = LoggerFactory.getLogger(Msg2MzsConverter.class); + + private static final String ANSWERS_MISSING_ERROR_MSG = "msg:DeliveryNotification contains no msg:Answer elements" + + " and cannot be converted to mzs:DeliveryNotification."; + private static final String UNKNOWN_ANSWER_ERROR_MSG = "msg:DeliveryNotification/msg:Answer is of unknown type" + + " and cannot be converted."; + private static final String TOO_MANY_ANSWERS_MSG = "msg:DeliveryNotification contains more msg:Answer elements than" + + " expected. All answers except the first one will be ignored."; + public DeliveryResponseType convert(DeliveryRequestStatusType status, Optional signedStatus) { var responseBuilder = deliveryResponseTypeBuilder(); @@ -31,12 +48,6 @@ public class Msg2MzsConverter { return responseBuilder.build(); } - public DeliveryNotificationType convert(at.gv.zustellung.msg.xsd.DeliveryNotificationType notificatione, Optional signedStatus) { - //TODO - return null; - } - - private SuccessType convert(DeliveryRequestStatusType.Success success, Optional signedStatus) { return successTypeBuilder() .withAppDeliveryID(success.getAppDeliveryID()) @@ -74,5 +85,80 @@ public class Msg2MzsConverter { return builder.build(); } + public DeliveryNotificationType convert(at.gv.zustellung.msg.xsd.DeliveryNotificationType msgNotification, + Optional signedNotification) { + + var builder = deliveryNotificationTypeBuilder(); + + if (msgNotification.getAnswer().isEmpty()) { + throw moaZSException(ANSWERS_MISSING_ERROR_MSG); + } + + if (msgNotification.getAnswer().size() > 1) { + log.warn(TOO_MANY_ANSWERS_MSG); + } + + JAXBElement answer = msgNotification.getAnswer().get(0); + + if(answer.getValue() instanceof AcceptedType) { + var accepted = (AcceptedType) answer.getValue(); + builder.withSuccess(extractSuccess(msgNotification, signedNotification)) + .withNotificationsPerformed(accepted.getNotificationsPerformed()); + } else if(answer.getValue() instanceof DeliveryErrorType) { + var error = (DeliveryErrorType) answer.getValue(); + builder.withError(extractError(msgNotification, error, signedNotification)) + .withNotificationsPerformed(error.getNotificationsPerformed()); + } else { + throw moaZSException(UNKNOWN_ANSWER_ERROR_MSG); + } + + return builder + .withSenderDetails(msgNotification.getSenderDetails()) + .withReceiverDetails(msgNotification.getReceiverDetails()) + .withUser(msgNotification.getUser()) + .withAdditionalFormat(convert(msgNotification.getAdditionalFormat())) + .build(); + } + + private ErrorType extractError(at.gv.zustellung.msg.xsd.DeliveryNotificationType msgNotification, + DeliveryErrorType error, Optional signedNotification) { + return errorTypeBuilder() + .withAppDeliveryID(msgNotification.getAppDeliveryID()) + .withDeliverySystem(msgNotification.getDeliverySystem()) + .withZSDeliveryID(msgNotification.getZSDeliveryID()) + .withGZ(msgNotification.getGZ()) + .withSignedDeliveryRequestStatus(signedNotification.orElse(null)) + .withCode(new BigInteger(error.getErrorInfo().getCode())) + .withText(error.getErrorInfo().getText()) + .build(); + } + + private SuccessType extractSuccess(at.gv.zustellung.msg.xsd.DeliveryNotificationType msgNotification, + Optional signedNotification) { + return successTypeBuilder() + .withAppDeliveryID(msgNotification.getAppDeliveryID()) + .withDeliverySystem(msgNotification.getDeliverySystem()) + .withZSDeliveryID(msgNotification.getZSDeliveryID()) + .withGZ(msgNotification.getGZ()) + .withSignedDeliveryRequestStatus(signedNotification.orElse(null)) + .withRelayedViaERV(msgNotification.isRelayedViaERV()) + .withDeliveryTimestamp(msgNotification.getTimestamp()) + .build(); + } + + private List convert( + List additionalFormat) { + return additionalFormat.stream() + .map(this::convert) + .collect(toList()); + } + + private DeliveryNotificationType.AdditionalFormat convert( + at.gv.zustellung.msg.xsd.DeliveryNotificationType.AdditionalFormat format) { + return additionalFormatBuilder() + .withType(format.getType()) + .withValue(format.getValue()) + .build(); + } } diff --git a/src/main/resources/mzs/app2mzs.xsd b/src/main/resources/mzs/app2mzs.xsd index a2d1bde..da49631 100644 --- a/src/main/resources/mzs/app2mzs.xsd +++ b/src/main/resources/mzs/app2mzs.xsd @@ -166,10 +166,25 @@ - - - - + + + + + + + + + + + + + + + + + + + -- cgit v1.2.3