/******************************************************************************* * Copyright 2020 Graz University of Technology * MOA ZS has been developed in a cooperation between EGIZ * and Graz University of Technology. * Licensed under the EUPL, Version 1.2 or - as soon they will be approved by * the European Commission - subsequent versions of the EUPL (the "Licence"); * You may not use this work except in compliance with the Licence. * You may obtain a copy of the Licence at: * https://joinup.ec.europa.eu/news/understanding-eupl-v12 * * Unless required by applicable law or agreed to in writing, software * distributed under the Licence is distributed on an "AS IS" basis, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the Licence for the specific language governing permissions and * limitations under the Licence. * * This product combines work with different licenses. See the "NOTICE" text * file for details on the various modules and licenses. * The "NOTICE" text file is part of the distribution. Any derivative works * that you distribute must include a readable copy of the "NOTICE" text file. *******************************************************************************/ 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.*; import at.gv.zustellung.msg.xsd.ERVConfirmedDeliveryType; import org.springframework.stereotype.Component; import javax.xml.bind.JAXBElement; 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.ERVConfirmedDeliveryType.ERVConfirmedDeliveryTypeBuilder; 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; /** * @author Christof Rabensteiner * */ @Component public class Msg2MzsConverter { private static final String UNKNOWN_ANSWER_ERROR_MSG = "msg:DeliveryNotification/msg:Answer is of unknown type" + " and cannot be converted."; public DeliveryResponseType convert(DeliveryRequestStatusType status, Optional signedStatus) { var responseBuilder = deliveryResponseTypeBuilder(); if (status.getError() != null) { responseBuilder.withError(convert(status.getError(), signedStatus, status.getRelayedViaERV())); } else if (status.getSuccess() != null) { responseBuilder.withSuccess(convert(status.getSuccess(), signedStatus, status.getRelayedViaERV())); } else { responseBuilder.withPartialSuccess(convert(status.getPartialSuccess(), signedStatus, status.getRelayedViaERV())); } return responseBuilder.build(); } private SuccessType convert(DeliveryRequestStatusType.Success success, Optional signedStatus, RelayedViaERV relayed) { return successTypeBuilder() .withAppDeliveryID(success.getAppDeliveryID()) .withDeliverySystem(success.getDeliverySystem()) .withGZ(success.getGZ()) .withZSDeliveryID(success.getZSDeliveryID()) .withSignedDeliveryRequestStatus(signedStatus.orElse(null)) .withRelayedViaERV(relayed) .withDeliveryTimestamp(success.getDeliveryTimestamp()) .build(); } private PartialSuccessType convert(DeliveryAnswerType answer, Optional signedStatus, RelayedViaERV relayed) { return partialSuccessTypeBuilder() .withAppDeliveryID(answer.getAppDeliveryID()) .withDeliverySystem(answer.getDeliverySystem()) .withGZ(answer.getGZ()) .withZSDeliveryID(answer.getZSDeliveryID()) .withRelayedViaERV(relayed) .withSignedDeliveryRequestStatus(signedStatus.orElse(null)) .build(); } private ErrorType convert(DeliveryRequestStatusType.Error error, Optional signedStatus, RelayedViaERV relayed) { var builder = errorTypeBuilder() .withAppDeliveryID(error.getAppDeliveryID()) .withDeliverySystem(error.getDeliverySystem()) .withGZ(error.getGZ()) .withZSDeliveryID(error.getZSDeliveryID()) .withSignedDeliveryRequestStatus(signedStatus.orElse(null)) .withPreAdviceNoteSent(error.getPreAdviceNoteSent()) .withRelayedViaERV(relayed) .withCode(error.getErrorInfo().getCode()); if (error.getErrorInfo().getText() != null) builder.withText(error.getErrorInfo().getText()); return builder.build(); } public DeliveryNotificationType convert(at.gv.zustellung.msg.xsd.DeliveryNotificationType msgNotification, Optional signedNotification) { var builder = deliveryNotificationTypeBuilder(); JAXBElement answer = msgNotification.getAnswer(); 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 if(answer.getValue() instanceof ERVConfirmedDeliveryType) { var ervConfirmed = (ERVConfirmedDeliveryType) answer.getValue(); builder.withERVConfirmedDelivery(extractERVconfirmedDelivery(msgNotification, ervConfirmed, signedNotification)); } 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(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)) .withDeliveryTimestamp(msgNotification.getTimestamp()) .build(); } private at.gv.zustellung.app2mzs.xsd.ERVConfirmedDeliveryType extractERVconfirmedDelivery( at.gv.zustellung.msg.xsd.DeliveryNotificationType msgNotification, ERVConfirmedDeliveryType ervConfirmed,Optional signedNotification) { return ERVConfirmedDeliveryTypeBuilder() .withAppDeliveryID(msgNotification.getAppDeliveryID()) .withDeliverySystem(msgNotification.getDeliverySystem()) .withZSDeliveryID(msgNotification.getZSDeliveryID()) .withGZ(msgNotification.getGZ()) .withSignedDeliveryRequestStatus(signedNotification.orElse(null)) .withERVConfirmedDelivery(ervConfirmed) .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(); } }