package at.gv.egiz.moazs.service; import at.gv.egiz.moazs.repository.DeliveryRepository; import at.gv.egiz.moazs.scheme.MsgResponse; import at.gv.egiz.moazs.scheme.NameSpace; import at.gv.egiz.moazs.scheme.NotificationResponse; import at.gv.egiz.moazs.scheme.RequestStatusResponse; import at.gv.zustellung.msg.xsd.*; import org.apache.cxf.annotations.SchemaValidation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.util.function.Consumer; import static at.gv.zustellung.msg.xsd.DeliveryNotificationACKType.deliveryNotificationACKTypeBuilder; import static at.gv.zustellung.msg.xsd.DeliveryRequestStatusACKType.deliveryRequestStatusACKTypeBuilder; import static at.gv.zustellung.msg.xsd.GetVersionResponse.getVersionResponseBuilder; import static java.util.concurrent.CompletableFuture.runAsync; @Service @SchemaValidation(type = SchemaValidation.SchemaValidationType.BOTH) @org.apache.cxf.feature.Features (features = "org.apache.cxf.ext.logging.LoggingFeature") public class MsgService implements Zuse2AppPort { private final DeliveryRepository repository; private final Consumer backend; @Autowired public MsgService(DeliveryRepository repository, Consumer msgResponseBackend) { this.repository = repository; this.backend = msgResponseBackend; } @Override public GetVersionResponse getVersion(SimpleRequestType getVersionRequest) { return getVersionResponseBuilder() .withVersion(NameSpace.MSG_VERSION) .build(); } @Override public DeliveryRequestStatusACKType status(DeliveryRequestStatusType status) { var response = new RequestStatusResponse(status); sendToWork(response); return statusAck(response.getAppDeliveryID(), response.getZSDeliveryID()); } private DeliveryRequestStatusACKType statusAck(String appDeliveryID, String zsDeliveryID) { return deliveryRequestStatusACKTypeBuilder() .withAppDeliveryID(appDeliveryID) .withZSDeliveryID(zsDeliveryID) .withVersion(NameSpace.MSG_VERSION) .build(); } @Override public DeliveryNotificationACKType notification(DeliveryNotificationType notification) { var response = new NotificationResponse(notification); sendToWork(response); return notificationAck(response.getAppDeliveryID(), response.getZSDeliveryID()); } private void sendToWork(MsgResponse response) { runAsync(() -> repository.store(response)) .thenRunAsync(() -> backend.accept(response.getResponseID())); } private DeliveryNotificationACKType notificationAck(String appDeliveryID, String zsDeliveryID) { return deliveryNotificationACKTypeBuilder() .withAppDeliveryID(appDeliveryID) .withZSDeliveryID(zsDeliveryID) .withVersion(NameSpace.MSG_VERSION) .build(); } }