diff --git a/iso20022-core/src/main/java/com/prowidesoftware/swift/model/MxId.java b/iso20022-core/src/main/java/com/prowidesoftware/swift/model/MxId.java index 40fa3cd07..49eab5393 100644 --- a/iso20022-core/src/main/java/com/prowidesoftware/swift/model/MxId.java +++ b/iso20022-core/src/main/java/com/prowidesoftware/swift/model/MxId.java @@ -323,7 +323,8 @@ public Optional getBusinessService() { * @param businessService a string value to set as discriminator, for example "swift.cbprplus.cov.02" * @since 9.5.0 */ - public void setBusinessService(String businessService) { + public MxId setBusinessService(String businessService) { this.businessService = businessService; + return this; } } diff --git a/iso20022-core/src/main/java/com/prowidesoftware/swift/model/mx/AppHdrFactory.java b/iso20022-core/src/main/java/com/prowidesoftware/swift/model/mx/AppHdrFactory.java index f37417be7..3e968c893 100644 --- a/iso20022-core/src/main/java/com/prowidesoftware/swift/model/mx/AppHdrFactory.java +++ b/iso20022-core/src/main/java/com/prowidesoftware/swift/model/mx/AppHdrFactory.java @@ -107,6 +107,9 @@ public static BusinessAppHdrV02 createBusinessAppHdrV02( if (id != null) { h.setMsgDefIdr(id.id()); + if (id.getBusinessService().isPresent()) { + h.setBizSvc(id.getBusinessService().get()); + } } h.setCreDt(OffsetDateTime.now()); @@ -151,6 +154,9 @@ public static BusinessAppHdrV03 createBusinessAppHdrV03( if (id != null) { h.setMsgDefIdr(id.id()); + if (id.getBusinessService().isPresent()) { + h.setBizSvc(id.getBusinessService().get()); + } } h.setCreDt(OffsetDateTime.now()); diff --git a/iso20022-core/src/main/java/com/prowidesoftware/swift/model/mx/MxParseUtils.java b/iso20022-core/src/main/java/com/prowidesoftware/swift/model/mx/MxParseUtils.java index 83df5d4f9..ec0f63b1e 100644 --- a/iso20022-core/src/main/java/com/prowidesoftware/swift/model/mx/MxParseUtils.java +++ b/iso20022-core/src/main/java/com/prowidesoftware/swift/model/mx/MxParseUtils.java @@ -194,7 +194,7 @@ public static String getBICFromDN(final String dn) { public static Optional identifyMessage(final String xml) { Optional namespace = NamespaceReader.findDocumentNamespace(xml); if (namespace.isPresent()) { - return namespace.map(MxId::new); + return enrichBusinessService(namespace.map(MxId::new).orElse(null), xml); } // if the Document does not have a namespace, try to identify the message from the header @@ -205,7 +205,7 @@ public static Optional identifyMessage(final String xml) { } if (element.isPresent()) { try { - return Optional.of(new MxId(element.get().getElementText())); + return enrichBusinessService(new MxId(element.get().getElementText()), xml); } catch (XMLStreamException e) { log.finer("Error identifying message: " + e.getMessage()); } @@ -214,6 +214,21 @@ public static Optional identifyMessage(final String xml) { return Optional.empty(); } + private static Optional enrichBusinessService(MxId mxId, final String xml) { + if (mxId == null) { + return Optional.empty(); + } + Optional element = NamespaceReader.findElement(xml, "BizSvc"); + if (element.isPresent()) { + try { + mxId.setBusinessService(element.get().getElementText()); + } catch (XMLStreamException e) { + log.finer("Error identifying business service: " + e.getMessage()); + } + } + return Optional.of(mxId); + } + /** * This method is intended to fix some malformed XML content that is not compliant with the XML specification * to enable the parsing and processing of the payload to be lenient. diff --git a/iso20022-core/src/test/java/com/prowidesoftware/swift/model/mx/AppHdrFactoryTest.java b/iso20022-core/src/test/java/com/prowidesoftware/swift/model/mx/AppHdrFactoryTest.java index de93fafca..ae1b41e51 100644 --- a/iso20022-core/src/test/java/com/prowidesoftware/swift/model/mx/AppHdrFactoryTest.java +++ b/iso20022-core/src/test/java/com/prowidesoftware/swift/model/mx/AppHdrFactoryTest.java @@ -22,4 +22,15 @@ void createBusinessAppHdrV01() { // for BAH v01 the date time must be ISONormalisedDateTime assertTrue(xml.contains("Z")); } + + @Test + void createBusinessAppHdrV02_WithBusinessService() { + BusinessAppHdrV02 h = AppHdrFactory.createBusinessAppHdrV02( + "AAAAUSXXXXX", + "BBBBUSXXXXX", + "REF12345", + new MxId("pacs.009.001.08").setBusinessService("swift.cbprplus.cov.02")); + assertNotNull(h); + assertEquals("swift.cbprplus.cov.02", h.getBizSvc()); + } } diff --git a/iso20022-core/src/test/java/com/prowidesoftware/swift/model/mx/MxParseUtilsTest.java b/iso20022-core/src/test/java/com/prowidesoftware/swift/model/mx/MxParseUtilsTest.java index 3cca91dbe..239e524f3 100644 --- a/iso20022-core/src/test/java/com/prowidesoftware/swift/model/mx/MxParseUtilsTest.java +++ b/iso20022-core/src/test/java/com/prowidesoftware/swift/model/mx/MxParseUtilsTest.java @@ -128,6 +128,7 @@ public void testIdentifyMessage_FromBAH() { MxId id = MxParseUtils.identifyMessage(xml).orElse(null); assertNotNull(id); assertEquals("seev.031.002.03", id.id()); + assertEquals("CSD", id.getBusinessService().orElse(null)); } @Test