package at.gv.egiz.moazs.scheme; import at.gv.egiz.moazs.MoaZSException; import at.gv.zustellung.app2mzs.xsd.Mzs2AppPortType; import at.gv.zustellung.msg.xsd.DeliveryAnswerType; import at.gv.zustellung.msg.xsd.DeliveryRequestStatusType; import at.gv.zustellung.msg.xsd.ErrorInfoType; import at.gv.zustellung.msg.xsd.ObjectFactory; import org.springframework.lang.Nullable; import javax.xml.bind.JAXBElement; import java.util.Optional; import java.util.concurrent.CompletableFuture; import static at.gv.egiz.moazs.MoaZSException.moaZSException; import static at.gv.egiz.moazs.util.NullCoalesce.coalesce; import static at.gv.zustellung.msg.xsd.DeliveryRequestStatusType.Error.errorBuilder; import static at.gv.zustellung.msg.xsd.DeliveryRequestStatusType.deliveryRequestStatusTypeBuilder; import static at.gv.zustellung.msg.xsd.ErrorInfoType.errorInfoTypeBuilder; public class RequestStatusResponse extends MsgResponse { private final DeliveryRequestStatusType status; private final DeliveryAnswerType answer; private static final String ID_SUFFIX = ".RS"; private static final ObjectFactory factory = new ObjectFactory(); public RequestStatusResponse(DeliveryRequestStatusType status) { this.status = status; this.answer = getAnswer(status); super.id = createResponseId(answer.getAppDeliveryID(), ID_SUFFIX); } /** * Convenience method to access DeliveryAnswerType's base fields of Success, PartialSuccess or Error. * @param status * @return Success, PartialSuccess, or Error casted to DeliveryAnswerType */ public static DeliveryAnswerType getAnswer(DeliveryRequestStatusType status) { return coalesce(status.getSuccess(), status.getPartialSuccess(), status.getError()).get(); } public static String getResponseID(String appDeliveryID) { return appDeliveryID + ID_SUFFIX; } @Override public DeliveryRequestStatusType getResponse() { return status; } @Override public JAXBElement getResponseAsJAXBElement() { return factory.createDeliveryRequestStatus(status); } @Override public String getAppDeliveryID() { return answer.getAppDeliveryID(); } @Override public String getZSDeliveryID() { return answer.getZSDeliveryID(); } @Override public DeliveryAnswerType getAnswer() { return this.answer; } @Override public MsgResponse generateError(String text, String code) { var auxException = moaZSException(text, code); return generateError(auxException, answer); } /** * Creates A MsgResponse with a DeliveryRequestStatus of type error and merges fields from * {@code exception} and {@code answer} * @param exception * @param answer * @return */ public static MsgResponse generateError(MoaZSException exception, @Nullable DeliveryAnswerType answer) { ErrorInfoType info = errorInfoTypeBuilder() .withText(exception.getMessage()) .withCode(exception.getCode()) .build(); var errorBuilder = errorBuilder() .withPreAdviceNoteSent(exception.getPreAdviceNoteSent()) .withErrorInfo(info); if (answer != null) { errorBuilder .withAppDeliveryID(answer.getAppDeliveryID()) .withDeliverySystem(answer.getDeliverySystem()) .withZSDeliveryID(answer.getZSDeliveryID()) .withGZ(answer.getGZ()); } var errorStatus = deliveryRequestStatusTypeBuilder() .withError(errorBuilder.build()) .build(); return new RequestStatusResponse(errorStatus); } @Override public CompletableFuture sendToMzsClient(Msg2MzsConverter converter, Optional signedStatus, Mzs2AppPortType client) { var msgStatus = converter.convert(status, signedStatus); client.forwardStatus(msgStatus); return CompletableFuture.completedFuture(null); } }