package at.gv.egiz.moazs.scheme; import at.gv.zustellung.app2mzs.xsd.DeliveryResponseType; import at.gv.zustellung.app2mzs.xsd.ErrorType; import at.gv.zustellung.app2mzs.xsd.PartialSuccessType; import at.gv.zustellung.app2mzs.xsd.SuccessType; import at.gv.zustellung.msg.xsd.DeliveryAnswerType; import at.gv.zustellung.msg.xsd.DeliveryRequestStatusType; import org.springframework.stereotype.Component; import java.math.BigInteger; import java.util.Optional; 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; @Component public class Msg2MzsConverter { public DeliveryResponseType convert(DeliveryRequestStatusType status, Optional signedStatus) { var responseBuilder = deliveryResponseTypeBuilder(); if (status.getError() != null) { responseBuilder.withError(convert(status.getError(), signedStatus)); } else if (status.getSuccess() != null) { responseBuilder.withSuccess(convert(status.getSuccess(), signedStatus)); } else { responseBuilder.withPartialSuccess(convert(status.getPartialSuccess(), signedStatus)); } return responseBuilder.build(); } private SuccessType convert(DeliveryRequestStatusType.Success success, Optional signedStatus) { return successTypeBuilder() .withAppDeliveryID(success.getAppDeliveryID()) .withDeliverySystem(success.getDeliverySystem()) .withGZ(success.getGZ()) .withZSDeliveryID(success.getZSDeliveryID()) .withSignedDeliveryRequestStatus(signedStatus.orElse(null)) .withRelayedViaERV(success.isRelayedViaERV()) .withDeliveryTimestamp(success.getDeliveryTimestamp()) .build(); } private PartialSuccessType convert(DeliveryAnswerType answer, Optional signedStatus) { return partialSuccessTypeBuilder() .withAppDeliveryID(answer.getAppDeliveryID()) .withDeliverySystem(answer.getDeliverySystem()) .withGZ(answer.getGZ()) .withZSDeliveryID(answer.getZSDeliveryID()) .withSignedDeliveryRequestStatus(signedStatus.orElse(null)) .build(); } private ErrorType convert(DeliveryRequestStatusType.Error error, Optional signedStatus) { var builder = errorTypeBuilder() .withAppDeliveryID(error.getAppDeliveryID()) .withDeliverySystem(error.getDeliverySystem()) .withGZ(error.getGZ()) .withZSDeliveryID(error.getZSDeliveryID()) .withSignedDeliveryRequestStatus(signedStatus.orElse(null)) .withPreAdviceNoteSent(error.getPreAdviceNoteSent()) .withCode(new BigInteger(error.getErrorInfo().getCode())); if(error.getErrorInfo().getText() != null) builder.withText(error.getErrorInfo().getText()); return builder.build(); } }